aboutsummaryrefslogtreecommitdiff
path: root/cmp.c
diff options
context:
space:
mode:
Diffstat (limited to 'cmp.c')
-rw-r--r--cmp.c32
1 files changed, 23 insertions, 9 deletions
diff --git a/cmp.c b/cmp.c
index 24b6c3f92..6d579461d 100644
--- a/cmp.c
+++ b/cmp.c
@@ -26,28 +26,42 @@
26#include <string.h> 26#include <string.h>
27#include <errno.h> 27#include <errno.h>
28#include <stdlib.h> 28#include <stdlib.h>
29#include <getopt.h>
29#include "busybox.h" 30#include "busybox.h"
30 31
31int cmp_main(int argc, char **argv) 32int cmp_main(int argc, char **argv)
32{ 33{
33 FILE *fp1 = NULL, *fp2 = stdin; 34 FILE *fp1 = NULL, *fp2 = stdin;
34 char *filename1 = argv[1], *filename2 = "-"; 35 char *filename1, *filename2 = "-";
35 int c1, c2, char_pos = 1, line_pos = 1; 36 int c, c1, c2, char_pos = 1, line_pos = 1, silent = FALSE;
36 37
37 /* parse argv[] */ 38 while ((c = getopt(argc, argv, "s")) != EOF) {
38 if (argc < 2 || 3 < argc) 39 switch (c) {
39 show_usage(); 40 case 's':
41 silent = TRUE;
42 break;
43 default:
44 show_usage();
45 }
46 }
40 47
41 fp1 = xfopen(argv[1], "r"); 48 filename1 = argv[optind];
42 if (argv[2] != NULL) { 49 switch (argc - optind) {
43 fp2 = xfopen(argv[2], "r"); 50 case 2:
44 filename2 = argv[2]; 51 fp2 = xfopen(filename2 = argv[optind + 1], "r");
52 case 1:
53 fp1 = xfopen(filename1, "r");
54 break;
55 default:
56 show_usage();
45 } 57 }
46 58
47 do { 59 do {
48 c1 = fgetc(fp1); 60 c1 = fgetc(fp1);
49 c2 = fgetc(fp2); 61 c2 = fgetc(fp2);
50 if (c1 != c2) { 62 if (c1 != c2) {
63 if (silent)
64 return EXIT_FAILURE;
51 if (c1 == EOF) 65 if (c1 == EOF)
52 printf("EOF on %s\n", filename1); 66 printf("EOF on %s\n", filename1);
53 else if (c2 == EOF) 67 else if (c2 == EOF)