debian/rules: Use `git' potty wrapper.
[qmail] / subgetopt.c
CommitLineData
2117e02e
MW
1/* subgetopt.c, subgetopt.h: (yet another) improved getopt clone, inner layer
2D. J. Bernstein, djb@pobox.com.
3No dependencies.
4No system requirements.
519970228: Cleanups.
6931129: Adapted from getopt.c.
7No known patent problems.
8
9Documentation in subgetopt.3.
10*/
11
12#define SUBGETOPTNOSHORT
13#include "subgetopt.h"
14
15#define sgopt subgetopt
16#define optind subgetoptind
17#define optpos subgetoptpos
18#define optarg subgetoptarg
19#define optproblem subgetoptproblem
20#define optdone subgetoptdone
21
22int optind = 1;
23int optpos = 0;
24char *optarg = 0;
25int optproblem = 0;
26int optdone = SUBGETOPTDONE;
27
28int sgopt(argc,argv,opts)
29int argc;
30char **argv;
31char *opts;
32{
33 int c;
34 char *s;
35
36 optarg = 0;
37 if (!argv || (optind >= argc) || !argv[optind]) return optdone;
38 if (optpos && !argv[optind][optpos]) {
39 ++optind;
40 optpos = 0;
41 if ((optind >= argc) || !argv[optind]) return optdone;
42 }
43 if (!optpos) {
44 if (argv[optind][0] != '-') return optdone;
45 ++optpos;
46 c = argv[optind][1];
47 if ((c == '-') || (c == 0)) {
48 if (c) ++optind;
49 optpos = 0;
50 return optdone;
51 }
52 /* otherwise c is reassigned below */
53 }
54 c = argv[optind][optpos];
55 ++optpos;
56 s = opts;
57 while (*s) {
58 if (c == *s) {
59 if (s[1] == ':') {
60 optarg = argv[optind] + optpos;
61 ++optind;
62 optpos = 0;
63 if (!*optarg) {
64 optarg = argv[optind];
65 if ((optind >= argc) || !optarg) { /* argument past end */
66 optproblem = c;
67 return '?';
68 }
69 ++optind;
70 }
71 }
72 return c;
73 }
74 ++s;
75 if (*s == ':') ++s;
76 }
77 optproblem = c;
78 return '?';
79}