Debian Codegraph @

cstool/getopt.c

raw symbols SCIP
1 #include <string.h>
2 #include <stdio.h>
3
4 #include "getopt.h"
5
6 int opterr = 1, /* if error message should be printed */
7 optind = 1, /* index into parent argv vector */
8 optopt, /* character checked for validity */
9 optreset; /* reset getopt */
10 const char *optarg; /* argument associated with option */
11
12 #define BADCH (int)'?'
13 #define BADARG (int)':'
14 #define EMSG ""
15
16 /*
17 * getopt --
18 * Parse argc/argv argument vector.
19 */
20 int getopt (int nargc, char * const nargv[], const char *ostr)
21 {
22 static const char *place = EMSG; /* option letter processing */
23 const char *oli; /* option letter list index */
24
25 if (optreset || !*place) { /* update scanning pointer */
26 optreset = 0;
27 if (optind >= nargc || *(place = nargv[optind]) != '-') {
28 place = EMSG;
29 return (-1);
30 }
31
32 if (place[1] && *++place == '-') { /* found "--" */
33 ++optind;
34 place = EMSG;
35 return (-1);
36 }
37 } /* option letter okay? */
38
39 if ((optopt = (int)*place++) == (int)':' ||
40 !(oli = strchr (ostr, optopt))) {
41 /*
42 * if the user didn't specify '-' as an option,
43 * assume it means -1.
44 */
45 if (optopt == (int)'-')
46 return (-1);
47 if (!*place)
48 ++optind;
49 if (opterr && *ostr != ':')
50 (void)printf ("illegal option -- %c\n", optopt);
51 return (BADCH);
52 }
53
54 if (*++oli != ':') { /* don't need argument */
55 optarg = NULL;
56 if (!*place)
57 ++optind;
58 } else { /* need an argument */
59 if (*place) /* no white space */
60 optarg = place;
61 else if (nargc <= ++optind) { /* no arg */
62 place = EMSG;
63 if (*ostr == ':')
64 return (BADARG);
65 if (opterr)
66 (void)printf ("option requires an argument -- %c\n", optopt);
67 return (BADCH);
68 } else /* white space */
69 optarg = nargv[optind];
70 place = EMSG;
71 ++optind;
72 }
73
74 return optopt; /* dump back option letter */
75 }
76