Handle stdin properly in cmp(1) and remove path comparison

Same paths are undefined behaviour, let's not endorse it.
This commit is contained in:
FRIGN 2015-02-07 21:36:36 +01:00
parent d756dca999
commit cb1b5d532b

15
cmp.c
View File

@ -35,12 +35,11 @@ main(int argc, char *argv[])
if (argc != 2 || (lflag && sflag)) if (argc != 2 || (lflag && sflag))
usage(); usage();
if (!strcmp(argv[0], argv[1]))
return Same;
for (n = 0; n < 2; n++) { for (n = 0; n < 2; n++) {
if (argv[n][0] == '-' && !argv[n][1]) if (argv[n][0] == '-' && !argv[n][1]) {
argv[n] = "/dev/fd/0"; argv[n] = "<stdin>";
fp[n] = stdin;
} else {
fp[n] = fopen(argv[n], "r"); fp[n] = fopen(argv[n], "r");
if (!fp[n]) { if (!fp[n]) {
if (!sflag) if (!sflag)
@ -48,6 +47,7 @@ main(int argc, char *argv[])
exit(Error); exit(Error);
} }
} }
}
for (n = 1; ; n++) { for (n = 1; ; n++) {
b[0] = getc(fp[0]); b[0] = getc(fp[0]);
@ -76,5 +76,10 @@ main(int argc, char *argv[])
same = 0; same = 0;
} }
} }
for (n = 1; n < 2; n++)
if (fp[n] != stdin)
fclose(fp[n]);
return same ? Same : Diff; return same ? Same : Diff;
} }