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