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
00059 getopt_long (argc, argv, options, long_options, opt_index)
00060 int argc;
00061 char *const *argv;
00062 const char *options;
00063 const struct option *long_options;
00064 int *opt_index;
00065 {
00066 return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
00067 }
00068
00069
00070
00071
00072
00073
00074 int
00075 getopt_long_only (argc, argv, options, long_options, opt_index)
00076 int argc;
00077 char *const *argv;
00078 const char *options;
00079 const struct option *long_options;
00080 int *opt_index;
00081 {
00082 return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
00083 }
00084
00085
00086 #endif
00087
00088 #ifdef TEST
00089
00090 #include <stdio.h>
00091
00092 int
00093 main (argc, argv)
00094 int argc;
00095 char **argv;
00096 {
00097 int c;
00098 int digit_optind = 0;
00099
00100 while (1)
00101 {
00102 int this_option_optind = optind ? optind : 1;
00103 int option_index = 0;
00104 static struct option long_options[] =
00105 {
00106 {"add", 1, 0, 0},
00107 {"append", 0, 0, 0},
00108 {"delete", 1, 0, 0},
00109 {"verbose", 0, 0, 0},
00110 {"create", 0, 0, 0},
00111 {"file", 1, 0, 0},
00112 {0, 0, 0, 0}
00113 };
00114
00115 c = getopt_long (argc, argv, "abc:d:0123456789",
00116 long_options, &option_index);
00117 if (c == EOF)
00118 break;
00119
00120 switch (c)
00121 {
00122 case 0:
00123 printf ("option %s", long_options[option_index].name);
00124 if (optarg)
00125 printf (" with arg %s", optarg);
00126 printf ("\n");
00127 break;
00128
00129 case '0':
00130 case '1':
00131 case '2':
00132 case '3':
00133 case '4':
00134 case '5':
00135 case '6':
00136 case '7':
00137 case '8':
00138 case '9':
00139 if (digit_optind != 0 && digit_optind != this_option_optind)
00140 printf ("digits occur in two different argv-elements.\n");
00141 digit_optind = this_option_optind;
00142 printf ("option %c\n", c);
00143 break;
00144
00145 case 'a':
00146 printf ("option a\n");
00147 break;
00148
00149 case 'b':
00150 printf ("option b\n");
00151 break;
00152
00153 case 'c':
00154 printf ("option c with value `%s'\n", optarg);
00155 break;
00156
00157 case 'd':
00158 printf ("option d with value `%s'\n", optarg);
00159 break;
00160
00161 case '?':
00162 break;
00163
00164 default:
00165 printf ("?? getopt returned character code 0%o ??\n", c);
00166 }
00167 }
00168
00169 if (optind < argc)
00170 {
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