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,98
00003      Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Library General Public License as
00008    published by the Free Software Foundation; either version 2 of the
00009    License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Library General Public License for more details.
00015 
00016    You should have received a copy of the GNU Library General Public
00017    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00018    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019    Boston, MA 02111-1307, USA.  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #include "getopt.h"
00026 
00027 #if !defined __STDC__ || !__STDC__
00028 /* This is a separate conditional since some stdc systems
00029    reject `defined (const)'.  */
00030 #ifndef const
00031 #define const
00032 #endif
00033 #endif
00034 
00035 #include <stdio.h>
00036 
00037 /* Comment out all this code if we are using the GNU C Library, and are not
00038    actually compiling the library itself.  This code is part of the GNU C
00039    Library, but also included in many other GNU distributions.  Compiling
00040    and linking in this code is a waste when using the GNU C library
00041    (especially if it is a shared library).  Rather than having every GNU
00042    program understand `configure --with-gnu-libc' and omit the object files,
00043    it is simpler to just do this in the source for each such file.  */
00044 
00045 #define GETOPT_INTERFACE_VERSION 2
00046 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
00047 #include <gnu-versions.h>
00048 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
00049 #define ELIDE_CODE
00050 #endif
00051 #endif
00052 
00053 #ifndef ELIDE_CODE
00054 
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 
00094 #endif  /* Not ELIDE_CODE.  */
00095 
00096 #ifdef TEST
00097 
00098 #include <stdio.h>
00099 
00100 int
00101 main (argc, argv)
00102      int argc;
00103      char **argv;
00104 {
00105   int c;
00106   int digit_optind = 0;
00107 
00108   while (1)
00109     {
00110       int this_option_optind = optind ? optind : 1;
00111       int option_index = 0;
00112       static struct option long_options[] =
00113       {
00114         {"add", 1, 0, 0},
00115         {"append", 0, 0, 0},
00116         {"delete", 1, 0, 0},
00117         {"verbose", 0, 0, 0},
00118         {"create", 0, 0, 0},
00119         {"file", 1, 0, 0},
00120         {0, 0, 0, 0}
00121       };
00122 
00123       c = getopt_long (argc, argv, "abc:d:0123456789",
00124                        long_options, &option_index);
00125       if (c == -1)
00126         break;
00127 
00128       switch (c)
00129         {
00130         case 0:
00131           printf ("option %s", long_options[option_index].name);
00132           if (optarg)
00133             printf (" with arg %s", optarg);
00134           printf ("\n");
00135           break;
00136 
00137         case '0':
00138         case '1':
00139         case '2':
00140         case '3':
00141         case '4':
00142         case '5':
00143         case '6':
00144         case '7':
00145         case '8':
00146         case '9':
00147           if (digit_optind != 0 && digit_optind != this_option_optind)
00148             printf ("digits occur in two different argv-elements.\n");
00149           digit_optind = this_option_optind;
00150           printf ("option %c\n", c);
00151           break;
00152 
00153         case 'a':
00154           printf ("option a\n");
00155           break;
00156 
00157         case 'b':
00158           printf ("option b\n");
00159           break;
00160 
00161         case 'c':
00162           printf ("option c with value `%s'\n", optarg);
00163           break;
00164 
00165         case 'd':
00166           printf ("option d with value `%s'\n", optarg);
00167           break;
00168 
00169         case '?':
00170           break;
00171 
00172         default:
00173           printf ("?? getopt returned character code 0%o ??\n", c);
00174         }
00175     }
00176 
00177   if (optind < argc)
00178     {
00179       printf ("non-option ARGV-elements: ");
00180       while (optind < argc)
00181         printf ("%s ", argv[optind++]);
00182       printf ("\n");
00183     }
00184 
00185   exit (0);
00186 }
00187 
00188 #endif /* TEST */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


portrait_painter
Author(s): Niklas Meinzer, Ina Baumgarten
autogenerated on Wed Dec 26 2012 16:00:43