getopt.c 22 KB

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