Implement cat -u and report correct exit condition

This commit is contained in:
sin 2014-11-13 14:15:20 +00:00
parent 5773a8f22b
commit c59894bd5c

18
cat.c
View File

@ -9,7 +9,7 @@
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [file...]\n", argv0); eprintf("usage: %s [-u] [file...]\n", argv0);
} }
int int
@ -17,9 +17,12 @@ main(int argc, char *argv[])
{ {
char *p; char *p;
FILE *fp; FILE *fp;
int i; int ret = 0;
ARGBEGIN { ARGBEGIN {
case 'u':
setbuf(stdout, NULL);
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -27,17 +30,18 @@ main(int argc, char *argv[])
if(argc == 0) { if(argc == 0) {
concat(stdin, "<stdin>", stdout, "<stdout>"); concat(stdin, "<stdin>", stdout, "<stdout>");
} else { } else {
for(i = 0; i < argc; i++) { for (; argc; argc--, argv++) {
p = argv[i]; p = argv[0];
if (argv[i][0] == '-') if (argv[0][0] == '-')
p = "/dev/fd/0"; p = "/dev/fd/0";
if(!(fp = fopen(p, "r"))) { if(!(fp = fopen(p, "r"))) {
weprintf("fopen %s:", argv[i]); weprintf("fopen %s:", p);
ret = 1;
continue; continue;
} }
concat(fp, p, stdout, "<stdout>"); concat(fp, p, stdout, "<stdout>");
fclose(fp); fclose(fp);
} }
} }
return 0; return ret;
} }