PostGIS  2.3.8dev-r@@SVN_REVISION@@
getopt.c
Go to the documentation of this file.
1 /*
2 ** @(#)getopt.c 2.5 (smail) 9/15/87
3 */
4 
5 #include "../postgis_config.h"
6 #include <stdio.h>
7 #include <string.h>
8 #ifdef HAVE_UNISTD_H
9 #include <unistd.h>
10 #endif
11 #include "getopt.h"
12 
13 /*
14  * Here's something you've all been waiting for: the AT&T public domain
15  * source for getopt(3). It is the code which was given out at the 1985
16  * UNIFORUM conference in Dallas. I obtained it by electronic mail
17  * directly from AT&T. The people there assure me that it is indeed
18  * in the public domain.
19  *
20  * There is no manual page. That is because the one they gave out at
21  * UNIFORUM was slightly different from the current System V Release 2
22  * manual page. The difference apparently involved a note about the
23  * famous rules 5 and 6, recommending using white space between an option
24  * and its first argument, and not grouping options that have arguments.
25  * Getopt itself is currently lenient about both of these things. White
26  * space is allowed, but not mandatory, and the last option in a group can
27  * have an argument. That particular version of the man page evidently
28  * has no official existence, and my source at AT&T did not send a copy.
29  * The current SVR2 man page reflects the actual behavor of this getopt.
30  * However, I am not about to post a copy of anything licensed by AT&T.
31  */
32 
33 #define ERR(s, c)\
34  if(pgis_opterr){\
35  fprintf(stderr, "%s%s%c\n", argv[0], s, c);\
36  }
37 
38 int pgis_opterr = 1;
39 int pgis_optind = 1;
42 
43 int
44 pgis_getopt(int argc, char **argv, char *opts)
45 {
46  static int sp = 1;
47  register int c;
48  register char *cp;
49 
50  if (sp == 1)
51  {
52  if (pgis_optind >= argc ||
53  argv[pgis_optind][0] != '-' /* && argv[pgis_optind][0] != '/' */ ||
54  argv[pgis_optind][1] == '\0')
55  {
56  return(EOF);
57  }
58  else if (strcmp(argv[pgis_optind], "--") == 0)
59  {
60  pgis_optind++;
61  return(EOF);
62  }
63  }
64  pgis_optopt = c = argv[pgis_optind][sp];
65  if (c == ':' || (cp=strchr(opts, c)) == 0)
66  {
67  ERR(": illegal option -- ", c);
68  if (argv[pgis_optind][++sp] == '\0')
69  {
70  pgis_optind++;
71  sp = 1;
72  }
73  return('?');
74  }
75  if (*++cp == ':')
76  {
77  if (argv[pgis_optind][sp+1] != '\0')
78  pgis_optarg = &argv[pgis_optind++][sp+1];
79  else if (++pgis_optind >= argc)
80  {
81  ERR(": option requires an argument -- ", c);
82  sp = 1;
83  return('?');
84  }
85  else
86  pgis_optarg = argv[pgis_optind++];
87  sp = 1;
88  }
89  else
90  {
91  if (argv[pgis_optind][++sp] == '\0')
92  {
93  sp = 1;
94  pgis_optind++;
95  }
96  pgis_optarg = NULL;
97  }
98  return(c);
99 }
100 
int pgis_opterr
Definition: getopt.c:38
int pgis_optopt
Definition: getopt.c:40
int pgis_optind
Definition: getopt.c:39
char * pgis_optarg
Definition: getopt.c:41
int pgis_getopt(int argc, char **argv, char *opts)
Definition: getopt.c:44
opts
Definition: ovdump.py:44
#define ERR(s, c)
Definition: getopt.c:33