summaryrefslogtreecommitdiffstats
path: root/sgetopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'sgetopt.c')
-rw-r--r--sgetopt.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/sgetopt.c b/sgetopt.c
new file mode 100644
index 0000000..a8bffc0
--- /dev/null
+++ b/sgetopt.c
@@ -0,0 +1,54 @@
1/* sgetopt.c, sgetopt.h: (yet another) improved getopt clone, outer layer
2D. J. Bernstein, djb@pobox.com.
3Depends on subgetopt.h, substdio.h, subfd.h.
4No system requirements.
519970208: Cleanups.
6931201: Baseline.
7No known patent problems.
8
9Documentation in sgetopt.3.
10*/
11
12#include "substdio.h"
13#include "subfd.h"
14#define SGETOPTNOSHORT
15#include "sgetopt.h"
16#define SUBGETOPTNOSHORT
17#include "subgetopt.h"
18
19#define getopt sgetoptmine
20#define optind subgetoptind
21#define opterr sgetopterr
22#define optproblem subgetoptproblem
23#define optprogname sgetoptprogname
24
25int opterr = 1;
26char *optprogname = 0;
27
28int getopt(argc,argv,opts)
29int argc;
30char **argv;
31char *opts;
32{
33 int c;
34 char *s;
35
36 if (!optprogname) {
37 optprogname = *argv;
38 if (!optprogname) optprogname = "";
39 for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1;
40 }
41 c = subgetopt(argc,argv,opts);
42 if (opterr)
43 if (c == '?') {
44 char chp[2]; chp[0] = optproblem; chp[1] = '\n';
45 substdio_puts(subfderr,optprogname);
46 if (argv[optind] && (optind < argc))
47 substdio_puts(subfderr,": illegal option -- ");
48 else
49 substdio_puts(subfderr,": option requires an argument -- ");
50 substdio_put(subfderr,chp,2);
51 substdio_flush(subfderr);
52 }
53 return c;
54}