Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/crypto/krb5/src/util/support/getopt.c
34889 views
1
/* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */
2
/* $NetBSD: getopt.c,v 1.16 1999/12/02 13:15:56 kleink Exp $ */
3
4
/*
5
* Copyright (c) 1987, 1993, 1994
6
* The Regents of the University of California. All rights reserved.
7
*
8
* Redistribution and use in source and binary forms, with or without
9
* modification, are permitted provided that the following conditions
10
* are met:
11
* 1. Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* 2. Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* 3. All advertising materials mentioning features or use of this software
17
* must display the following acknowledgement:
18
* This product includes software developed by the University of
19
* California, Berkeley and its contributors.
20
* 4. Neither the name of the University nor the names of its contributors
21
* may be used to endorse or promote products derived from this software
22
* without specific prior written permission.
23
*
24
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
* SUCH DAMAGE.
35
*/
36
37
#if !defined(HAVE_GETOPT) || !defined(HAVE_UNISTD_H)
38
#if 0
39
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
40
#endif
41
42
#define K5_GETOPT_C
43
44
#include <assert.h>
45
#include <errno.h>
46
#include <stdio.h>
47
#include <string.h>
48
#include "k5-platform.h"
49
50
#define __P(x) x
51
#define _DIAGASSERT(x) assert(x)
52
53
int opterr = 1, /* if error message should be printed */
54
optind = 1, /* index into parent argv vector */
55
optopt; /* character checked for validity */
56
char *optarg; /* argument associated with option */
57
58
static char * _progname __P((char *));
59
int getopt_internal __P((int, char * const *, const char *));
60
61
static char *
62
_progname(nargv0)
63
char * nargv0;
64
{
65
char * tmp;
66
67
_DIAGASSERT(nargv0 != NULL);
68
69
tmp = strrchr(nargv0, '/');
70
if (tmp)
71
tmp++;
72
else
73
tmp = nargv0;
74
return(tmp);
75
}
76
77
#define BADCH (int)'?'
78
#define BADARG (int)':'
79
#define EMSG ""
80
81
/*
82
* getopt --
83
* Parse argc/argv argument vector.
84
*/
85
int
86
getopt(nargc, nargv, ostr)
87
int nargc;
88
char * const nargv[];
89
const char *ostr;
90
{
91
static char *__progname = 0;
92
static char *place = EMSG; /* option letter processing */
93
char *oli; /* option letter list index */
94
__progname = __progname?__progname:_progname(*nargv);
95
96
_DIAGASSERT(nargv != NULL);
97
_DIAGASSERT(ostr != NULL);
98
99
if (!*place) { /* update scanning pointer */
100
if (optind >= nargc || *(place = nargv[optind]) != '-') {
101
place = EMSG;
102
return (-1);
103
}
104
if (place[1] && *++place == '-' /* found "--" */
105
&& place[1] == '\0') {
106
++optind;
107
place = EMSG;
108
return (-1);
109
}
110
} /* option letter okay? */
111
if ((optopt = (int)*place++) == (int)':' ||
112
!(oli = strchr(ostr, optopt))) {
113
/*
114
* if the user didn't specify '-' as an option,
115
* assume it means -1.
116
*/
117
if (optopt == (int)'-')
118
return (-1);
119
if (!*place)
120
++optind;
121
if (opterr && *ostr != ':')
122
(void)fprintf(stderr,
123
"%s: illegal option -- %c\n", __progname, optopt);
124
return (BADCH);
125
}
126
if (*++oli != ':') { /* don't need argument */
127
optarg = NULL;
128
if (!*place)
129
++optind;
130
}
131
else { /* need an argument */
132
if (*place) /* no white space */
133
optarg = place;
134
else if (nargc <= ++optind) { /* no arg */
135
place = EMSG;
136
if (*ostr == ':')
137
return (BADARG);
138
if (opterr)
139
(void)fprintf(stderr,
140
"%s: option requires an argument -- %c\n",
141
__progname, optopt);
142
return (BADCH);
143
}
144
else /* white space */
145
optarg = nargv[optind];
146
place = EMSG;
147
++optind;
148
}
149
return (optopt); /* dump back option letter */
150
}
151
#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_UNISTD_H) */
152
153