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