release
[hcoop/zz_old/debian/djbdns.git] / sgetopt.c
1 /* sgetopt.c, sgetopt.h: (yet another) improved getopt clone, outer layer
2 D. J. Bernstein, djb@pobox.com.
3 Depends on subgetopt.h, buffer.h.
4 No system requirements.
5 19991219: Switched to buffer.h.
6 19970208: Cleanups.
7 931201: Baseline.
8 No known patent problems.
9
10 Documentation in sgetopt.3.
11 */
12
13 #include "buffer.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
25 int opterr = 1;
26 const char *optprogname = 0;
27
28 int getopt(int argc,char **argv,const char *opts)
29 {
30 int c;
31 const char *s;
32
33 if (!optprogname) {
34 optprogname = *argv;
35 if (!optprogname) optprogname = "";
36 for (s = optprogname;*s;++s) if (*s == '/') optprogname = s + 1;
37 }
38 c = subgetopt(argc,argv,opts);
39 if (opterr)
40 if (c == '?') {
41 char chp[2]; chp[0] = optproblem; chp[1] = '\n';
42 buffer_puts(buffer_2,optprogname);
43 if (argv[optind] && (optind < argc))
44 buffer_puts(buffer_2,": illegal option -- ");
45 else
46 buffer_puts(buffer_2,": option requires an argument -- ");
47 buffer_put(buffer_2,chp,2);
48 buffer_flush(buffer_2);
49 }
50 return c;
51 }