comm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 tarball for details.
  8. */
  9. #include "busybox.h"
  10. #define COMM_OPT_1 0x01
  11. #define COMM_OPT_2 0x02
  12. #define COMM_OPT_3 0x04
  13. /* These three variables control behaviour if non-zero */
  14. static int only_file_1;
  15. static int only_file_2;
  16. static int both;
  17. /* writeline outputs the input given, appropriately aligned according to class */
  18. static void writeline(char *line, int class)
  19. {
  20. if (class == 0) {
  21. if (!only_file_1)
  22. return;
  23. } else if (class == 1) {
  24. if (!only_file_2)
  25. return;
  26. if (only_file_1)
  27. putchar('\t');
  28. }
  29. else /*if (class == 2)*/ {
  30. if (!both)
  31. return;
  32. if (only_file_1)
  33. putchar('\t');
  34. if (only_file_2)
  35. putchar('\t');
  36. }
  37. fputs(line, stdout);
  38. }
  39. /* This is the real core of the program - lines are compared here */
  40. static void cmp_files(char **infiles)
  41. {
  42. #define LINE_LEN 100
  43. #define BB_EOF_0 0x1
  44. #define BB_EOF_1 0x2
  45. char thisline[2][LINE_LEN];
  46. FILE *streams[2];
  47. int i;
  48. for (i = 0; i < 2; ++i) {
  49. streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : xfopen(infiles[i], "r"));
  50. fgets(thisline[i], LINE_LEN, streams[i]);
  51. }
  52. while (*thisline[0] || *thisline[1]) {
  53. int order = 0;
  54. i = 0;
  55. if (feof(streams[0])) i |= BB_EOF_0;
  56. if (feof(streams[1])) i |= BB_EOF_1;
  57. if (!*thisline[0])
  58. order = 1;
  59. else if (!*thisline[1])
  60. order = -1;
  61. else {
  62. int tl0_len, tl1_len;
  63. tl0_len = strlen(thisline[0]);
  64. tl1_len = strlen(thisline[1]);
  65. order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
  66. if (!order)
  67. order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
  68. }
  69. if (order == 0 && !i)
  70. writeline(thisline[1], 2);
  71. else if (order > 0 && !(i & BB_EOF_1))
  72. writeline(thisline[1], 1);
  73. else if (order < 0 && !(i & BB_EOF_0))
  74. writeline(thisline[0], 0);
  75. if (i & BB_EOF_0 & BB_EOF_1) {
  76. break;
  77. } else if (i) {
  78. i = (i & BB_EOF_0 ? 1 : 0);
  79. while (!feof(streams[i])) {
  80. if ((order < 0 && i) || (order > 0 && !i))
  81. writeline(thisline[i], i);
  82. fgets(thisline[i], LINE_LEN, streams[i]);
  83. }
  84. break;
  85. } else {
  86. if (order >= 0)
  87. fgets(thisline[1], LINE_LEN, streams[1]);
  88. if (order <= 0)
  89. fgets(thisline[0], LINE_LEN, streams[0]);
  90. }
  91. }
  92. fclose(streams[0]);
  93. fclose(streams[1]);
  94. }
  95. int comm_main(int argc, char **argv)
  96. {
  97. unsigned long flags;
  98. flags = getopt32(argc, argv, "123");
  99. if (optind + 2 != argc)
  100. bb_show_usage();
  101. only_file_1 = !(flags & COMM_OPT_1);
  102. only_file_2 = !(flags & COMM_OPT_2);
  103. both = !(flags & COMM_OPT_3);
  104. cmp_files(argv + optind);
  105. exit(EXIT_SUCCESS);
  106. }