00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifdef HAVE_CONFIG_H
00021 # include <config.h>
00022 #endif
00023
00024 #ifdef _LIBC
00025 # include <getopt.h>
00026 #else
00027 # include "getopt.h"
00028 #endif
00029 #include "getopt_int.h"
00030
00031 #include <stdio.h>
00032
00033
00034
00035 #ifdef __GNU_LIBRARY__
00036 #include <stdlib.h>
00037 #endif
00038
00039 #ifndef NULL
00040 #define NULL 0
00041 #endif
00042
00043 #ifndef __getopt_argv_const
00044 # define __getopt_argv_const const
00045 #endif
00046
00047 int
00048 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
00049 const struct option *long_options, int *opt_index)
00050 {
00051 return _getopt_internal (argc, (char **) argv, options, long_options,
00052 opt_index, 0, 0);
00053 }
00054
00055 int
00056 _getopt_long_r (int argc, char **argv, const char *options,
00057 const struct option *long_options, int *opt_index,
00058 struct _getopt_data *d)
00059 {
00060 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00061 0, 0, d);
00062 }
00063
00064
00065
00066
00067
00068
00069 int
00070 getopt_long_only (int argc, char *__getopt_argv_const *argv,
00071 const char *options,
00072 const struct option *long_options, int *opt_index)
00073 {
00074 return _getopt_internal (argc, (char **) argv, options, long_options,
00075 opt_index, 1, 0);
00076 }
00077
00078 int
00079 _getopt_long_only_r (int argc, char **argv, const char *options,
00080 const struct option *long_options, int *opt_index,
00081 struct _getopt_data *d)
00082 {
00083 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
00084 1, 0, d);
00085 }
00086
00087
00088 #ifdef TEST
00089
00090 #include <stdio.h>
00091
00092 int
00093 main (int argc, char **argv)
00094 {
00095 int c;
00096 int digit_optind = 0;
00097
00098 while (1)
00099 {
00100 int this_option_optind = optind ? optind : 1;
00101 int option_index = 0;
00102 static struct option long_options[] =
00103 {
00104 {"add", 1, 0, 0},
00105 {"append", 0, 0, 0},
00106 {"delete", 1, 0, 0},
00107 {"verbose", 0, 0, 0},
00108 {"create", 0, 0, 0},
00109 {"file", 1, 0, 0},
00110 {0, 0, 0, 0}
00111 };
00112
00113 c = getopt_long (argc, argv, "abc:d:0123456789",
00114 long_options, &option_index);
00115 if (c == -1)
00116 break;
00117
00118 switch (c)
00119 {
00120 case 0:
00121 printf ("option %s", long_options[option_index].name);
00122 if (optarg)
00123 printf (" with arg %s", optarg);
00124 printf ("\n");
00125 break;
00126
00127 case '0':
00128 case '1':
00129 case '2':
00130 case '3':
00131 case '4':
00132 case '5':
00133 case '6':
00134 case '7':
00135 case '8':
00136 case '9':
00137 if (digit_optind != 0 && digit_optind != this_option_optind)
00138 printf ("digits occur in two different argv-elements.\n");
00139 digit_optind = this_option_optind;
00140 printf ("option %c\n", c);
00141 break;
00142
00143 case 'a':
00144 printf ("option a\n");
00145 break;
00146
00147 case 'b':
00148 printf ("option b\n");
00149 break;
00150
00151 case 'c':
00152 printf ("option c with value `%s'\n", optarg);
00153 break;
00154
00155 case 'd':
00156 printf ("option d with value `%s'\n", optarg);
00157 break;
00158
00159 case '?':
00160 break;
00161
00162 default:
00163 printf ("?? getopt returned character code 0%o ??\n", c);
00164 }
00165 }
00166
00167 if (optind < argc)
00168 {
00169 printf ("non-option ARGV-elements: ");
00170 while (optind < argc)
00171 printf ("%s ", argv[optind++]);
00172 printf ("\n");
00173 }
00174
00175 exit (0);
00176 }
00177
00178 #endif