cmp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "busybox.h"
  22. static FILE *cmp_xfopen_input(const char * const filename)
  23. {
  24. FILE *fp;
  25. fp = fopen_or_warn_stdin(filename);
  26. if (fp)
  27. return fp;
  28. exit(xfunc_error_retval); /* We already output an error message. */
  29. }
  30. static const char fmt_eof[] = "cmp: EOF on %s\n";
  31. static const char fmt_differ[] = "%s %s differ: char %d, line %d\n";
  32. // This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%d %o %o\n"
  33. static const char fmt_l_opt[] = "%.0s%.0s%d %3o %3o\n";
  34. static const char opt_chars[] = "sl";
  35. #define CMP_OPT_s (1<<0)
  36. #define CMP_OPT_l (1<<1)
  37. int cmp_main(int argc, char **argv)
  38. {
  39. FILE *fp1, *fp2, *outfile = stdout;
  40. const char *filename1, *filename2 = "-";
  41. const char *fmt;
  42. int c1, c2, char_pos = 0, line_pos = 1;
  43. unsigned opt;
  44. int retval = 0;
  45. xfunc_error_retval = 2; /* 1 is returned if files are different. */
  46. opt = getopt32(argc, argv, opt_chars);
  47. if (((opt & (CMP_OPT_s|CMP_OPT_l)) == (CMP_OPT_s|CMP_OPT_l))
  48. || (((unsigned int)(--argc - optind)) > 1))
  49. bb_show_usage();
  50. fp1 = cmp_xfopen_input(filename1 = *(argv += optind));
  51. if (*++argv) {
  52. filename2 = *argv;
  53. }
  54. fp2 = cmp_xfopen_input(filename2);
  55. if (fp1 == fp2) { /* Paranioa check... stdin == stdin? */
  56. /* Note that we don't bother reading stdin. Neither does gnu wc.
  57. * But perhaps we should, so that other apps down the chain don't
  58. * get the input. Consider 'echo hello | (cmp - - && cat -)'.
  59. */
  60. return 0;
  61. }
  62. if (opt & CMP_OPT_l)
  63. fmt = fmt_l_opt;
  64. else
  65. fmt = fmt_differ;
  66. do {
  67. c1 = getc(fp1);
  68. c2 = getc(fp2);
  69. ++char_pos;
  70. if (c1 != c2) { /* Remember: a read error may have occurred. */
  71. retval = 1; /* But assume the files are different for now. */
  72. if (c2 == EOF) {
  73. /* We know that fp1 isn't at EOF or in an error state. But to
  74. * save space below, things are setup to expect an EOF in fp1
  75. * if an EOF occurred. So, swap things around.
  76. */
  77. fp1 = fp2;
  78. filename1 = filename2;
  79. c1 = c2;
  80. }
  81. if (c1 == EOF) {
  82. die_if_ferror(fp1, filename1);
  83. fmt = fmt_eof; /* Well, no error, so it must really be EOF. */
  84. outfile = stderr;
  85. /* There may have been output to stdout (option -l), so
  86. * make sure we fflush before writing to stderr. */
  87. xfflush_stdout();
  88. }
  89. if (!(opt & CMP_OPT_s)) {
  90. if (opt & CMP_OPT_l) {
  91. line_pos = c1; /* line_pos is unused in the -l case. */
  92. }
  93. fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
  94. if (opt) { /* This must be -l since not -s. */
  95. /* If we encountered an EOF,
  96. * the while check will catch it. */
  97. continue;
  98. }
  99. }
  100. break;
  101. }
  102. if (c1 == '\n') {
  103. ++line_pos;
  104. }
  105. } while (c1 != EOF);
  106. die_if_ferror(fp1, filename1);
  107. die_if_ferror(fp2, filename2);
  108. fflush_stdout_and_exit(retval);
  109. }