ReactOS  0.4.13-dev-99-g7e18b6d
getopt.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <errno.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <getopt.h>
6 #include <stdarg.h>
7 #include <stdio.h>
8 
9 #define REPLACE_GETOPT
10 
11 #define _DIAGASSERT(x) do {} while (0)
12 
13 #ifdef REPLACE_GETOPT
14 #ifdef __weak_alias
15 __weak_alias(getopt,_getopt)
16 #endif
17 int opterr = 1;
18 int optind = 1;
19 int optopt = '?';
21 char *optarg;
22 #endif
23 
24 #ifdef __weak_alias
25 __weak_alias(getopt_long,_getopt_long)
26 #endif
27 
28 #ifndef __CYGWIN__
29 #define __progname __argv[0]
30 #else
31 extern char __declspec(dllimport) *__progname;
32 #endif
33 
34 #define IGNORE_FIRST (*options == '-' || *options == '+')
35 #define PRINT_ERROR ((opterr) && ((*options != ':') || (IGNORE_FIRST && options[1] != ':')))
36 
37 #ifndef IS_POSIXLY_CORRECT
38 #define IS_POSIXLY_CORRECT (getenv("POSIXLY_CORRECT") != NULL)
39 #endif
40 
41 #define PERMUTE (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
42 
43 #define IN_ORDER (!IS_POSIXLY_CORRECT && *options == '-')
44 
45 #define BADCH (int)'?'
46 #define BADARG ((IGNORE_FIRST && options[1] == ':') || (*options == ':') ? (int)':' : (int)'?')
47 #define INORDER (int)1
48 
49 static char EMSG[1];
50 
51 static int getopt_internal (int,char * const *,const char *);
52 static int gcd (int,int);
53 static void permute_args (int,int,int,char * const *);
54 
55 static char *place = EMSG;
56 
57 static int nonopt_start = -1;
58 static int nonopt_end = -1;
59 
60 static const char recargchar[] = "option requires an argument -- %c";
61 static const char recargstring[] = "option requires an argument -- %s";
62 static const char ambig[] = "ambiguous option -- %.*s";
63 static const char noarg[] = "option doesn't take an argument -- %.*s";
64 static const char illoptchar[] = "unknown option -- %c";
65 static const char illoptstring[] = "unknown option -- %s";
66 
67 static void
68 _vwarnx(const char *fmt,va_list ap)
69 {
70  (void)fprintf(stderr,"%s: ",__progname);
71  if (fmt != NULL)
73  (void)fprintf(stderr,"\n");
74 }
75 
76 static void
77 warnx(const char *fmt,...)
78 {
79  va_list ap;
80  va_start(ap,fmt);
81  _vwarnx(fmt,ap);
82  va_end(ap);
83 }
84 
85 static int
87  int a;
88  int b;
89 {
90  int c;
91 
92  c = a % b;
93  while (c != 0) {
94  a = b;
95  b = c;
96  c = a % b;
97  }
98 
99  return b;
100 }
101 
102 static void
103 permute_args(panonopt_start,panonopt_end,opt_end,nargv)
104  int panonopt_start;
105  int panonopt_end;
106  int opt_end;
107  char * const *nargv;
108 {
109  int cstart,cyclelen,i,j,ncycle,nnonopts,nopts,pos;
110  char *swap;
111 
112  _DIAGASSERT(nargv != NULL);
113 
114  nnonopts = panonopt_end - panonopt_start;
115  nopts = opt_end - panonopt_end;
116  ncycle = gcd(nnonopts,nopts);
117  cyclelen = (opt_end - panonopt_start) / ncycle;
118 
119  for (i = 0; i < ncycle; i++) {
120  cstart = panonopt_end+i;
121  pos = cstart;
122  for (j = 0; j < cyclelen; j++) {
123  if (pos >= panonopt_end)
124  pos -= nnonopts;
125  else
126  pos += nopts;
127  swap = nargv[pos];
128 
129  ((char **) nargv)[pos] = nargv[cstart];
130 
131  ((char **)nargv)[cstart] = swap;
132  }
133  }
134 }
135 
136 static int
138  int nargc;
139  char * const *nargv;
140  const char *options;
141 {
142  char *oli;
143  int optchar;
144 
145  _DIAGASSERT(nargv != NULL);
147 
148  optarg = NULL;
149 
150  if (optind == 0)
151  optind = 1;
152 
153  if (optreset)
154  nonopt_start = nonopt_end = -1;
155 start:
156  if (optreset || !*place) {
157  optreset = 0;
158  if (optind >= nargc) {
159  place = EMSG;
160  if (nonopt_end != -1) {
161 
164  }
165  else if (nonopt_start != -1) {
166 
168  }
169  nonopt_start = nonopt_end = -1;
170  return -1;
171  }
172  if ((*(place = nargv[optind]) != '-')
173  || (place[1] == '\0')) {
174  place = EMSG;
175  if (IN_ORDER) {
176 
177  optarg = nargv[optind++];
178  return INORDER;
179  }
180  if (!PERMUTE) {
181 
182  return -1;
183  }
184 
185  if (nonopt_start == -1)
187  else if (nonopt_end != -1) {
189  nonopt_start = optind -
191  nonopt_end = -1;
192  }
193  optind++;
194 
195  goto start;
196  }
197  if (nonopt_start != -1 && nonopt_end == -1)
198  nonopt_end = optind;
199  if (place[1] && *++place == '-') {
200  place++;
201  return -2;
202  }
203  }
204  if ((optchar = (int)*place++) == (int)':' ||
205  (oli = strchr(options + (IGNORE_FIRST ? 1 : 0),optchar)) == NULL) {
206 
207  if (!*place)
208  ++optind;
209  if (PRINT_ERROR)
210  warnx(illoptchar,optchar);
211  optopt = optchar;
212  return BADCH;
213  }
214  if (optchar == 'W' && oli[1] == ';') {
215 
216  if (*place)
217  return -2;
218 
219  if (++optind >= nargc) {
220  place = EMSG;
221  if (PRINT_ERROR)
222  warnx(recargchar,optchar);
223  optopt = optchar;
224  return BADARG;
225  } else
226  place = nargv[optind];
227 
228  return -2;
229  }
230  if (*++oli != ':') {
231  if (!*place)
232  ++optind;
233  } else {
234  optarg = NULL;
235  if (*place)
236  optarg = place;
237 
238  else if (oli[1] != ':') {
239  if (++optind >= nargc) {
240  place = EMSG;
241  if (PRINT_ERROR)
242  warnx(recargchar,optchar);
243  optopt = optchar;
244  return BADARG;
245  } else
246  optarg = nargv[optind];
247  }
248  place = EMSG;
249  ++optind;
250  }
251 
252  return optchar;
253 }
254 
255 #ifdef REPLACE_GETOPT
256 
257 int
258 getopt(nargc,nargv,options)
259  int nargc;
260  char * const *nargv;
261  const char *options;
262 {
263  int retval;
264 
265  _DIAGASSERT(nargv != NULL);
267 
268  if ((retval = getopt_internal(nargc,nargv,options)) == -2) {
269  ++optind;
270 
271  if (nonopt_end != -1) {
274  }
275  nonopt_start = nonopt_end = -1;
276  retval = -1;
277  }
278  return retval;
279 }
280 #endif
281 
282 int
284  int nargc;
285  char * const *nargv;
286  const char *options;
287  const struct option *long_options;
288  int *idx;
289 {
290  int retval;
291 
292  _DIAGASSERT(nargv != NULL);
295 
296  if ((retval = getopt_internal(nargc,nargv,options)) == -2) {
297  char *current_argv,*has_equal;
298  size_t current_argv_len;
299  int i,match;
300 
301  current_argv = place;
302  match = -1;
303 
304  optind++;
305  place = EMSG;
306 
307  if (*current_argv == '\0') {
308 
309  if (nonopt_end != -1) {
312  }
313  nonopt_start = nonopt_end = -1;
314  return -1;
315  }
316  if ((has_equal = strchr(current_argv,'=')) != NULL) {
317 
318  current_argv_len = has_equal - current_argv;
319  has_equal++;
320  } else
321  current_argv_len = strlen(current_argv);
322 
323  for (i = 0; long_options[i].name; i++) {
324 
325  if (strncmp(current_argv,long_options[i].name,current_argv_len))
326  continue;
327 
328  if (strlen(long_options[i].name) ==
329  (unsigned)current_argv_len) {
330 
331  match = i;
332  break;
333  }
334  if (match == -1)
335  match = i;
336  else {
337 
338  if (PRINT_ERROR)
339  warnx(ambig,(int)current_argv_len,current_argv);
340  optopt = 0;
341  return BADCH;
342  }
343  }
344  if (match != -1) {
346  && has_equal) {
347  if (PRINT_ERROR)
348  warnx(noarg,(int)current_argv_len,current_argv);
349 
350  if (long_options[match].flag == NULL)
351  optopt = long_options[match].val;
352  else
353  optopt = 0;
354  return BADARG;
355  }
358  if (has_equal)
359  optarg = has_equal;
360  else if (long_options[match].has_arg ==
362 
363  optarg = nargv[optind++];
364  }
365  }
367  && (optarg == NULL)) {
368 
369  if (PRINT_ERROR)
370  warnx(recargstring,current_argv);
371 
372  if (long_options[match].flag == NULL)
373  optopt = long_options[match].val;
374  else
375  optopt = 0;
376  --optind;
377  return BADARG;
378  }
379  } else {
380  if (PRINT_ERROR)
381  warnx(illoptstring,current_argv);
382  optopt = 0;
383  return BADCH;
384  }
385  if (long_options[match].flag) {
386  *long_options[match].flag = long_options[match].val;
387  retval = 0;
388  } else
389  retval = long_options[match].val;
390  if (idx)
391  *idx = match;
392  }
393  return retval;
394 }
static char EMSG[1]
Definition: getopt.c:49
int optopt
Definition: getopt.c:47
#define BADARG
Definition: getopt.c:46
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define swap(a, b)
Definition: qsort.c:63
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
Definition: match.c:28
int optind
Definition: getopt.c:47
static const char noarg[]
Definition: getopt.c:63
static const char illoptchar[]
Definition: getopt.c:64
int optreset
Definition: getopt.c:20
#define BADCH
Definition: getopt.c:45
struct _EXCEPTION_POINTERS *_CRTIMP __declspec(noreturn) void __cdecl terminate(void)
Definition: eh.h:27
int getopt_long(int nargc, char *const *nargv, const char *options, const struct option *long_options, int *idx)
Definition: getopt.c:283
static void permute_args(int, int, int, char *const *)
Definition: getopt.c:103
#define PERMUTE
Definition: getopt.c:41
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static const struct option long_options[]
Definition: widl.c:185
#define va_end(ap)
Definition: acmsvcex.h:90
#define IN_ORDER
Definition: getopt.c:43
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
#define a
Definition: ke_i.h:78
unsigned int idx
Definition: utils.c:41
smooth NULL
Definition: ftsmooth.c:416
Definition: getopt.h:108
char * va_list
Definition: acmsvcex.h:78
static void warnx(const char *fmt,...)
Definition: getopt.c:77
struct match match
Definition: match.c:33
int options
Definition: main.c:106
#define b
Definition: ke_i.h:79
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
#define INORDER
Definition: getopt.c:47
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
static char * place
Definition: getopt.c:55
const GLubyte * c
Definition: glext.h:8905
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
static int getopt_internal(int, char *const *, const char *)
Definition: getopt.c:137
#define _DIAGASSERT(x)
Definition: getopt.c:11
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:55
static const char recargchar[]
Definition: getopt.c:60
#define __progname
Definition: getopt.c:29
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean flag
Definition: glfuncs.h:52
static int nonopt_start
Definition: getopt.c:57
int opterr
Definition: getopt.c:46
GLuint start
Definition: gl.h:1545
const char * optarg
Definition: getopt.c:49
#define PRINT_ERROR
Definition: getopt.c:35
#define va_start(ap, A)
Definition: acmsvcex.h:91
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define IGNORE_FIRST
Definition: getopt.c:34
Definition: name.c:36
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
#define c
Definition: ke_i.h:80
FILE * stderr
int has_arg
Definition: getopt.h:113
static const char ambig[]
Definition: getopt.c:62
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
static int nonopt_end
Definition: getopt.c:58
Definition: dsound.c:943
static const char illoptstring[]
Definition: getopt.c:65
static void _vwarnx(const char *fmt, va_list ap)
Definition: getopt.c:68
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
static const char recargstring[]
Definition: getopt.c:61
static int gcd(int, int)
Definition: getopt.c:86