3
0

cmp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini cmp implementation for busybox
  4. *
  5. * Copyright (C) 2000,2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */
  11. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  12. *
  13. * Original version majorly reworked for SUSv3 compliance, bug fixes, and
  14. * size optimizations. Changes include:
  15. * 1) Now correctly distinguishes between errors and actual file differences.
  16. * 2) Proper handling of '-' args.
  17. * 3) Actual error checking of i/o.
  18. * 4) Accept SUSv3 -l option. Note that we use the slightly nicer gnu format
  19. * in the '-l' case.
  20. */
  21. #include "libbb.h"
  22. static FILE *cmp_xfopen_input(const char *filename)
  23. {
  24. FILE *fp;
  25. fp = fopen_or_warn_stdin(filename);
  26. if (fp)
  27. return fp;
  28. xfunc_die(); /* We already output an error message. */
  29. }
  30. static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n";
  31. static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"d, line %d\n";
  32. // This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"d %o %o\n"
  33. static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"d %3o %3o\n";
  34. static const char opt_chars[] ALIGN1 = "sl";
  35. #define CMP_OPT_s (1<<0)
  36. #define CMP_OPT_l (1<<1)
  37. int cmp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  38. int cmp_main(int argc, char **argv)
  39. {
  40. FILE *fp1, *fp2, *outfile = stdout;
  41. const char *filename1, *filename2 = "-";
  42. USE_DESKTOP(off_t skip1 = 0, skip2 = 0;)
  43. off_t char_pos = 0;
  44. int line_pos = 1; /* Hopefully won't overflow... */
  45. const char *fmt;
  46. int c1, c2;
  47. unsigned opt;
  48. int retval = 0;
  49. xfunc_error_retval = 2; /* 1 is returned if files are different. */
  50. opt_complementary = "-1"
  51. USE_DESKTOP(":?4")
  52. SKIP_DESKTOP(":?2")
  53. ":l--s:s--l";
  54. opt = getopt32(argv, opt_chars);
  55. argv += optind;
  56. filename1 = *argv;
  57. fp1 = cmp_xfopen_input(filename1);
  58. if (*++argv) {
  59. filename2 = *argv;
  60. #if ENABLE_DESKTOP
  61. if (*++argv) {
  62. skip1 = XATOOFF(*argv);
  63. if (*++argv) {
  64. skip2 = XATOOFF(*argv);
  65. }
  66. }
  67. #endif
  68. }
  69. fp2 = cmp_xfopen_input(filename2);
  70. if (fp1 == fp2) { /* Paranoia check... stdin == stdin? */
  71. /* Note that we don't bother reading stdin. Neither does gnu wc.
  72. * But perhaps we should, so that other apps down the chain don't
  73. * get the input. Consider 'echo hello | (cmp - - && cat -)'.
  74. */
  75. return 0;
  76. }
  77. if (opt & CMP_OPT_l)
  78. fmt = fmt_l_opt;
  79. else
  80. fmt = fmt_differ;
  81. #if ENABLE_DESKTOP
  82. while (skip1) { getc(fp1); skip1--; }
  83. while (skip2) { getc(fp2); skip2--; }
  84. #endif
  85. do {
  86. c1 = getc(fp1);
  87. c2 = getc(fp2);
  88. ++char_pos;
  89. if (c1 != c2) { /* Remember: a read error may have occurred. */
  90. retval = 1; /* But assume the files are different for now. */
  91. if (c2 == EOF) {
  92. /* We know that fp1 isn't at EOF or in an error state. But to
  93. * save space below, things are setup to expect an EOF in fp1
  94. * if an EOF occurred. So, swap things around.
  95. */
  96. fp1 = fp2;
  97. filename1 = filename2;
  98. c1 = c2;
  99. }
  100. if (c1 == EOF) {
  101. die_if_ferror(fp1, filename1);
  102. fmt = fmt_eof; /* Well, no error, so it must really be EOF. */
  103. outfile = stderr;
  104. /* There may have been output to stdout (option -l), so
  105. * make sure we fflush before writing to stderr. */
  106. xfflush_stdout();
  107. }
  108. if (!(opt & CMP_OPT_s)) {
  109. if (opt & CMP_OPT_l) {
  110. line_pos = c1; /* line_pos is unused in the -l case. */
  111. }
  112. fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
  113. if (opt) { /* This must be -l since not -s. */
  114. /* If we encountered an EOF,
  115. * the while check will catch it. */
  116. continue;
  117. }
  118. }
  119. break;
  120. }
  121. if (c1 == '\n') {
  122. ++line_pos;
  123. }
  124. } while (c1 != EOF);
  125. die_if_ferror(fp1, filename1);
  126. die_if_ferror(fp2, filename2);
  127. fflush_stdout_and_exit(retval);
  128. }