PostGIS  2.1.10dev-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  char errbuf[2];\
36  errbuf[0] = (char)c; errbuf[1] = '\n';\
37  (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
38  (void) write(2, s, (unsigned)strlen(s));\
39  (void) write(2, errbuf, 2);\
40  }
41 
42 int pgis_opterr = 1;
43 int pgis_optind = 1;
46 
47 int
48 pgis_getopt(int argc, char **argv, char *opts)
49 {
50  static int sp = 1;
51  register int c;
52  register char *cp;
53 
54  if (sp == 1)
55  {
56  if (pgis_optind >= argc ||
57  argv[pgis_optind][0] != '-' /* && argv[pgis_optind][0] != '/' */ ||
58  argv[pgis_optind][1] == '\0')
59  {
60  return(EOF);
61  }
62  else if (strcmp(argv[pgis_optind], "--") == 0)
63  {
64  pgis_optind++;
65  return(EOF);
66  }
67  }
68  pgis_optopt = c = argv[pgis_optind][sp];
69  if (c == ':' || (cp=strchr(opts, c)) == 0)
70  {
71  ERR(": illegal option -- ", c);
72  if (argv[pgis_optind][++sp] == '\0')
73  {
74  pgis_optind++;
75  sp = 1;
76  }
77  return('?');
78  }
79  if (*++cp == ':')
80  {
81  if (argv[pgis_optind][sp+1] != '\0')
82  pgis_optarg = &argv[pgis_optind++][sp+1];
83  else if (++pgis_optind >= argc)
84  {
85  ERR(": option requires an argument -- ", c);
86  sp = 1;
87  return('?');
88  }
89  else
90  pgis_optarg = argv[pgis_optind++];
91  sp = 1;
92  }
93  else
94  {
95  if (argv[pgis_optind][++sp] == '\0')
96  {
97  sp = 1;
98  pgis_optind++;
99  }
100  pgis_optarg = NULL;
101  }
102  return(c);
103 }
104 
int pgis_opterr
Definition: getopt.c:42
int pgis_optopt
Definition: getopt.c:44
int pgis_optind
Definition: getopt.c:43
char * pgis_optarg
Definition: getopt.c:45
int pgis_getopt(int argc, char **argv, char *opts)
Definition: getopt.c:48
#define ERR(s, c)
Definition: getopt.c:33