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