diff options
Diffstat (limited to 'coreutils/cmp.c')
-rw-r--r-- | coreutils/cmp.c | 47 |
1 files changed, 21 insertions, 26 deletions
diff --git a/coreutils/cmp.c b/coreutils/cmp.c index a569eb3fe..ae3f50279 100644 --- a/coreutils/cmp.c +++ b/coreutils/cmp.c | |||
@@ -23,7 +23,7 @@ | |||
23 | 23 | ||
24 | #include "busybox.h" | 24 | #include "busybox.h" |
25 | 25 | ||
26 | static FILE *cmp_xfopen_input(const char *filename) | 26 | static FILE *cmp_xfopen_input(const char * const filename) |
27 | { | 27 | { |
28 | FILE *fp; | 28 | FILE *fp; |
29 | 29 | ||
@@ -40,32 +40,28 @@ static const char fmt_differ[] = "%s %s differ: char %d, line %d\n"; | |||
40 | static const char fmt_l_opt[] = "%.0s%.0s%d %3o %3o\n"; | 40 | static const char fmt_l_opt[] = "%.0s%.0s%d %3o %3o\n"; |
41 | 41 | ||
42 | static const char opt_chars[] = "sl"; | 42 | static const char opt_chars[] = "sl"; |
43 | 43 | #define CMP_OPT_s (1<<0) | |
44 | enum { | 44 | #define CMP_OPT_l (1<<1) |
45 | OPT_s = 1, | ||
46 | OPT_l = 2 | ||
47 | }; | ||
48 | 45 | ||
49 | int cmp_main(int argc, char **argv) | 46 | int cmp_main(int argc, char **argv) |
50 | { | 47 | { |
51 | FILE *fp1, *fp2, *outfile = stdout; | 48 | FILE *fp1, *fp2, *outfile = stdout; |
52 | const char *filename1, *filename2; | 49 | const char *filename1, *filename2 = "-"; |
53 | const char *fmt; | 50 | const char *fmt; |
54 | int c1, c2, char_pos, line_pos; | 51 | int c1, c2, char_pos = 0, line_pos = 1; |
55 | int opt_flags; | 52 | unsigned opt; |
56 | int exit_val = 0; | 53 | int retval = 0; |
57 | 54 | ||
58 | bb_default_error_retval = 2; /* 1 is returned if files are different. */ | 55 | bb_default_error_retval = 2; /* 1 is returned if files are different. */ |
59 | 56 | ||
60 | opt_flags = bb_getopt_ulflags(argc, argv, opt_chars); | 57 | opt = bb_getopt_ulflags(argc, argv, opt_chars); |
61 | 58 | ||
62 | if ((opt_flags == 3) || (((unsigned int)(--argc - optind)) > 1)) { | 59 | if ((opt & (CMP_OPT_s|CMP_OPT_l)) |
60 | || (((unsigned int)(--argc - optind)) > 1)) | ||
63 | bb_show_usage(); | 61 | bb_show_usage(); |
64 | } | ||
65 | 62 | ||
66 | fp1 = cmp_xfopen_input(filename1 = *(argv += optind)); | 63 | fp1 = cmp_xfopen_input(filename1 = *(argv += optind)); |
67 | 64 | ||
68 | filename2 = "-"; | ||
69 | if (*++argv) { | 65 | if (*++argv) { |
70 | filename2 = *argv; | 66 | filename2 = *argv; |
71 | } | 67 | } |
@@ -79,19 +75,17 @@ int cmp_main(int argc, char **argv) | |||
79 | return 0; | 75 | return 0; |
80 | } | 76 | } |
81 | 77 | ||
82 | fmt = fmt_differ; | 78 | if (opt & CMP_OPT_l) |
83 | if (opt_flags == OPT_l) { | ||
84 | fmt = fmt_l_opt; | 79 | fmt = fmt_l_opt; |
85 | } | 80 | else |
81 | fmt = fmt_differ; | ||
86 | 82 | ||
87 | char_pos = 0; | ||
88 | line_pos = 1; | ||
89 | do { | 83 | do { |
90 | c1 = getc(fp1); | 84 | c1 = getc(fp1); |
91 | c2 = getc(fp2); | 85 | c2 = getc(fp2); |
92 | ++char_pos; | 86 | ++char_pos; |
93 | if (c1 != c2) { /* Remember -- a read error may have occurred. */ | 87 | if (c1 != c2) { /* Remember: a read error may have occurred. */ |
94 | exit_val = 1; /* But assume the files are different for now. */ | 88 | retval = 1; /* But assume the files are different for now. */ |
95 | if (c2 == EOF) { | 89 | if (c2 == EOF) { |
96 | /* We know that fp1 isn't at EOF or in an error state. But to | 90 | /* We know that fp1 isn't at EOF or in an error state. But to |
97 | * save space below, things are setup to expect an EOF in fp1 | 91 | * save space below, things are setup to expect an EOF in fp1 |
@@ -109,13 +103,14 @@ int cmp_main(int argc, char **argv) | |||
109 | * make sure we fflush before writing to stderr. */ | 103 | * make sure we fflush before writing to stderr. */ |
110 | xfflush_stdout(); | 104 | xfflush_stdout(); |
111 | } | 105 | } |
112 | if (opt_flags != OPT_s) { | 106 | if (!opt & CMP_OPT_s) { |
113 | if (opt_flags == OPT_l) { | 107 | if (opt & CMP_OPT_l) { |
114 | line_pos = c1; /* line_pos is unused in the -l case. */ | 108 | line_pos = c1; /* line_pos is unused in the -l case. */ |
115 | } | 109 | } |
116 | bb_fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2); | 110 | bb_fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2); |
117 | if (opt_flags) { /* This must be -l since not -s. */ | 111 | if (opt) { /* This must be -l since not -s. */ |
118 | /* If we encountered and EOF, the while check will catch it. */ | 112 | /* If we encountered an EOF, |
113 | * the while check will catch it. */ | ||
119 | continue; | 114 | continue; |
120 | } | 115 | } |
121 | } | 116 | } |
@@ -129,5 +124,5 @@ int cmp_main(int argc, char **argv) | |||
129 | xferror(fp1, filename1); | 124 | xferror(fp1, filename1); |
130 | xferror(fp2, filename2); | 125 | xferror(fp2, filename2); |
131 | 126 | ||
132 | bb_fflush_stdout_and_exit(exit_val); | 127 | bb_fflush_stdout_and_exit(retval); |
133 | } | 128 | } |