summaryrefslogtreecommitdiff
path: root/openbsd-compat/getopt.c
diff options
context:
space:
mode:
Diffstat (limited to 'openbsd-compat/getopt.c')
-rw-r--r--openbsd-compat/getopt.c131
1 files changed, 0 insertions, 131 deletions
diff --git a/openbsd-compat/getopt.c b/openbsd-compat/getopt.c
deleted file mode 100644
index e5e80af06..000000000
--- a/openbsd-compat/getopt.c
+++ /dev/null
@@ -1,131 +0,0 @@
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#include "includes.h"
33#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)
34
35/* some defines to make it easier to keep the code in sync with upstream */
36/* #define getopt BSDgetopt is in defines.h */
37#define opterr BSDopterr
38#define optind BSDoptind
39#define optopt BSDoptopt
40#define optreset BSDoptreset
41#define optarg BSDoptarg
42
43#if defined(LIBC_SCCS) && !defined(lint)
44static char *rcsid = "$OpenBSD: getopt.c,v 1.5 2003/06/02 20:18:37 millert Exp $";
45#endif /* LIBC_SCCS and not lint */
46
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50
51int opterr = 1, /* if error message should be printed */
52 optind = 1, /* index into parent argv vector */
53 optopt, /* character checked for validity */
54 optreset; /* reset getopt */
55char *optarg; /* argument associated with option */
56
57#define BADCH (int)'?'
58#define BADARG (int)':'
59#define EMSG ""
60
61/*
62 * getopt --
63 * Parse argc/argv argument vector.
64 */
65int
66getopt(nargc, nargv, ostr)
67 int nargc;
68 char * const *nargv;
69 const char *ostr;
70{
71 extern char *__progname;
72 static char *place = EMSG; /* option letter processing */
73 char *oli; /* option letter list index */
74
75 if (ostr == NULL)
76 return (-1);
77
78 if (optreset || !*place) { /* update scanning pointer */
79 optreset = 0;
80 if (optind >= nargc || *(place = nargv[optind]) != '-') {
81 place = EMSG;
82 return (-1);
83 }
84 if (place[1] && *++place == '-') { /* found "--" */
85 ++optind;
86 place = EMSG;
87 return (-1);
88 }
89 } /* option letter okay? */
90 if ((optopt = (int)*place++) == (int)':' ||
91 !(oli = strchr(ostr, optopt))) {
92 /*
93 * if the user didn't specify '-' as an option,
94 * assume it means -1.
95 */
96 if (optopt == (int)'-')
97 return (-1);
98 if (!*place)
99 ++optind;
100 if (opterr && *ostr != ':')
101 (void)fprintf(stderr,
102 "%s: illegal option -- %c\n", __progname, optopt);
103 return (BADCH);
104 }
105 if (*++oli != ':') { /* don't need argument */
106 optarg = NULL;
107 if (!*place)
108 ++optind;
109 }
110 else { /* need an argument */
111 if (*place) /* no white space */
112 optarg = place;
113 else if (nargc <= ++optind) { /* no arg */
114 place = EMSG;
115 if (*ostr == ':')
116 return (BADARG);
117 if (opterr)
118 (void)fprintf(stderr,
119 "%s: option requires an argument -- %c\n",
120 __progname, optopt);
121 return (BADCH);
122 }
123 else /* white space */
124 optarg = nargv[optind];
125 place = EMSG;
126 ++optind;
127 }
128 return (optopt); /* dump back option letter */
129}
130
131#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */