00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00035
00036
00037
00038
00039
00040
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
00054
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
00080
00081
00082
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
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