getopt.c
Go to the documentation of this file.
1 /*
2 POSIX getopt for Windows
3 
4 AT&T Public License
5 
6 Code given out at the 1985 UNIFORUM conference in Dallas.
7 
8 Significant updates
9  2014-12-02: Updated to use CRL_HAVE_GETOPT preprocessor definition and work with modern dialects of C and C++
10 */
11 
12 #include "getopt.h"
13 
14 #ifndef CRL_HAVE_GETOPT
15 
16 #include <stdio.h>
17 #include <string.h>
18 
19 #define ERR(s, c) \
20  if (opterr) { \
21  char errbuf[2]; \
22  errbuf[0] = c; \
23  errbuf[1] = '\n'; \
24  fputs (argv[0], stderr); \
25  fputs (s, stderr); \
26  fputc(c, stderr); \
27  } \
28 
29 int opterr = 1;
30 int optind = 1;
31 int optopt;
32 char* optarg;
33 
34 int getopt(int argc, char** argv, char* opts)
35 {
36  static int sp = 1;
37  register int c;
38  register char *cp;
39 
40  if (sp == 1)
41  {
42  if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
43  {
44  return EOF;
45  }
46  else if (strcmp (argv[optind], "--") == 0)
47  {
48  optind++;
49  return EOF;
50  }
51  }
52  optopt = c = argv[optind][sp];
53  if (c == ':' || (cp = strchr(opts, c)) == NULL)
54  {
55  ERR (": illegal option -- ", c);
56  if(argv[optind][++sp] == '\0')
57  {
58  optind++;
59  sp = 1;
60  }
61  return '?';
62  }
63  if (*++cp == ':')
64  {
65  if (argv[optind][sp+1] != '\0')
66  {
67  optarg = &argv[optind++][sp+1];
68  }
69  else if (++optind >= argc)
70  {
71  ERR (": option requires an argument -- ", c);
72  sp = 1;
73  return '?';
74  }
75  else
76  {
77  optarg = argv[optind++];
78  }
79  sp = 1;
80  }
81  else
82  {
83  if (argv[optind][++sp] == '\0')
84  {
85  sp = 1;
86  optind++;
87  }
88  optarg = NULL;
89  }
90  return c;
91 }
92 
93 #endif
int optopt
Definition: getopt.c:31
int optind
Definition: getopt.c:30
int opterr
Definition: getopt.c:29
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:34
#define ERR(s, c)
Definition: getopt.c:19
char * optarg
Definition: getopt.c:32


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46