[hackers] [sbase] We should not require both files to be present for cmp(1) || sin
commit c28e2a39ecf560291a68db3d506a5d2177c7a80d
Author: sin <sin_AT_2f30.org>
Date: Tue Mar 4 10:33:51 2014 +0000
We should not require both files to be present for cmp(1)
If the second file is not present, read from standard input.
diff --git a/cmp.c b/cmp.c
index 6bc97cc..9c0b316 100644
--- a/cmp.c
+++ b/cmp.c
_AT_@ -10,7 +10,7 @@ enum { Same = 0, Diff = 1, Error = 2 };
static void
usage(void)
{
- enprintf(Error, "usage: %s [-ls] file1 file2
", argv0);
+ enprintf(Error, "usage: %s [-ls] file1 [file2]
", argv0);
}
int
_AT_@ -34,13 +34,19 @@ main(int argc, char *argv[])
usage();
} ARGEND;
- if(argc < 2)
+ if (argc < 1 || argc > 2)
usage();
- for(i = 0; i < 2; i++) {
- if(!(fp[i] = fopen(argv[i], "r")))
- enprintf(Error, "fopen %s:", argv[i]);
- }
+ fp[0] = fopen(argv[0], "r");
+ if (!fp[0])
+ enprintf(Error, "fopen %s:", argv[0]);
+ fp[1] = stdin;
+
+ if (argc == 2)
+ fp[1] = fopen(argv[1], "r");
+ if (!fp[1])
+ enprintf(Error, "fopen %s:", argv[1]);
+
for(n = 1; ((b[0] = getc(fp[0])) != EOF) \
| ((b[1] = getc(fp[1])) != EOF); n++) {
if(b[0] == '
')
_AT_@ -49,11 +55,12 @@ main(int argc, char *argv[])
continue;
for(i = 0; i < 2; i++)
if(b[i] == EOF)
- enprintf(Diff, "cmp: EOF on %s
", argv[i]);
+ enprintf(Diff, "cmp: EOF on %s
",
+ !argv[i] ? "<stdin>" : argv[1]);
if(!lflag) {
if(!sflag)
printf("%s %s differ: char %ld, line %ld
",
- argv[0], argv[1], n, line);
+ argv[0], !argv[1] ? "<stdin>" : argv[1], n, line);
exit(Diff);
} else {
printf("%4ld %3o %3o
", n, b[0], b[1]);
_AT_@ -62,4 +69,3 @@ main(int argc, char *argv[])
}
return same ? Same : Diff;
}
-
Received on Tue Mar 04 2014 - 11:37:03 CET
This archive was generated by hypermail 2.3.0
: Tue Mar 04 2014 - 11:48:16 CET