comm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "libbb.h"
  10. #define COMM_OPT_1 (1 << 0)
  11. #define COMM_OPT_2 (1 << 1)
  12. #define COMM_OPT_3 (1 << 2)
  13. /* writeline outputs the input given, appropriately aligned according to class */
  14. static void writeline(char *line, int class, int flags)
  15. {
  16. if (class == 0) {
  17. if (flags & COMM_OPT_1)
  18. return;
  19. } else if (class == 1) {
  20. if (flags & COMM_OPT_2)
  21. return;
  22. if (!(flags & COMM_OPT_1))
  23. putchar('\t');
  24. } else /*if (class == 2)*/ {
  25. if (flags & COMM_OPT_3)
  26. return;
  27. if (!(flags & COMM_OPT_1))
  28. putchar('\t');
  29. if (!(flags & COMM_OPT_2))
  30. putchar('\t');
  31. }
  32. fputs(line, stdout);
  33. }
  34. int comm_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  35. int comm_main(int argc ATTRIBUTE_UNUSED, char **argv)
  36. {
  37. #define LINE_LEN 100
  38. #define BB_EOF_0 0x1
  39. #define BB_EOF_1 0x2
  40. char thisline[2][LINE_LEN];
  41. FILE *streams[2];
  42. int i;
  43. unsigned flags;
  44. opt_complementary = "=2";
  45. flags = getopt32(argv, "123");
  46. argv += optind;
  47. for (i = 0; i < 2; ++i) {
  48. streams[i] = (argv[i][0] == '-' && !argv[i][1]) ? stdin : xfopen(argv[i], "r");
  49. fgets(thisline[i], LINE_LEN, streams[i]);
  50. }
  51. /* This is the real core of the program - lines are compared here */
  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, flags);
  71. else if (order > 0 && !(i & BB_EOF_1))
  72. writeline(thisline[1], 1, flags);
  73. else if (order < 0 && !(i & BB_EOF_0))
  74. writeline(thisline[0], 0, flags);
  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, flags);
  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. if (ENABLE_FEATURE_CLEAN_UP) {
  93. fclose(streams[0]);
  94. fclose(streams[1]);
  95. }
  96. return EXIT_SUCCESS;
  97. }