s/getopt/ARGBEGIN/ wc

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

57
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)
switch(c) { ARGBEGIN {
case 'c': case 'c':
case 'm': cmode = 'c';
cmode = c; break;
break; case 'm':
case 'l': cmode = 'm';
lflag = true; break;
break; case 'l':
case 'w': lflag = true;
wflag = true; break;
break; case 'w':
default: wflag = true;
exit(EXIT_FAILURE); break;
} default:
many = (argc > optind+1); eprintf("usage: %s [-clmw] [files...]\n", argv0);
} 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;
} }