ReactOS 0.4.15-dev-8135-g1bc6c90
getopt.c File Reference
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdarg.h>
#include <stdio.h>
Include dependency graph for getopt.c:

Go to the source code of this file.

Macros

#define REPLACE_GETOPT
 
#define _DIAGASSERT(x)   do {} while (0)
 
#define __progname   __argv[0]
 
#define IGNORE_FIRST   (*options == '-' || *options == '+')
 
#define PRINT_ERROR   ((opterr) && ((*options != ':') || (IGNORE_FIRST && options[1] != ':')))
 
#define IS_POSIXLY_CORRECT   (getenv("POSIXLY_CORRECT") != NULL)
 
#define PERMUTE   (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)
 
#define IN_ORDER   (!IS_POSIXLY_CORRECT && *options == '-')
 
#define BADCH   (int)'?'
 
#define BADARG   ((IGNORE_FIRST && options[1] == ':') || (*options == ':') ? (int)':' : (int)'?')
 
#define INORDER   (int)1
 

Functions

static int getopt_internal (int, char *const *, const char *)
 
static int gcd (int, int)
 
static void permute_args (int, int, int, char *const *)
 
static void _vwarnx (const char *fmt, va_list ap)
 
static void warnx (const char *fmt,...)
 
int getopt (int nargc, char *const *nargv, const char *options)
 
int getopt_long (int nargc, char *const *nargv, const char *options, const struct option *long_options, int *idx)
 

Variables

int opterr = 1
 
int optind = 1
 
int optopt = '?'
 
int optreset
 
charoptarg
 
static char EMSG [1]
 
static charplace = EMSG
 
static int nonopt_start = -1
 
static int nonopt_end = -1
 
static const char recargchar [] = "option requires an argument -- %c"
 
static const char recargstring [] = "option requires an argument -- %s"
 
static const char ambig [] = "ambiguous option -- %.*s"
 
static const char noarg [] = "option doesn't take an argument -- %.*s"
 
static const char illoptchar [] = "unknown option -- %c"
 
static const char illoptstring [] = "unknown option -- %s"
 

Macro Definition Documentation

◆ __progname

#define __progname   __argv[0]

Definition at line 29 of file getopt.c.

◆ _DIAGASSERT

#define _DIAGASSERT (   x)    do {} while (0)

Definition at line 11 of file getopt.c.

◆ BADARG

#define BADARG   ((IGNORE_FIRST && options[1] == ':') || (*options == ':') ? (int)':' : (int)'?')

Definition at line 46 of file getopt.c.

◆ BADCH

#define BADCH   (int)'?'

Definition at line 45 of file getopt.c.

◆ IGNORE_FIRST

#define IGNORE_FIRST   (*options == '-' || *options == '+')

Definition at line 34 of file getopt.c.

◆ IN_ORDER

#define IN_ORDER   (!IS_POSIXLY_CORRECT && *options == '-')

Definition at line 43 of file getopt.c.

◆ INORDER

#define INORDER   (int)1

Definition at line 47 of file getopt.c.

◆ IS_POSIXLY_CORRECT

#define IS_POSIXLY_CORRECT   (getenv("POSIXLY_CORRECT") != NULL)

Definition at line 38 of file getopt.c.

◆ PERMUTE

#define PERMUTE   (!IS_POSIXLY_CORRECT && !IGNORE_FIRST)

Definition at line 41 of file getopt.c.

◆ PRINT_ERROR

#define PRINT_ERROR   ((opterr) && ((*options != ':') || (IGNORE_FIRST && options[1] != ':')))

Definition at line 35 of file getopt.c.

◆ REPLACE_GETOPT

#define REPLACE_GETOPT

Definition at line 9 of file getopt.c.

Function Documentation

◆ _vwarnx()

static void _vwarnx ( const char fmt,
va_list  ap 
)
static

Definition at line 68 of file getopt.c.

69{
71 if (fmt != NULL)
73 (void)fprintf(stderr,"\n");
74}
#define NULL
Definition: types.h:112
#define stderr
Definition: stdio.h:100
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_opt_ _CRTIMP int __cdecl vfprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format, va_list _ArgList)
#define __progname
Definition: getopt.c:29
Definition: dsound.c:943
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36

Referenced by warnx().

◆ gcd()

static int gcd ( int  a,
int  b 
)
static

Definition at line 86 of file getopt.c.

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}
const GLubyte * c
Definition: glext.h:8905
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
#define c
Definition: ke_i.h:80
#define b
Definition: ke_i.h:79

Referenced by GetClipboardData(), and permute_args().

◆ getopt()

int getopt ( int  nargc,
char * const nargv,
const char options 
)

Definition at line 258 of file getopt.c.

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 }
276 retval = -1;
277 }
278 return retval;
279}
int optind
Definition: getopt.c:47
static int getopt_internal(int, char *const *, const char *)
Definition: getopt.c:137
static int nonopt_start
Definition: getopt.c:57
static int nonopt_end
Definition: getopt.c:58
static void permute_args(int, int, int, char *const *)
Definition: getopt.c:103
#define _DIAGASSERT(x)
Definition: getopt.c:11

◆ getopt_internal()

static int getopt_internal ( int  nargc,
char *const nargv,
const char options 
)
static

Definition at line 137 of file getopt.c.

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)
155start:
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 }
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) {
191 nonopt_end = -1;
192 }
193 optind++;
194
195 goto start;
196 }
197 if (nonopt_start != -1 && nonopt_end == -1)
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}
char * strchr(const char *String, int ch)
Definition: utclib.c:501
#define BADCH
Definition: getopt.c:51
int optopt
Definition: getopt.c:48
#define EMSG
Definition: getopt.c:52
const char * optarg
Definition: getopt.c:49
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLuint start
Definition: gl.h:1545
#define warnx(...)
#define IGNORE_FIRST
Definition: getopt.c:34
int optreset
Definition: getopt.c:20
static const char illoptchar[]
Definition: getopt.c:64
static char * place
Definition: getopt.c:55
#define PRINT_ERROR
Definition: getopt.c:35
#define BADARG
Definition: getopt.c:46
#define INORDER
Definition: getopt.c:47
static const char recargchar[]
Definition: getopt.c:60
#define IN_ORDER
Definition: getopt.c:43
#define PERMUTE
Definition: getopt.c:41

Referenced by getopt(), and getopt_long().

◆ getopt_long()

int getopt_long ( int  nargc,
char * const nargv,
const char options,
const struct option long_options,
int idx 
)

Definition at line 283 of file getopt.c.

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 }
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) {
345 if (long_options[match].has_arg == no_argument
346 && has_equal) {
347 if (PRINT_ERROR)
348 warnx(noarg,(int)current_argv_len,current_argv);
349
350 if (long_options[match].flag == NULL)
352 else
353 optopt = 0;
354 return BADARG;
355 }
356 if (long_options[match].has_arg == required_argument ||
358 if (has_equal)
359 optarg = has_equal;
360 else if (long_options[match].has_arg ==
362
363 optarg = nargv[optind++];
364 }
365 }
366 if ((long_options[match].has_arg == required_argument)
367 && (optarg == NULL)) {
368
369 if (PRINT_ERROR)
370 warnx(recargstring,current_argv);
371
372 if (long_options[match].flag == NULL)
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}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
unsigned int idx
Definition: utils.c:41
#define no_argument
Definition: getopt.h:122
#define required_argument
Definition: getopt.h:123
#define optional_argument
Definition: getopt.h:124
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
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 char noarg[]
Definition: getopt.c:63
static const char recargstring[]
Definition: getopt.c:61
static const char illoptstring[]
Definition: getopt.c:65
static const char ambig[]
Definition: getopt.c:62
Definition: match.c:28
Definition: name.c:39
static const struct option long_options[]
Definition: widl.c:185

◆ permute_args()

static void permute_args ( int  panonopt_start,
int  panonopt_end,
int  opt_end,
char *const nargv 
)
static

Definition at line 103 of file getopt.c.

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}
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 swap(a, b)
Definition: qsort.c:63
static int gcd(int, int)
Definition: getopt.c:86

Referenced by getopt(), getopt_internal(), and getopt_long().

◆ warnx()

static void warnx ( const char fmt,
  ... 
)
static

Definition at line 77 of file getopt.c.

78{
79 va_list ap;
81 _vwarnx(fmt,ap);
82 va_end(ap);
83}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
static void _vwarnx(const char *fmt, va_list ap)
Definition: getopt.c:68

Variable Documentation

◆ ambig

const char ambig[] = "ambiguous option -- %.*s"
static

Definition at line 62 of file getopt.c.

Referenced by _getopt_internal(), and getopt_long().

◆ EMSG

char EMSG[1]
static

Definition at line 49 of file getopt.c.

◆ illoptchar

const char illoptchar[] = "unknown option -- %c"
static

Definition at line 64 of file getopt.c.

Referenced by getopt_internal().

◆ illoptstring

const char illoptstring[] = "unknown option -- %s"
static

Definition at line 65 of file getopt.c.

Referenced by getopt_long().

◆ noarg

const char noarg[] = "option doesn't take an argument -- %.*s"
static

Definition at line 63 of file getopt.c.

Referenced by getopt_long().

◆ nonopt_end

int nonopt_end = -1
static

Definition at line 58 of file getopt.c.

Referenced by getopt(), getopt_internal(), and getopt_long().

◆ nonopt_start

int nonopt_start = -1
static

Definition at line 57 of file getopt.c.

Referenced by getopt(), getopt_internal(), and getopt_long().

◆ optarg

char* optarg

Definition at line 21 of file getopt.c.

◆ opterr

int opterr = 1

Definition at line 17 of file getopt.c.

◆ optind

int optind = 1

Definition at line 18 of file getopt.c.

◆ optopt

int optopt = '?'

Definition at line 19 of file getopt.c.

◆ optreset

int optreset

Definition at line 20 of file getopt.c.

Referenced by getopt_internal().

◆ place

◆ recargchar

const char recargchar[] = "option requires an argument -- %c"
static

Definition at line 60 of file getopt.c.

Referenced by getopt_internal().

◆ recargstring

const char recargstring[] = "option requires an argument -- %s"
static

Definition at line 61 of file getopt.c.

Referenced by getopt_long().