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

Generated on Fri Jan 9 16:44:30 2009 for libextractor by  doxygen 1.5.1