OpenCoverage

getopt_long.c

Absolute File Name:/home/opencoverage/opencoverage/guest-scripts/openssh/src/openbsd-compat/getopt_long.c
Source codeSwitch to Preprocessed file
LineSourceCount
1/* $OpenBSD: getopt_long.c,v 1.25 2011/03/05 22:10:11 guenther Exp $ */-
2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */-
3-
4/*-
5 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>-
6 *-
7 * Permission to use, copy, modify, and distribute this software for any-
8 * purpose with or without fee is hereby granted, provided that the above-
9 * copyright notice and this permission notice appear in all copies.-
10 *-
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES-
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF-
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR-
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES-
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN-
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF-
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.-
18 *-
19 * Sponsored in part by the Defense Advanced Research Projects-
20 * Agency (DARPA) and Air Force Research Laboratory, Air Force-
21 * Materiel Command, USAF, under agreement number F39502-99-1-0512.-
22 */-
23/*--
24 * Copyright (c) 2000 The NetBSD Foundation, Inc.-
25 * All rights reserved.-
26 *-
27 * This code is derived from software contributed to The NetBSD Foundation-
28 * by Dieter Baron and Thomas Klausner.-
29 *-
30 * Redistribution and use in source and binary forms, with or without-
31 * modification, are permitted provided that the following conditions-
32 * are met:-
33 * 1. Redistributions of source code must retain the above copyright-
34 * notice, this list of conditions and the following disclaimer.-
35 * 2. Redistributions in binary form must reproduce the above copyright-
36 * notice, this list of conditions and the following disclaimer in the-
37 * documentation and/or other materials provided with the distribution.-
38 *-
39 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS-
40 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED-
41 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR-
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS-
43 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR-
44 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF-
45 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS-
46 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN-
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)-
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE-
49 * POSSIBILITY OF SUCH DAMAGE.-
50 */-
51-
52/* OPENBSD ORIGINAL: lib/libc/stdlib/getopt_long.c */-
53#include "includes.h"-
54-
55#if !defined(HAVE_GETOPT) || !defined(HAVE_GETOPT_OPTRESET)-
56-
57/*-
58 * Some defines to make it easier to keep the code in sync with upstream.-
59 * getopt opterr optind optopt optreset optarg are all in defines.h which is-
60 * pulled in by includes.h.-
61 */-
62#define warnx logit-
63-
64#if 0-
65#include <err.h>-
66#include <getopt.h>-
67#endif-
68#include <errno.h>-
69#include <stdlib.h>-
70#include <string.h>-
71#include <stdarg.h>-
72-
73#include "log.h"-
74-
75int opterr = 1; /* if error message should be printed */-
76int optind = 1; /* index into parent argv vector */-
77int optopt = '?'; /* character checked for validity */-
78int optreset; /* reset getopt */-
79char *optarg; /* argument associated with option */-
80-
81#define PRINT_ERROR ((opterr) && (*options != ':'))-
82-
83#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */-
84#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */-
85#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */-
86-
87/* return values */-
88#define BADCH (int)'?'-
89#define BADARG ((*options == ':') ? (int)':' : (int)'?')-
90#define INORDER (int)1-
91-
92#define EMSG ""-
93-
94static int getopt_internal(int, char * const *, const char *,-
95 const struct option *, int *, int);-
96static int parse_long_options(char * const *, const char *,-
97 const struct option *, int *, int);-
98static int gcd(int, int);-
99static void permute_args(int, int, int, char * const *);-
100-
101static char *place = EMSG; /* option letter processing */-
102-
103/* XXX: set optreset to 1 rather than these two */-
104static int nonopt_start = -1; /* first non option argument (for permute) */-
105static int nonopt_end = -1; /* first option after non options (for permute) */-
106-
107/* Error messages */-
108static const char recargchar[] = "option requires an argument -- %c";-
109static const char recargstring[] = "option requires an argument -- %s";-
110static const char ambig[] = "ambiguous option -- %.*s";-
111static const char noarg[] = "option doesn't take an argument -- %.*s";-
112static const char illoptchar[] = "unknown option -- %c";-
113static const char illoptstring[] = "unknown option -- %s";-
114-
115/*-
116 * Compute the greatest common divisor of a and b.-
117 */-
118static int-
119gcd(int a, int b)-
120{-
121 int c;-
122-
123 c = a % b;-
124 while (c != 0) {
c != 0Description
TRUEnever evaluated
FALSEnever evaluated
0
125 a = b;-
126 b = c;-
127 c = a % b;-
128 }
never executed: end of block
0
129-
130 return (b);
never executed: return (b);
0
131}-
132-
133/*-
134 * Exchange the block from nonopt_start to nonopt_end with the block-
135 * from nonopt_end to opt_end (keeping the same order of arguments-
136 * in each block).-
137 */-
138static void-
139permute_args(int panonopt_start, int panonopt_end, int opt_end,-
140 char * const *nargv)-
141{-
142 int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;-
143 char *swap;-
144-
145 /*-
146 * compute lengths of blocks and number and size of cycles-
147 */-
148 nnonopts = panonopt_end - panonopt_start;-
149 nopts = opt_end - panonopt_end;-
150 ncycle = gcd(nnonopts, nopts);-
151 cyclelen = (opt_end - panonopt_start) / ncycle;-
152-
153 for (i = 0; i < ncycle; i++) {
i < ncycleDescription
TRUEnever evaluated
FALSEnever evaluated
0
154 cstart = panonopt_end+i;-
155 pos = cstart;-
156 for (j = 0; j < cyclelen; j++) {
j < cyclelenDescription
TRUEnever evaluated
FALSEnever evaluated
0
157 if (pos >= panonopt_end)
pos >= panonopt_endDescription
TRUEnever evaluated
FALSEnever evaluated
0
158 pos -= nnonopts;
never executed: pos -= nnonopts;
0
159 else-
160 pos += nopts;
never executed: pos += nopts;
0
161 swap = nargv[pos];-
162 /* LINTED const cast */-
163 ((char **) nargv)[pos] = nargv[cstart];-
164 /* LINTED const cast */-
165 ((char **)nargv)[cstart] = swap;-
166 }
never executed: end of block
0
167 }
never executed: end of block
0
168}
never executed: end of block
0
169-
170/*-
171 * parse_long_options ---
172 * Parse long options in argc/argv argument vector.-
173 * Returns -1 if short_too is set and the option does not match long_options.-
174 */-
175static int-
176parse_long_options(char * const *nargv, const char *options,-
177 const struct option *long_options, int *idx, int short_too)-
178{-
179 char *current_argv, *has_equal;-
180 size_t current_argv_len;-
181 int i, match;-
182-
183 current_argv = place;-
184 match = -1;-
185-
186 optind++;-
187-
188 if ((has_equal = strchr(current_argv, '=')) != NULL) {
(has_equal = (...!= ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( '=' )Description
TRUEnever evaluated
FALSEnever evaluated
!__builtin_con...current_argv )Description
TRUEnever evaluated
FALSEnever evaluated
( '=' ) == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0
189 /* argument found (--option=arg) */-
190 current_argv_len = has_equal - current_argv;-
191 has_equal++;-
192 } else
never executed: end of block
0
193 current_argv_len = strlen(current_argv);
never executed: current_argv_len = strlen(current_argv);
0
194-
195 for (i = 0; long_options[i].name; i++) {
long_options[i].nameDescription
TRUEnever evaluated
FALSEnever evaluated
0
196 /* find matching long option */-
197 if (strncmp(current_argv, long_options[i].name,
never executed: __result = (((const unsigned char *) (const char *) ( current_argv ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
never executed: __result = (((const unsigned char *) (const char *) ( long_options[i].name ))[3] - __s2[3]);
never executed: end of block
never executed: end of block
(__extension__...t_argv_len )))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...ent_argv_len )Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...current_argv )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( curre...nt_argv_len ))Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...ions[i].name )Description
TRUEnever evaluated
FALSEnever evaluated
strlen ( long_...nt_argv_len ))Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s1_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 0Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 1Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
__s2_len > 2Description
TRUEnever evaluated
FALSEnever evaluated
__result == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
198 current_argv_len))-
199 continue;
never executed: continue;
0
200-
201 if (strlen(long_options[i].name) == current_argv_len) {
strlen(long_op...rrent_argv_lenDescription
TRUEnever evaluated
FALSEnever evaluated
0
202 /* exact match */-
203 match = i;-
204 break;
never executed: break;
0
205 }-
206 /*-
207 * If this is a known short option, don't allow-
208 * a partial match of a single character.-
209 */-
210 if (short_too && current_argv_len == 1)
short_tooDescription
TRUEnever evaluated
FALSEnever evaluated
current_argv_len == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
211 continue;
never executed: continue;
0
212-
213 if (match == -1) /* partial match */
match == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
214 match = i;
never executed: match = i;
0
215 else {-
216 /* ambiguous abbreviation */-
217 if (PRINT_ERROR)
(BSDopterr)Description
TRUEnever evaluated
FALSEnever evaluated
(*options != ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
218 warnx(ambig, (int)current_argv_len,
never executed: logit(ambig, (int)current_argv_len, current_argv);
0
219 current_argv);
never executed: logit(ambig, (int)current_argv_len, current_argv);
0
220 optopt = 0;-
221 return (BADCH);
never executed: return ((int)'?');
0
222 }-
223 }-
224 if (match != -1) { /* option found */
match != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
225 if (long_options[match].has_arg == no_argument
long_options[m...].has_arg == 0Description
TRUEnever evaluated
FALSEnever evaluated
0
226 && has_equal) {
has_equalDescription
TRUEnever evaluated
FALSEnever evaluated
0
227 if (PRINT_ERROR)
(BSDopterr)Description
TRUEnever evaluated
FALSEnever evaluated
(*options != ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
228 warnx(noarg, (int)current_argv_len,
never executed: logit(noarg, (int)current_argv_len, current_argv);
0
229 current_argv);
never executed: logit(noarg, (int)current_argv_len, current_argv);
0
230 /*-
231 * XXX: GNU sets optopt to val regardless of flag-
232 */-
233 if (long_options[match].flag == NULL)
long_options[m...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
234 optopt = long_options[match].val;
never executed: BSDoptopt = long_options[match].val;
0
235 else-
236 optopt = 0;
never executed: BSDoptopt = 0;
0
237 return (BADARG);
never executed: return (((*options == ':') ? (int)':' : (int)'?'));
(*options == ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
238 }-
239 if (long_options[match].has_arg == required_argument ||
long_options[m...].has_arg == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
240 long_options[match].has_arg == optional_argument) {
long_options[m...].has_arg == 2Description
TRUEnever evaluated
FALSEnever evaluated
0
241 if (has_equal)
has_equalDescription
TRUEnever evaluated
FALSEnever evaluated
0
242 optarg = has_equal;
never executed: BSDoptarg = has_equal;
0
243 else if (long_options[match].has_arg ==
long_options[m...].has_arg == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
244 required_argument) {
long_options[m...].has_arg == 1Description
TRUEnever evaluated
FALSEnever evaluated
0
245 /*-
246 * optional argument doesn't use next nargv-
247 */-
248 optarg = nargv[optind++];-
249 }
never executed: end of block
0
250 }
never executed: end of block
0
251 if ((long_options[match].has_arg == required_argument)
(long_options[....has_arg == 1)Description
TRUEnever evaluated
FALSEnever evaluated
0
252 && (optarg == NULL)) {
(BSDoptarg == ((void *)0) )Description
TRUEnever evaluated
FALSEnever evaluated
0
253 /*-
254 * Missing argument; leading ':' indicates no error-
255 * should be generated.-
256 */-
257 if (PRINT_ERROR)
(BSDopterr)Description
TRUEnever evaluated
FALSEnever evaluated
(*options != ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
258 warnx(recargstring,
never executed: logit(recargstring, current_argv);
0
259 current_argv);
never executed: logit(recargstring, current_argv);
0
260 /*-
261 * XXX: GNU sets optopt to val regardless of flag-
262 */-
263 if (long_options[match].flag == NULL)
long_options[m...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
0
264 optopt = long_options[match].val;
never executed: BSDoptopt = long_options[match].val;
0
265 else-
266 optopt = 0;
never executed: BSDoptopt = 0;
0
267 --optind;-
268 return (BADARG);
never executed: return (((*options == ':') ? (int)':' : (int)'?'));
(*options == ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
269 }-
270 } else { /* unknown option */
never executed: end of block
0
271 if (short_too) {
short_tooDescription
TRUEnever evaluated
FALSEnever evaluated
0
272 --optind;-
273 return (-1);
never executed: return (-1);
0
274 }-
275 if (PRINT_ERROR)
(BSDopterr)Description
TRUEnever evaluated
FALSEnever evaluated
(*options != ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
276 warnx(illoptstring, current_argv);
never executed: logit(illoptstring, current_argv);
0
277 optopt = 0;-
278 return (BADCH);
never executed: return ((int)'?');
0
279 }-
280 if (idx)
idxDescription
TRUEnever evaluated
FALSEnever evaluated
0
281 *idx = match;
never executed: *idx = match;
0
282 if (long_options[match].flag) {
long_options[match].flagDescription
TRUEnever evaluated
FALSEnever evaluated
0
283 *long_options[match].flag = long_options[match].val;-
284 return (0);
never executed: return (0);
0
285 } else-
286 return (long_options[match].val);
never executed: return (long_options[match].val);
0
287}-
288-
289/*-
290 * getopt_internal ---
291 * Parse argc/argv argument vector. Called by user level routines.-
292 */-
293static int-
294getopt_internal(int nargc, char * const *nargv, const char *options,-
295 const struct option *long_options, int *idx, int flags)-
296{-
297 char *oli; /* option letter list index */-
298 int optchar, short_too;-
299 static int posixly_correct = -1;-
300-
301 if (options == NULL)
options == ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-117
302 return (-1);
never executed: return (-1);
0
303-
304 /*-
305 * XXX Some GNU programs (like cvs) set optind to 0 instead of-
306 * XXX using optreset. Work around this braindamage.-
307 */-
308 if (optind == 0)
BSDoptind == 0Description
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-117
309 optind = optreset = 1;
never executed: BSDoptind = BSDoptreset = 1;
0
310-
311 /*-
312 * Disable GNU extensions if POSIXLY_CORRECT is set or options-
313 * string begins with a '+'.-
314 */-
315 if (posixly_correct == -1 || optreset)
posixly_correct == -1Description
TRUEevaluated 38 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
BSDoptresetDescription
TRUEnever evaluated
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
0-79
316 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
executed 38 times by 10 tests: posixly_correct = (getenv("POSIXLY_CORRECT") != ((void *)0) );
Executed by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
38
317 if (*options == '-')
*options == '-'Description
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-117
318 flags |= FLAG_ALLARGS;
never executed: flags |= 0x02;
0
319 else if (posixly_correct || *options == '+')
posixly_correctDescription
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
*options == '+'Description
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-117
320 flags &= ~FLAG_PERMUTE;
never executed: flags &= ~0x01;
0
321 if (*options == '+' || *options == '-')
*options == '+'Description
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
*options == '-'Description
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-117
322 options++;
never executed: options++;
0
323-
324 optarg = NULL;-
325 if (optreset)
BSDoptresetDescription
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-117
326 nonopt_start = nonopt_end = -1;
never executed: nonopt_start = nonopt_end = -1;
0
327start:
code before this statement executed 117 times by 10 tests: start:
Executed by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
117
328 if (optreset || !*place) { /* update scanning pointer */
BSDoptresetDescription
TRUEnever evaluated
FALSEevaluated 117 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
!*placeDescription
TRUEevaluated 96 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
FALSEevaluated 21 times by 1 test
Evaluated by:
  • ssh-keygen
0-117
329 optreset = 0;-
330 if (optind >= nargc) { /* end of argument vector */
BSDoptind >= nargcDescription
TRUEevaluated 38 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
FALSEevaluated 58 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
38-58
331 place = EMSG;-
332 if (nonopt_end != -1) {
nonopt_end != -1Description
TRUEnever evaluated
FALSEevaluated 38 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-38
333 /* do permutation, if we have to */-
334 permute_args(nonopt_start, nonopt_end,-
335 optind, nargv);-
336 optind -= nonopt_end - nonopt_start;-
337 }
never executed: end of block
0
338 else if (nonopt_start != -1) {
nonopt_start != -1Description
TRUEnever evaluated
FALSEevaluated 38 times by 10 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
0-38
339 /*-
340 * If we skipped non-options, set optind-
341 * to the first of them.-
342 */-
343 optind = nonopt_start;-
344 }
never executed: end of block
0
345 nonopt_start = nonopt_end = -1;-
346 return (-1);
executed 38 times by 10 tests: return (-1);
Executed by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
38
347 }-
348 if (*(place = nargv[optind]) != '-' ||
*(place = narg...ptind]) != '-'Description
TRUEnever evaluated
FALSEevaluated 58 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
0-58
349 (place[1] == '\0' && strchr(options, '-') == NULL)) {
place[1] == '\0'Description
TRUEnever evaluated
FALSEevaluated 58 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
(__extension__...== ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_constant_p ( '-' )Description
TRUEnever evaluated
FALSEnever evaluated
!__builtin_con..._p ( options )Description
TRUEnever evaluated
FALSEnever evaluated
( '-' ) == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0-58
350 place = EMSG; /* found non-option */-
351 if (flags & FLAG_ALLARGS) {
flags & 0x02Description
TRUEnever evaluated
FALSEnever evaluated
0
352 /*-
353 * GNU extension:-
354 * return non-option as argument to option 1-
355 */-
356 optarg = nargv[optind++];-
357 return (INORDER);
never executed: return ((int)1);
0
358 }-
359 if (!(flags & FLAG_PERMUTE)) {
!(flags & 0x01)Description
TRUEnever evaluated
FALSEnever evaluated
0
360 /*-
361 * If no permutation wanted, stop parsing-
362 * at first non-option.-
363 */-
364 return (-1);
never executed: return (-1);
0
365 }-
366 /* do permutation */-
367 if (nonopt_start == -1)
nonopt_start == -1Description
TRUEnever evaluated
FALSEnever evaluated
0
368 nonopt_start = optind;
never executed: nonopt_start = BSDoptind;
0
369 else if (nonopt_end != -1) {
nonopt_end != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
370 permute_args(nonopt_start, nonopt_end,-
371 optind, nargv);-
372 nonopt_start = optind --
373 (nonopt_end - nonopt_start);-
374 nonopt_end = -1;-
375 }
never executed: end of block
0
376 optind++;-
377 /* process next argument */-
378 goto start;
never executed: goto start;
0
379 }-
380 if (nonopt_start != -1 && nonopt_end == -1)
nonopt_start != -1Description
TRUEnever evaluated
FALSEevaluated 58 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
nonopt_end == -1Description
TRUEnever evaluated
FALSEnever evaluated
0-58
381 nonopt_end = optind;
never executed: nonopt_end = BSDoptind;
0
382-
383 /*-
384 * If we have "-" do nothing, if "--" we are done.-
385 */-
386 if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
place[1] != '\0'Description
TRUEevaluated 58 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
FALSEnever evaluated
*++place == '-'Description
TRUEnever evaluated
FALSEevaluated 58 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
place[1] == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0-58
387 optind++;-
388 place = EMSG;-
389 /*-
390 * We found an option (--), so if we skipped-
391 * non-options, we have to permute.-
392 */-
393 if (nonopt_end != -1) {
nonopt_end != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
394 permute_args(nonopt_start, nonopt_end,-
395 optind, nargv);-
396 optind -= nonopt_end - nonopt_start;-
397 }
never executed: end of block
0
398 nonopt_start = nonopt_end = -1;-
399 return (-1);
never executed: return (-1);
0
400 }-
401 }
executed 58 times by 4 tests: end of block
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
58
402-
403 /*-
404 * Check long options if:-
405 * 1) we were passed some-
406 * 2) the arg is not just "-"-
407 * 3) either the arg starts with -- we are getopt_long_only()-
408 */-
409 if (long_options != NULL && place != nargv[optind] &&
long_options != ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
place != nargv[BSDoptind]Description
TRUEnever evaluated
FALSEnever evaluated
0-79
410 (*place == '-' || (flags & FLAG_LONGONLY))) {
*place == '-'Description
TRUEnever evaluated
FALSEnever evaluated
(flags & 0x04)Description
TRUEnever evaluated
FALSEnever evaluated
0
411 short_too = 0;-
412 if (*place == '-')
*place == '-'Description
TRUEnever evaluated
FALSEnever evaluated
0
413 place++; /* --foo long option */
never executed: place++;
0
414 else if (*place != ':' && strchr(options, *place) != NULL)
*place != ':'Description
TRUEnever evaluated
FALSEnever evaluated
(__extension__...!= ((void *)0)Description
TRUEnever evaluated
FALSEnever evaluated
__builtin_cons...t_p ( *place )Description
TRUEnever evaluated
FALSEnever evaluated
!__builtin_con..._p ( options )Description
TRUEnever evaluated
FALSEnever evaluated
( *place ) == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0
415 short_too = 1; /* could be short option too */
never executed: short_too = 1;
0
416-
417 optchar = parse_long_options(nargv, options, long_options,-
418 idx, short_too);-
419 if (optchar != -1) {
optchar != -1Description
TRUEnever evaluated
FALSEnever evaluated
0
420 place = EMSG;-
421 return (optchar);
never executed: return (optchar);
0
422 }-
423 }
never executed: end of block
0
424-
425 if ((optchar = (int)*place++) == (int)':' ||
(optchar = (in...+) == (int)':'Description
TRUEnever evaluated
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
0-79
426 (optchar == (int)'-' && *place != '\0') ||
optchar == (int)'-'Description
TRUEnever evaluated
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
*place != '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0-79
427 (oli = strchr(options, optchar)) == NULL) {
(oli = (__exte...== ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
__builtin_cons..._p ( optchar )Description
TRUEnever evaluated
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
!__builtin_con..._p ( options )Description
TRUEnever evaluated
FALSEnever evaluated
( optchar ) == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0-79
428 /*-
429 * If the user specified "-" and '-' isn't listed in-
430 * options, return -1 (non-option) as per POSIX.-
431 * Otherwise, it is an unknown option character (or ':').-
432 */-
433 if (optchar == (int)'-' && *place == '\0')
optchar == (int)'-'Description
TRUEnever evaluated
FALSEnever evaluated
*place == '\0'Description
TRUEnever evaluated
FALSEnever evaluated
0
434 return (-1);
never executed: return (-1);
0
435 if (!*place)
!*placeDescription
TRUEnever evaluated
FALSEnever evaluated
0
436 ++optind;
never executed: ++BSDoptind;
0
437 if (PRINT_ERROR)
(BSDopterr)Description
TRUEnever evaluated
FALSEnever evaluated
(*options != ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
438 warnx(illoptchar, optchar);
never executed: logit(illoptchar, optchar);
0
439 optopt = optchar;-
440 return (BADCH);
never executed: return ((int)'?');
0
441 }-
442 if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
long_options != ((void *)0)Description
TRUEnever evaluated
FALSEevaluated 79 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
optchar == 'W'Description
TRUEnever evaluated
FALSEnever evaluated
oli[1] == ';'Description
TRUEnever evaluated
FALSEnever evaluated
0-79
443 /* -W long-option */-
444 if (*place) /* no space */
*placeDescription
TRUEnever evaluated
FALSEnever evaluated
0
445 /* NOTHING */;
never executed: ;
0
446 else if (++optind >= nargc) { /* no arg */
++BSDoptind >= nargcDescription
TRUEnever evaluated
FALSEnever evaluated
0
447 place = EMSG;-
448 if (PRINT_ERROR)
(BSDopterr)Description
TRUEnever evaluated
FALSEnever evaluated
(*options != ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
449 warnx(recargchar, optchar);
never executed: logit(recargchar, optchar);
0
450 optopt = optchar;-
451 return (BADARG);
never executed: return (((*options == ':') ? (int)':' : (int)'?'));
(*options == ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
452 } else /* white space */-
453 place = nargv[optind];
never executed: place = nargv[BSDoptind];
0
454 optchar = parse_long_options(nargv, options, long_options,-
455 idx, 0);-
456 place = EMSG;-
457 return (optchar);
never executed: return (optchar);
0
458 }-
459 if (*++oli != ':') { /* doesn't take argument */
*++oli != ':'Description
TRUEevaluated 30 times by 2 tests
Evaluated by:
  • ssh-keygen
  • sshd
FALSEevaluated 49 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
30-49
460 if (!*place)
!*placeDescription
TRUEevaluated 9 times by 2 tests
Evaluated by:
  • ssh-keygen
  • sshd
FALSEevaluated 21 times by 1 test
Evaluated by:
  • ssh-keygen
9-21
461 ++optind;
executed 9 times by 2 tests: ++BSDoptind;
Executed by:
  • ssh-keygen
  • sshd
9
462 } else { /* takes (optional) argument */
executed 30 times by 2 tests: end of block
Executed by:
  • ssh-keygen
  • sshd
30
463 optarg = NULL;-
464 if (*place) /* no white space */
*placeDescription
TRUEnever evaluated
FALSEevaluated 49 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
0-49
465 optarg = place;
never executed: BSDoptarg = place;
0
466 else if (oli[1] != ':') { /* arg not optional */
oli[1] != ':'Description
TRUEevaluated 49 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
FALSEnever evaluated
0-49
467 if (++optind >= nargc) { /* no arg */
++BSDoptind >= nargcDescription
TRUEnever evaluated
FALSEevaluated 49 times by 4 tests
Evaluated by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
0-49
468 place = EMSG;-
469 if (PRINT_ERROR)
(BSDopterr)Description
TRUEnever evaluated
FALSEnever evaluated
(*options != ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
470 warnx(recargchar, optchar);
never executed: logit(recargchar, optchar);
0
471 optopt = optchar;-
472 return (BADARG);
never executed: return (((*options == ':') ? (int)':' : (int)'?'));
(*options == ':')Description
TRUEnever evaluated
FALSEnever evaluated
0
473 } else-
474 optarg = nargv[optind];
executed 49 times by 4 tests: BSDoptarg = nargv[BSDoptind];
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
49
475 }-
476 place = EMSG;-
477 ++optind;-
478 }
executed 49 times by 4 tests: end of block
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
49
479 /* dump back option letter */-
480 return (optchar);
executed 79 times by 4 tests: return (optchar);
Executed by:
  • ssh-keygen
  • sshd
  • test_hostkeys
  • test_sshkey
79
481}-
482-
483/*-
484 * getopt ---
485 * Parse argc/argv argument vector.-
486 *-
487 * [eventually this will replace the BSD getopt]-
488 */-
489int-
490getopt(int nargc, char * const *nargv, const char *options)-
491{-
492-
493 /*-
494 * We don't pass FLAG_PERMUTE to getopt_internal() since-
495 * the BSD getopt(3) (unlike GNU) has never done this.-
496 *-
497 * Furthermore, since many privileged programs call getopt()-
498 * before dropping privileges it makes sense to keep things-
499 * as simple (and bug-free) as possible.-
500 */-
501 return (getopt_internal(nargc, nargv, options, NULL, NULL, 0));
executed 117 times by 10 tests: return (getopt_internal(nargc, nargv, options, ((void *)0) , ((void *)0) , 0));
Executed by:
  • ssh-keygen
  • sshd
  • test_bitmap
  • test_conversion
  • test_hostkeys
  • test_kex
  • test_match
  • test_sshbuf
  • test_sshkey
  • test_utf8
117
502}-
503-
504#if 0-
505/*-
506 * getopt_long ---
507 * Parse argc/argv argument vector.-
508 */-
509int-
510getopt_long(int nargc, char * const *nargv, const char *options,-
511 const struct option *long_options, int *idx)-
512{-
513-
514 return (getopt_internal(nargc, nargv, options, long_options, idx,-
515 FLAG_PERMUTE));-
516}-
517-
518/*-
519 * getopt_long_only ---
520 * Parse argc/argv argument vector.-
521 */-
522int-
523getopt_long_only(int nargc, char * const *nargv, const char *options,-
524 const struct option *long_options, int *idx)-
525{-
526-
527 return (getopt_internal(nargc, nargv, options, long_options, idx,-
528 FLAG_PERMUTE|FLAG_LONGONLY));-
529}-
530#endif-
531-
532#endif /* !defined(HAVE_GETOPT) || !defined(HAVE_OPTRESET) */-
Source codeSwitch to Preprocessed file

Generated by Squish Coco 4.2.2