Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

getopt1.c

Go to the documentation of this file.
00001 /* getopt_long and getopt_long_only entry points for GNU getopt.
00002    Copyright (C) 1987, 88, 89, 90, 91, 92, 1993, 1994
00003         Free Software Foundation, Inc.
00004 
00005    This program is free software; you can redistribute it and/or modify it
00006    under the terms of the GNU General Public License as published by the
00007    Free Software Foundation; either version 2, or (at your option) any later
00008    version.
00009 
00010    This program is distributed in the hope that it will be useful, but
00011    WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
00013    Public License for more details.
00014 
00015    You should have received a copy of the GNU General Public License along
00016    with this program; if not, write to the Free Software Foundation, 675
00017    Mass Ave, Cambridge, MA 02139, USA.  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #include <config.h>
00021 #endif
00022 
00023 #include "getopt.h"
00024 
00025 #if !defined (__STDC__) || !__STDC__
00026 /* This is a separate conditional since some stdc systems
00027    reject `defined (const)'.  */
00028 #ifndef const
00029 #define const
00030 #endif
00031 #endif
00032 
00033 #include <stdio.h>
00034 
00035 /* Comment out all this code if we are using the GNU C Library, and are not
00036    actually compiling the library itself.  This code is part of the GNU C
00037    Library, but also included in many other GNU distributions.  Compiling
00038    and linking in this code is a waste when using the GNU C library
00039    (especially if it is a shared library).  Rather than having every GNU
00040    program understand `configure --with-gnu-libc' and omit the object files,
00041    it is simpler to just do this in the source for each such file.  */
00042 
00043 #if defined (_LIBC) || !defined (__GNU_LIBRARY__)
00044 
00045 
00046 /* This needs to come after some library #include
00047    to get __GNU_LIBRARY__ defined.  */
00048 #ifdef __GNU_LIBRARY__
00049 #include <stdlib.h>
00050 #else
00051 char *getenv();
00052 #endif
00053 
00054 #ifndef NULL
00055 #define NULL 0
00056 #endif
00057 
00058 int getopt_long(argc, argv, options, long_options, opt_index)
00059 int argc;
00060 char *const *argv;
00061 const char *options;
00062 const struct option *long_options;
00063 int *opt_index;
00064 {
00065         return _getopt_internal(argc, argv, options, long_options,
00066                                 opt_index, 0);
00067 }
00068 
00069 /* Like getopt_long, but '-' as well as '--' can indicate a long option.  If
00070    an option that starts with '-' (not '--') doesn't match a long option,
00071    but does match a short option, it is parsed as a short option instead. 
00072  */
00073 
00074 int getopt_long_only(argc, argv, options, long_options, opt_index)
00075 int argc;
00076 char *const *argv;
00077 const char *options;
00078 const struct option *long_options;
00079 int *opt_index;
00080 {
00081         return _getopt_internal(argc, argv, options, long_options,
00082                                 opt_index, 1);
00083 }
00084 
00085 
00086 #endif                          /* _LIBC or not __GNU_LIBRARY__.  */
00087 
00088 #ifdef TEST
00089 
00090 #include <stdio.h>
00091 
00092 int main(argc, argv)
00093 int argc;
00094 char **argv;
00095 {
00096         int c;
00097         int digit_optind = 0;
00098 
00099         while (1) {
00100                 int this_option_optind = optind ? optind : 1;
00101                 int option_index = 0;
00102                 static struct option long_options[] == {
00103                         {"add", 1, 0, 0},
00104                         {"append", 0, 0, 0},
00105                         {"delete", 1, 0, 0},
00106                         {"verbose", 0, 0, 0},
00107                         {"create", 0, 0, 0},
00108                         {"file", 1, 0, 0},
00109                         {0, 0, 0, 0}
00110                 };
00111 
00112                 c = getopt_long(argc, argv, "abc:d:0123456789",
00113                                 long_options, &option_index);
00114                 if (c == EOF)
00115                         break;
00116 
00117                 switch (c) {
00118                 case 0:
00119                         printf("option %s",
00120                                long_options[option_index].name);
00121                         if (optarg)
00122                                 printf(" with arg %s", optarg);
00123                         printf("\n");
00124                         break;
00125 
00126                 case '0':
00127                 case '1':
00128                 case '2':
00129                 case '3':
00130                 case '4':
00131                 case '5':
00132                 case '6':
00133                 case '7':
00134                 case '8':
00135                 case '9':
00136                         if (digit_optind != 0
00137                             && digit_optind != this_option_optind)
00138                                 printf
00139                                     ("digits occur in two different argv-elements.\n");
00140                         digit_optind = this_option_optind;
00141                         printf("option %c\n", c);
00142                         break;
00143 
00144                 case 'a':
00145                         printf("option a\n");
00146                         break;
00147 
00148                 case 'b':
00149                         printf("option b\n");
00150                         break;
00151 
00152                 case 'c':
00153                         printf("option c with value `%s'\n", optarg);
00154                         break;
00155 
00156                 case 'd':
00157                         printf("option d with value `%s'\n", optarg);
00158                         break;
00159 
00160                 case '?':
00161                         break;
00162 
00163                 default:
00164                         printf
00165                             ("?? getopt returned character code 0%o ??\n",
00166                              c);
00167                 }
00168         }
00169 
00170         if (optind < argc) {
00171                 printf("non-option ARGV-elements: ");
00172                 while (optind < argc)
00173                         printf("%s ", argv[optind++]);
00174                 printf("\n");
00175         }
00176 
00177         exit(0);
00178 }
00179 
00180 #endif                          /* TEST */

Generated on Tue Apr 9 13:53:07 2002 for RWIL - Real World Interface Library by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001