Back to index

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