Handle '-' consistently

In general, POSIX does not define /dev/std{in, out, err} because it
does not want to depend on the dev-filesystem.
For utilities, it thus introduced the '-'-keyword to denote standard
input (and output in some cases) and the programs have to deal with
it accordingly.

Sadly, the design of many tools doesn't allow strict shell-redirections
and many scripts don't even use this feature when possible.

Thus, we made the decision to implement it consistently across all
tools where it makes sense (namely those which read files).

Along the way, I spotted some behavioural bugs in libutil/crypt.c and
others where it was forgotten to fshut the files after use.
This commit is contained in:
FRIGN 2015-05-15 13:28:39 +02:00 committed by sin
parent 6be7eaff1b
commit 0545d32ce9
21 changed files with 121 additions and 69 deletions

10
cat.c
View File

@ -30,18 +30,18 @@ main(int argc, char *argv[])
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if ((*argv)[0] == '-' && !(*argv)[1]) { if ((*argv)[0] == '-' && !(*argv)[1]) {
concat(stdin, "<stdin>", stdout, "<stdout>"); *argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) { } else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
} else { continue;
}
concat(fp, *argv, stdout, "<stdout>"); concat(fp, *argv, stdout, "<stdout>");
if (fshut(fp, *argv)) { if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }
}
}
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

10
cksum.c
View File

@ -96,17 +96,19 @@ main(int argc, char *argv[])
cksum(stdin, NULL); cksum(stdin, NULL);
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
cksum(fp, *argv); cksum(fp, *argv);
if (fshut(fp, *argv)) { if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }
}
return fshut(stdout, "<stdout>") || ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

7
cols.c
View File

@ -48,13 +48,16 @@ main(int argc, char *argv[])
getlines(stdin, &b); getlines(stdin, &b);
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
getlines(fp, &b); getlines(fp, &b);
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }

11
cut.c
View File

@ -187,20 +187,19 @@ main(int argc, char *argv[])
cut(stdin, "<stdin>"); cut(stdin, "<stdin>");
else { else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (*argv[0] == '-' && !*argv[1]) { if ((*argv)[0] == '-' && !(*argv)[1]) {
cut(stdin, "<stdin>"); *argv = "<stdin>";
} else { fp = stdin;
if (!(fp = fopen(*argv, "r"))) { } else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
cut(fp, *argv); cut(fp, *argv);
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }
}
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

View File

@ -110,13 +110,16 @@ main(int argc, char *argv[])
expand("<stdin>", stdin); expand("<stdin>", stdin);
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
expand(*argv, fp); expand(*argv, fp);
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }

13
fold.c
View File

@ -95,14 +95,17 @@ main(int argc, char *argv[])
fold(stdin, "<stdin>"); fold(stdin, "<stdin>");
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
} else { continue;
fold(fp, *argv);
if (fshut(fp, *argv))
ret = 1;
} }
fold(fp, *argv);
if (fp != stdin && fshut(fp, *argv))
ret = 1;
} }
} }

13
grep.c
View File

@ -259,17 +259,20 @@ main(int argc, char *argv[])
if (argc == 0) { if (argc == 0) {
match = grep(stdin, "<stdin>"); match = grep(stdin, "<stdin>");
} else { } else {
for (i = 0; i < argc; i++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(argv[i], "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
if (!sflag) if (!sflag)
weprintf("fopen %s:", argv[i]); weprintf("fopen %s:", *argv);
match = Error; match = Error;
continue; continue;
} }
m = grep(fp, argv[i]); m = grep(fp, *argv);
if (m == Error || (match != Error && m == Match)) if (m == Error || (match != Error && m == Match))
match = m; match = m;
if (fshut(fp, argv[i])) if (fp != stdin && fshut(fp, *argv))
match = Error; match = Error;
} }
} }

9
head.c
View File

@ -32,7 +32,7 @@ main(int argc, char *argv[])
{ {
size_t n = 10; size_t n = 10;
FILE *fp; FILE *fp;
int ret = 0, newline, many; int ret = 0, newline = 0, many = 0;
ARGBEGIN { ARGBEGIN {
case 'n': case 'n':
@ -50,7 +50,10 @@ main(int argc, char *argv[])
} else { } else {
many = argc > 1; many = argc > 1;
for (newline = 0; *argv; argc--, argv++) { for (newline = 0; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
@ -62,7 +65,7 @@ main(int argc, char *argv[])
} }
newline = 1; newline = 1;
head(fp, *argv, n); head(fp, *argv, n);
if(fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }

2
join.c
View File

@ -548,5 +548,5 @@ main(int argc, char *argv[])
if (fp[0] != fp[1]) if (fp[0] != fp[1])
enfshut(2, fp[1], argv[1]); enfshut(2, fp[1], argv[1]);
enfshut(2, stdout, "<stdout>"); enfshut(2, stdout, "<stdout>");
exit(0); return 0;
} }

View File

@ -126,18 +126,24 @@ cryptmain(int argc, char *argv[], struct crypt_ops *ops, uint8_t *md, size_t sz)
mdprint(md, "<stdin>", sz); mdprint(md, "<stdin>", sz);
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
if (cryptsum(ops, fp, *argv, md) == 1) if (cryptsum(ops, fp, *argv, md)) {
ret = 1; ret = 1;
else } else {
mdprint(md, *argv, sz); mdprint(md, *argv, sz);
fclose(fp); }
if (fp != stdin && fshut(fp, *argv))
ret = 1;
} }
} }
return ret; return ret;
} }

10
nl.c
View File

@ -190,11 +190,15 @@ main(int argc, char *argv[])
if (!argc) { if (!argc) {
nl("<stdin>", stdin); nl("<stdin>", stdin);
} else { } else {
if (!(fp = fopen(argv[0], "r"))) if (argv[0][0] == '-' && !argv[0][1]) {
argv[0] = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(argv[0], "r"))) {
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
}
nl(argv[0], fp); nl(argv[0], fp);
} }
return !!((fp && fshut(fp, argv[0])) + fshut(stdin, "<stdin>") + return !!((fp && fp != stdin && fshut(fp, argv[0]))
fshut(stdout, "<stdout>")); + fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>"));
} }

View File

@ -116,7 +116,8 @@ main(int argc, char *argv[])
dsc = ereallocarray(NULL, argc, sizeof(*dsc)); dsc = ereallocarray(NULL, argc, sizeof(*dsc));
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-")) { if (argv[i][0] == '-' && !argv[i][1]) {
argv[i] = "<stdin>";
dsc[i].fp = stdin; dsc[i].fp = stdin;
} else if (!(dsc[i].fp = fopen(argv[i], "r"))) { } else if (!(dsc[i].fp = fopen(argv[i], "r"))) {
eprintf("fopen %s:", argv[i]); eprintf("fopen %s:", argv[i]);
@ -134,5 +135,5 @@ main(int argc, char *argv[])
if (dsc[i].fp != stdin && fshut(dsc[i].fp, argv[i])) if (dsc[i].fp != stdin && fshut(dsc[i].fp, argv[i]))
ret = 1; ret = 1;
return fshut(stdin, "<stdin>") || fshut(stdout, "<stdout>") || ret; return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
} }

3
sed.c
View File

@ -1114,7 +1114,7 @@ next_file(void)
if (first) /* given no files, default to stdin */ if (first) /* given no files, default to stdin */
file = stdin; file = stdin;
/* else we've used all our files, leave file = NULL */ /* else we've used all our files, leave file = NULL */
} else if (!strcmp(*files, "-")) { } else if ((*files)[0] == '-' && !(*files)[1]) {
file = stdin; file = stdin;
files++; files++;
} else if (!(file = fopen(*files++, "r"))) { } else if (!(file = fopen(*files++, "r"))) {
@ -1123,6 +1123,7 @@ next_file(void)
} }
} while (!file && *files); } while (!file && *files);
first = 0; first = 0;
return !file; return !file;
} }

7
sort.c
View File

@ -301,7 +301,10 @@ main(int argc, char *argv[])
getlines(stdin, &linebuf); getlines(stdin, &linebuf);
} }
} else for (; *argv; argc--, argv++) { } else for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
enprintf(2, "fopen %s:", *argv); enprintf(2, "fopen %s:", *argv);
continue; continue;
} }
@ -310,7 +313,7 @@ main(int argc, char *argv[])
} else { } else {
getlines(fp, &linebuf); getlines(fp, &linebuf);
} }
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }

View File

@ -79,13 +79,16 @@ main(int argc, char *argv[])
strings(stdin, "<stdin>", len); strings(stdin, "<stdin>", len);
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
strings(fp, *argv, len); strings(fp, *argv, len);
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }

9
tail.c
View File

@ -83,7 +83,7 @@ main(int argc, char *argv[])
struct stat st1, st2; struct stat st1, st2;
FILE *fp; FILE *fp;
size_t tmpsize; size_t tmpsize;
int ret = 0, newline, many; int ret = 0, newline = 0, many = 0;
char *numstr, *tmp; char *numstr, *tmp;
void (*tail)(FILE *, const char *) = taketail; void (*tail)(FILE *, const char *) = taketail;
@ -115,7 +115,10 @@ main(int argc, char *argv[])
if ((many = argc > 1) && fflag) if ((many = argc > 1) && fflag)
usage(); usage();
for (newline = 0; *argv; argc--, argv++) { for (newline = 0; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
@ -130,7 +133,7 @@ main(int argc, char *argv[])
tail(fp, *argv); tail(fp, *argv);
if (!fflag) { if (!fflag) {
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
continue; continue;
} }

View File

@ -153,16 +153,21 @@ main(int argc, char *argv[])
unexpand("<stdin>", stdin); unexpand("<stdin>", stdin);
} else { } else {
for (; *argv; argc--, argv++) { for (; *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
unexpand(*argv, fp); unexpand(*argv, fp);
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
} }
efshut(stdin, "<stdin>");
efshut(stdout, "<stdout>");
return ret; return ret;
} }

19
uniq.c
View File

@ -96,7 +96,7 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
FILE *fp = stdin, *ofp = stdout; FILE *fp, *ofp;
ARGBEGIN { ARGBEGIN {
case 'c': case 'c':
@ -124,16 +124,23 @@ main(int argc, char *argv[])
if (!argc) { if (!argc) {
uniq(stdin, stdout); uniq(stdin, stdout);
} else { } else {
if (strcmp(argv[0], "-") && !(fp = fopen(argv[0], "r"))) if (argv[0][0] == '-' && !argv[0][1]) {
argv[0] = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(argv[0], "r"))) {
eprintf("fopen %s:", argv[0]); eprintf("fopen %s:", argv[0]);
if (argc == 2) { }
if (strcmp(argv[1], "-") && !(ofp = fopen(argv[1], "w"))) if (argc == 1 || (argv[1][0] == '-' && !argv[1][1])) {
argv[1] = "<stdout>";
ofp = stdout;
} else if (!(ofp = fopen(argv[1], "w"))) {
eprintf("fopen %s:", argv[1]); eprintf("fopen %s:", argv[1]);
} }
uniq(fp, ofp); uniq(fp, ofp);
} }
uniqfinish(ofp); uniqfinish(ofp);
return !!(fshut(fp, fp == stdin ? "<stdin>" : argv[0]) + efshut(fp, argv[0]);
fshut(ofp, ofp == stdout ? "<stdout>" : argv[1])); efshut(ofp, argv[1]);
return 0;
} }

View File

@ -246,7 +246,7 @@ main(int argc, char *argv[])
if (argc > 1) if (argc > 1)
usage(); usage();
if (!argc) { if (!argc || (argv[0][0] == '-' && !argv[0][1])) {
fp = stdin; fp = stdin;
ifname = "<stdin>"; ifname = "<stdin>";
} else { } else {

View File

@ -114,7 +114,7 @@ main(int argc, char *argv[])
if (!argc || argc > 2) if (!argc || argc > 2)
usage(); usage();
if (argc == 1) { if (argc == 1 || (argv[0][0] == '-' && !argv[0][1])) {
if (mflag) if (mflag)
uuencodeb64(stdin, argv[0], "<stdin>"); uuencodeb64(stdin, argv[0], "<stdin>");
else else

7
wc.c
View File

@ -84,13 +84,16 @@ main(int argc, char *argv[])
wc(stdin, NULL); wc(stdin, NULL);
} else { } else {
for (many = (argc > 1); *argv; argc--, argv++) { for (many = (argc > 1); *argv; argc--, argv++) {
if (!(fp = fopen(*argv, "r"))) { if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv); weprintf("fopen %s:", *argv);
ret = 1; ret = 1;
continue; continue;
} }
wc(fp, *argv); wc(fp, *argv);
if (fshut(fp, *argv)) if (fp != stdin && fshut(fp, *argv))
ret = 1; ret = 1;
} }
if (many) if (many)