gnu_getopt.c 23 KB

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