diff options
author | joe <joe@jerkface.net> | 2015-05-08 23:07:47 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2015-05-08 23:07:47 -0400 |
commit | 220534453eecc2c07ad710d72268aafd20b83138 (patch) | |
tree | 469bd5db135e31b7ea908152aa3389b9b1e06d5b /sgetopt.c |
initial commit
Diffstat (limited to 'sgetopt.c')
-rw-r--r-- | sgetopt.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/sgetopt.c b/sgetopt.c new file mode 100644 index 0000000..8bb608f --- /dev/null +++ b/sgetopt.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* Public domain. */ | ||
2 | |||
3 | /* sgetopt.c, sgetopt.h: (yet another) improved getopt clone, outer layer | ||
4 | D. J. Bernstein, djb@pobox.com. | ||
5 | Depends on subgetopt.h, buffer.h. | ||
6 | No system requirements. | ||
7 | 19991219: Switched to buffer.h. | ||
8 | 19970208: Cleanups. | ||
9 | 931201: Baseline. | ||
10 | No known patent problems. | ||
11 | |||
12 | Documentation in sgetopt.3. | ||
13 | */ | ||
14 | |||
15 | #include "buffer.h" | ||
16 | #define SGETOPTNOSHORT | ||
17 | #include "sgetopt.h" | ||
18 | #define SUBGETOPTNOSHORT | ||
19 | #include "subgetopt.h" | ||
20 | |||
21 | #define getopt sgetoptmine | ||
22 | #define optind subgetoptind | ||
23 | #define opterr sgetopterr | ||
24 | #define optproblem subgetoptproblem | ||
25 | #define optprogname sgetoptprogname | ||
26 | |||
27 | int opterr = 1; | ||
28 | const char *optprogname = 0; | ||
29 | |||
30 | int getopt(int argc,const char *const *argv,const char *opts) | ||
31 | { | ||
32 | int c; | ||
33 | const char *s; | ||
34 | |||
35 | if (!optprogname) { | ||
36 | optprogname = *argv; | ||
37 | if (!optprogname) optprogname = ""; | ||
38 | for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1; | ||
39 | } | ||
40 | c = subgetopt(argc,argv,opts); | ||
41 | if (opterr) | ||
42 | if (c == '?') { | ||
43 | char chp[2]; chp[0] = optproblem; chp[1] = '\n'; | ||
44 | buffer_puts(buffer_2,optprogname); | ||
45 | if (argv[optind] && (optind < argc)) | ||
46 | buffer_puts(buffer_2,": illegal option -- "); | ||
47 | else | ||
48 | buffer_puts(buffer_2,": option requires an argument -- "); | ||
49 | buffer_put(buffer_2,chp,2); | ||
50 | buffer_flush(buffer_2); | ||
51 | } | ||
52 | return c; | ||
53 | } | ||