3
0

comm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini comm implementation for busybox
  4. *
  5. * Copyright (C) 2005 by Robert Sullivan <cogito.ergo.cogito@gmail.com>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config COMM
  10. //config: bool "comm (4.2 kb)"
  11. //config: default y
  12. //config: help
  13. //config: comm is used to compare two files line by line and return
  14. //config: a three-column output.
  15. //applet:IF_COMM(APPLET(comm, BB_DIR_USR_BIN, BB_SUID_DROP))
  16. //kbuild:lib-$(CONFIG_COMM) += comm.o
  17. //usage:#define comm_trivial_usage
  18. //usage: "[-123] FILE1 FILE2"
  19. //usage:#define comm_full_usage "\n\n"
  20. //usage: "Compare FILE1 with FILE2\n"
  21. //usage: "\n -1 Suppress lines unique to FILE1"
  22. //usage: "\n -2 Suppress lines unique to FILE2"
  23. //usage: "\n -3 Suppress lines common to both files"
  24. #include "libbb.h"
  25. #define COMM_OPT_1 (1 << 0)
  26. #define COMM_OPT_2 (1 << 1)
  27. #define COMM_OPT_3 (1 << 2)
  28. /* writeline outputs the input given, appropriately aligned according to class */
  29. static void writeline(char *line, int class)
  30. {
  31. int flags = option_mask32;
  32. if (class == 0) {
  33. if (flags & COMM_OPT_1)
  34. return;
  35. } else if (class == 1) {
  36. if (flags & COMM_OPT_2)
  37. return;
  38. if (!(flags & COMM_OPT_1))
  39. putchar('\t');
  40. } else /*if (class == 2)*/ {
  41. if (flags & COMM_OPT_3)
  42. return;
  43. if (!(flags & COMM_OPT_1))
  44. putchar('\t');
  45. if (!(flags & COMM_OPT_2))
  46. putchar('\t');
  47. }
  48. puts(line);
  49. }
  50. int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  51. int comm_main(int argc UNUSED_PARAM, char **argv)
  52. {
  53. char *thisline[2];
  54. FILE *stream[2];
  55. int i;
  56. int order;
  57. getopt32(argv, "^" "123" "\0" "=2");
  58. argv += optind;
  59. for (i = 0; i < 2; ++i) {
  60. stream[i] = xfopen_stdin(argv[i]);
  61. }
  62. order = 0;
  63. thisline[1] = thisline[0] = NULL;
  64. while (1) {
  65. if (order <= 0) {
  66. free(thisline[0]);
  67. thisline[0] = xmalloc_fgetline(stream[0]);
  68. }
  69. if (order >= 0) {
  70. free(thisline[1]);
  71. thisline[1] = xmalloc_fgetline(stream[1]);
  72. }
  73. i = !thisline[0] + (!thisline[1] << 1);
  74. if (i)
  75. break;
  76. order = strcmp(thisline[0], thisline[1]);
  77. if (order >= 0)
  78. writeline(thisline[1], order ? 1 : 2);
  79. else
  80. writeline(thisline[0], 0);
  81. }
  82. /* EOF at least on one of the streams */
  83. i &= 1;
  84. if (thisline[i]) {
  85. /* stream[i] is not at EOF yet */
  86. /* we did not print thisline[i] yet */
  87. char *p = thisline[i];
  88. writeline(p, i);
  89. while (1) {
  90. free(p);
  91. p = xmalloc_fgetline(stream[i]);
  92. if (!p)
  93. break;
  94. writeline(p, i);
  95. }
  96. }
  97. if (ENABLE_FEATURE_CLEAN_UP) {
  98. fclose(stream[0]);
  99. fclose(stream[1]);
  100. }
  101. return EXIT_SUCCESS;
  102. }