3
0

cmp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 source tree.
  8. */
  9. /* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */
  11. #include "libbb.h"
  12. static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n";
  13. static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"u, line %u\n";
  14. // This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"u %o %o\n"
  15. static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"u %3o %3o\n";
  16. static const char opt_chars[] ALIGN1 = "sl";
  17. #define CMP_OPT_s (1<<0)
  18. #define CMP_OPT_l (1<<1)
  19. int cmp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  20. int cmp_main(int argc UNUSED_PARAM, char **argv)
  21. {
  22. FILE *fp1, *fp2, *outfile = stdout;
  23. const char *filename1, *filename2 = "-";
  24. off_t skip1 = 0, skip2 = 0, char_pos = 0;
  25. int line_pos = 1; /* Hopefully won't overflow... */
  26. const char *fmt;
  27. int c1, c2;
  28. unsigned opt;
  29. int retval = 0;
  30. opt_complementary = "-1"
  31. IF_DESKTOP(":?4")
  32. IF_NOT_DESKTOP(":?2")
  33. ":l--s:s--l";
  34. opt = getopt32(argv, opt_chars);
  35. argv += optind;
  36. filename1 = *argv;
  37. if (*++argv) {
  38. filename2 = *argv;
  39. if (ENABLE_DESKTOP && *++argv) {
  40. skip1 = XATOOFF(*argv);
  41. if (*++argv) {
  42. skip2 = XATOOFF(*argv);
  43. }
  44. }
  45. }
  46. xfunc_error_retval = 2; /* missing file results in exitcode 2 */
  47. if (opt & CMP_OPT_s)
  48. logmode = 0; /* -s suppresses open error messages */
  49. fp1 = xfopen_stdin(filename1);
  50. fp2 = xfopen_stdin(filename2);
  51. if (fp1 == fp2) { /* Paranoia check... stdin == stdin? */
  52. /* Note that we don't bother reading stdin. Neither does gnu wc.
  53. * But perhaps we should, so that other apps down the chain don't
  54. * get the input. Consider 'echo hello | (cmp - - && cat -)'.
  55. */
  56. return 0;
  57. }
  58. logmode = LOGMODE_STDIO;
  59. if (opt & CMP_OPT_l)
  60. fmt = fmt_l_opt;
  61. else
  62. fmt = fmt_differ;
  63. if (ENABLE_DESKTOP) {
  64. while (skip1) { getc(fp1); skip1--; }
  65. while (skip2) { getc(fp2); skip2--; }
  66. }
  67. do {
  68. c1 = getc(fp1);
  69. c2 = getc(fp2);
  70. ++char_pos;
  71. if (c1 != c2) { /* Remember: a read error may have occurred. */
  72. retval = 1; /* But assume the files are different for now. */
  73. if (c2 == EOF) {
  74. /* We know that fp1 isn't at EOF or in an error state. But to
  75. * save space below, things are setup to expect an EOF in fp1
  76. * if an EOF occurred. So, swap things around.
  77. */
  78. fp1 = fp2;
  79. filename1 = filename2;
  80. c1 = c2;
  81. }
  82. if (c1 == EOF) {
  83. die_if_ferror(fp1, filename1);
  84. fmt = fmt_eof; /* Well, no error, so it must really be EOF. */
  85. outfile = stderr;
  86. /* There may have been output to stdout (option -l), so
  87. * make sure we fflush before writing to stderr. */
  88. fflush_all();
  89. }
  90. if (!(opt & CMP_OPT_s)) {
  91. if (opt & CMP_OPT_l) {
  92. line_pos = c1; /* line_pos is unused in the -l case. */
  93. }
  94. fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
  95. if (opt) { /* This must be -l since not -s. */
  96. /* If we encountered an EOF,
  97. * the while check will catch it. */
  98. continue;
  99. }
  100. }
  101. break;
  102. }
  103. if (c1 == '\n') {
  104. ++line_pos;
  105. }
  106. } while (c1 != EOF);
  107. die_if_ferror(fp1, filename1);
  108. die_if_ferror(fp2, filename2);
  109. fflush_stdout_and_exit(retval);
  110. }