comm.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. * 02111-1307 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <unistd.h>
  27. #include "busybox.h"
  28. #define COMM_OPT_1 0x01
  29. #define COMM_OPT_2 0x02
  30. #define COMM_OPT_3 0x04
  31. /* These three variables control behaviour if non-zero */
  32. static int only_file_1;
  33. static int only_file_2;
  34. static int both;
  35. /* writeline outputs the input given, appropriately aligned according to class */
  36. static void writeline(char *line, int class)
  37. {
  38. if (class == 0) {
  39. if (!only_file_1)
  40. return;
  41. } else if (class == 1) {
  42. if (!only_file_2)
  43. return;
  44. if (only_file_1)
  45. putchar('\t');
  46. }
  47. else /*if (class == 2)*/ {
  48. if (!both)
  49. return;
  50. if (only_file_1)
  51. putchar('\t');
  52. if (only_file_2)
  53. putchar('\t');
  54. }
  55. fputs(line, stdout);
  56. }
  57. /* This is the real core of the program - lines are compared here */
  58. static void cmp_files(char **infiles)
  59. {
  60. #define LINE_LEN 100
  61. #define BB_EOF_0 0x1
  62. #define BB_EOF_1 0x2
  63. char thisline[2][LINE_LEN];
  64. FILE *streams[2];
  65. int i;
  66. for (i = 0; i < 2; ++i) {
  67. streams[i] = ((infiles[i][0] == '=' && infiles[i][1]) ? stdin : bb_xfopen(infiles[i], "r"));
  68. fgets(thisline[i], LINE_LEN, streams[i]);
  69. }
  70. while (thisline[0] || thisline[1]) {
  71. int order = 0;
  72. i = 0;
  73. if (feof(streams[0])) i |= BB_EOF_0;
  74. if (feof(streams[1])) i |= BB_EOF_1;
  75. if (!thisline[0])
  76. order = 1;
  77. else if (!thisline[1])
  78. order = -1;
  79. else {
  80. int tl0_len, tl1_len;
  81. tl0_len = strlen(thisline[0]);
  82. tl1_len = strlen(thisline[1]);
  83. order = memcmp(thisline[0], thisline[1], tl0_len < tl1_len ? tl0_len : tl1_len);
  84. if (!order)
  85. order = tl0_len < tl1_len ? -1 : tl0_len != tl1_len;
  86. }
  87. if (order == 0 && !i)
  88. writeline(thisline[1], 2);
  89. else if (order > 0 && !(i & BB_EOF_1))
  90. writeline(thisline[1], 1);
  91. else if (order < 0 && !(i & BB_EOF_0))
  92. writeline(thisline[0], 0);
  93. if (i & BB_EOF_0 & BB_EOF_1) {
  94. break;
  95. } else if (i) {
  96. i = (i & BB_EOF_0 ? 1 : 0);
  97. while (!feof(streams[i])) {
  98. if ((order < 0 && i) || (order > 0 && !i))
  99. writeline(thisline[i], i);
  100. fgets(thisline[i], LINE_LEN, streams[i]);
  101. }
  102. break;
  103. } else {
  104. if (order >= 0)
  105. fgets(thisline[1], LINE_LEN, streams[1]);
  106. if (order <= 0)
  107. fgets(thisline[0], LINE_LEN, streams[0]);
  108. }
  109. }
  110. fclose(streams[0]);
  111. fclose(streams[1]);
  112. }
  113. int comm_main(int argc, char **argv)
  114. {
  115. unsigned long flags;
  116. flags = bb_getopt_ulflags(argc, argv, "123");
  117. if (optind + 2 != argc)
  118. bb_show_usage();
  119. only_file_1 = !(flags & COMM_OPT_1);
  120. only_file_2 = !(flags & COMM_OPT_2);
  121. both = !(flags & COMM_OPT_3);
  122. cmp_files(argv + optind);
  123. exit(EXIT_SUCCESS);
  124. }