2
* getopt_long() -- long options parser
4
* Portions Copyright (c) 1987, 1993, 1994
5
* The Regents of the University of California. All rights reserved.
7
* Portions Copyright (c) 2003
8
* PostgreSQL Global Development Group
10
* Redistribution and use in source and binary forms, with or without
11
* modification, are permitted provided that the following conditions
13
* 1. Redistributions of source code must retain the above copyright
14
* notice, this list of conditions and the following disclaimer.
15
* 2. Redistributions in binary form must reproduce the above copyright
16
* notice, this list of conditions and the following disclaimer in the
17
* documentation and/or other materials provided with the distribution.
18
* 3. All advertising materials mentioning features or use of this software
19
* must display the following acknowledgement:
20
* This product includes software developed by the University of
21
* California, Berkeley and its contributors.
22
* 4. Neither the name of the University nor the names of its contributors
23
* may be used to endorse or promote products derived from this software
24
* without specific prior written permission.
26
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38
* $PostgreSQL: pgsql/src/port/getopt_long.c,v 1.3 2003-11-29 19:52:13 pgsql Exp $
45
#include "getopt_long.h"
52
getopt_long(int argc, char *const argv[],
53
const char *optstring,
54
const struct option * longopts, int *longindex)
56
static char *place = EMSG; /* option letter processing */
57
char *oli; /* option letter list index */
59
if (optreset || !*place)
60
{ /* update scanning pointer */
79
if (place[0] && place[0] == '-' && place[1] == '\0')
86
if (place[0] && place[0] == '-' && place[1])
94
namelen = strcspn(place, "=");
95
for (i = 0; longopts[i].name != NULL; i++)
97
if (strlen(longopts[i].name) == namelen
98
&& strncmp(place, longopts[i].name, namelen) == 0)
100
if (longopts[i].has_arg)
102
if (place[namelen] == '=')
103
optarg = place + namelen + 1;
104
else if (optind < argc - 1)
107
optarg = argv[optind];
111
if (optstring[0] == ':')
115
"%s: option requires an argument -- %s\n",
125
if (place[namelen] != 0)
138
if (longopts[i].flag == NULL)
139
return longopts[i].val;
142
*longopts[i].flag = longopts[i].val;
148
if (opterr && optstring[0] != ':')
150
"%s: illegal option -- %s\n", argv[0], place);
158
optopt = (int) *place++;
160
oli = strchr(optstring, optopt);
165
if (opterr && *optstring != ':')
167
"%s: illegal option -- %c\n", argv[0], optopt);
172
{ /* don't need argument */
178
{ /* need an argument */
179
if (*place) /* no white space */
181
else if (argc <= ++optind)
184
if (*optstring == ':')
188
"%s: option requires an argument -- %c\n",
194
optarg = argv[optind];