comm.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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"
  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. opt_complementary = "=2";
  58. getopt32(argv, "123");
  59. argv += optind;
  60. for (i = 0; i < 2; ++i) {
  61. stream[i] = xfopen_stdin(argv[i]);
  62. }
  63. order = 0;
  64. thisline[1] = thisline[0] = NULL;
  65. while (1) {
  66. if (order <= 0) {
  67. free(thisline[0]);
  68. thisline[0] = xmalloc_fgetline(stream[0]);
  69. }
  70. if (order >= 0) {
  71. free(thisline[1]);
  72. thisline[1] = xmalloc_fgetline(stream[1]);
  73. }
  74. i = !thisline[0] + (!thisline[1] << 1);
  75. if (i)
  76. break;
  77. order = strcmp(thisline[0], thisline[1]);
  78. if (order >= 0)
  79. writeline(thisline[1], order ? 1 : 2);
  80. else
  81. writeline(thisline[0], 0);
  82. }
  83. /* EOF at least on one of the streams */
  84. i &= 1;
  85. if (thisline[i]) {
  86. /* stream[i] is not at EOF yet */
  87. /* we did not print thisline[i] yet */
  88. char *p = thisline[i];
  89. writeline(p, i);
  90. while (1) {
  91. free(p);
  92. p = xmalloc_fgetline(stream[i]);
  93. if (!p)
  94. break;
  95. writeline(p, i);
  96. }
  97. }
  98. if (ENABLE_FEATURE_CLEAN_UP) {
  99. fclose(stream[0]);
  100. fclose(stream[1]);
  101. }
  102. return EXIT_SUCCESS;
  103. }