1 | /* getopt_long and getopt_long_only entry points for GNU getopt. |
2 | Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98, 2016 |
3 | Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | This program is free software: you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 3 of the License, or |
9 | (at your option) any later version. |
10 | |
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 | |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifdef HAVE_CONFIG_H |
20 | #include <config.h> |
21 | #endif |
22 | |
23 | #include "getopt.h" |
24 | |
25 | #include <stdio.h> |
26 | |
27 | /* Comment out all this code if we are using the GNU C Library, and are not |
28 | actually compiling the library itself. This code is part of the GNU C |
29 | Library, but also included in many other GNU distributions. Compiling |
30 | and linking in this code is a waste when using the GNU C library |
31 | (especially if it is a shared library). Rather than having every GNU |
32 | program understand `configure --with-gnu-libc' and omit the object files, |
33 | it is simpler to just do this in the source for each such file. */ |
34 | |
35 | #define GETOPT_INTERFACE_VERSION 2 |
36 | #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2 |
37 | #include <gnu-versions.h> |
38 | #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION |
39 | #define ELIDE_CODE |
40 | #endif |
41 | #endif |
42 | |
43 | #ifndef ELIDE_CODE |
44 | |
45 | |
46 | /* This needs to come after some library #include |
47 | to get __GNU_LIBRARY__ defined. */ |
48 | #ifdef __GNU_LIBRARY__ |
49 | #include <stdlib.h> |
50 | #endif |
51 | |
52 | int |
53 | getopt_long (int argc, char *const *argv, const char *options, |
54 | const struct option *long_options, int *opt_index) |
55 | { |
56 | return _getopt_internal (argc, argv, options, long_options, opt_index, 0); |
57 | } |
58 | |
59 | /* Like getopt_long, but '-' as well as '--' can indicate a long option. |
60 | If an option that starts with '-' (not '--') doesn't match a long option, |
61 | but does match a short option, it is parsed as a short option |
62 | instead. */ |
63 | |
64 | int |
65 | getopt_long_only (int argc, char *const *argv, const char *options, |
66 | const struct option *long_options, int *opt_index) |
67 | { |
68 | return _getopt_internal (argc, argv, options, long_options, opt_index, 1); |
69 | } |
70 | |
71 | |
72 | #endif /* Not ELIDE_CODE. */ |
73 | |
74 | #ifdef TEST |
75 | |
76 | #include <stdio.h> |
77 | |
78 | int |
79 | main (int argc, char **argv) |
80 | { |
81 | int c; |
82 | int digit_optind = 0; |
83 | |
84 | while (1) |
85 | { |
86 | int this_option_optind = optind ? optind : 1; |
87 | int option_index = 0; |
88 | static struct option long_options[] = |
89 | { |
90 | {"add" , 1, 0, 0}, |
91 | {"append" , 0, 0, 0}, |
92 | {"delete" , 1, 0, 0}, |
93 | {"verbose" , 0, 0, 0}, |
94 | {"create" , 0, 0, 0}, |
95 | {"file" , 1, 0, 0}, |
96 | {0, 0, 0, 0} |
97 | }; |
98 | |
99 | c = getopt_long (argc, argv, "abc:d:0123456789" , |
100 | long_options, &option_index); |
101 | if (c == -1) |
102 | break; |
103 | |
104 | switch (c) |
105 | { |
106 | case 0: |
107 | printf ("option %s" , long_options[option_index].name); |
108 | if (optarg) |
109 | printf (" with arg %s" , optarg); |
110 | printf ("\n" ); |
111 | break; |
112 | |
113 | case '0': |
114 | case '1': |
115 | case '2': |
116 | case '3': |
117 | case '4': |
118 | case '5': |
119 | case '6': |
120 | case '7': |
121 | case '8': |
122 | case '9': |
123 | if (digit_optind != 0 && digit_optind != this_option_optind) |
124 | printf ("digits occur in two different argv-elements.\n" ); |
125 | digit_optind = this_option_optind; |
126 | printf ("option %c\n" , c); |
127 | break; |
128 | |
129 | case 'a': |
130 | printf ("option a\n" ); |
131 | break; |
132 | |
133 | case 'b': |
134 | printf ("option b\n" ); |
135 | break; |
136 | |
137 | case 'c': |
138 | printf ("option c with value `%s'\n" , optarg); |
139 | break; |
140 | |
141 | case 'd': |
142 | printf ("option d with value `%s'\n" , optarg); |
143 | break; |
144 | |
145 | case '?': |
146 | break; |
147 | |
148 | default: |
149 | printf ("?? getopt returned character code 0%o ??\n" , c); |
150 | } |
151 | } |
152 | |
153 | if (optind < argc) |
154 | { |
155 | printf ("non-option ARGV-elements: " ); |
156 | while (optind < argc) |
157 | printf ("%s " , argv[optind++]); |
158 | printf ("\n" ); |
159 | } |
160 | |
161 | exit (0); |
162 | } |
163 | |
164 | #endif /* TEST */ |
165 | |