release
[hcoop/zz_old/debian/djbdns.git] / sgetopt.c
CommitLineData
dc0d77d7
CE
1/* sgetopt.c, sgetopt.h: (yet another) improved getopt clone, outer layer
2D. J. Bernstein, djb@pobox.com.
3Depends on subgetopt.h, buffer.h.
4No system requirements.
519991219: Switched to buffer.h.
619970208: Cleanups.
7931201: Baseline.
8No known patent problems.
9
10Documentation 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
25int opterr = 1;
26const char *optprogname = 0;
27
28int 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}