Main Page | Modules | Class Hierarchy | Class List | Directories | File List | Class Members | File Members | Related Pages

getopt1.c

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

Generated on Tue Dec 20 10:14:59 2005 for vlc-0.8.4a by  doxygen 1.4.2