[hackers] [sbase] s/getopt/ARGBEGIN/ wc || Federico G. Benavento

From: <git_AT_suckless.org>
Date: Mon, 11 Mar 2013 01:12:34 +0100

commit 5c7b7e3fa85157e9771304ab0b5a0ea8218b25ae
Author: Federico G. Benavento <benavento_AT_gmail.com>
Date: Sun Mar 10 21:12:10 2013 -0300

    s/getopt/ARGBEGIN/ wc

diff --git a/wc.c b/wc.c
index d8f4c98..0f3ef82 100644
--- a/wc.c
+++ b/wc.c
_AT_@ -17,37 +17,38 @@ static long tc = 0, tl = 0, tw = 0;
 int
 main(int argc, char *argv[])
 {
- bool many;
- char c;
         FILE *fp;
-
- while((c = getopt(argc, argv, "clmw")) != -1)
- switch(c) {
- case 'c':
- case 'm':
- cmode = c;
- break;
- case 'l':
- lflag = true;
- break;
- case 'w':
- wflag = true;
- break;
- default:
- exit(EXIT_FAILURE);
- }
- many = (argc > optind+1);
-
- if(optind == argc)
+ int i;
+
+ ARGBEGIN {
+ case 'c':
+ cmode = 'c';
+ break;
+ case 'm':
+ cmode = 'm';
+ break;
+ case 'l':
+ lflag = true;
+ break;
+ case 'w':
+ wflag = true;
+ break;
+ default:
+ eprintf("usage: %s [-clmw] [files...]
", argv0);
+ } ARGEND;
+
+ 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]);
- fclose(fp);
+ } 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 (argc > 1)
+ output("total", tc, tl, tw);
         }
- if(many)
- output("total", tc, tl, tw);
         return EXIT_SUCCESS;
 }
 
Received on Mon Mar 11 2013 - 01:12:34 CET

This archive was generated by hypermail 2.3.0 : Mon Mar 11 2013 - 01:24:07 CET