MCPcopy Create free account
hub / github.com/F-Stack/f-stack / getopt

Function getopt

tools/compat/getopt.c:67–153  ·  view source on GitHub ↗

* getopt -- * Parse argc/argv argument vector. */

Source from the content-addressed store, hash-verified

65 * Parse argc/argv argument vector.
66 */
67int
68getopt(int nargc, char * const nargv[], const char *ostr)
69{
70 static char *place = EMSG; /* option letter processing */
71 char *oli; /* option letter list index */
72
73 if (optreset || *place == 0) { /* update scanning pointer */
74 optreset = 0;
75 place = nargv[optind];
76 if (optind >= nargc || *place++ != '-') {
77 /* Argument is absent or is not an option */
78 place = EMSG;
79 return (-1);
80 }
81 optopt = *place++;
82 if (optopt == '-' && *place == 0) {
83 /* "--" => end of options */
84 ++optind;
85 place = EMSG;
86 return (-1);
87 }
88 if (optopt == 0) {
89 /* Solitary '-', treat as a '-' option
90 if the program (eg su) is looking for it. */
91 place = EMSG;
92 if (strchr(ostr, '-') == NULL)
93 return (-1);
94 optopt = '-';
95 }
96 } else
97 optopt = *place++;
98
99 /* See if option letter is one the caller wanted... */
100 if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
101 if (*place == 0)
102 ++optind;
103 if (opterr && *ostr != ':')
104 (void)fprintf(stderr,
105#ifndef FSTACK
106 "%s: illegal option -- %c\n", _getprogname(),
107#else
108 "%s: illegal option -- %c\n", getprogname(),
109#endif
110 optopt);
111 return (BADCH);
112 }
113
114 /* Does this option need an argument? */
115 if (oli[1] != ':') {
116 /* don't need argument */
117 optarg = NULL;
118 if (*place == 0)
119 ++optind;
120 } else {
121 /* Option-argument is either the rest of this argument or the
122 entire next argument. */
123 if (*place)
124 optarg = place;

Callers 15

sysctl.cFile · 0.50
mainFunction · 0.50
read_optionsFunction · 0.50
main.cFile · 0.50
mainFunction · 0.50
mainFunction · 0.50
route.cFile · 0.50
main.cFile · 0.50
mainFunction · 0.50
mainFunction · 0.50
mainFunction · 0.50
ShowCmdFunction · 0.50

Calls 2

strchrFunction · 0.85
getprognameFunction · 0.85

Tested by 15

read_optionsFunction · 0.40
process_optionsFunction · 0.40
process_optionsFunction · 0.40
mainFunction · 0.40
parse_optionsFunction · 0.40
draid_generateFunction · 0.40
draid_verifyFunction · 0.40
draid_dumpFunction · 0.40
draid_mergeFunction · 0.40
mainFunction · 0.40
mainFunction · 0.40
parse_optionsFunction · 0.40