Git init
[pkgs/e/elektra.git] / src / kdb / BSDgetopt.c
1 /*
2  * Copyright (c) 1987, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 /* OPENBSD ORIGINAL: lib/libc/stdlib/getopt.c */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include "BSDgetopt.h"
37
38 #ifndef HASGETOPT
39
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char *rcsid = "$OpenBSD: getopt.c,v 1.5 2003/06/02 20:18:37 millert Exp $";
42 #endif /* LIBC_SCCS and not lint */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #define BADCH   (int)'?'
49 #define BADARG  (int)':'
50 #define EMSG    ""
51
52 int BSDopterr = 1;              /* if error message should be printed */
53 int BSDoptind = 1;              /* index into parent argv vector */
54 /*
55  * getopt --
56  *      Parse argc/argv argument vector.
57  */
58 int
59 BSDgetopt(nargc, nargv, ostr)
60         int nargc;
61         char * const *nargv;
62         const char *ostr;
63 {
64         extern char *__progname;
65         static char *place = EMSG;              /* option letter processing */
66         char *oli;                              /* option letter list index */
67
68         if (ostr == NULL)
69                 return (-1);
70
71         if (BSDoptreset || !*place) {           /* update scanning pointer */
72                 BSDoptreset = 0;
73                 if (BSDoptind >= nargc || *(place = nargv[BSDoptind]) != '-') {
74                         place = EMSG;
75                         return (-1);
76                 }
77                 if (place[1] && *++place == '-') {      /* found "--" */
78                         ++BSDoptind;
79                         place = EMSG;
80                         return (-1);
81                 }
82         }                                       /* option letter okay? */
83         if ((BSDoptopt = (int)*place++) == (int)':' ||
84             !(oli = strchr(ostr, BSDoptopt))) {
85                 /*
86                  * if the user didn't specify '-' as an option,
87                  * assume it means -1.
88                  */
89                 if (BSDoptopt == (int)'-')
90                         return (-1);
91                 if (!*place)
92                         ++BSDoptind;
93                 if (BSDopterr && *ostr != ':')
94                         (void)fprintf(stderr,
95                             "%s: illegal option -- %c\n", __progname, BSDoptopt);
96                 return (BADCH);
97         }
98         if (*++oli != ':') {                    /* don't need argument */
99                 BSDoptarg = NULL;
100                 if (!*place)
101                         ++BSDoptind;
102         }
103         else {                                  /* need an argument */
104                 if (*place)                     /* no white space */
105                         BSDoptarg = place;
106                 else if (nargc <= ++BSDoptind) {        /* no arg */
107                         place = EMSG;
108                         if (*ostr == ':')
109                                 return (BADARG);
110                         if (BSDopterr)
111                                 (void)fprintf(stderr,
112                                     "%s: option requires an argument -- %c\n",
113                                     __progname, BSDoptopt);
114                         return (BADCH);
115                 }
116                 else                            /* white space */
117                         BSDoptarg = nargv[BSDoptind];
118                 place = EMSG;
119                 ++BSDoptind;
120         }
121         return (BSDoptopt);                     /* dump back option letter */
122 }
123
124 #endif