Back to index

nagios-plugins  1.4.16
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, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997,
00003    1998, 2004, 2006, 2009, 2010 Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005 
00006    This program is free software: you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 3 of the License, or
00009    (at your option) any later version.
00010 
00011    This program 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
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00018 
00019 #ifdef _LIBC
00020 # include <getopt.h>
00021 #else
00022 # include <config.h>
00023 # include "getopt.h"
00024 #endif
00025 #include "getopt_int.h"
00026 
00027 #include <stdio.h>
00028 
00029 /* This needs to come after some library #include
00030    to get __GNU_LIBRARY__ defined.  */
00031 #ifdef __GNU_LIBRARY__
00032 #include <stdlib.h>
00033 #endif
00034 
00035 #ifndef NULL
00036 #define NULL 0
00037 #endif
00038 
00039 int
00040 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
00041              const struct option *long_options, int *opt_index)
00042 {
00043   return _getopt_internal (argc, (char **) argv, options, long_options,
00044                            opt_index, 0, 0);
00045 }
00046 
00047 int
00048 _getopt_long_r (int argc, char **argv, const char *options,
00049                 const struct option *long_options, int *opt_index,
00050                 struct _getopt_data *d)
00051 {
00052   return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00053                              0, d, 0);
00054 }
00055 
00056 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
00057    If an option that starts with '-' (not '--') doesn't match a long option,
00058    but does match a short option, it is parsed as a short option
00059    instead.  */
00060 
00061 int
00062 getopt_long_only (int argc, char *__getopt_argv_const *argv,
00063                   const char *options,
00064                   const struct option *long_options, int *opt_index)
00065 {
00066   return _getopt_internal (argc, (char **) argv, options, long_options,
00067                            opt_index, 1, 0);
00068 }
00069 
00070 int
00071 _getopt_long_only_r (int argc, char **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                              1, d, 0);
00077 }
00078 
00079 
00080 #ifdef TEST
00081 
00082 #include <stdio.h>
00083 
00084 int
00085 main (int argc, char **argv)
00086 {
00087   int c;
00088   int digit_optind = 0;
00089 
00090   while (1)
00091     {
00092       int this_option_optind = optind ? optind : 1;
00093       int option_index = 0;
00094       static const struct option long_options[] =
00095       {
00096         {"add", 1, 0, 0},
00097         {"append", 0, 0, 0},
00098         {"delete", 1, 0, 0},
00099         {"verbose", 0, 0, 0},
00100         {"create", 0, 0, 0},
00101         {"file", 1, 0, 0},
00102         {0, 0, 0, 0}
00103       };
00104 
00105       c = getopt_long (argc, argv, "abc:d:0123456789",
00106                        long_options, &option_index);
00107       if (c == -1)
00108         break;
00109 
00110       switch (c)
00111         {
00112         case 0:
00113           printf ("option %s", long_options[option_index].name);
00114           if (optarg)
00115             printf (" with arg %s", optarg);
00116           printf ("\n");
00117           break;
00118 
00119         case '0':
00120         case '1':
00121         case '2':
00122         case '3':
00123         case '4':
00124         case '5':
00125         case '6':
00126         case '7':
00127         case '8':
00128         case '9':
00129           if (digit_optind != 0 && digit_optind != this_option_optind)
00130             printf ("digits occur in two different argv-elements.\n");
00131           digit_optind = this_option_optind;
00132           printf ("option %c\n", c);
00133           break;
00134 
00135         case 'a':
00136           printf ("option a\n");
00137           break;
00138 
00139         case 'b':
00140           printf ("option b\n");
00141           break;
00142 
00143         case 'c':
00144           printf ("option c with value `%s'\n", optarg);
00145           break;
00146 
00147         case 'd':
00148           printf ("option d with value `%s'\n", optarg);
00149           break;
00150 
00151         case '?':
00152           break;
00153 
00154         default:
00155           printf ("?? getopt returned character code 0%o ??\n", c);
00156         }
00157     }
00158 
00159   if (optind < argc)
00160     {
00161       printf ("non-option ARGV-elements: ");
00162       while (optind < argc)
00163         printf ("%s ", argv[optind++]);
00164       printf ("\n");
00165     }
00166 
00167   exit (0);
00168 }
00169 
00170 #endif /* TEST */