5151// SYNOPSIS
5252// int sc_getopt(int argc, char *argv[], char *optstring)
5353//
54- // extern char *optarg ;
55- // extern int optind ;
54+ // extern char *sc_optarg ;
55+ // extern int sc_optind ;
5656//
5757// DESCRIPTION
5858// The sc_getopt() function parses the command line arguments. Its
6767//
6868// optstring is a string of recognized option letters; if a letter
6969// is followed by a colon, the option is expected to have an argument
70- // that may or may not be separated from it by white space. optarg
70+ // that may or may not be separated from it by white space. sc_optarg
7171// is set to point to the start of the option argument on return from
7272// sc_getopt.
7373//
7474// Option letters may be combined, e.g., "-ab" is equivalent to
7575// "-a -b". Option letters are case sensitive.
7676//
77- // sc_getopt places in the external variable optind the argv index
78- // of the next argument to be processed. optind is initialized
77+ // sc_getopt places in the external variable sc_optind the argv index
78+ // of the next argument to be processed. sc_optind is initialized
7979// to 0 before the first call to sc_getopt.
8080//
8181// When all options have been processed (i.e., up to the first
82- // non-option argument), sc_getopt returns EOF, optarg will point
83- // to the argument, and optind will be set to the argv index of
84- // the argument. If there are no non-option arguments, optarg
82+ // non-option argument), sc_getopt returns EOF, sc_optarg will point
83+ // to the argument, and sc_optind will be set to the argv index of
84+ // the argument. If there are no non-option arguments, sc_optarg
8585// will be set to NULL.
8686//
8787// The special option "--" may be used to delimit the end of the
127127// break;
128128//
129129// case _T('n'):
130- // TRACE(_T("option n: value=%d\n"), atoi(optarg ));
130+ // TRACE(_T("option n: value=%d\n"), atoi(sc_optarg ));
131131// //
132132// // do something with value here
133133// //
134134// break;
135135//
136136// case _T('?'):
137- // TRACE(_T("ERROR: illegal option %s\n"), argv[optind -1]);
137+ // TRACE(_T("ERROR: illegal option %s\n"), argv[sc_optind -1]);
138138// return FALSE;
139139// break;
140140//
152152//
153153// /////////////////////////////////////////////////////////////////////////////
154154
155- char * optarg ; // global argument pointer
156- int optind = 0 ; // global argv index
155+ char * sc_optarg ; // global argument pointer
156+ int sc_optind = 0 ; // global argv index
157157
158158int sc_getopt ( int argc, char * argv[], char * optstring ) {
159159 static char * next = NULL ;
160- if ( optind == 0 ) {
160+ if ( sc_optind == 0 ) {
161161 next = NULL ;
162162 }
163163
164- optarg = NULL ;
164+ sc_optarg = NULL ;
165165
166166 if ( next == NULL || *next == ' \0 ' ) {
167- if ( optind == 0 ) {
168- optind ++;
167+ if ( sc_optind == 0 ) {
168+ sc_optind ++;
169169 }
170170
171- if ( optind >= argc || argv[optind ][0 ] != ' -' || argv[optind ][1 ] == ' \0 ' ) {
172- optarg = NULL ;
173- if ( optind < argc ) {
174- optarg = argv[optind ];
171+ if ( sc_optind >= argc || argv[sc_optind ][0 ] != ' -' || argv[sc_optind ][1 ] == ' \0 ' ) {
172+ sc_optarg = NULL ;
173+ if ( sc_optind < argc ) {
174+ sc_optarg = argv[sc_optind ];
175175 }
176176 return EOF;
177177 }
178178
179- if ( strcmp ( argv[optind ], " --" ) == 0 ) {
180- optind ++;
181- optarg = NULL ;
182- if ( optind < argc ) {
183- optarg = argv[optind ];
179+ if ( strcmp ( argv[sc_optind ], " --" ) == 0 ) {
180+ sc_optind ++;
181+ sc_optarg = NULL ;
182+ if ( sc_optind < argc ) {
183+ sc_optarg = argv[sc_optind ];
184184 }
185185 return EOF;
186186 }
187187
188- next = argv[optind ];
188+ next = argv[sc_optind ];
189189 next++; // skip past -
190- optind ++;
190+ sc_optind ++;
191191 }
192192
193193 char c = *next++;
@@ -200,11 +200,11 @@ int sc_getopt( int argc, char * argv[], char * optstring ) {
200200 cp++;
201201 if ( *cp == ' :' ) {
202202 if ( *next != ' \0 ' ) {
203- optarg = next;
203+ sc_optarg = next;
204204 next = NULL ;
205- } else if ( optind < argc ) {
206- optarg = argv[optind ];
207- optind ++;
205+ } else if ( sc_optind < argc ) {
206+ sc_optarg = argv[sc_optind ];
207+ sc_optind ++;
208208 } else {
209209 return ' ?' ;
210210 }
0 commit comments