+++ /dev/null
-#include <stdio.h>\r
-#include <string.h>\r
-\r
-#include <getopt.h>\r
-\rchar *\r optarg = NULL;
-\r\rint \r optind = 0;
-\r\rint \r optopt = '?';
-\r\rint \r opterr = 1;
-\r\rstatic char *\r nextchar;
-\r\rstatic \r int first_nonopt;
-\r\rstatic int \r last_nonopt;
-\r\r\rstatic enum \r { \rREQUIRE_ORDER, \rPERMUTE, \rRETURN_IN_ORDER \r
-} ordering;
-\r\r\rstatic const char *\r __getopt_initialize(const char *optstring);
-\r\rstatic int \r
-__getopt_internal(int argc, char *const *argv, const char *optstring,
- const struct option *longopts, int *longind,
- int long_only);
-\r\rstatic void \r __exchange(char **argv);
-\r\rint \r getopt(int argc, char *const argv[], const char *optstring) \r
-{
- \rreturn __getopt_internal(argc, argv, optstring,
- (const struct option *) 0, (int *) 0, 0);
-\r} \r\rstatic const char *\r __getopt_initialize(const char *optstring) \r
-{
- \r
- /* Start processing options with ARGV-element 1 (since ARGV-element 0\r
- is the program name); the sequence of previously skipped\r
- non-option ARGV-elements is empty. */ \r
- \rfirst_nonopt = last_nonopt = optind = 1;
- \rnextchar = NULL;
- \r\r
- /* Determine how to handle the ordering of options and nonoptions. */ \r
- \rif (optstring[0] == '-')
- \r {
- \rordering = RETURN_IN_ORDER;
- \r++optstring;
- \r}
- \r
- /* si la chaîne d'options commence par un + alors la fonction getopt() s'arrête\r
- * dès qu'un argument de la ligne de commande n'est pas une option\r
- */ \r
- else if (optstring[0] == '+')
- \r {
- \rordering = REQUIRE_ORDER;
- \r++optstring;
- \r}
- \r
- else
- \r {
- \rordering = PERMUTE;
- \r}
- \r\rreturn optstring;
-\r}
-
-\r\rint \r
-__getopt_internal(int argc, char *const *argv, const char *optstring,
- const struct option *longopts, int *longind,
- int long_only) \r
-{
- \roptarg = NULL;
- \r\rif (optind == 0)
- \roptstring = __getopt_initialize(optstring);
- \r\rif (nextchar == NULL || *nextchar == '\0')
- \r {
- \r
- /* Advance to the next ARGV-element. */ \r
- \rif (ordering == PERMUTE)
- \r {
- \r
- /* If we have just processed some options following some non-options,\r
- __exchange them so that the options come first. */ \r
- \rif (first_nonopt != last_nonopt && last_nonopt != optind)
- \r__exchange((char **) argv);
- \r
- else if (last_nonopt != optind)
- \rfirst_nonopt = optind;
- \r\r
- /* Skip any additional non-options\r
- and extend the range of non-options previously skipped. */ \r
- \rwhile (optind < argc
- && (argv[optind][0] != '-' || argv[optind][1] == '\0'))
- \roptind++;
- \r\rlast_nonopt = optind;
- \r}
- \r\r
- /* The special ARGV-element `--' means premature end of options.\r
- Skip it like a null option,\r
- then __exchange with previous non-options as if it were an option,\r
- then skip everything else like a non-option. */ \r
- \rif (optind != argc && !strcmp(argv[optind], "--"))
- \r {
- \roptind++;
- \r\rif (first_nonopt != last_nonopt && last_nonopt != optind)
- \r__exchange((char **) argv);
- \r
- else if (first_nonopt == last_nonopt)
- \rfirst_nonopt = optind;
- \r\rlast_nonopt = argc;
- \r\roptind = argc;
- \r}
- \r\r
- /* If we have done all the ARGV-elements, stop the scan\r
- and back over any non-options that we skipped and permuted. */ \r
- \rif (optind == argc)
- \r {
- \r
- /* Set the next-arg-index to point at the non-options\r
- that we previously skipped, so the caller will digest them. */ \r
- if (first_nonopt != last_nonopt)
- \roptind = first_nonopt;
- \r\rreturn EOF;
- \r}
- \r\r
- /* If we have come to a non-option and did not permute it,\r
- either stop the scan or describe it to the caller and pass it by. */ \r
- \rif ((argv[optind][0] != '-' || argv[optind][1] == '\0'))
- \r {
- \rif (ordering == REQUIRE_ORDER)
- \rreturn EOF;
- \roptarg = argv[optind++];
- \rreturn 1;
- \r}
- \r\r
- /* We have found another option-ARGV-element.\r
- Skip the initial punctuation. */ \r
- \rnextchar =
- (argv[optind] + 1 + (longopts != NULL && argv[optind][1] == '-'));
- \r}
- \r\r
- /* Decode the current option-ARGV-element. */ \r
- \r
- /* Check whether the ARGV-element is a long option.\r
- \r
- If long_only and the ARGV-element has the form "-f", where f is\r
- a valid short option, don't consider it an abbreviated form of\r
- a long option that starts with f. Otherwise there would be no\r
- way to give the -f short option.\r
- \r
- On the other hand, if there's a long option "fubar" and\r
- the ARGV-element is "-fu", do consider that an abbreviation of\r
- the long option, just like "--fu", and not "-f" with arg "u".\r
- \r
- This distinction seems to be the most useful approach. */ \r
- \rif (longopts != NULL
- && (argv[optind][1] == '-'
- || (long_only
- && (argv[optind][2]
- || !strchr(optstring, argv[optind][1])))))
- \r {
- \rchar *nameend;
- \rconst struct option *p;
- \rconst struct option *pfound = NULL;
- \rint exact = 0;
- \rint ambig = 0;
- \rint indfound = 0;
- \rint option_index;
- \r\rfor (nameend = nextchar; *nameend != '\0' && *nameend != '=';
- nameend++)
- \r
- /* Do nothing. */ ;
- \r\r
- /* Test all long options for either exact match\r
- or abbreviated matches. */ \r
- for (p = longopts, option_index = 0; p->name; p++, option_index++)
- \r {
- \rif (!strncmp(p->name, nextchar, nameend - nextchar))
- \r {
- \r\rif (nameend - nextchar == strlen(p->name))
- \r {
- \r
- /* Exact match found. */ \r
- pfound = p;
- \rindfound = option_index;
- \rexact = 1;
- \rbreak;
- \r}
- \r
- else if (pfound == NULL)
- \r {
- \r
- /* First nonexact match found. */ \r
- exact = 0;
- \r
- /* begin change\r
- pfound = p;\r
- indfound = option_index;\r
- end change */ \r
- break;
- \r}
- \r
- else
- \r {
- \r\r
- /* Second or later nonexact match found. */ \r
- ambig = 1;
- \r}
- \r}
- \r}
- \r\rif (ambig && !exact)
- \r {
- \rif (opterr)
- \rfprintf(stderr, "error : %s: option `%s' is ambiguous\n",
- argv[0], argv[optind]);
- \r\rnextchar += strlen(nextchar);
- \roptind++;
- \rreturn '?';
- \r}
- \r\rif (pfound != NULL)
- \r {
- \roption_index = indfound;
- \roptind++;
- \r\rif (*nameend)
- \r {
- \r
- /* Don't test has_arg with >, because some C compilers don't\r
- allow it to be used on enums. */ \r
- if (pfound->has_arg)
- \roptarg = nameend + 1;
- \r
- else
- \r {
- \rif (opterr)
- \r {
- \rif (argv[optind - 1][1] == '-')
- \r
- /* --option */ \r
- fprintf(stderr,
- "error : %s: option `--%s' doesn't allow an argument\n",
- argv[0], pfound->name);
- \r
- else
- \r
- /* +option or -option */ \r
- fprintf(stderr,
- "error : %s: option `%c%s' doesn't allow an argument\n",
- argv[0], argv[optind - 1][0], pfound->name);
- \r}
- \r\rnextchar += strlen(nextchar);
- \rreturn '?';
- \r}
- \r}
- \r
- else if (pfound->has_arg == 1)
- \r {
- \rif (optind < argc)
- \roptarg = argv[optind++];
- \r
- else
- \r {
- \rif (opterr)
- \rfprintf(stderr,
- "error : %s: option `%s' requires an argument\n",
- argv[0], argv[optind - 1]);
- \r\rnextchar += strlen(nextchar);
- \rreturn optstring[0] == ':' ? ':' : '?';
- \r}
- \r}
- \r\rnextchar += strlen(nextchar);
- \r\rif (longind != NULL)
- \r*longind = option_index;
- \r\rif (pfound->flag)
- \r {
- \r*(pfound->flag) = pfound->val;
- \rreturn 0;
- \r}
- \r\rreturn pfound->val;
- \r}
- \r\r
- /* Can't find it as a long option. If this is not getopt_long_only,\r
- or the option starts with '--' or is not a valid short\r
- option, then it's an error.\r
- Otherwise interpret it as a short option. */ \r
- if (!long_only || argv[optind][1] == '-'
- || strchr(optstring, *nextchar) == NULL)
- \r {
- \rif (opterr)
- \r {
- \rif (argv[optind][1] == '-')
- \r
- /* --option */ \r
- fprintf(stderr, "error : %s: unrecognized option `--%s'\n",
- argv[0], nextchar);
- \r
- else
- \r
- /* +option or -option */ \r
- fprintf(stderr, "error : %s: unrecognized option `%c%s'\n",
- argv[0], argv[optind][0], nextchar);
- \r}
- \r\rnextchar = (char *) "";
- \roptind++;
- \rreturn '?';
- \r}
- \r}
- \r\r
- /* Look at and handle the next short option-character. */ \r
- \r {
- \rchar c = *nextchar++;
- \rchar *temp = strchr(optstring, c);
- \r\r
- /* Increment `optind' when we start to process its last character. */ \r
- if (*nextchar == '\0')
- \r++optind;
- \r\rif (temp == NULL || c == ':')
- \r {
- \rif (opterr)
- \rfprintf(stderr, "error : %s: invalid option -- %c\n", argv[0],
- c);
- \r\roptopt = c;
- \rreturn '?';
- \r}
- \r\rif (temp[1] == ':')
- \r {
- \rif (temp[2] == ':')
- \r {
- \r
- /* This is an option that accepts an argument optionally. */ \r
- if (*nextchar != '\0')
- \r {
- \roptarg = nextchar;
- \roptind++;
- \r}
- \r
- else
- \roptarg = NULL;
- \r\rnextchar = NULL;
- \r}
- \r
- else
- \r {
- \r
- /* This is an option that requires an argument. */ \r
- if (*nextchar != '\0')
- \r {
- \roptarg = nextchar;
- \r
- /* If we end this ARGV-element by taking the rest as an arg,\r
- we must advance to the next element now. */ \r
- optind++;
- \r}
- \r
- else if (optind == argc)
- \r {
- \rif (opterr)
- \r {
- \r
- /* 1003.2 specifies the format of this message. */ \r
- fprintf(stderr,
- "error : %s: option requires an argument -- %c\n",
- argv[0], c);
- \r}
- \roptopt = c;
- \r\rif (optstring[0] == ':')
- \rc = ':';
- \r
- else
- \rc = '?';
- \r}
- \r
- else
- \r
- /* We already incremented `optind' once;\r
- increment it again when taking next ARGV-elt as argument. */ \r
- optarg = argv[optind++];
- \r\rnextchar = NULL;
- \r}
- \r}
- \rreturn c;
- \r\r}
-\r}
-
-\r\r\rstatic void \r __exchange(char **argv) \r
-{
- \rint bottom = first_nonopt;
- \rint middle = last_nonopt;
- \rint top = optind;
- \rchar *tem;
- \r\r
- /* Exchange the shorter segment with the far end of the longer segment.\r
- That puts the shorter segment into the right place.\r
- It leaves the longer segment in the right place overall,\r
- but it consists of two parts that need to be swapped next. */ \r
- \rwhile (top > middle && middle > bottom)
- \r {
- \rif (top - middle > middle - bottom)
- \r {
- \r
- /* Bottom segment is the short one. */ \r
- int len = middle - bottom;
- \rregister int i;
- \r\r
- /* Swap it with the top part of the top segment. */ \r
- for (i = 0; i < len; i++)
- \r {
- \rtem = argv[bottom + i];
- \rargv[bottom + i] = argv[top - (middle - bottom) + i];
- \rargv[top - (middle - bottom) + i] = tem;
- \r}
- \r
- /* Exclude the moved bottom segment from further swapping. */ \r
- \rtop -= len;
- \r}
- \r
- else
- \r {
- \r
- /* Top segment is the short one. */ \r
- int len = top - middle;
- \rregister int i;
- \r\r
- /* Swap it with the bottom part of the bottom segment. */ \r
- for (i = 0; i < len; i++)
- \r {
- \rtem = argv[bottom + i];
- \rargv[bottom + i] = argv[middle + i];
- \rargv[middle + i] = tem;
- \r}
- \r
- /* Exclude the moved top segment from further swapping. */ \r
- bottom += len;
- \r}
- \r}
- \r\r
- /* Update records for the slots the non-options now occupy. */ \r
- \rfirst_nonopt += (optind - last_nonopt);
- \rlast_nonopt = optind;
-\r}
-
-\r\rint \r
-getopt_long(int argc, char *const *argv, const char *options,
- const struct option *long_options, int *opt_index) \r
-{
- \rreturn __getopt_internal(argc, argv, options, long_options, opt_index,
- 0);
-\r}
-
-\r\r\rint \r
-getopt_long_only(int argc, char *const *argv, const char *options,
- const struct option *long_options, int *opt_index) \r
-{
- \rreturn __getopt_internal(argc, argv, options, long_options, opt_index,
- 1);
-\r}
-
-\r\r\r