ReactOS  0.4.15-dev-499-g1f31905
getopt.c
Go to the documentation of this file.
1 /*
2  * This is an unpublished work copyright (c) 1998 HELIOS Software GmbH
3  * 30827 Garbsen, Germany
4  */
5 
6 
7 #include <stdio.h>
8 #include <string.h>
9 #ifdef HAS_UNISTD
10 # include <unistd.h>
11 #endif
12 
13 char *optarg;
14 int optind = 1;
15 int opterr = 1;
16 int optopt;
17 static int subopt;
18 static int suboptind = 1;
19 
20 int getopt(int argc, char *const argv[], const char * optstring)
21 {
22  char *curopt;
23  char *p;
24  int cursubopt;
25 
26  if (suboptind == optind-1 && argv[suboptind][subopt] != '\0') {
27  curopt = (char *)argv[suboptind];
28  } else {
29  curopt = (char *)argv[optind];
30  if (curopt == NULL || curopt[0] != '-' || strcmp(curopt, "-") == 0)
31  return -1;
32  suboptind = optind;
33  subopt = 1;
34  optind++;
35  if (strcmp(curopt, "--") == 0)
36  return -1;
37  }
38  cursubopt = subopt++;
39  if ((p = strchr(optstring, curopt[cursubopt])) == NULL) {
40  optopt = curopt[cursubopt];
41  if (opterr)
42  fprintf(stderr, "%s: illegal option -- %c\n", argv[0], optopt);
43  return '?';
44  }
45  if (p[1] == ':') {
46  if (curopt[cursubopt+1] != '\0') {
47  optarg = curopt+cursubopt+1;
48  suboptind++;
49  return p[0];
50  }
51  if (argv[optind] == NULL) {
52  optopt = p[0];
53  if (opterr)
54  fprintf(stderr, "%s: option requires an argument -- %c\n", argv[0], optopt);
55  if (*optstring == ':')
56  return ':';
57  return '?';
58  }
59  optarg = argv[optind++];
60  }
61  return p[0];
62 }
static int argc
Definition: ServiceArgs.c:12
int optopt
Definition: getopt.c:47
int optind
Definition: getopt.c:47
#define argv
Definition: mplay32.c:18
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
static int suboptind
Definition: getopt.c:18
smooth NULL
Definition: ftsmooth.c:416
int getopt(int nargc, char *const *nargv, const char *ostr)
Definition: getopt.c:55
int opterr
Definition: getopt.c:46
const char * optarg
Definition: getopt.c:49
char * strchr(const char *String, int ch)
Definition: utclib.c:501
FILE * stderr
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
GLfloat GLfloat p
Definition: glext.h:8902
static int subopt
Definition: getopt.c:17