gnu_getopt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /* Getopt for GNU.
  2. NOTE: getopt is now part of the C library, so if you don't know what
  3. "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
  4. before changing it!
  5. Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94
  6. Free Software Foundation, Inc.
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License
  9. as published by the Free Software Foundation; either version 2, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. /*
  19. * Modified for uClibc by Manuel Novoa III on 1/5/01.
  20. */
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  25. but it behaves differently for the user, since it allows the user
  26. to intersperse the options with the other arguments.
  27. As `getopt' works, it permutes the elements of ARGV so that,
  28. when it is done, all the options precede everything else. Thus
  29. all application programs are extended to handle flexible argument order.
  30. Setting the environment variable POSIXLY_CORRECT disables permutation.
  31. Then the behavior is completely standard.
  32. GNU application programs can use a third alternative mode in which
  33. they can distinguish the relative order of options and other arguments. */
  34. #include <getopt.h>
  35. extern int _getopt_internal (int argc, char *const *argv,
  36. const char *optstring,
  37. const struct option *longopts,
  38. int *longind, int long_only);
  39. #ifdef L__gnu_getopt_internal
  40. /* external getopt vars */
  41. extern int optind;
  42. extern int opterr;
  43. extern int optopt;
  44. extern char *optarg;
  45. /* The next char to be scanned in the option-element
  46. in which the last option character we returned was found.
  47. This allows us to pick up the scan where we left off.
  48. If this is zero, or a null string, it means resume the scan
  49. by advancing to the next ARGV-element. */
  50. static char *nextchar;
  51. /* Describe how to deal with options that follow non-option ARGV-elements.
  52. If the caller did not specify anything,
  53. the default is REQUIRE_ORDER if the environment variable
  54. POSIXLY_CORRECT is defined, PERMUTE otherwise.
  55. REQUIRE_ORDER means don't recognize them as options;
  56. stop option processing when the first non-option is seen.
  57. This is what Unix does.
  58. This mode of operation is selected by either setting the environment
  59. variable POSIXLY_CORRECT, or using `+' as the first character
  60. of the list of option characters.
  61. PERMUTE is the default. We permute the contents of ARGV as we scan,
  62. so that eventually all the non-options are at the end. This allows options
  63. to be given in any order, even with programs that were not written to
  64. expect this.
  65. RETURN_IN_ORDER is an option available to programs that were written
  66. to expect options and other ARGV-elements in any order and that care about
  67. the ordering of the two. We describe each non-option ARGV-element
  68. as if it were the argument of an option with character code 1.
  69. Using `-' as the first character of the list of option characters
  70. selects this mode of operation.
  71. The special argument `--' forces an end of option-scanning regardless
  72. of the value of `ordering'. In the case of RETURN_IN_ORDER, only
  73. `--' can cause `getopt' to return EOF with `optind' != ARGC. */
  74. static enum
  75. {
  76. REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  77. } ordering;
  78. #include <string.h>
  79. #define my_index strchr
  80. /* Handle permutation of arguments. */
  81. /* Describe the part of ARGV that contains non-options that have
  82. been skipped. `first_nonopt' is the index in ARGV of the first of them;
  83. `last_nonopt' is the index after the last of them. */
  84. static int first_nonopt;
  85. static int last_nonopt;
  86. /* Exchange two adjacent subsequences of ARGV.
  87. One subsequence is elements [first_nonopt,last_nonopt)
  88. which contains all the non-options that have been skipped so far.
  89. The other is elements [last_nonopt,optind), which contains all
  90. the options processed since those non-options were skipped.
  91. `first_nonopt' and `last_nonopt' are relocated so that they describe
  92. the new indices of the non-options in ARGV after they are moved. */
  93. static void
  94. exchange (argv)
  95. char **argv;
  96. {
  97. int bottom = first_nonopt;
  98. int middle = last_nonopt;
  99. int top = optind;
  100. char *tem;
  101. /* Exchange the shorter segment with the far end of the longer segment.
  102. That puts the shorter segment into the right place.
  103. It leaves the longer segment in the right place overall,
  104. but it consists of two parts that need to be swapped next. */
  105. while (top > middle && middle > bottom)
  106. {
  107. if (top - middle > middle - bottom)
  108. {
  109. /* Bottom segment is the short one. */
  110. int len = middle - bottom;
  111. register int i;
  112. /* Swap it with the top part of the top segment. */
  113. for (i = 0; i < len; i++)
  114. {
  115. tem = argv[bottom + i];
  116. argv[bottom + i] = argv[top - (middle - bottom) + i];
  117. argv[top - (middle - bottom) + i] = tem;
  118. }
  119. /* Exclude the moved bottom segment from further swapping. */
  120. top -= len;
  121. }
  122. else
  123. {
  124. /* Top segment is the short one. */
  125. int len = top - middle;
  126. register int i;
  127. /* Swap it with the bottom part of the bottom segment. */
  128. for (i = 0; i < len; i++)
  129. {
  130. tem = argv[bottom + i];
  131. argv[bottom + i] = argv[middle + i];
  132. argv[middle + i] = tem;
  133. }
  134. /* Exclude the moved top segment from further swapping. */
  135. bottom += len;
  136. }
  137. }
  138. /* Update records for the slots the non-options now occupy. */
  139. first_nonopt += (optind - last_nonopt);
  140. last_nonopt = optind;
  141. }
  142. /* Initialize the internal data when the first call is made. */
  143. static const char *
  144. _getopt_initialize (optstring)
  145. const char *optstring;
  146. {
  147. /* Start processing options with ARGV-element 1 (since ARGV-element 0
  148. is the program name); the sequence of previously skipped
  149. non-option ARGV-elements is empty. */
  150. first_nonopt = last_nonopt = optind = 1;
  151. nextchar = NULL;
  152. /* Determine how to handle the ordering of options and nonoptions. */
  153. if (optstring[0] == '-')
  154. {
  155. ordering = RETURN_IN_ORDER;
  156. ++optstring;
  157. }
  158. else if (optstring[0] == '+')
  159. {
  160. ordering = REQUIRE_ORDER;
  161. ++optstring;
  162. }
  163. else if (getenv ("POSIXLY_CORRECT") != NULL)
  164. ordering = REQUIRE_ORDER;
  165. else
  166. ordering = PERMUTE;
  167. return optstring;
  168. }
  169. /* Scan elements of ARGV (whose length is ARGC) for option characters
  170. given in OPTSTRING.
  171. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  172. then it is an option element. The characters of this element
  173. (aside from the initial '-') are option characters. If `getopt'
  174. is called repeatedly, it returns successively each of the option characters
  175. from each of the option elements.
  176. If `getopt' finds another option character, it returns that character,
  177. updating `optind' and `nextchar' so that the next call to `getopt' can
  178. resume the scan with the following option character or ARGV-element.
  179. If there are no more option characters, `getopt' returns `EOF'.
  180. Then `optind' is the index in ARGV of the first ARGV-element
  181. that is not an option. (The ARGV-elements have been permuted
  182. so that those that are not options now come last.)
  183. OPTSTRING is a string containing the legitimate option characters.
  184. If an option character is seen that is not listed in OPTSTRING,
  185. return '?' after printing an error message. If you set `opterr' to
  186. zero, the error message is suppressed but we still return '?'.
  187. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  188. so the following text in the same ARGV-element, or the text of the following
  189. ARGV-element, is returned in `optarg'. Two colons mean an option that
  190. wants an optional arg; if there is text in the current ARGV-element,
  191. it is returned in `optarg', otherwise `optarg' is set to zero.
  192. If OPTSTRING starts with `-' or `+', it requests different methods of
  193. handling the non-option ARGV-elements.
  194. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  195. Long-named options begin with `--' instead of `-'.
  196. Their names may be abbreviated as long as the abbreviation is unique
  197. or is an exact match for some defined option. If they have an
  198. argument, it follows the option name in the same ARGV-element, separated
  199. from the option name by a `=', or else the in next ARGV-element.
  200. When `getopt' finds a long-named option, it returns 0 if that option's
  201. `flag' field is nonzero, the value of the option's `val' field
  202. if the `flag' field is zero.
  203. The elements of ARGV aren't really const, because we permute them.
  204. But we pretend they're const in the prototype to be compatible
  205. with other systems.
  206. LONGOPTS is a vector of `struct option' terminated by an
  207. element containing a name which is zero.
  208. LONGIND returns the index in LONGOPT of the long-named option found.
  209. It is only valid when a long-named option has been found by the most
  210. recent call.
  211. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  212. long-named options. */
  213. #if NLS
  214. #include "nl_types.h"
  215. #endif
  216. int
  217. _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
  218. int argc;
  219. char *const *argv;
  220. const char *optstring;
  221. const struct option *longopts;
  222. int *longind;
  223. int long_only;
  224. {
  225. optarg = NULL;
  226. #if NLS
  227. libc_nls_init();
  228. #endif
  229. if (optind == 0)
  230. optstring = _getopt_initialize (optstring);
  231. if (nextchar == NULL || *nextchar == '\0')
  232. {
  233. /* Advance to the next ARGV-element. */
  234. if (ordering == PERMUTE)
  235. {
  236. /* If we have just processed some options following some non-options,
  237. exchange them so that the options come first. */
  238. if (first_nonopt != last_nonopt && last_nonopt != optind)
  239. exchange ((char **) argv);
  240. else if (last_nonopt != optind)
  241. first_nonopt = optind;
  242. /* Skip any additional non-options
  243. and extend the range of non-options previously skipped. */
  244. while (optind < argc
  245. && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
  246. optind++;
  247. last_nonopt = optind;
  248. }
  249. /* The special ARGV-element `--' means premature end of options.
  250. Skip it like a null option,
  251. then exchange with previous non-options as if it were an option,
  252. then skip everything else like a non-option. */
  253. if (optind != argc && !strcmp (argv[optind], "--"))
  254. {
  255. optind++;
  256. if (first_nonopt != last_nonopt && last_nonopt != optind)
  257. exchange ((char **) argv);
  258. else if (first_nonopt == last_nonopt)
  259. first_nonopt = optind;
  260. last_nonopt = argc;
  261. optind = argc;
  262. }
  263. /* If we have done all the ARGV-elements, stop the scan
  264. and back over any non-options that we skipped and permuted. */
  265. if (optind == argc)
  266. {
  267. /* Set the next-arg-index to point at the non-options
  268. that we previously skipped, so the caller will digest them. */
  269. if (first_nonopt != last_nonopt)
  270. optind = first_nonopt;
  271. return EOF;
  272. }
  273. /* If we have come to a non-option and did not permute it,
  274. either stop the scan or describe it to the caller and pass it by. */
  275. if ((argv[optind][0] != '-' || argv[optind][1] == '\0'))
  276. {
  277. if (ordering == REQUIRE_ORDER)
  278. return EOF;
  279. optarg = argv[optind++];
  280. return 1;
  281. }
  282. /* We have found another option-ARGV-element.
  283. Skip the initial punctuation. */
  284. nextchar = (argv[optind] + 1
  285. + (longopts != NULL && argv[optind][1] == '-'));
  286. }
  287. /* Decode the current option-ARGV-element. */
  288. /* Check whether the ARGV-element is a long option.
  289. If long_only and the ARGV-element has the form "-f", where f is
  290. a valid short option, don't consider it an abbreviated form of
  291. a long option that starts with f. Otherwise there would be no
  292. way to give the -f short option.
  293. On the other hand, if there's a long option "fubar" and
  294. the ARGV-element is "-fu", do consider that an abbreviation of
  295. the long option, just like "--fu", and not "-f" with arg "u".
  296. This distinction seems to be the most useful approach. */
  297. if (longopts != NULL
  298. && (argv[optind][1] == '-'
  299. || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
  300. {
  301. char *nameend;
  302. const struct option *p;
  303. const struct option *pfound = NULL;
  304. int exact = 0;
  305. int ambig = 0;
  306. int indfound = 0;
  307. int option_index;
  308. for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
  309. /* Do nothing. */ ;
  310. /* Test all long options for either exact match
  311. or abbreviated matches. */
  312. for (p = longopts, option_index = 0; p->name; p++, option_index++)
  313. if (!strncmp (p->name, nextchar, nameend - nextchar))
  314. {
  315. if (nameend - nextchar == strlen (p->name))
  316. {
  317. /* Exact match found. */
  318. pfound = p;
  319. indfound = option_index;
  320. exact = 1;
  321. break;
  322. }
  323. else if (pfound == NULL)
  324. {
  325. /* First nonexact match found. */
  326. pfound = p;
  327. indfound = option_index;
  328. }
  329. else
  330. /* Second or later nonexact match found. */
  331. ambig = 1;
  332. }
  333. if (ambig && !exact)
  334. {
  335. if (opterr)
  336. #if NLS
  337. fprintf (stderr,
  338. catgets(_libc_cat, GetoptSet, GetoptAmbiguous,
  339. "%s: option `%s' is ambiguous\n"),
  340. argv[0], argv[optind]);
  341. #else
  342. fprintf (stderr, "%s: option `%s' is ambiguous\n",
  343. argv[0], argv[optind]);
  344. #endif
  345. nextchar += strlen (nextchar);
  346. optind++;
  347. return '?';
  348. }
  349. if (pfound != NULL)
  350. {
  351. option_index = indfound;
  352. optind++;
  353. if (*nameend)
  354. {
  355. /* Don't test has_arg with >, because some C compilers don't
  356. allow it to be used on enums. */
  357. if (pfound->has_arg)
  358. optarg = nameend + 1;
  359. else
  360. {
  361. if (opterr)
  362. {
  363. if (argv[optind - 1][1] == '-')
  364. /* --option */
  365. #if NLS
  366. fprintf (stderr,
  367. catgets(_libc_cat, GetoptSet, GetoptNoArgumentsAllowed1,
  368. "%s: option `--%s' doesn't allow an argument\n"),
  369. argv[0], pfound->name);
  370. #else
  371. fprintf (stderr,
  372. "%s: option `--%s' doesn't allow an argument\n",
  373. argv[0], pfound->name);
  374. #endif
  375. else
  376. /* +option or -option */
  377. #if NLS
  378. fprintf (stderr,
  379. catgets(_libc_cat, GetoptSet, GetoptNoArgumentsAllowed2,
  380. "%s: option `%c%s' doesn't allow an argument\n"),
  381. argv[0], argv[optind - 1][0], pfound->name);
  382. #else
  383. fprintf (stderr,
  384. "%s: option `%c%s' doesn't allow an argument\n",
  385. argv[0], argv[optind - 1][0], pfound->name);
  386. #endif
  387. }
  388. nextchar += strlen (nextchar);
  389. return '?';
  390. }
  391. }
  392. else if (pfound->has_arg == 1)
  393. {
  394. if (optind < argc)
  395. optarg = argv[optind++];
  396. else
  397. {
  398. if (opterr)
  399. #if NLS
  400. fprintf (stderr,
  401. catgets(_libc_cat, GetoptSet, GetoptRequiresArgument1,
  402. "%s: option `%s' requires an argument\n"),
  403. argv[0], argv[optind - 1]);
  404. #else
  405. fprintf (stderr, "%s: option `%s' requires an argument\n",
  406. argv[0], argv[optind - 1]);
  407. #endif
  408. nextchar += strlen (nextchar);
  409. return optstring[0] == ':' ? ':' : '?';
  410. }
  411. }
  412. nextchar += strlen (nextchar);
  413. if (longind != NULL)
  414. *longind = option_index;
  415. if (pfound->flag)
  416. {
  417. *(pfound->flag) = pfound->val;
  418. return 0;
  419. }
  420. return pfound->val;
  421. }
  422. /* Can't find it as a long option. If this is not getopt_long_only,
  423. or the option starts with '--' or is not a valid short
  424. option, then it's an error.
  425. Otherwise interpret it as a short option. */
  426. if (!long_only || argv[optind][1] == '-'
  427. || my_index (optstring, *nextchar) == NULL)
  428. {
  429. if (opterr)
  430. {
  431. if (argv[optind][1] == '-')
  432. /* --option */
  433. #if NLS
  434. fprintf (stderr,
  435. catgets(_libc_cat, GetoptSet, GetoptUnrecognized1,
  436. "%s: unrecognized option `--%s'\n"),
  437. argv[0], nextchar);
  438. #else
  439. fprintf (stderr, "%s: unrecognized option `--%s'\n",
  440. argv[0], nextchar);
  441. #endif
  442. else
  443. /* +option or -option */
  444. #if NLS
  445. fprintf (stderr,
  446. catgets(_libc_cat, GetoptSet, GetoptUnrecognized2,
  447. "%s: unrecognized option `%c%s'\n"),
  448. argv[0], argv[optind][0], nextchar);
  449. #else
  450. fprintf (stderr, "%s: unrecognized option `%c%s'\n",
  451. argv[0], argv[optind][0], nextchar);
  452. #endif
  453. }
  454. nextchar = (char *) "";
  455. optind++;
  456. return '?';
  457. }
  458. }
  459. /* Look at and handle the next short option-character. */
  460. {
  461. char c = *nextchar++;
  462. char *temp = my_index (optstring, c);
  463. /* Increment `optind' when we start to process its last character. */
  464. if (*nextchar == '\0')
  465. ++optind;
  466. if (temp == NULL || c == ':')
  467. {
  468. if (opterr)
  469. {
  470. /* 1003.2 specifies the format of this message. */
  471. #if NLS
  472. fprintf (stderr,
  473. catgets(_libc_cat, GetoptSet, GetoptIllegal,
  474. "%s: illegal option -- %c\n"),
  475. argv[0], c);
  476. #else
  477. fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
  478. #endif
  479. }
  480. optopt = c;
  481. return '?';
  482. }
  483. if (temp[1] == ':')
  484. {
  485. if (temp[2] == ':')
  486. {
  487. /* This is an option that accepts an argument optionally. */
  488. if (*nextchar != '\0')
  489. {
  490. optarg = nextchar;
  491. optind++;
  492. }
  493. else
  494. optarg = NULL;
  495. nextchar = NULL;
  496. }
  497. else
  498. {
  499. /* This is an option that requires an argument. */
  500. if (*nextchar != '\0')
  501. {
  502. optarg = nextchar;
  503. /* If we end this ARGV-element by taking the rest as an arg,
  504. we must advance to the next element now. */
  505. optind++;
  506. }
  507. else if (optind == argc)
  508. {
  509. if (opterr)
  510. {
  511. /* 1003.2 specifies the format of this message. */
  512. #if NLS
  513. fprintf (stderr,
  514. catgets(_libc_cat, GetoptSet,
  515. GetoptRequiresArgument2,
  516. "%s: option requires an argument -- %c\n"),
  517. argv[0], c);
  518. #else
  519. fprintf (stderr, "%s: option requires an argument -- %c\n",
  520. argv[0], c);
  521. #endif
  522. }
  523. optopt = c;
  524. if (optstring[0] == ':')
  525. c = ':';
  526. else
  527. c = '?';
  528. }
  529. else
  530. /* We already incremented `optind' once;
  531. increment it again when taking next ARGV-elt as argument. */
  532. optarg = argv[optind++];
  533. nextchar = NULL;
  534. }
  535. }
  536. return c;
  537. }
  538. }
  539. int
  540. getopt (argc, argv, optstring)
  541. int argc;
  542. char *const *argv;
  543. const char *optstring;
  544. {
  545. return _getopt_internal (argc, argv, optstring,
  546. (const struct option *) 0,
  547. (int *) 0,
  548. 0);
  549. }
  550. #endif /* L__gnu_getopt_internal */
  551. #ifdef L_gnu_getopt_long
  552. int
  553. getopt_long (argc, argv, options, long_options, opt_index)
  554. int argc;
  555. char *const *argv;
  556. const char *options;
  557. const struct option *long_options;
  558. int *opt_index;
  559. {
  560. return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  561. }
  562. #endif /* L_gnu_getopt_long */
  563. #ifdef L_gnu_getopt_long_only
  564. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  565. If an option that starts with '-' (not '--') doesn't match a long option,
  566. but does match a short option, it is parsed as a short option
  567. instead. */
  568. int
  569. getopt_long_only (argc, argv, options, long_options, opt_index)
  570. int argc;
  571. char *const *argv;
  572. const char *options;
  573. const struct option *long_options;
  574. int *opt_index;
  575. {
  576. return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  577. }
  578. #endif /* L_gnu_getopt_long_only */
  579. #ifdef TEST
  580. /* Compile with -DTEST to make an executable for use in testing
  581. the above definition of `getopt'. */
  582. int
  583. main (argc, argv)
  584. int argc;
  585. char **argv;
  586. {
  587. int c;
  588. int digit_optind = 0;
  589. while (1)
  590. {
  591. int this_option_optind = optind ? optind : 1;
  592. c = getopt (argc, argv, "abc:d:0123456789");
  593. if (c == EOF)
  594. break;
  595. switch (c)
  596. {
  597. case '0':
  598. case '1':
  599. case '2':
  600. case '3':
  601. case '4':
  602. case '5':
  603. case '6':
  604. case '7':
  605. case '8':
  606. case '9':
  607. if (digit_optind != 0 && digit_optind != this_option_optind)
  608. printf ("digits occur in two different argv-elements.\n");
  609. digit_optind = this_option_optind;
  610. printf ("option %c\n", c);
  611. break;
  612. case 'a':
  613. printf ("option a\n");
  614. break;
  615. case 'b':
  616. printf ("option b\n");
  617. break;
  618. case 'c':
  619. printf ("option c with value `%s'\n", optarg);
  620. break;
  621. case '?':
  622. break;
  623. default:
  624. printf ("?? getopt returned character code 0%o ??\n", c);
  625. }
  626. }
  627. if (optind < argc)
  628. {
  629. printf ("non-option ARGV-elements: ");
  630. while (optind < argc)
  631. printf ("%s ", argv[optind++]);
  632. printf ("\n");
  633. }
  634. exit (0);
  635. }
  636. #endif /* TEST */
  637. /* getopt_long testing */
  638. #ifdef TEST_LONG
  639. /* Compile with -DTEST_LONG to make an executable for use in testing
  640. the above definition of `getopt'. */
  641. int
  642. main (argc, argv)
  643. int argc;
  644. char **argv;
  645. {
  646. int c;
  647. int digit_optind = 0;
  648. while (1)
  649. {
  650. int this_option_optind = optind ? optind : 1;
  651. int option_index = 0;
  652. static struct option long_options[] =
  653. {
  654. {"add", 1, 0, 0},
  655. {"append", 0, 0, 0},
  656. {"delete", 1, 0, 0},
  657. {"verbose", 0, 0, 0},
  658. {"create", 0, 0, 0},
  659. {"file", 1, 0, 0},
  660. {0, 0, 0, 0}
  661. };
  662. c = getopt_long (argc, argv, "abc:d:0123456789",
  663. long_options, &option_index);
  664. if (c == EOF)
  665. break;
  666. switch (c)
  667. {
  668. case 0:
  669. printf ("option %s", long_options[option_index].name);
  670. if (optarg)
  671. printf (" with arg %s", optarg);
  672. printf ("\n");
  673. break;
  674. case '0':
  675. case '1':
  676. case '2':
  677. case '3':
  678. case '4':
  679. case '5':
  680. case '6':
  681. case '7':
  682. case '8':
  683. case '9':
  684. if (digit_optind != 0 && digit_optind != this_option_optind)
  685. printf ("digits occur in two different argv-elements.\n");
  686. digit_optind = this_option_optind;
  687. printf ("option %c\n", c);
  688. break;
  689. case 'a':
  690. printf ("option a\n");
  691. break;
  692. case 'b':
  693. printf ("option b\n");
  694. break;
  695. case 'c':
  696. printf ("option c with value `%s'\n", optarg);
  697. break;
  698. case 'd':
  699. printf ("option d with value `%s'\n", optarg);
  700. break;
  701. case '?':
  702. break;
  703. default:
  704. printf ("?? getopt returned character code 0%o ??\n", c);
  705. }
  706. }
  707. if (optind < argc)
  708. {
  709. printf ("non-option ARGV-elements: ");
  710. while (optind < argc)
  711. printf ("%s ", argv[optind++]);
  712. printf ("\n");
  713. }
  714. exit (0);
  715. }
  716. #endif /* TEST_LONG */