getopt.c revision 1.1 1 1.1 rin /* $NetBSD: getopt.c,v 1.1 2024/06/29 07:55:05 rin Exp $ */
2 1.1 rin
3 1.1 rin /*
4 1.1 rin * Copyright (c) 1987, 1993, 1994
5 1.1 rin * The Regents of the University of California. All rights reserved.
6 1.1 rin *
7 1.1 rin * Redistribution and use in source and binary forms, with or without
8 1.1 rin * modification, are permitted provided that the following conditions
9 1.1 rin * are met:
10 1.1 rin * 1. Redistributions of source code must retain the above copyright
11 1.1 rin * notice, this list of conditions and the following disclaimer.
12 1.1 rin * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rin * notice, this list of conditions and the following disclaimer in the
14 1.1 rin * documentation and/or other materials provided with the distribution.
15 1.1 rin * 3. Neither the name of the University nor the names of its contributors
16 1.1 rin * may be used to endorse or promote products derived from this software
17 1.1 rin * without specific prior written permission.
18 1.1 rin *
19 1.1 rin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 rin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 rin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 rin * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 rin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 rin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 rin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 rin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 rin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 rin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 rin * SUCH DAMAGE.
30 1.1 rin */
31 1.1 rin
32 1.1 rin #include <sys/cdefs.h>
33 1.1 rin __RCSID("$NetBSD: getopt.c,v 1.1 2024/06/29 07:55:05 rin Exp $");
34 1.1 rin
35 1.1 rin #include "namespace.h"
36 1.1 rin
37 1.1 rin #include <assert.h>
38 1.1 rin #include <errno.h>
39 1.1 rin #include <stdio.h>
40 1.1 rin #include <stdlib.h>
41 1.1 rin #include <string.h>
42 1.1 rin #include <unistd.h>
43 1.1 rin
44 1.1 rin #ifdef __weak_alias
45 1.1 rin __weak_alias(getopt,_getopt)
46 1.1 rin #endif
47 1.1 rin
48 1.1 rin int opterr = 1, /* if error message should be printed */
49 1.1 rin optind = 1, /* index into parent argv vector */
50 1.1 rin optopt, /* character checked for validity */
51 1.1 rin optreset; /* reset getopt */
52 1.1 rin char *optarg; /* argument associated with option */
53 1.1 rin
54 1.1 rin #define BADCH (int)'?'
55 1.1 rin #define BADARG (int)':'
56 1.1 rin #define EMSG ""
57 1.1 rin
58 1.1 rin /*
59 1.1 rin * getopt --
60 1.1 rin * Parse argc/argv argument vector.
61 1.1 rin */
62 1.1 rin int
63 1.1 rin getopt(int nargc, char * const nargv[], const char *ostr)
64 1.1 rin {
65 1.1 rin static const char *place = EMSG; /* option letter processing */
66 1.1 rin const char *oli; /* option letter list index */
67 1.1 rin
68 1.1 rin _DIAGASSERT(nargv != NULL);
69 1.1 rin _DIAGASSERT(ostr != NULL);
70 1.1 rin
71 1.1 rin if (optreset || *place == 0) { /* update scanning pointer */
72 1.1 rin optreset = 0;
73 1.1 rin place = nargv[optind];
74 1.1 rin if (optind >= nargc || *place++ != '-') {
75 1.1 rin /* Argument is absent or is not an option */
76 1.1 rin place = EMSG;
77 1.1 rin return (-1);
78 1.1 rin }
79 1.1 rin optopt = *place++;
80 1.1 rin if (optopt == '-' && *place == 0) {
81 1.1 rin /* "--" => end of options */
82 1.1 rin ++optind;
83 1.1 rin place = EMSG;
84 1.1 rin return (-1);
85 1.1 rin }
86 1.1 rin if (optopt == 0) {
87 1.1 rin /* Solitary '-', treat as a '-' option
88 1.1 rin if the program (eg su) is looking for it. */
89 1.1 rin place = EMSG;
90 1.1 rin if (strchr(ostr, '-') == NULL)
91 1.1 rin return -1;
92 1.1 rin optopt = '-';
93 1.1 rin }
94 1.1 rin } else
95 1.1 rin optopt = *place++;
96 1.1 rin
97 1.1 rin /* See if option letter is one the caller wanted... */
98 1.1 rin if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
99 1.1 rin if (*place == 0)
100 1.1 rin ++optind;
101 1.1 rin if (opterr && *ostr != ':')
102 1.1 rin (void)fprintf(stderr,
103 1.1 rin "%s: unknown option -- %c\n", getprogname(),
104 1.1 rin optopt);
105 1.1 rin return (BADCH);
106 1.1 rin }
107 1.1 rin
108 1.1 rin /* Does this option need an argument? */
109 1.1 rin if (oli[1] != ':') {
110 1.1 rin /* don't need argument */
111 1.1 rin optarg = NULL;
112 1.1 rin if (*place == 0)
113 1.1 rin ++optind;
114 1.1 rin } else {
115 1.1 rin /* Option-argument is either the rest of this argument or the
116 1.1 rin entire next argument. */
117 1.1 rin if (*place)
118 1.1 rin optarg = __UNCONST(place);
119 1.1 rin else if (oli[2] == ':')
120 1.1 rin /*
121 1.1 rin * GNU Extension, for optional arguments if the rest of
122 1.1 rin * the argument is empty, we return NULL
123 1.1 rin */
124 1.1 rin optarg = NULL;
125 1.1 rin else if (nargc > ++optind)
126 1.1 rin optarg = nargv[optind];
127 1.1 rin else {
128 1.1 rin /* option-argument absent */
129 1.1 rin place = EMSG;
130 1.1 rin if (*ostr == ':')
131 1.1 rin return (BADARG);
132 1.1 rin if (opterr)
133 1.1 rin (void)fprintf(stderr,
134 1.1 rin "%s: option requires an argument -- %c\n",
135 1.1 rin getprogname(), optopt);
136 1.1 rin return (BADCH);
137 1.1 rin }
138 1.1 rin place = EMSG;
139 1.1 rin ++optind;
140 1.1 rin }
141 1.1 rin return (optopt); /* return option letter */
142 1.1 rin }
143