comparison lib-src/getopt1.c @ 155:43dd3413c7c7 r20-3b4

Import from CVS: tag r20-3b4
author cvs
date Mon, 13 Aug 2007 09:39:39 +0200
parents 376386a54a3c
children 15872534500d
comparison
equal deleted inserted replaced
154:94141801dd7e 155:43dd3413c7c7
1 /* getopt_long and getopt_long_only entry points for GNU getopt. 1 /* getopt_long and getopt_long_only entry points for GNU getopt.
2 Copyright (C) 1987, 88, 89, 90, 91, 92, 1993, 1994 2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3 Free Software Foundation, Inc.
4 3
5 This program is free software; you can redistribute it and/or modify it 4 NOTE: The canonical source of this file is maintained with the GNU C Library.
6 under the terms of the GNU General Public License as published by the 5 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9 6
10 This program is distributed in the hope that it will be useful, 7 This program is free software; you can redistribute it and/or modify it
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 8 under the terms of the GNU General Public License as published by the
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 9 Free Software Foundation; either version 2, or (at your option) any
13 GNU General Public License for more details. 10 later version.
14 11
15 You should have received a copy of the GNU General Public License 12 This program is distributed in the hope that it will be useful,
16 along with this program; see the file COPYING. If not, write to 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 Boston, MA 02111-1307, USA. */ 15 GNU General Public License for more details.
19 16
20 /* Synched up with: FSF 19.29. */ 17 You should have received a copy of the GNU General Public License
21 18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 USA. */
22 21
23 #ifdef HAVE_CONFIG_H 22 #ifdef HAVE_CONFIG_H
24 #if defined (emacs) || defined (CONFIG_BROKETS)
25 /* We use <config.h> instead of "config.h" so that a compilation
26 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
27 (which it would do because it found this file in $srcdir). */
28 #include <config.h> 23 #include <config.h>
29 #else
30 #include "config.h"
31 #endif
32 #endif 24 #endif
33 25
34 /* This must be done before getopt.h is included so that the 26 #include "getopt.h"
35 prototypes contained in it are also affected. */ 27
36 #if !defined (__STDC__) || !__STDC__ 28 #if !defined (__STDC__) || !__STDC__
37 /* This is a separate conditional since some stdc systems 29 /* This is a separate conditional since some stdc systems
38 reject `defined (const)'. */ 30 reject `defined (const)'. */
39 #ifndef const 31 #ifndef const
40 #define const 32 #define const
41 #endif 33 #endif
42 #endif 34 #endif
43
44 #include "getopt.h"
45 35
46 #include <stdio.h> 36 #include <stdio.h>
47 37
48 /* Comment out all this code if we are using the GNU C Library, and are not 38 /* Comment out all this code if we are using the GNU C Library, and are not
49 actually compiling the library itself. This code is part of the GNU C 39 actually compiling the library itself. This code is part of the GNU C
51 and linking in this code is a waste when using the GNU C library 41 and linking in this code is a waste when using the GNU C library
52 (especially if it is a shared library). Rather than having every GNU 42 (especially if it is a shared library). Rather than having every GNU
53 program understand `configure --with-gnu-libc' and omit the object files, 43 program understand `configure --with-gnu-libc' and omit the object files,
54 it is simpler to just do this in the source for each such file. */ 44 it is simpler to just do this in the source for each such file. */
55 45
56 #if defined (_LIBC) || !defined (__GNU_LIBRARY__) 46 #define GETOPT_INTERFACE_VERSION 2
47 #if !defined (_LIBC) && defined (__GLIBC__) && __GLIBC__ >= 2
48 #include <gnu-versions.h>
49 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
50 #define ELIDE_CODE
51 #endif
52 #endif
53
54 #ifndef ELIDE_CODE
57 55
58 56
59 /* This needs to come after some library #include 57 /* This needs to come after some library #include
60 to get __GNU_LIBRARY__ defined. */ 58 to get __GNU_LIBRARY__ defined. */
61 #ifdef __GNU_LIBRARY__ 59 #ifdef __GNU_LIBRARY__
62 #include <stdlib.h> 60 #include <stdlib.h>
63 #else
64 char *getenv ();
65 #endif 61 #endif
66 62
67 #ifndef NULL 63 #ifndef NULL
68 #define NULL 0 64 #define NULL 0
69 #endif 65 #endif
79 If an option that starts with '-' (not '--') doesn't match a long option, 75 If an option that starts with '-' (not '--') doesn't match a long option,
80 but does match a short option, it is parsed as a short option 76 but does match a short option, it is parsed as a short option
81 instead. */ 77 instead. */
82 78
83 int 79 int
84 getopt_long_only (argc, argv, options, long_options, opt_index) 80 getopt_long_only (int argc, char *const *argv, const char *options,
85 int argc; 81 const struct option *long_options, int *opt_index)
86 char *const *argv;
87 const char *options;
88 const struct option *long_options;
89 int *opt_index;
90 { 82 {
91 return _getopt_internal (argc, argv, options, long_options, opt_index, 1); 83 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
92 } 84 }
93 85
94 86
95 #endif /* _LIBC or not __GNU_LIBRARY__. */ 87 #endif /* Not ELIDE_CODE. */
96 88
97 #ifdef TEST 89 #ifdef TEST
98 90
99 #include <stdio.h> 91 #include <stdio.h>
100 92
101 int 93 int
102 main (argc, argv) 94 main (int argc, char **argv)
103 int argc;
104 char **argv;
105 { 95 {
106 int c; 96 int c;
107 int digit_optind = 0; 97 int digit_optind = 0;
108 98
109 while (1) 99 while (1)
121 {0, 0, 0, 0} 111 {0, 0, 0, 0}
122 }; 112 };
123 113
124 c = getopt_long (argc, argv, "abc:d:0123456789", 114 c = getopt_long (argc, argv, "abc:d:0123456789",
125 long_options, &option_index); 115 long_options, &option_index);
126 if (c == EOF) 116 if (c == -1)
127 break; 117 break;
128 118
129 switch (c) 119 switch (c)
130 { 120 {
131 case 0: 121 case 0: