OpenCOBOL 1.1pre-rel
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 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
00018    the Free Software Foundation, 51 Franklin Street, Fifth Floor
00019    Boston, MA 02110-1301 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 
00031 #if !defined __STDC__ || !__STDC__
00032 /* This is a separate conditional since some stdc systems
00033    reject `defined (const)'.  */
00034 #ifndef const
00035 #define const
00036 #endif
00037 #endif
00038 
00039 #include <stdio.h>
00040 
00041 /* Comment out all this code if we are using the GNU C Library, and are not
00042    actually compiling the library itself.  This code is part of the GNU C
00043    Library, but also included in many other GNU distributions.  Compiling
00044    and linking in this code is a waste when using the GNU C library
00045    (especially if it is a shared library).  Rather than having every GNU
00046    program understand `configure --with-gnu-libc' and omit the object files,
00047    it is simpler to just do this in the source for each such file.  */
00048 
00049 #define GETOPT_INTERFACE_VERSION 2
00050 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
00051 #include <gnu-versions.h>
00052 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
00053 #define ELIDE_CODE
00054 #endif
00055 #endif
00056 
00057 #ifndef ELIDE_CODE
00058 
00059 
00060 /* This needs to come after some library #include
00061    to get __GNU_LIBRARY__ defined.  */
00062 #ifdef __GNU_LIBRARY__
00063 #include <stdlib.h>
00064 #endif
00065 
00066 #ifndef NULL
00067 #define NULL 0
00068 #endif
00069 
00070 int
00071 getopt_long (argc, argv, options, long_options, opt_index)
00072      int argc;
00073      char *const *argv;
00074      const char *options;
00075      const struct option *long_options;
00076      int *opt_index;
00077 {
00078   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
00079 }
00080 
00081 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00082    If an option that starts with '-' (not '--') doesn't match a long option,
00083    but does match a short option, it is parsed as a short option
00084    instead.  */
00085 
00086 int
00087 getopt_long_only (argc, argv, options, long_options, opt_index)
00088      int argc;
00089      char *const *argv;
00090      const char *options;
00091      const struct option *long_options;
00092      int *opt_index;
00093 {
00094   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
00095 }
00096 
00097 # ifdef _LIBC
00098 libc_hidden_def (getopt_long)
00099 libc_hidden_def (getopt_long_only)
00100 # endif
00101 
00102 #endif  /* Not ELIDE_CODE.  */
00103 
00104 #ifdef TEST
00105 
00106 #include <stdio.h>
00107 
00108 int
00109 main (argc, argv)
00110      int argc;
00111      char **argv;
00112 {
00113   int c;
00114   int digit_optind = 0;
00115 
00116   while (1)
00117     {
00118       int this_option_optind = optind ? optind : 1;
00119       int option_index = 0;
00120       static struct option long_options[] =
00121       {
00122         {"add", 1, 0, 0},
00123         {"append", 0, 0, 0},
00124         {"delete", 1, 0, 0},
00125         {"verbose", 0, 0, 0},
00126         {"create", 0, 0, 0},
00127         {"file", 1, 0, 0},
00128         {0, 0, 0, 0}
00129       };
00130 
00131       c = getopt_long (argc, argv, "abc:d:0123456789",
00132                        long_options, &option_index);
00133       if (c == -1)
00134         break;
00135 
00136       switch (c)
00137         {
00138         case 0:
00139           printf ("option %s", long_options[option_index].name);
00140           if (optarg)
00141             printf (" with arg %s", optarg);
00142           printf ("\n");
00143           break;
00144 
00145         case '0':
00146         case '1':
00147         case '2':
00148         case '3':
00149         case '4':
00150         case '5':
00151         case '6':
00152         case '7':
00153         case '8':
00154         case '9':
00155           if (digit_optind != 0 && digit_optind != this_option_optind)
00156             printf ("digits occur in two different argv-elements.\n");
00157           digit_optind = this_option_optind;
00158           printf ("option %c\n", c);
00159           break;
00160 
00161         case 'a':
00162           printf ("option a\n");
00163           break;
00164 
00165         case 'b':
00166           printf ("option b\n");
00167           break;
00168 
00169         case 'c':
00170           printf ("option c with value `%s'\n", optarg);
00171           break;
00172 
00173         case 'd':
00174           printf ("option d with value `%s'\n", optarg);
00175           break;
00176 
00177         case '?':
00178           break;
00179 
00180         default:
00181           printf ("?? getopt returned character code 0%o ??\n", c);
00182         }
00183     }
00184 
00185   if (optind < argc)
00186     {
00187       printf ("non-option ARGV-elements: ");
00188       while (optind < argc)
00189         printf ("%s ", argv[optind++]);
00190       printf ("\n");
00191     }
00192 
00193   exit (0);
00194 }
00195 
00196 #endif /* TEST */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines