1 /* getopt_long and getopt_long_only entry points for GNU getopt.
2 Copyright (C) 1987-1994, 1996-2012 Free Software Foundation, Inc.
4 NOTE: The canonical source of this file is maintained with the GNU C Library.
5 Bugs can be reported to bug-glibc@gnu.org.
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see <http://www.gnu.org/licenses/>. */
25 #if !defined __STDC__ || !__STDC__
26 /* This is a separate conditional since some stdc systems
27 reject `defined (const)'. */
35 /* Comment out all this code if we are using the GNU C Library, and are not
36 actually compiling the library itself. This code is part of the GNU C
37 Library, but also included in many other GNU distributions. Compiling
38 and linking in this code is a waste when using the GNU C library
39 (especially if it is a shared library). Rather than having every GNU
40 program understand `configure --with-gnu-libc' and omit the object files,
41 it is simpler to just do this in the source for each such file. */
43 #define GETOPT_INTERFACE_VERSION 2
44 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
45 #include <gnu-versions.h>
46 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
54 /* This needs to come after some library #include
55 to get __GNU_LIBRARY__ defined. */
56 #ifdef __GNU_LIBRARY__
65 getopt_long (int argc
, char *const *argv
, const char *options
,
66 const struct option
*long_options
, int *opt_index
)
68 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 0);
71 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
72 If an option that starts with '-' (not '--') doesn't match a long option,
73 but does match a short option, it is parsed as a short option
77 getopt_long_only (int argc
, char *const *argv
, const char *options
,
78 const struct option
*long_options
, int *opt_index
)
80 return _getopt_internal (argc
, argv
, options
, long_options
, opt_index
, 1);
84 #endif /* Not ELIDE_CODE. */
91 main (int argc
, char **argv
)
98 int this_option_optind
= optind
? optind
: 1;
100 static struct option long_options
[] =
105 {"verbose", 0, 0, 0},
111 c
= getopt_long (argc
, argv
, "abc:d:0123456789",
112 long_options
, &option_index
);
119 printf ("option %s", long_options
[option_index
].name
);
121 printf (" with arg %s", optarg
);
135 if (digit_optind
!= 0 && digit_optind
!= this_option_optind
)
136 printf ("digits occur in two different argv-elements.\n");
137 digit_optind
= this_option_optind
;
138 printf ("option %c\n", c
);
142 printf ("option a\n");
146 printf ("option b\n");
150 printf ("option c with value '%s'\n", optarg
);
154 printf ("option d with value '%s'\n", optarg
);
161 printf ("?? getopt returned character code 0%o ??\n", c
);
167 printf ("non-option ARGV-elements: ");
168 while (optind
< argc
)
169 printf ("%s ", argv
[optind
++]);