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