s/getopt/ARGBEGIN/ wc

This commit is contained in:
Federico G. Benavento 2013-03-10 21:12:10 -03:00
parent ae3423e366
commit 5c7b7e3fa8
1 changed files with 29 additions and 28 deletions

29
wc.c
View File

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