s/getopt/ARGBEGIN/ wc

This commit is contained in:
Federico G. Benavento 2013-03-10 21:12:10 -03:00
parent ae3423e366
commit 5c7b7e3fa8

29
wc.c
View File

@ -17,15 +17,15 @@ static long tc = 0, tl = 0, tw = 0;
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
bool many;
char c;
FILE *fp; FILE *fp;
int i;
while((c = getopt(argc, argv, "clmw")) != -1) ARGBEGIN {
switch(c) {
case 'c': case 'c':
cmode = 'c';
break;
case 'm': case 'm':
cmode = c; cmode = 'm';
break; break;
case 'l': case 'l':
lflag = true; lflag = true;
@ -34,20 +34,21 @@ main(int argc, char *argv[])
wflag = true; wflag = true;
break; break;
default: default:
exit(EXIT_FAILURE); eprintf("usage: %s [-clmw] [files...]\n", argv0);
} } ARGEND;
many = (argc > optind+1);
if(optind == argc) if (argc == 0) {
wc(stdin, NULL); wc(stdin, NULL);
else for(; optind < argc; optind++) { } else {
if(!(fp = fopen(argv[optind], "r"))) for (i = 0; i < argc; i++) {
eprintf("fopen %s:", argv[optind]); if(!(fp = fopen(argv[i], "r")))
wc(fp, argv[optind]); eprintf("fopen %s:", argv[i]);
wc(fp, argv[i]);
fclose(fp); fclose(fp);
} }
if(many) if (argc > 1)
output("total", tc, tl, tw); output("total", tc, tl, tw);
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }