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  fputs (argv[0], stderr); \
22  fputs (s, stderr); \
23  fputc (c, stderr); \
24  } \
25 
26 int opterr = 1;
27 int optind = 1;
28 int optopt;
29 char* optarg;
30 
31 int getopt(int argc, char** argv, char* opts)
32 {
33  static int sp = 1;
34  register int c;
35  register char *cp;
36 
37  if (sp == 1)
38  {
39  if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
40  {
41  return EOF;
42  }
43  else if (strcmp (argv[optind], "--") == 0)
44  {
45  optind++;
46  return EOF;
47  }
48  }
49  optopt = c = argv[optind][sp];
50  if (c == ':' || (cp = strchr(opts, c)) == NULL)
51  {
52  ERR (": illegal option -- ", c);
53  if(argv[optind][++sp] == '\0')
54  {
55  optind++;
56  sp = 1;
57  }
58  return '?';
59  }
60  if (*++cp == ':')
61  {
62  if (argv[optind][sp+1] != '\0')
63  {
64  optarg = &argv[optind++][sp+1];
65  }
66  else if (++optind >= argc)
67  {
68  ERR (": option requires an argument -- ", c);
69  sp = 1;
70  return '?';
71  }
72  else
73  {
74  optarg = argv[optind++];
75  }
76  sp = 1;
77  }
78  else
79  {
80  if (argv[optind][++sp] == '\0')
81  {
82  sp = 1;
83  optind++;
84  }
85  optarg = NULL;
86  }
87  return c;
88 }
89 
90 #endif
int optopt
Definition: getopt.c:28
int optind
Definition: getopt.c:27
int opterr
Definition: getopt.c:26
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:31
#define ERR(s, c)
Definition: getopt.c:19
char * optarg
Definition: getopt.c:29


multisense_lib
Author(s):
autogenerated on Sun Mar 14 2021 02:34:50