File indexing completed on 2024-12-22 04:04:11

0001 /* getopt_long and getopt_long_only entry points for GNU getopt.
0002    Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
0003      Free Software Foundation, Inc.
0004    This file is part of the GNU C Library.
0005 
0006    The GNU C Library is free software; you can redistribute it and/or
0007    modify it under the terms of the GNU Library General Public License as
0008    published by the Free Software Foundation; either version 2 of the
0009    License, or (at your option) any later version.
0010 
0011    The GNU C Library is distributed in the hope that it will be useful,
0012    but WITHOUT ANY WARRANTY; without even the implied warranty of
0013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014    Library General Public License for more details.
0015 
0016    You should have received a copy of the GNU Library General Public
0017    License along with the GNU C Library; see the file COPYING.LIB.  If not,
0018    write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019    Boston, MA 02110-1301, USA.  */
0020 
0021 #ifdef HAVE_CONFIG_H
0022 #include <config.h>
0023 #endif
0024 
0025 #include "getopt.h"
0026 
0027 #if !defined __STDC__ || !__STDC__
0028 /* This is a separate conditional since some stdc systems
0029    reject `defined (const)'.  */
0030 #ifndef const
0031 #define const
0032 #endif
0033 #endif
0034 
0035 #include <stdio.h>
0036 
0037 /* This needs to come after some library #include
0038    to get __GNU_LIBRARY__ defined.  */
0039 #ifdef __GNU_LIBRARY__
0040 #include <stdlib.h>
0041 #endif
0042 
0043 #ifndef NULL
0044 #define NULL 0
0045 #endif
0046 
0047 int
0048 getopt_long (argc, argv, options, long_options, opt_index)
0049      int argc;
0050      char *const *argv;
0051      const char *options;
0052      const struct option *long_options;
0053      int *opt_index;
0054 {
0055   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
0056 }
0057 
0058 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
0059    If an option that starts with '-' (not '--') doesn't match a long option,
0060    but does match a short option, it is parsed as a short option
0061    instead.  */
0062 
0063 int
0064 getopt_long_only (argc, argv, options, long_options, opt_index)
0065      int argc;
0066      char *const *argv;
0067      const char *options;
0068      const struct option *long_options;
0069      int *opt_index;
0070 {
0071   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
0072 }
0073 
0074 
0075 
0076 #ifdef TEST
0077 
0078 #include <stdio.h>
0079 
0080 int
0081 main (argc, argv)
0082      int argc;
0083      char **argv;
0084 {
0085   int c;
0086   int digit_optind = 0;
0087 
0088   while (1)
0089     {
0090       int this_option_optind = optind ? optind : 1;
0091       int option_index = 0;
0092       static struct option long_options[] =
0093       {
0094     {"add", 1, 0, 0},
0095     {"append", 0, 0, 0},
0096     {"delete", 1, 0, 0},
0097     {"verbose", 0, 0, 0},
0098     {"create", 0, 0, 0},
0099     {"file", 1, 0, 0},
0100     {0, 0, 0, 0}
0101       };
0102 
0103       c = getopt_long (argc, argv, "abc:d:0123456789",
0104                long_options, &option_index);
0105       if (c == -1)
0106     break;
0107 
0108       switch (c)
0109     {
0110     case 0:
0111       printf ("option %s", long_options[option_index].name);
0112       if (optarg)
0113         printf (" with arg %s", optarg);
0114       printf ("\n");
0115       break;
0116 
0117     case '0':
0118     case '1':
0119     case '2':
0120     case '3':
0121     case '4':
0122     case '5':
0123     case '6':
0124     case '7':
0125     case '8':
0126     case '9':
0127       if (digit_optind != 0 && digit_optind != this_option_optind)
0128         printf ("digits occur in two different argv-elements.\n");
0129       digit_optind = this_option_optind;
0130       printf ("option %c\n", c);
0131       break;
0132 
0133     case 'a':
0134       printf ("option a\n");
0135       break;
0136 
0137     case 'b':
0138       printf ("option b\n");
0139       break;
0140 
0141     case 'c':
0142       printf ("option c with value `%s'\n", optarg);
0143       break;
0144 
0145     case 'd':
0146       printf ("option d with value `%s'\n", optarg);
0147       break;
0148 
0149     case '?':
0150       break;
0151 
0152     default:
0153       printf ("?? getopt returned character code 0%o ??\n", c);
0154     }
0155     }
0156 
0157   if (optind < argc)
0158     {
0159       printf ("non-option ARGV-elements: ");
0160       while (optind < argc)
0161     printf ("%s ", argv[optind++]);
0162       printf ("\n");
0163     }
0164 
0165   exit (0);
0166 }
0167 
0168 #endif /* TEST */