sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

commit 9714d7b1d32f97e63ea99ba614673e1e39c051c0
parent 687e5411ee19d822b722e76ea257b8efb8fac429
Author: Connor Lane Smith <cls@lubutu.com>
Date:   Tue, 24 May 2011 01:13:34 +0100

getopt
Diffstat:
Mdate.c | 25+++++++++++++------------
Mecho.c | 20+++++++++++++-------
Mgrep.1 | 7+------
Mgrep.c | 50++++++++++++++++++++++++++------------------------
Mtee.c | 20+++++++++++++-------
Mtouch.c | 22++++++++++++----------
Mwc.1 | 5+----
Mwc.c | 36++++++++++++++++++++----------------
8 files changed, 99 insertions(+), 86 deletions(-)

diff --git a/date.c b/date.c @@ -1,29 +1,30 @@ /* See LICENSE file for copyright and license details. */ #include <stdio.h> #include <stdlib.h> -#include <string.h> #include <time.h> +#include <unistd.h> #include "util.h" int main(int argc, char *argv[]) { - char buf[BUFSIZ]; + char buf[BUFSIZ], c; char *fmt = "%c"; - int i; struct tm *now = NULL; time_t t; t = time(NULL); - for(i = 1; i < argc; i++) - if(!strncmp("+", argv[i], 1)) - fmt = &argv[i][1]; - else if(!strcmp("-d", argv[i]) && i+1 < argc) - t = strtol(argv[++i], NULL, 0); - else - eprintf("usage: %s [-d time] [+format]\n", argv[0]); - now = localtime(&t); - if(!now) + while((c = getopt(argc, argv, "d:")) != -1) + switch(c) { + case 'd': + t = strtol(optarg, NULL, 0); + break; + default: + exit(EXIT_FAILURE); + } + if(optind < argc && argv[optind][0] == '+') + fmt = &argv[optind][1]; + if(!(now = localtime(&t))) eprintf("localtime failed\n"); strftime(buf, sizeof buf, fmt, now); diff --git a/echo.c b/echo.c @@ -2,19 +2,25 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> +#include <unistd.h> int main(int argc, char *argv[]) { bool nflag = false; - int i; + char c; - if(argc > 1 && !strcmp(argv[1], "-n")) - nflag = true; - for(i = nflag ? 2 : 1; i < argc; i++) { - fputs(argv[i], stdout); - if(i+1 < argc) + while((c = getopt(argc, argv, "n")) != -1) + switch(c) { + case 'n': + nflag = true; + break; + default: + exit(EXIT_FAILURE); + } + for(; optind < argc; optind++) { + fputs(argv[optind], stdout); + if(optind+1 < argc) fputc(' ', stdout); } if(!nflag) diff --git a/grep.1 b/grep.1 @@ -3,12 +3,7 @@ grep \- search files for a pattern .SH SYNOPSIS .B grep -.RB [ \-c ] -.RB [ \-i ] -.RB [ \-l ] -.RB [ \-n ] -.RB [ \-q ] -.RB [ \-v ] +.RB [ \-cilnqv ] .I pattern .RI [ file ...] .SH DESCRIPTION diff --git a/grep.c b/grep.c @@ -3,7 +3,7 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> +#include <unistd.h> static void grep(FILE *, const char *, regex_t *); @@ -16,46 +16,48 @@ static char mode = 0; int main(int argc, char *argv[]) { - int i, flags = 0; + char c; + int flags = 0; regex_t preg; FILE *fp; - for(i = 1; i < argc; i++) - if(!strcmp(argv[i], "-c")) - mode = 'c'; - else if(!strcmp(argv[i], "-i")) + while((c = getopt(argc, argv, "cilnqv")) != -1) + switch(c) { + case 'c': + case 'l': + case 'n': + case 'q': + mode = c; + break; + case 'i': iflag = true; - else if(!strcmp(argv[i], "-l")) - mode = 'l'; - else if(!strcmp(argv[i], "-n")) - mode = 'n'; - else if(!strcmp(argv[i], "-q")) - mode = 'q'; - else if(!strcmp(argv[i], "-v")) + break; + case 'v': vflag = true; - else break; - - if(i == argc) { - fprintf(stderr, "usage: %s [-c] [-i] [-l] [-n] [-v] pattern [files...]\n", argv[0]); + default: + exit(EXIT_FAILURE); + } + if(optind == argc) { + fprintf(stderr, "usage: %s [-cilnqv] pattern [files...]\n", argv[0]); exit(2); } if(mode == 'c') flags |= REG_NOSUB; if(iflag) flags |= REG_ICASE; - regcomp(&preg, argv[i++], flags); + regcomp(&preg, argv[optind++], flags); - many = (argc > i+1); - if(i == argc) + many = (argc > optind+1); + if(optind == argc) grep(stdin, "<stdin>", &preg); - else for(; i < argc; i++) { - if(!(fp = fopen(argv[i], "r"))) { - fprintf(stderr, "fopen %s: ", argv[i]); + else for(; optind < argc; optind++) { + if(!(fp = fopen(argv[optind], "r"))) { + fprintf(stderr, "fopen %s: ", argv[optind]); perror(NULL); exit(2); } - grep(fp, argv[i], &preg); + grep(fp, argv[optind], &preg); fclose(fp); } return match ? 0 : 1; diff --git a/tee.c b/tee.c @@ -2,29 +2,35 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> +#include <unistd.h> #include "util.h" int main(int argc, char *argv[]) { bool aflag = false; - char buf[BUFSIZ]; + char buf[BUFSIZ], c; int i, nfps = 1; size_t n; FILE **fps; - if(argc > 1 && !strcmp(argv[1], "-a")) - aflag = true; + while((c = getopt(argc, argv, "a")) != -1) + switch(c) { + case 'a': + aflag = true; + break; + default: + exit(EXIT_FAILURE); + } if(!(fps = malloc(sizeof *fps))) eprintf("malloc:"); fps[nfps-1] = stdout; - for(i = aflag ? 2 : 1; i < argc; i++) { + for(; optind < argc; optind++) { if(!(fps = realloc(fps, ++nfps * sizeof *fps))) eprintf("realloc:"); - if(!(fps[nfps-1] = fopen(argv[i], aflag ? "a" : "w"))) - eprintf("fopen %s:", argv[i]); + if(!(fps[nfps-1] = fopen(argv[optind], aflag ? "a" : "w"))) + eprintf("fopen %s:", argv[optind]); } while((n = fread(buf, 1, sizeof buf, stdin)) > 0) for(i = 0; i < nfps; i++) diff --git a/touch.c b/touch.c @@ -3,7 +3,6 @@ #include <fcntl.h> #include <stdbool.h> #include <stdlib.h> -#include <string.h> #include <time.h> #include <unistd.h> #include <utime.h> @@ -18,19 +17,22 @@ static time_t t; int main(int argc, char *argv[]) { - int i; + char c; t = time(NULL); - for(i = 1; i < argc; i++) - if(!strcmp(argv[i], "-c")) + while((c = getopt(argc, argv, "ct:")) != -1) + switch(c) { + case 'c': cflag = true; - else if(!strcmp(argv[i], "-t") && i+1 < argc) - t = strtol(argv[++i], NULL, 0); - else break; - - for(; i < argc; i++) - touch(argv[i]); + case 't': + t = strtol(optarg, NULL, 0); + break; + default: + exit(EXIT_FAILURE); + } + for(; optind < argc; optind++) + touch(argv[optind]); return EXIT_SUCCESS; } diff --git a/wc.1 b/wc.1 @@ -3,10 +3,7 @@ wc \- word count .SH SYNOPSIS .B wc -.RB [ \-c ] -.RB [ \-l ] -.RB [ \-m ] -.RB [ \-w ] +.RB [ \-clmw ] .RI [ file ...] .SH DESCRIPTION .B wc diff --git a/wc.c b/wc.c @@ -3,7 +3,7 @@ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> -#include <string.h> +#include <unistd.h> #include "util.h" static void output(const char *, long, long, long); @@ -18,28 +18,32 @@ int main(int argc, char *argv[]) { bool many; - int i; + char c; FILE *fp; - for(i = 1; i < argc; i++) - if(!strcmp(argv[i], "-c")) - cmode = 'c'; - else if(!strcmp(argv[i], "-l")) + while((c = getopt(argc, argv, "clmw")) != -1) + switch(c) { + case 'c': + case 'm': + cmode = c; + break; + case 'l': lflag = true; - else if(!strcmp(argv[i], "-m")) - cmode = 'm'; - else if(!strcmp(argv[i], "-w")) + break; + case 'w': wflag = true; - else break; - many = (argc > i+1); + default: + exit(EXIT_FAILURE); + } + many = (argc > optind+1); - if(i == argc) + if(optind == argc) wc(stdin, NULL); - else for(; i < argc; i++) { - if(!(fp = fopen(argv[i], "r"))) - eprintf("fopen %s:", argv[i]); - wc(fp, argv[i]); + else for(; optind < argc; optind++) { + if(!(fp = fopen(argv[optind], "r"))) + eprintf("fopen %s:", argv[optind]); + wc(fp, argv[optind]); fclose(fp); } if(many)