comm.c 2.7 KB

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