Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / php / sapi / isapi / stresstest / getopt.c
blob57faa0f890c1724ac94dbecbcdb38e6154f14d66
1 /* Borrowed from Apache NT Port */
3 #include <stdio.h>
4 #include <string.h>
5 #include <assert.h>
6 #include <stdlib.h>
7 #include "getopt.h"
8 #define OPTERRCOLON (1)
9 #define OPTERRNF (2)
10 #define OPTERRARG (3)
13 char *ap_optarg;
14 int ap_optind = 1;
15 static int ap_opterr = 1;
16 static int ap_optopt;
18 static int
19 ap_optiserr(int argc, char * const *argv, int oint, const char *optstr,
20 int optchr, int err)
22 if (ap_opterr)
24 fprintf(stderr, "Error in argument %d, char %d: ", oint, optchr+1);
25 switch(err)
27 case OPTERRCOLON:
28 fprintf(stderr, ": in flags\n");
29 break;
30 case OPTERRNF:
31 fprintf(stderr, "option not found %c\n", argv[oint][optchr]);
32 break;
33 case OPTERRARG:
34 fprintf(stderr, "no argument for option %c\n", argv[oint][optchr]);
35 break;
36 default:
37 fprintf(stderr, "unknown\n");
38 break;
41 ap_optopt = argv[oint][optchr];
42 return('?');
45 int ap_getopt(int argc, char* const *argv, const char *optstr)
47 static int optchr = 0;
48 static int dash = 0; /* have already seen the - */
50 char *cp;
52 if (ap_optind >= argc)
53 return(EOF);
54 if (!dash && (argv[ap_optind][0] != '-'))
55 return(EOF);
56 if (!dash && (argv[ap_optind][0] == '-') && !argv[ap_optind][1])
59 * use to specify stdin. Need to let pgm process this and
60 * the following args
62 return(EOF);
64 if ((argv[ap_optind][0] == '-') && (argv[ap_optind][1] == '-'))
66 /* -- indicates end of args */
67 ap_optind++;
68 return(EOF);
70 if (!dash)
72 assert((argv[ap_optind][0] == '-') && argv[ap_optind][1]);
73 dash = 1;
74 optchr = 1;
77 /* Check if the guy tries to do a -: kind of flag */
78 assert(dash);
79 if (argv[ap_optind][optchr] == ':')
81 dash = 0;
82 ap_optind++;
83 return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRCOLON));
85 if (!(cp = strchr(optstr, argv[ap_optind][optchr])))
87 int errind = ap_optind;
88 int errchr = optchr;
90 if (!argv[ap_optind][optchr+1])
92 dash = 0;
93 ap_optind++;
95 else
96 optchr++;
97 return(ap_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
99 if (cp[1] == ':')
101 /* Check for cases where the value of the argument
102 is in the form -<arg> <val> or in the form -<arg><val> */
103 dash = 0;
104 if(!argv[ap_optind][2]) {
105 ap_optind++;
106 if (ap_optind == argc)
107 return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRARG));
108 ap_optarg = argv[ap_optind++];
110 else
112 ap_optarg = &argv[ap_optind][2];
113 ap_optind++;
115 return(*cp);
117 else
119 if (!argv[ap_optind][optchr+1])
121 dash = 0;
122 ap_optind++;
124 else
125 optchr++;
126 return(*cp);
128 assert(0);
129 return(0);
132 #ifdef TESTGETOPT
134 main (int argc, char **argv)
136 int c;
137 extern char *ap_optarg;
138 extern int ap_optind;
139 int aflg = 0;
140 int bflg = 0;
141 int errflg = 0;
142 char *ofile = NULL;
144 while ((c = ap_getopt(argc, argv, "abo:")) != EOF)
145 switch (c) {
146 case 'a':
147 if (bflg)
148 errflg++;
149 else
150 aflg++;
151 break;
152 case 'b':
153 if (aflg)
154 errflg++;
155 else
156 bflg++;
157 break;
158 case 'o':
159 ofile = ap_optarg;
160 (void)printf("ofile = %s\n", ofile);
161 break;
162 case '?':
163 errflg++;
165 if (errflg) {
166 (void)fprintf(stderr,
167 "usage: cmd [-a|-b] [-o <filename>] files...\n");
168 exit (2);
170 for ( ; ap_optind < argc; ap_optind++)
171 (void)printf("%s\n", argv[ap_optind]);
172 return 0;
175 #endif /* TESTGETOPT */