Back to index

glibc  2.9
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,2004
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 Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the 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    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 #include <config.h>
00023 #endif
00024 
00025 #ifdef _LIBC
00026 # include <getopt.h>
00027 #else
00028 # include "getopt.h"
00029 #endif
00030 #include "getopt_int.h"
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 (int argc, char *const *argv, const char *options,
00065             const struct option *long_options, int *opt_index)
00066 {
00067   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
00068 }
00069 
00070 int
00071 _getopt_long_r (int argc, char *const *argv, const char *options,
00072               const struct option *long_options, int *opt_index,
00073               struct _getopt_data *d)
00074 {
00075   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00076                           0, d);
00077 }
00078 
00079 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00080    If an option that starts with '-' (not '--') doesn't match a long option,
00081    but does match a short option, it is parsed as a short option
00082    instead.  */
00083 
00084 int
00085 getopt_long_only (int argc, char *const *argv, const char *options,
00086                 const struct option *long_options, int *opt_index)
00087 {
00088   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
00089 }
00090 
00091 int
00092 _getopt_long_only_r (int argc, char *const *argv, const char *options,
00093                    const struct option *long_options, int *opt_index,
00094                    struct _getopt_data *d)
00095 {
00096   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00097                           1, d);
00098 }
00099 
00100 #endif /* Not ELIDE_CODE.  */
00101 
00102 #ifdef TEST
00103 
00104 #include <stdio.h>
00105 
00106 int
00107 main (int argc, char **argv)
00108 {
00109   int c;
00110   int digit_optind = 0;
00111 
00112   while (1)
00113     {
00114       int this_option_optind = optind ? optind : 1;
00115       int option_index = 0;
00116       static struct option long_options[] =
00117       {
00118        {"add", 1, 0, 0},
00119        {"append", 0, 0, 0},
00120        {"delete", 1, 0, 0},
00121        {"verbose", 0, 0, 0},
00122        {"create", 0, 0, 0},
00123        {"file", 1, 0, 0},
00124        {0, 0, 0, 0}
00125       };
00126 
00127       c = getopt_long (argc, argv, "abc:d:0123456789",
00128                      long_options, &option_index);
00129       if (c == -1)
00130        break;
00131 
00132       switch (c)
00133        {
00134        case 0:
00135          printf ("option %s", long_options[option_index].name);
00136          if (optarg)
00137            printf (" with arg %s", optarg);
00138          printf ("\n");
00139          break;
00140 
00141        case '0':
00142        case '1':
00143        case '2':
00144        case '3':
00145        case '4':
00146        case '5':
00147        case '6':
00148        case '7':
00149        case '8':
00150        case '9':
00151          if (digit_optind != 0 && digit_optind != this_option_optind)
00152            printf ("digits occur in two different argv-elements.\n");
00153          digit_optind = this_option_optind;
00154          printf ("option %c\n", c);
00155          break;
00156 
00157        case 'a':
00158          printf ("option a\n");
00159          break;
00160 
00161        case 'b':
00162          printf ("option b\n");
00163          break;
00164 
00165        case 'c':
00166          printf ("option c with value `%s'\n", optarg);
00167          break;
00168 
00169        case 'd':
00170          printf ("option d with value `%s'\n", optarg);
00171          break;
00172 
00173        case '?':
00174          break;
00175 
00176        default:
00177          printf ("?? getopt returned character code 0%o ??\n", c);
00178        }
00179     }
00180 
00181   if (optind < argc)
00182     {
00183       printf ("non-option ARGV-elements: ");
00184       while (optind < argc)
00185        printf ("%s ", argv[optind++]);
00186       printf ("\n");
00187     }
00188 
00189   exit (0);
00190 }
00191 
00192 #endif /* TEST */