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

© sourcejam.com 2005-2008