gnu_getopt.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* $OpenBSD: getopt_long.c,v 1.21 2006/09/22 17:22:05 millert Exp $ */
  2. /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */
  3. /*
  4. * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
  5. * Copyright (c) 2012 William Pitcock
  6. * Copyright (c) 2012 rofl0r
  7. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. * Sponsored in part by the Defense Advanced Research Projects
  21. * Agency (DARPA) and Air Force Research Laboratory, Air Force
  22. * Materiel Command, USAF, under agreement number F39502-99-1-0512.
  23. */
  24. /*-
  25. * Copyright (c) 2000 The NetBSD Foundation, Inc.
  26. * All rights reserved.
  27. *
  28. * This code is derived from software contributed to The NetBSD Foundation
  29. * by Dieter Baron and Thomas Klausner.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions
  33. * are met:
  34. * 1. Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * 2. Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * 3. All advertising materials mentioning features or use of this software
  40. * must display the following acknowledgement:
  41. * This product includes software developed by the NetBSD
  42. * Foundation, Inc. and its contributors.
  43. * 4. Neither the name of The NetBSD Foundation nor the names of its
  44. * contributors may be used to endorse or promote products derived
  45. * from this software without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  48. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  49. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  51. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  52. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  53. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  54. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  55. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  56. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  57. * POSSIBILITY OF SUCH DAMAGE.
  58. */
  59. #include "gnu_getopt.h"
  60. #include <errno.h>
  61. #include <stdio.h>
  62. #include <stdlib.h>
  63. #include <string.h>
  64. //#if HAVE_STRICT_MODE > 0
  65. #if 1
  66. # define GNU_COMPATIBLE /* Be more compatible, configure's use us! */
  67. #endif
  68. #if 0
  69. int gnu_opterr = 1; /* if error message should be printed */
  70. int gnu_optind = 1; /* index into parent argv vector */
  71. int gnu_optopt = '?'; /* character checked for validity */
  72. int gnu_optreset; /* reset getopt */
  73. char *gnu_optarg; /* argument associated with option */
  74. #else
  75. // TODO: check whether it has an effect that musl's optopt is not initialized to '?'
  76. #include <getopt.h>
  77. #define gnu_opterr opterr
  78. #define gnu_optind optind
  79. #define gnu_optopt optopt
  80. #define gnu_optreset optreset
  81. #define gnu_optarg optarg
  82. int optreset;
  83. #endif
  84. char* argv0 = NULL;
  85. #define PRINT_ERROR ((gnu_opterr) && (*options != ':'))
  86. #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
  87. #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
  88. #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
  89. /* return values */
  90. #define BADCH (int)'?'
  91. #define BADARG ((*options == ':') ? (int)':' : (int)'?')
  92. #define INORDER (int)1
  93. #define EMSG ""
  94. #ifdef GNU_COMPATIBLE
  95. #define NO_PREFIX (-1)
  96. #define D_PREFIX 0
  97. #define DD_PREFIX 1
  98. #define W_PREFIX 2
  99. #endif
  100. static int getopt_internal(int, char * const *, const char *,
  101. const struct gnu_option *, int *, int);
  102. static int parse_long_options(char * const *, const char *,
  103. const struct gnu_option *, int *, int, int);
  104. static int gcd(int, int);
  105. static void permute_args(int, int, int, char * const *);
  106. static char *place = EMSG; /* option letter processing */
  107. /* XXX: set gnu_optreset to 1 rather than these two */
  108. static int nonopt_start = -1; /* first non option argument (for permute) */
  109. static int nonopt_end = -1; /* first option after non options (for permute) */
  110. /* Error messages */
  111. static const char recargchar[] = "option requires an argument -- %c";
  112. static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */
  113. #ifdef GNU_COMPATIBLE
  114. static int dash_prefix = NO_PREFIX;
  115. static const char gnuoptchar[] = "invalid option -- %c";
  116. static const char recargstring[] = "option `%s%s' requires an argument";
  117. static const char ambig[] = "option `%s%.*s' is ambiguous";
  118. static const char noarg[] = "option `%s%.*s' doesn't allow an argument";
  119. static const char illoptstring[] = "unrecognized option `%s%s'";
  120. #else
  121. static const char recargstring[] = "option requires an argument -- %s";
  122. static const char ambig[] = "ambiguous option -- %.*s";
  123. static const char noarg[] = "option doesn't take an argument -- %.*s";
  124. static const char illoptstring[] = "unknown option -- %s";
  125. #endif
  126. /*
  127. * Compute the greatest common divisor of a and b.
  128. */
  129. static int
  130. gcd(int a, int b)
  131. {
  132. int c;
  133. c = a % b;
  134. while (c != 0) {
  135. a = b;
  136. b = c;
  137. c = a % b;
  138. }
  139. return (b);
  140. }
  141. /*
  142. * Exchange the block from nonopt_start to nonopt_end with the block
  143. * from nonopt_end to opt_end (keeping the same order of arguments
  144. * in each block).
  145. */
  146. static void
  147. permute_args(int panonopt_start, int panonopt_end, int opt_end,
  148. char * const *nargv)
  149. {
  150. int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos;
  151. char *swap;
  152. /*
  153. * compute lengths of blocks and number and size of cycles
  154. */
  155. nnonopts = panonopt_end - panonopt_start;
  156. nopts = opt_end - panonopt_end;
  157. ncycle = gcd(nnonopts, nopts);
  158. cyclelen = (opt_end - panonopt_start) / ncycle;
  159. for (i = 0; i < ncycle; i++) {
  160. cstart = panonopt_end+i;
  161. pos = cstart;
  162. for (j = 0; j < cyclelen; j++) {
  163. if (pos >= panonopt_end)
  164. pos -= nnonopts;
  165. else
  166. pos += nopts;
  167. swap = nargv[pos];
  168. /* LINTED const cast */
  169. ((char **) nargv)[pos] = nargv[cstart];
  170. /* LINTED const cast */
  171. ((char **)nargv)[cstart] = swap;
  172. }
  173. }
  174. }
  175. /*
  176. * parse_long_options --
  177. * Parse long options in argc/argv argument vector.
  178. * Returns -1 if short_too is set and the option does not match long_options.
  179. */
  180. static int
  181. parse_long_options(char * const *nargv, const char *options,
  182. const struct gnu_option *long_options, int *idx, int short_too, int flags)
  183. {
  184. char *current_argv, *has_equal;
  185. #ifdef GNU_COMPATIBLE
  186. char *current_dash;
  187. #endif
  188. size_t current_argv_len;
  189. int i, match, exact_match, second_partial_match;
  190. current_argv = place;
  191. #ifdef GNU_COMPATIBLE
  192. switch (dash_prefix) {
  193. case D_PREFIX:
  194. current_dash = "-";
  195. break;
  196. case DD_PREFIX:
  197. current_dash = "--";
  198. break;
  199. case W_PREFIX:
  200. current_dash = "-W ";
  201. break;
  202. default:
  203. current_dash = "";
  204. break;
  205. }
  206. #endif
  207. match = -1;
  208. exact_match = 0;
  209. second_partial_match = 0;
  210. gnu_optind++;
  211. if ((has_equal = strchr(current_argv, '=')) != NULL) {
  212. /* argument found (--option=arg) */
  213. current_argv_len = has_equal - current_argv;
  214. has_equal++;
  215. } else
  216. current_argv_len = strlen(current_argv);
  217. for (i = 0; long_options[i].name; i++) {
  218. /* find matching long option */
  219. if (strncmp(current_argv, long_options[i].name,
  220. current_argv_len))
  221. continue;
  222. if (strlen(long_options[i].name) == current_argv_len) {
  223. /* exact match */
  224. match = i;
  225. exact_match = 1;
  226. break;
  227. }
  228. /*
  229. * If this is a known short option, don't allow
  230. * a partial match of a single character.
  231. */
  232. if (short_too && current_argv_len == 1)
  233. continue;
  234. if (match == -1) /* first partial match */
  235. match = i;
  236. else if ((flags & FLAG_LONGONLY) ||
  237. long_options[i].has_arg !=
  238. long_options[match].has_arg ||
  239. long_options[i].flag != long_options[match].flag ||
  240. long_options[i].val != long_options[match].val)
  241. second_partial_match = 1;
  242. }
  243. if (!exact_match && second_partial_match) {
  244. /* ambiguous abbreviation */
  245. if (PRINT_ERROR) {
  246. fprintf(stderr, "pkgconf: ");
  247. fprintf(stderr, ambig,
  248. #ifdef GNU_COMPATIBLE
  249. current_dash,
  250. #endif
  251. (int)current_argv_len,
  252. current_argv);
  253. fprintf(stderr, "\n");
  254. }
  255. gnu_optopt = 0;
  256. return (BADCH);
  257. }
  258. if (match != -1) { /* option found */
  259. if (long_options[match].has_arg == no_argument
  260. && has_equal) {
  261. if (PRINT_ERROR) {
  262. fprintf(stderr, "pkgconf: ");
  263. fprintf(stderr, noarg,
  264. #ifdef GNU_COMPATIBLE
  265. current_dash,
  266. #endif
  267. (int)current_argv_len,
  268. current_argv);
  269. fprintf(stderr, "\n");
  270. }
  271. /*
  272. * XXX: GNU sets gnu_optopt to val regardless of flag
  273. */
  274. if (long_options[match].flag == NULL)
  275. gnu_optopt = long_options[match].val;
  276. else
  277. gnu_optopt = 0;
  278. #ifdef GNU_COMPATIBLE
  279. return (BADCH);
  280. #else
  281. return (BADARG);
  282. #endif
  283. }
  284. if (long_options[match].has_arg == required_argument ||
  285. long_options[match].has_arg == optional_argument) {
  286. if (has_equal)
  287. gnu_optarg = has_equal;
  288. else if (long_options[match].has_arg ==
  289. required_argument) {
  290. /*
  291. * optional argument doesn't use next nargv
  292. */
  293. gnu_optarg = nargv[gnu_optind++];
  294. }
  295. }
  296. if ((long_options[match].has_arg == required_argument)
  297. && (gnu_optarg == NULL)) {
  298. /*
  299. * Missing argument; leading ':' indicates no error
  300. * should be generated.
  301. */
  302. if (PRINT_ERROR) {
  303. fprintf(stderr, "pkgconf: ");
  304. fprintf(stderr, recargstring,
  305. #ifdef GNU_COMPATIBLE
  306. current_dash,
  307. #endif
  308. current_argv);
  309. fprintf(stderr, "\n");
  310. }
  311. /*
  312. * XXX: GNU sets gnu_optopt to val regardless of flag
  313. */
  314. if (long_options[match].flag == NULL)
  315. gnu_optopt = long_options[match].val;
  316. else
  317. gnu_optopt = 0;
  318. --gnu_optind;
  319. return (BADARG);
  320. }
  321. } else { /* unknown option */
  322. if (short_too) {
  323. --gnu_optind;
  324. return (-1);
  325. }
  326. if (PRINT_ERROR) {
  327. fprintf(stderr, "pkgconf: ");
  328. fprintf(stderr, illoptstring,
  329. #ifdef GNU_COMPATIBLE
  330. current_dash,
  331. #endif
  332. current_argv);
  333. fprintf(stderr, "\n");
  334. }
  335. gnu_optopt = 0;
  336. return (BADCH);
  337. }
  338. if (idx)
  339. *idx = match;
  340. if (long_options[match].flag) {
  341. *long_options[match].flag |= long_options[match].val;
  342. return (0);
  343. } else
  344. return (long_options[match].val);
  345. }
  346. /*
  347. * getopt_internal --
  348. * Parse argc/argv argument vector. Called by user level routines.
  349. */
  350. static int
  351. getopt_internal(int nargc, char * const *nargv, const char *options,
  352. const struct gnu_option *long_options, int *idx, int flags)
  353. {
  354. char *oli; /* option letter list index */
  355. int optchar, short_too;
  356. int posixly_correct; /* no static, can be changed on the fly */
  357. if(!argv0) argv0 = nargv[0];
  358. if (options == NULL)
  359. return (-1);
  360. /*
  361. * Disable GNU extensions if POSIXLY_CORRECT is set or options
  362. * string begins with a '+'.
  363. */
  364. posixly_correct = (getenv("POSIXLY_CORRECT") != NULL);
  365. #ifdef GNU_COMPATIBLE
  366. if (*options == '-')
  367. flags |= FLAG_ALLARGS;
  368. else if (posixly_correct || *options == '+')
  369. flags &= ~FLAG_PERMUTE;
  370. #else
  371. if (posixly_correct || *options == '+')
  372. flags &= ~FLAG_PERMUTE;
  373. else if (*options == '-')
  374. flags |= FLAG_ALLARGS;
  375. #endif
  376. #if HAVE_STRICT_MODE >= 1
  377. flags &= ~FLAG_PERMUTE;
  378. #endif
  379. if (*options == '+' || *options == '-')
  380. options++;
  381. /*
  382. * XXX Some GNU programs (like cvs) set gnu_optind to 0 instead of
  383. * XXX using gnu_optreset. Work around this braindamage.
  384. */
  385. if (gnu_optind == 0)
  386. gnu_optind = gnu_optreset = 1;
  387. gnu_optarg = NULL;
  388. if (gnu_optreset)
  389. nonopt_start = nonopt_end = -1;
  390. start:
  391. if (gnu_optreset || !*place) { /* update scanning pointer */
  392. gnu_optreset = 0;
  393. if (gnu_optind >= nargc) { /* end of argument vector */
  394. place = EMSG;
  395. if (nonopt_end != -1) {
  396. /* do permutation, if we have to */
  397. permute_args(nonopt_start, nonopt_end,
  398. gnu_optind, nargv);
  399. gnu_optind -= nonopt_end - nonopt_start;
  400. }
  401. else if (nonopt_start != -1) {
  402. /*
  403. * If we skipped non-options, set gnu_optind
  404. * to the first of them.
  405. */
  406. gnu_optind = nonopt_start;
  407. }
  408. nonopt_start = nonopt_end = -1;
  409. return (-1);
  410. }
  411. if (*(place = nargv[gnu_optind]) != '-' ||
  412. #ifdef GNU_COMPATIBLE
  413. place[1] == '\0') {
  414. #else
  415. (place[1] == '\0' && strchr(options, '-') == NULL)) {
  416. #endif
  417. place = EMSG; /* found non-option */
  418. if (flags & FLAG_ALLARGS) {
  419. /*
  420. * GNU extension:
  421. * return non-option as argument to option 1
  422. */
  423. gnu_optarg = nargv[gnu_optind++];
  424. return (INORDER);
  425. }
  426. if (!(flags & FLAG_PERMUTE)) {
  427. /*
  428. * If no permutation wanted, stop parsing
  429. * at first non-option.
  430. */
  431. return (-1);
  432. }
  433. /* do permutation */
  434. if (nonopt_start == -1)
  435. nonopt_start = gnu_optind;
  436. else if (nonopt_end != -1) {
  437. permute_args(nonopt_start, nonopt_end,
  438. gnu_optind, nargv);
  439. nonopt_start = gnu_optind -
  440. (nonopt_end - nonopt_start);
  441. nonopt_end = -1;
  442. }
  443. gnu_optind++;
  444. /* process next argument */
  445. goto start;
  446. }
  447. if (nonopt_start != -1 && nonopt_end == -1)
  448. nonopt_end = gnu_optind;
  449. /*
  450. * If we have "-" do nothing, if "--" we are done.
  451. */
  452. if (place[1] != '\0' && *++place == '-' && place[1] == '\0') {
  453. gnu_optind++;
  454. place = EMSG;
  455. /*
  456. * We found an option (--), so if we skipped
  457. * non-options, we have to permute.
  458. */
  459. if (nonopt_end != -1) {
  460. permute_args(nonopt_start, nonopt_end,
  461. gnu_optind, nargv);
  462. gnu_optind -= nonopt_end - nonopt_start;
  463. }
  464. nonopt_start = nonopt_end = -1;
  465. return (-1);
  466. }
  467. }
  468. /*
  469. * Check long options if:
  470. * 1) we were passed some
  471. * 2) the arg is not just "-"
  472. * 3) either the arg starts with -- we are getopt_long_only()
  473. */
  474. if (long_options != NULL && place != nargv[gnu_optind] &&
  475. (*place == '-' || (flags & FLAG_LONGONLY))) {
  476. short_too = 0;
  477. #ifdef GNU_COMPATIBLE
  478. dash_prefix = D_PREFIX;
  479. #endif
  480. if (*place == '-') {
  481. place++; /* --foo long option */
  482. #ifdef GNU_COMPATIBLE
  483. dash_prefix = DD_PREFIX;
  484. #endif
  485. } else if (*place != ':' && strchr(options, *place) != NULL)
  486. short_too = 1; /* could be short option too */
  487. optchar = parse_long_options(nargv, options, long_options,
  488. idx, short_too, flags);
  489. if (optchar != -1) {
  490. place = EMSG;
  491. return (optchar);
  492. }
  493. }
  494. if ((optchar = (int)*place++) == (int)':' ||
  495. (optchar == (int)'-' && *place != '\0') ||
  496. (oli = strchr(options, optchar)) == NULL) {
  497. /*
  498. * If the user specified "-" and '-' isn't listed in
  499. * options, return -1 (non-option) as per POSIX.
  500. * Otherwise, it is an unknown option character (or ':').
  501. */
  502. if (optchar == (int)'-' && *place == '\0')
  503. return (-1);
  504. if (!*place)
  505. ++gnu_optind;
  506. #ifdef GNU_COMPATIBLE
  507. if (PRINT_ERROR) {
  508. fprintf(stderr, "%s: ", argv0);
  509. fprintf(stderr, posixly_correct ? illoptchar : gnuoptchar,
  510. optchar);
  511. fprintf(stderr, "\n");
  512. }
  513. #else
  514. if (PRINT_ERROR) {
  515. fprintf(stderr, "%s: ", argv0);
  516. fprintf(stderr, illoptchar, optchar);
  517. fprintf(stderr, "\n");
  518. }
  519. #endif
  520. gnu_optopt = optchar;
  521. return (BADCH);
  522. }
  523. if (long_options != NULL && optchar == 'W' && oli[1] == ';') {
  524. /* -W long-option */
  525. if (*place) /* no space */
  526. /* NOTHING */;
  527. else if (++gnu_optind >= nargc) { /* no arg */
  528. place = EMSG;
  529. if (PRINT_ERROR) {
  530. fprintf(stderr, "%s: ", argv0);
  531. fprintf(stderr, recargchar, optchar);
  532. fprintf(stderr, "\n");
  533. }
  534. gnu_optopt = optchar;
  535. return (BADARG);
  536. } else /* white space */
  537. place = nargv[gnu_optind];
  538. #ifdef GNU_COMPATIBLE
  539. dash_prefix = W_PREFIX;
  540. #endif
  541. optchar = parse_long_options(nargv, options, long_options,
  542. idx, 0, flags);
  543. place = EMSG;
  544. return (optchar);
  545. }
  546. if (*++oli != ':') { /* doesn't take argument */
  547. if (!*place)
  548. ++gnu_optind;
  549. } else { /* takes (optional) argument */
  550. gnu_optarg = NULL;
  551. if (*place) /* no white space */
  552. gnu_optarg = place;
  553. else if (oli[1] != ':') { /* arg not optional */
  554. if (++gnu_optind >= nargc) { /* no arg */
  555. place = EMSG;
  556. if (PRINT_ERROR) {
  557. fprintf(stderr, "%s: ", argv0);
  558. fprintf(stderr, recargchar, optchar);
  559. fprintf(stderr, "\n");
  560. }
  561. gnu_optopt = optchar;
  562. return (BADARG);
  563. } else
  564. gnu_optarg = nargv[gnu_optind];
  565. }
  566. place = EMSG;
  567. ++gnu_optind;
  568. }
  569. /* dump back option letter */
  570. return (optchar);
  571. }
  572. /*
  573. * getopt --
  574. * Parse argc/argv argument vector.
  575. *
  576. * [eventually this will replace the BSD getopt]
  577. */
  578. int
  579. gnu_getopt(int nargc, char * const *nargv, const char *options)
  580. {
  581. /*
  582. * We don't pass FLAG_PERMUTE to getopt_internal() since
  583. * the BSD getopt(3) (unlike GNU) has never done this.
  584. *
  585. * Furthermore, since many privileged programs call getopt()
  586. * before dropping privileges it makes sense to keep things
  587. * as simple (and bug-free) as possible.
  588. */
  589. return (getopt_internal(nargc, nargv, options, NULL, NULL, FLAG_PERMUTE));
  590. }
  591. /*
  592. * getopt_long --
  593. * Parse argc/argv argument vector.
  594. */
  595. int
  596. gnu_getopt_long(int nargc, char * const *nargv, const char *options,
  597. const struct gnu_option *long_options, int *idx)
  598. {
  599. return (getopt_internal(nargc, nargv, options, long_options, idx,
  600. FLAG_PERMUTE));
  601. }
  602. /*
  603. * getopt_long_only --
  604. * Parse argc/argv argument vector.
  605. */
  606. int
  607. gnu_getopt_long_only(int nargc, char * const *nargv, const char *options,
  608. const struct gnu_option *long_options, int *idx)
  609. {
  610. return (getopt_internal(nargc, nargv, options, long_options, idx,
  611. FLAG_PERMUTE|FLAG_LONGONLY));
  612. }