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