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