getopt.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /**********************************************************************
  2. Each of the companies; Lucent, Motorola, Nokia, and Qualcomm (hereinafter
  3. referred to individually as "Source" or collectively as "Sources") do
  4. hereby state:
  5. To the extent to which the Source(s) may legally and freely do so, the
  6. Source(s), upon submission of a Contribution, grant(s) a free,
  7. irrevocable, non-exclusive, license to the Third Generation Partnership
  8. Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA, TTA,
  9. and TTC, under the Source's copyright or copyright license rights in the
  10. Contribution, to, in whole or in part, copy, make derivative works,
  11. perform, display and distribute the Contribution and derivative works
  12. thereof consistent with 3GPP2's and each Organizational Partner's
  13. policies and procedures, with the right to (i) sublicense the foregoing
  14. rights consistent with 3GPP2's and each Organizational Partner's policies
  15. and procedures and (ii) copyright and sell, if applicable) in 3GPP2's name
  16. or each Organizational Partner's name any 3GPP2 or transposed Publication
  17. even though this Publication may contain the Contribution or a derivative
  18. work thereof. The Contribution shall disclose any known limitations on
  19. the Source's rights to license as herein provided.
  20. When a Contribution is submitted by the Source(s) to assist the
  21. formulating groups of 3GPP2 or any of its Organizational Partners, it
  22. is proposed to the Committee as a basis for discussion and is not to
  23. be construed as a binding proposal on the Source(s). The Source(s)
  24. specifically reserve(s) the right to amend or modify the material
  25. contained in the Contribution. Nothing contained in the Contribution
  26. shall, except as herein expressly provided, be construed as conferring
  27. by implication, estoppel or otherwise, any license or right under (i)
  28. any existing or later issuing patent, whether or not the use of
  29. information in the document necessarily employs an invention of any
  30. existing or later issued patent, (ii) any copyright, (iii) any
  31. trademark, or (iv) any other intellectual property right.
  32. With respect to the Software necessary for the practice of any or
  33. all Normative portions of the Enhanced Variable Rate Codec (EVRC) as
  34. it exists on the date of submittal of this form, should the EVRC be
  35. approved as a Specification or Report by 3GPP2, or as a transposed
  36. Standard by any of the 3GPP2's Organizational Partners, the Source(s)
  37. state(s) that a worldwide license to reproduce, use and distribute the
  38. Software, the license rights to which are held by the Source(s), will
  39. be made available to applicants under terms and conditions that are
  40. reasonable and non-discriminatory, which may include monetary compensation,
  41. and only to the extent necessary for the practice of any or all of the
  42. Normative portions of the EVRC or the field of use of practice of the
  43. EVRC Specification, Report, or Standard. The statement contained above
  44. is irrevocable and shall be binding upon the Source(s). In the event
  45. the rights of the Source(s) in and to copyright or copyright license
  46. rights subject to such commitment are assigned or transferred, the
  47. Source(s) shall notify the assignee or transferee of the existence of
  48. such commitments.
  49. *******************************************************************/
  50. /* Getopt for GNU.
  51. NOTE: getopt is now part of the C library, so if you don't know what
  52. "Keep this file name-space clean" means, talk to roland@gnu.ai.mit.edu
  53. before changing it!
  54. Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
  55. Free Software Foundation, Inc.
  56. This program is free software; you can redistribute it and/or modify it
  57. under the terms of the GNU General Public License as published by the
  58. Free Software Foundation; either version 2, or (at your option) any
  59. later version.
  60. This program is distributed in the hope that it will be useful,
  61. but WITHOUT ANY WARRANTY; without even the implied warranty of
  62. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  63. GNU General Public License for more details.
  64. You should have received a copy of the GNU General Public License
  65. along with this program; if not, write to the Free Software
  66. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  67. #ifdef HAVE_CONFIG_H
  68. #if defined (emacs) || defined (CONFIG_BROKETS)
  69. /* We use <config.h> instead of "config.h" so that a compilation
  70. using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  71. (which it would do because it found this file in $srcdir). */
  72. #include <config.h>
  73. #else
  74. #include "config.h"
  75. #endif
  76. #endif
  77. #ifndef __STDC__
  78. /* This is a separate conditional since some stdc systems
  79. reject `defined (const)'. */
  80. #ifndef const
  81. #define const
  82. #endif
  83. #endif
  84. /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>. */
  85. #ifndef _NO_PROTO
  86. #define _NO_PROTO
  87. #endif
  88. #include <stdio.h>
  89. #include <string.h>
  90. /* Comment out all this code if we are using the GNU C Library, and are not
  91. actually compiling the library itself. This code is part of the GNU C
  92. Library, but also included in many other GNU distributions. Compiling
  93. and linking in this code is a waste when using the GNU C library
  94. (especially if it is a shared library). Rather than having every GNU
  95. program understand `configure --with-gnu-libc' and omit the object files,
  96. it is simpler to just do this in the source for each such file. */
  97. #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
  98. /* This needs to come after some library #include
  99. to get __GNU_LIBRARY__ defined. */
  100. #ifdef __GNU_LIBRARY__
  101. /* Don't include stdlib.h for non-GNU C libraries because some of them
  102. contain conflicting prototypes for getopt. */
  103. #include <stdlib.h>
  104. #endif /* GNU C library. */
  105. /* If GETOPT_COMPAT is defined, `+' as well as `--' can introduce a
  106. long-named option. Because this is not POSIX.2 compliant, it is
  107. being phased out. */
  108. /* #define GETOPT_COMPAT */
  109. /* This version of `getopt' appears to the caller like standard Unix `getopt'
  110. but it behaves differently for the user, since it allows the user
  111. to intersperse the options with the other arguments.
  112. As `getopt' works, it permutes the elements of ARGV so that,
  113. when it is done, all the options precede everything else. Thus
  114. all application programs are extended to handle flexible argument order.
  115. Setting the environment variable POSIXLY_CORRECT disables permutation.
  116. Then the behavior is completely standard.
  117. GNU application programs can use a third alternative mode in which
  118. they can distinguish the relative order of options and other arguments. */
  119. #include "getopt.h"
  120. /* For communication from `getopt' to the caller.
  121. When `getopt' finds an option that takes an argument,
  122. the argument value is returned here.
  123. Also, when `ordering' is RETURN_IN_ORDER,
  124. each non-option ARGV-element is returned here. */
  125. char *optarg = 0;
  126. /* Index in ARGV of the next element to be scanned.
  127. This is used for communication to and from the caller
  128. and for communication between successive calls to `getopt'.
  129. On entry to `getopt', zero means this is the first call; initialize.
  130. When `getopt' returns EOF, this is the index of the first of the
  131. non-option elements that the caller should itself scan.
  132. Otherwise, `optind' communicates from one call to the next
  133. how much of ARGV has been scanned so far. */
  134. /* XXX 1003.2 says this must be 1 before any call. */
  135. int optind = 0;
  136. /* The next char to be scanned in the option-element
  137. in which the last option character we returned was found.
  138. This allows us to pick up the scan where we left off.
  139. If this is zero, or a null string, it means resume the scan
  140. by advancing to the next ARGV-element. */
  141. static char *nextchar;
  142. /* Callers store zero here to inhibit the error message
  143. for unrecognized options. */
  144. int opterr = 1;
  145. /* Set to an option character which was unrecognized.
  146. This must be initialized on some systems to avoid linking in the
  147. system's own getopt implementation. */
  148. int optopt = '?';
  149. /* Describe how to deal with options that follow non-option ARGV-elements.
  150. If the caller did not specify anything,
  151. the default is REQUIRE_ORDER if the environment variable
  152. POSIXLY_CORRECT is defined, PERMUTE otherwise.
  153. REQUIRE_ORDER means don't recognize them as options;
  154. stop option processing when the first non-option is seen.
  155. This is what Unix does.
  156. This mode of operation is selected by either setting the environment
  157. variable POSIXLY_CORRECT, or using `+' as the first character
  158. of the list of option characters.
  159. PERMUTE is the default. We permute the contents of ARGV as we scan,
  160. so that eventually all the non-options are at the end. This allows options
  161. to be given in any order, even with programs that were not written to
  162. expect this.
  163. RETURN_IN_ORDER is an option available to programs that were written
  164. to expect options and other ARGV-elements in any order and that care about
  165. the ordering of the two. We describe each non-option ARGV-element
  166. as if it were the argument of an option with character code 1.
  167. Using `-' as the first character of the list of option characters
  168. selects this mode of operation.
  169. The special argument `--' forces an end of option-scanning regardless
  170. of the value of `ordering'. In the case of RETURN_IN_ORDER, only
  171. `--' can cause `getopt' to return EOF with `optind' != ARGC. */
  172. static enum
  173. {
  174. REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
  175. } ordering;
  176. #ifdef __GNU_LIBRARY__
  177. /* We want to avoid inclusion of string.h with non-GNU libraries
  178. because there are many ways it can cause trouble.
  179. On some systems, it contains special magic macros that don't work
  180. in GCC. */
  181. #include <string.h>
  182. #define my_index strchr
  183. #else
  184. /* Avoid depending on library functions or files
  185. whose names are inconsistent. */
  186. char *getenv ();
  187. static char *
  188. my_index (str, chr)
  189. const char *str;
  190. int chr;
  191. {
  192. while (*str)
  193. {
  194. if (*str == chr)
  195. return (char *) str;
  196. str++;
  197. }
  198. return 0;
  199. }
  200. /* If using GCC, we can safely declare strlen this way.
  201. If not using GCC, it is ok not to declare it.
  202. (Supposedly there are some machines where it might get a warning,
  203. but changing this conditional to __STDC__ is too risky.) */
  204. #ifdef __GNUC__
  205. #ifdef IN_GCC
  206. #include "gstddef.h"
  207. #else
  208. #include <stddef.h>
  209. #endif
  210. extern size_t strlen (const char *);
  211. #endif
  212. #endif /* GNU C library. */
  213. /* Handle permutation of arguments. */
  214. /* Describe the part of ARGV that contains non-options that have
  215. been skipped. `first_nonopt' is the index in ARGV of the first of them;
  216. `last_nonopt' is the index after the last of them. */
  217. static int first_nonopt;
  218. static int last_nonopt;
  219. /* Exchange two adjacent subsequences of ARGV.
  220. One subsequence is elements [first_nonopt,last_nonopt)
  221. which contains all the non-options that have been skipped so far.
  222. The other is elements [last_nonopt,optind), which contains all
  223. the options processed since those non-options were skipped.
  224. `first_nonopt' and `last_nonopt' are relocated so that they describe
  225. the new indices of the non-options in ARGV after they are moved. */
  226. static void
  227. exchange (argv)
  228. char **argv;
  229. {
  230. int bottom = first_nonopt;
  231. int middle = last_nonopt;
  232. int top = optind;
  233. char *tem;
  234. /* Exchange the shorter segment with the far end of the longer segment.
  235. That puts the shorter segment into the right place.
  236. It leaves the longer segment in the right place overall,
  237. but it consists of two parts that need to be swapped next. */
  238. while (top > middle && middle > bottom)
  239. {
  240. if (top - middle > middle - bottom)
  241. {
  242. /* Bottom segment is the short one. */
  243. int len = middle - bottom;
  244. register int i;
  245. /* Swap it with the top part of the top segment. */
  246. for (i = 0; i < len; i++)
  247. {
  248. tem = argv[bottom + i];
  249. argv[bottom + i] = argv[top - (middle - bottom) + i];
  250. argv[top - (middle - bottom) + i] = tem;
  251. }
  252. /* Exclude the moved bottom segment from further swapping. */
  253. top -= len;
  254. }
  255. else
  256. {
  257. /* Top segment is the short one. */
  258. int len = top - middle;
  259. register int i;
  260. /* Swap it with the bottom part of the bottom segment. */
  261. for (i = 0; i < len; i++)
  262. {
  263. tem = argv[bottom + i];
  264. argv[bottom + i] = argv[middle + i];
  265. argv[middle + i] = tem;
  266. }
  267. /* Exclude the moved top segment from further swapping. */
  268. bottom += len;
  269. }
  270. }
  271. /* Update records for the slots the non-options now occupy. */
  272. first_nonopt += (optind - last_nonopt);
  273. last_nonopt = optind;
  274. }
  275. /* Scan elements of ARGV (whose length is ARGC) for option characters
  276. given in OPTSTRING.
  277. If an element of ARGV starts with '-', and is not exactly "-" or "--",
  278. then it is an option element. The characters of this element
  279. (aside from the initial '-') are option characters. If `getopt'
  280. is called repeatedly, it returns successively each of the option characters
  281. from each of the option elements.
  282. If `getopt' finds another option character, it returns that character,
  283. updating `optind' and `nextchar' so that the next call to `getopt' can
  284. resume the scan with the following option character or ARGV-element.
  285. If there are no more option characters, `getopt' returns `EOF'.
  286. Then `optind' is the index in ARGV of the first ARGV-element
  287. that is not an option. (The ARGV-elements have been permuted
  288. so that those that are not options now come last.)
  289. OPTSTRING is a string containing the legitimate option characters.
  290. If an option character is seen that is not listed in OPTSTRING,
  291. return '?' after printing an error message. If you set `opterr' to
  292. zero, the error message is suppressed but we still return '?'.
  293. If a char in OPTSTRING is followed by a colon, that means it wants an arg,
  294. so the following text in the same ARGV-element, or the text of the following
  295. ARGV-element, is returned in `optarg'. Two colons mean an option that
  296. wants an optional arg; if there is text in the current ARGV-element,
  297. it is returned in `optarg', otherwise `optarg' is set to zero.
  298. If OPTSTRING starts with `-' or `+', it requests different methods of
  299. handling the non-option ARGV-elements.
  300. See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
  301. Long-named options begin with `--' instead of `-'.
  302. Their names may be abbreviated as long as the abbreviation is unique
  303. or is an exact match for some defined option. If they have an
  304. argument, it follows the option name in the same ARGV-element, separated
  305. from the option name by a `=', or else the in next ARGV-element.
  306. When `getopt' finds a long-named option, it returns 0 if that option's
  307. `flag' field is nonzero, the value of the option's `val' field
  308. if the `flag' field is zero.
  309. The elements of ARGV aren't really const, because we permute them.
  310. But we pretend they're const in the prototype to be compatible
  311. with other systems.
  312. LONGOPTS is a vector of `struct option' terminated by an
  313. element containing a name which is zero.
  314. LONGIND returns the index in LONGOPT of the long-named option found.
  315. It is only valid when a long-named option has been found by the most
  316. recent call.
  317. If LONG_ONLY is nonzero, '-' as well as '--' can introduce
  318. long-named options. */
  319. int
  320. _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
  321. int argc;
  322. char *const *argv;
  323. const char *optstring;
  324. const struct option *longopts;
  325. int *longind;
  326. int long_only;
  327. {
  328. int option_index;
  329. optarg = 0;
  330. /* Initialize the internal data when the first call is made.
  331. Start processing options with ARGV-element 1 (since ARGV-element 0
  332. is the program name); the sequence of previously skipped
  333. non-option ARGV-elements is empty. */
  334. if (optind == 0)
  335. {
  336. first_nonopt = last_nonopt = optind = 1;
  337. nextchar = NULL;
  338. /* Determine how to handle the ordering of options and nonoptions. */
  339. if (optstring[0] == '-')
  340. {
  341. ordering = RETURN_IN_ORDER;
  342. ++optstring;
  343. }
  344. else if (optstring[0] == '+')
  345. {
  346. ordering = REQUIRE_ORDER;
  347. ++optstring;
  348. }
  349. else if (getenv ("POSIXLY_CORRECT") != NULL)
  350. ordering = REQUIRE_ORDER;
  351. else
  352. ordering = PERMUTE;
  353. }
  354. if (nextchar == NULL || *nextchar == '\0')
  355. {
  356. if (ordering == PERMUTE)
  357. {
  358. /* If we have just processed some options following some non-options,
  359. exchange them so that the options come first. */
  360. if (first_nonopt != last_nonopt && last_nonopt != optind)
  361. exchange ((char **) argv);
  362. else if (last_nonopt != optind)
  363. first_nonopt = optind;
  364. /* Now skip any additional non-options
  365. and extend the range of non-options previously skipped. */
  366. while (optind < argc
  367. && (argv[optind][0] != '-' || argv[optind][1] == '\0')
  368. #ifdef GETOPT_COMPAT
  369. && (longopts == NULL
  370. || argv[optind][0] != '+' || argv[optind][1] == '\0')
  371. #endif /* GETOPT_COMPAT */
  372. )
  373. optind++;
  374. last_nonopt = optind;
  375. }
  376. /* Special ARGV-element `--' means premature end of options.
  377. Skip it like a null option,
  378. then exchange with previous non-options as if it were an option,
  379. then skip everything else like a non-option. */
  380. if (optind != argc && !strcmp (argv[optind], "--"))
  381. {
  382. optind++;
  383. if (first_nonopt != last_nonopt && last_nonopt != optind)
  384. exchange ((char **) argv);
  385. else if (first_nonopt == last_nonopt)
  386. first_nonopt = optind;
  387. last_nonopt = argc;
  388. optind = argc;
  389. }
  390. /* If we have done all the ARGV-elements, stop the scan
  391. and back over any non-options that we skipped and permuted. */
  392. if (optind == argc)
  393. {
  394. /* Set the next-arg-index to point at the non-options
  395. that we previously skipped, so the caller will digest them. */
  396. if (first_nonopt != last_nonopt)
  397. optind = first_nonopt;
  398. return EOF;
  399. }
  400. /* If we have come to a non-option and did not permute it,
  401. either stop the scan or describe it to the caller and pass it by. */
  402. if ((argv[optind][0] != '-' || argv[optind][1] == '\0')
  403. #ifdef GETOPT_COMPAT
  404. && (longopts == NULL
  405. || argv[optind][0] != '+' || argv[optind][1] == '\0')
  406. #endif /* GETOPT_COMPAT */
  407. )
  408. {
  409. if (ordering == REQUIRE_ORDER)
  410. return EOF;
  411. optarg = argv[optind++];
  412. return 1;
  413. }
  414. /* We have found another option-ARGV-element.
  415. Start decoding its characters. */
  416. nextchar = (argv[optind] + 1
  417. + (longopts != NULL && argv[optind][1] == '-'));
  418. }
  419. if (longopts != NULL
  420. && ((argv[optind][0] == '-'
  421. && (argv[optind][1] == '-' || long_only))
  422. #ifdef GETOPT_COMPAT
  423. || argv[optind][0] == '+'
  424. #endif /* GETOPT_COMPAT */
  425. ))
  426. {
  427. const struct option *p;
  428. char *s = nextchar;
  429. int exact = 0;
  430. int ambig = 0;
  431. const struct option *pfound = NULL;
  432. int indfound;
  433. while (*s && *s != '=')
  434. s++;
  435. /* Test all options for either exact match or abbreviated matches. */
  436. for (p = longopts, option_index = 0; p->name;
  437. p++, option_index++)
  438. if (!strncmp (p->name, nextchar, s - nextchar))
  439. {
  440. if (s - nextchar == strlen (p->name))
  441. {
  442. /* Exact match found. */
  443. pfound = p;
  444. indfound = option_index;
  445. exact = 1;
  446. break;
  447. }
  448. else if (pfound == NULL)
  449. {
  450. /* First nonexact match found. */
  451. pfound = p;
  452. indfound = option_index;
  453. }
  454. else
  455. /* Second nonexact match found. */
  456. ambig = 1;
  457. }
  458. if (ambig && !exact)
  459. {
  460. if (opterr)
  461. wfprintf (stderr, "%s: option `%s' is ambiguous\n",
  462. argv[0], argv[optind]);
  463. nextchar += strlen (nextchar);
  464. optind++;
  465. return '?';
  466. }
  467. if (pfound != NULL)
  468. {
  469. option_index = indfound;
  470. optind++;
  471. if (*s)
  472. {
  473. /* Don't test has_arg with >, because some C compilers don't
  474. allow it to be used on enums. */
  475. if (pfound->has_arg)
  476. optarg = s + 1;
  477. else
  478. {
  479. if (opterr)
  480. {
  481. if (argv[optind - 1][1] == '-')
  482. /* --option */
  483. wfprintf (stderr,
  484. "%s: option `--%s' doesn't allow an argument\n",
  485. argv[0], pfound->name);
  486. else
  487. /* +option or -option */
  488. wfprintf (stderr,
  489. "%s: option `%c%s' doesn't allow an argument\n",
  490. argv[0], argv[optind - 1][0], pfound->name);
  491. }
  492. nextchar += strlen (nextchar);
  493. return '?';
  494. }
  495. }
  496. else if (pfound->has_arg == 1)
  497. {
  498. if (optind < argc)
  499. optarg = argv[optind++];
  500. else
  501. {
  502. if (opterr)
  503. wfprintf (stderr, "%s: option `%s' requires an argument\n",
  504. argv[0], argv[optind - 1]);
  505. nextchar += strlen (nextchar);
  506. return optstring[0] == ':' ? ':' : '?';
  507. }
  508. }
  509. nextchar += strlen (nextchar);
  510. if (longind != NULL)
  511. *longind = option_index;
  512. if (pfound->flag)
  513. {
  514. *(pfound->flag) = pfound->val;
  515. return 0;
  516. }
  517. return pfound->val;
  518. }
  519. /* Can't find it as a long option. If this is not getopt_long_only,
  520. or the option starts with '--' or is not a valid short
  521. option, then it's an error.
  522. Otherwise interpret it as a short option. */
  523. if (!long_only || argv[optind][1] == '-'
  524. #ifdef GETOPT_COMPAT
  525. || argv[optind][0] == '+'
  526. #endif /* GETOPT_COMPAT */
  527. || my_index (optstring, *nextchar) == NULL)
  528. {
  529. if (opterr)
  530. {
  531. if (argv[optind][1] == '-')
  532. /* --option */
  533. wfprintf (stderr, "%s: unrecognized option `--%s'\n",
  534. argv[0], nextchar);
  535. else
  536. /* +option or -option */
  537. wfprintf (stderr, "%s: unrecognized option `%c%s'\n",
  538. argv[0], argv[optind][0], nextchar);
  539. }
  540. nextchar = (char *) "";
  541. optind++;
  542. return '?';
  543. }
  544. }
  545. /* Look at and handle the next option-character. */
  546. {
  547. char c = *nextchar++;
  548. char *temp = my_index (optstring, c);
  549. /* Increment `optind' when we start to process its last character. */
  550. if (*nextchar == '\0')
  551. ++optind;
  552. if (temp == NULL || c == ':')
  553. {
  554. if (opterr)
  555. {
  556. #if 0
  557. if (c < 040 || c >= 0177)
  558. wfprintf (stderr, "%s: unrecognized option, character code 0%o\n",
  559. argv[0], c);
  560. else
  561. wfprintf (stderr, "%s: unrecognized option `-%c'\n", argv[0], c);
  562. #else
  563. /* 1003.2 specifies the format of this message. */
  564. wfprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);
  565. #endif
  566. }
  567. optopt = c;
  568. return '?';
  569. }
  570. if (temp[1] == ':')
  571. {
  572. if (temp[2] == ':')
  573. {
  574. /* This is an option that accepts an argument optionally. */
  575. if (*nextchar != '\0')
  576. {
  577. optarg = nextchar;
  578. optind++;
  579. }
  580. else
  581. optarg = 0;
  582. nextchar = NULL;
  583. }
  584. else
  585. {
  586. /* This is an option that requires an argument. */
  587. if (*nextchar != '\0')
  588. {
  589. optarg = nextchar;
  590. /* If we end this ARGV-element by taking the rest as an arg,
  591. we must advance to the next element now. */
  592. optind++;
  593. }
  594. else if (optind == argc)
  595. {
  596. if (opterr)
  597. {
  598. #if 0
  599. wfprintf (stderr, "%s: option `-%c' requires an argument\n",
  600. argv[0], c);
  601. #else
  602. /* 1003.2 specifies the format of this message. */
  603. wfprintf (stderr, "%s: option requires an argument -- %c\n",
  604. argv[0], c);
  605. #endif
  606. }
  607. optopt = c;
  608. if (optstring[0] == ':')
  609. c = ':';
  610. else
  611. c = '?';
  612. }
  613. else
  614. /* We already incremented `optind' once;
  615. increment it again when taking next ARGV-elt as argument. */
  616. optarg = argv[optind++];
  617. nextchar = NULL;
  618. }
  619. }
  620. return c;
  621. }
  622. }
  623. int
  624. getopt (argc, argv, optstring)
  625. int argc;
  626. char *const *argv;
  627. const char *optstring;
  628. {
  629. return _getopt_internal (argc, argv, optstring,
  630. (const struct option *) 0,
  631. (int *) 0,
  632. 0);
  633. }
  634. #endif /* _LIBC or not __GNU_LIBRARY__. */
  635. #ifdef TEST
  636. /* Compile with -DTEST to make an executable for use in testing
  637. the above definition of `getopt'. */
  638. int
  639. main (argc, argv)
  640. int argc;
  641. char **argv;
  642. {
  643. int c;
  644. int digit_optind = 0;
  645. while (1)
  646. {
  647. int this_option_optind = optind ? optind : 1;
  648. c = getopt (argc, argv, "abc:d:0123456789");
  649. if (c == EOF)
  650. break;
  651. switch (c)
  652. {
  653. case '0':
  654. case '1':
  655. case '2':
  656. case '3':
  657. case '4':
  658. case '5':
  659. case '6':
  660. case '7':
  661. case '8':
  662. case '9':
  663. if (digit_optind != 0 && digit_optind != this_option_optind)
  664. wprintf ("digits occur in two different argv-elements.\n");
  665. digit_optind = this_option_optind;
  666. wprintf ("option %c\n", c);
  667. break;
  668. case 'a':
  669. wprintf ("option a\n");
  670. break;
  671. case 'b':
  672. wprintf ("option b\n");
  673. break;
  674. case 'c':
  675. wprintf ("option c with value `%s'\n", optarg);
  676. break;
  677. case '?':
  678. break;
  679. default:
  680. wprintf ("?? getopt returned character code 0%o ??\n", c);
  681. }
  682. }
  683. if (optind < argc)
  684. {
  685. wprintf ("non-option ARGV-elements: ");
  686. while (optind < argc)
  687. wprintf ("%s ", argv[optind++]);
  688. wprintf ("\n");
  689. }
  690. #ifndef NO_EXIT_ABORT_CALL
  691. exit(0);
  692. #endif
  693. }
  694. #endif /* TEST */