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

53
wc.c
View File

@ -17,37 +17,38 @@ 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';
case 'm': break;
cmode = c; case 'm':
break; cmode = 'm';
case 'l': break;
lflag = true; case 'l':
break; lflag = true;
case 'w': break;
wflag = true; case 'w':
break; wflag = true;
default: break;
exit(EXIT_FAILURE); default:
} eprintf("usage: %s [-clmw] [files...]\n", argv0);
many = (argc > optind+1); } ARGEND;
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]);
fclose(fp); wc(fp, argv[i]);
fclose(fp);
}
if (argc > 1)
output("total", tc, tl, tw);
} }
if(many)
output("total", tc, tl, tw);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }