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