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