320
315
if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
322
317
/* We must extend the array. The user plays games with us and
323
presents new arguments. */
318
presents new arguments. */
324
319
char *new_str = malloc (top + 1);
325
320
if (new_str == NULL)
326
nonoption_flags_len = nonoption_flags_max_len = 0;
321
nonoption_flags_len = nonoption_flags_max_len = 0;
329
memset (__mempcpy (new_str, __getopt_nonoption_flags,
330
nonoption_flags_max_len),
331
'\0', top + 1 - nonoption_flags_max_len);
332
nonoption_flags_max_len = top + 1;
333
__getopt_nonoption_flags = new_str;
324
memset (__mempcpy (new_str, __getopt_nonoption_flags,
325
nonoption_flags_max_len),
326
'\0', top + 1 - nonoption_flags_max_len);
327
nonoption_flags_max_len = top + 1;
328
__getopt_nonoption_flags = new_str;
338
333
while (top > middle && middle > bottom)
340
335
if (top - middle > middle - bottom)
342
/* Bottom segment is the short one. */
343
int len = middle - bottom;
337
/* Bottom segment is the short one. */
338
int len = middle - bottom;
346
/* Swap it with the top part of the top segment. */
347
for (i = 0; i < len; i++)
349
tem = argv[bottom + i];
350
argv[bottom + i] = argv[top - (middle - bottom) + i];
351
argv[top - (middle - bottom) + i] = tem;
352
SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
354
/* Exclude the moved bottom segment from further swapping. */
341
/* Swap it with the top part of the top segment. */
342
for (i = 0; i < len; i++)
344
tem = argv[bottom + i];
345
argv[bottom + i] = argv[top - (middle - bottom) + i];
346
argv[top - (middle - bottom) + i] = tem;
347
SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
349
/* Exclude the moved bottom segment from further swapping. */
359
/* Top segment is the short one. */
360
int len = top - middle;
354
/* Top segment is the short one. */
355
int len = top - middle;
363
/* Swap it with the bottom part of the bottom segment. */
364
for (i = 0; i < len; i++)
366
tem = argv[bottom + i];
367
argv[bottom + i] = argv[middle + i];
368
argv[middle + i] = tem;
369
SWAP_FLAGS (bottom + i, middle + i);
371
/* Exclude the moved top segment from further swapping. */
358
/* Swap it with the bottom part of the bottom segment. */
359
for (i = 0; i < len; i++)
361
tem = argv[bottom + i];
362
argv[bottom + i] = argv[middle + i];
363
argv[middle + i] = tem;
364
SWAP_FLAGS (bottom + i, middle + i);
366
/* Exclude the moved top segment from further swapping. */
376
371
/* Update records for the slots the non-options now occupy. */
422
417
&& argc == original_argc && argv == original_argv)
424
419
if (nonoption_flags_max_len == 0)
426
if (__getopt_nonoption_flags == NULL
427
|| __getopt_nonoption_flags[0] == '\0')
428
nonoption_flags_max_len = -1;
431
const char *orig_str = __getopt_nonoption_flags;
432
int len = nonoption_flags_max_len = strlen (orig_str);
433
if (nonoption_flags_max_len < argc)
434
nonoption_flags_max_len = argc;
435
__getopt_nonoption_flags =
436
(char *) malloc (nonoption_flags_max_len);
437
if (__getopt_nonoption_flags == NULL)
438
nonoption_flags_max_len = -1;
440
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
441
'\0', nonoption_flags_max_len - len);
421
if (__getopt_nonoption_flags == NULL
422
|| __getopt_nonoption_flags[0] == '\0')
423
nonoption_flags_max_len = -1;
426
const char *orig_str = __getopt_nonoption_flags;
427
int len = nonoption_flags_max_len = strlen (orig_str);
428
if (nonoption_flags_max_len < argc)
429
nonoption_flags_max_len = argc;
430
__getopt_nonoption_flags =
431
(char *) malloc (nonoption_flags_max_len);
432
if (__getopt_nonoption_flags == NULL)
433
nonoption_flags_max_len = -1;
435
memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
436
'\0', nonoption_flags_max_len - len);
444
439
nonoption_flags_len = nonoption_flags_max_len;
542
537
/* Advance to the next ARGV-element. */
544
539
/* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
545
moved back by the user (who may also have changed the arguments). */
540
moved back by the user (who may also have changed the arguments). */
546
541
if (last_nonopt > optind)
547
last_nonopt = optind;
542
last_nonopt = optind;
548
543
if (first_nonopt > optind)
549
first_nonopt = optind;
544
first_nonopt = optind;
551
546
if (ordering == PERMUTE)
553
/* If we have just processed some options following some non-options,
554
exchange them so that the options come first. */
556
if (first_nonopt != last_nonopt && last_nonopt != optind)
557
exchange ((char **) argv);
558
else if (last_nonopt != optind)
559
first_nonopt = optind;
561
/* Skip any additional non-options
562
and extend the range of non-options previously skipped. */
564
while (optind < argc && NONOPTION_P)
566
last_nonopt = optind;
548
/* If we have just processed some options following some non-options,
549
exchange them so that the options come first. */
551
if (first_nonopt != last_nonopt && last_nonopt != optind)
552
exchange ((char **) argv);
553
else if (last_nonopt != optind)
554
first_nonopt = optind;
556
/* Skip any additional non-options
557
and extend the range of non-options previously skipped. */
559
while (optind < argc && NONOPTION_P)
561
last_nonopt = optind;
569
564
/* The special ARGV-element `--' means premature end of options.
570
Skip it like a null option,
571
then exchange with previous non-options as if it were an option,
572
then skip everything else like a non-option. */
565
Skip it like a null option,
566
then exchange with previous non-options as if it were an option,
567
then skip everything else like a non-option. */
574
569
if (optind != argc && !strcmp (argv[optind], "--"))
578
if (first_nonopt != last_nonopt && last_nonopt != optind)
579
exchange ((char **) argv);
580
else if (first_nonopt == last_nonopt)
581
first_nonopt = optind;
573
if (first_nonopt != last_nonopt && last_nonopt != optind)
574
exchange ((char **) argv);
575
else if (first_nonopt == last_nonopt)
576
first_nonopt = optind;
587
582
/* If we have done all the ARGV-elements, stop the scan
588
and back over any non-options that we skipped and permuted. */
583
and back over any non-options that we skipped and permuted. */
590
585
if (optind == argc)
592
/* Set the next-arg-index to point at the non-options
593
that we previously skipped, so the caller will digest them. */
594
if (first_nonopt != last_nonopt)
595
optind = first_nonopt;
587
/* Set the next-arg-index to point at the non-options
588
that we previously skipped, so the caller will digest them. */
589
if (first_nonopt != last_nonopt)
590
optind = first_nonopt;
599
594
/* If we have come to a non-option and did not permute it,
600
either stop the scan or describe it to the caller and pass it by. */
595
either stop the scan or describe it to the caller and pass it by. */
604
if (ordering == REQUIRE_ORDER)
606
optarg = argv[optind++];
599
if (ordering == REQUIRE_ORDER)
601
optarg = argv[optind++];
610
605
/* We have found another option-ARGV-element.
611
Skip the initial punctuation. */
606
Skip the initial punctuation. */
613
608
nextchar = (argv[optind] + 1
614
+ (longopts != NULL && argv[optind][1] == '-'));
609
+ (longopts != NULL && argv[optind][1] == '-'));
617
612
/* Decode the current option-ARGV-element. */
642
637
int option_index;
644
639
for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
647
642
/* Test all long options for either exact match
648
or abbreviated matches. */
643
or abbreviated matches. */
649
644
for (p = longopts, option_index = 0; p->name; p++, option_index++)
650
if (!strncmp (p->name, nextchar, nameend - nextchar))
652
if ((unsigned int) (nameend - nextchar)
653
== (unsigned int) strlen (p->name))
655
/* Exact match found. */
657
indfound = option_index;
661
else if (pfound == NULL)
663
/* First nonexact match found. */
665
indfound = option_index;
668
/* Second or later nonexact match found. */
645
if (!strncmp (p->name, nextchar, nameend - nextchar))
647
if ((unsigned int) (nameend - nextchar)
648
== (unsigned int) strlen (p->name))
650
/* Exact match found. */
652
indfound = option_index;
656
else if (pfound == NULL)
658
/* First nonexact match found. */
660
indfound = option_index;
663
/* Second or later nonexact match found. */
672
667
if (ambig && !exact)
675
fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
676
argv[0], argv[optind]);
677
nextchar += strlen (nextchar);
670
fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
671
argv[0], argv[optind]);
672
nextchar += strlen (nextchar);
683
678
if (pfound != NULL)
685
option_index = indfound;
689
/* Don't test has_arg with >, because some C compilers don't
690
allow it to be used on enums. */
692
optarg = nameend + 1;
697
if (argv[optind - 1][1] == '-')
700
_("%s: option `--%s' doesn't allow an argument\n"),
701
argv[0], pfound->name);
703
/* +option or -option */
705
_("%s: option `%c%s' doesn't allow an argument\n"),
706
argv[0], argv[optind - 1][0], pfound->name);
709
nextchar += strlen (nextchar);
711
optopt = pfound->val;
715
else if (pfound->has_arg == 1)
718
optarg = argv[optind++];
723
_("%s: option `%s' requires an argument\n"),
724
argv[0], argv[optind - 1]);
725
nextchar += strlen (nextchar);
726
optopt = pfound->val;
727
return optstring[0] == ':' ? ':' : '?';
730
nextchar += strlen (nextchar);
732
*longind = option_index;
735
*(pfound->flag) = pfound->val;
680
option_index = indfound;
684
/* Don't test has_arg with >, because some C compilers don't
685
allow it to be used on enums. */
687
optarg = nameend + 1;
692
if (argv[optind - 1][1] == '-')
695
_("%s: option `--%s' doesn't allow an argument\n"),
696
argv[0], pfound->name);
698
/* +option or -option */
700
_("%s: option `%c%s' doesn't allow an argument\n"),
701
argv[0], argv[optind - 1][0], pfound->name);
704
nextchar += strlen (nextchar);
706
optopt = pfound->val;
710
else if (pfound->has_arg == 1)
713
optarg = argv[optind++];
718
_("%s: option `%s' requires an argument\n"),
719
argv[0], argv[optind - 1]);
720
nextchar += strlen (nextchar);
721
optopt = pfound->val;
722
return optstring[0] == ':' ? ':' : '?';
725
nextchar += strlen (nextchar);
727
*longind = option_index;
730
*(pfound->flag) = pfound->val;
741
736
/* Can't find it as a long option. If this is not getopt_long_only,
742
or the option starts with '--' or is not a valid short
743
option, then it's an error.
744
Otherwise interpret it as a short option. */
737
or the option starts with '--' or is not a valid short
738
option, then it's an error.
739
Otherwise interpret it as a short option. */
745
740
if (!long_only || argv[optind][1] == '-'
746
|| my_index (optstring, *nextchar) == NULL)
750
if (argv[optind][1] == '-')
752
fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
755
/* +option or -option */
756
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
757
argv[0], argv[optind][0], nextchar);
759
nextchar = (char *) "";
741
|| my_index (optstring, *nextchar) == NULL)
745
if (argv[optind][1] == '-')
747
fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
750
/* +option or -option */
751
fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
752
argv[0], argv[optind][0], nextchar);
754
nextchar = (char *) "";
766
761
/* Look at and handle the next short option-character. */
776
771
if (temp == NULL || c == ':')
781
/* 1003.2 specifies the format of this message. */
782
fprintf (stderr, _("%s: illegal option -- %c\n"),
785
fprintf (stderr, _("%s: invalid option -- %c\n"),
776
/* 1003.2 specifies the format of this message. */
777
fprintf (stderr, _("%s: illegal option -- %c\n"),
780
fprintf (stderr, _("%s: invalid option -- %c\n"),
791
786
/* Convenience. Treat POSIX -W foo same as long option --foo */
792
787
if (temp[0] == 'W' && temp[1] == ';')
795
const struct option *p;
796
const struct option *pfound = NULL;
802
/* This is an option that requires an argument. */
803
if (*nextchar != '\0')
806
/* If we end this ARGV-element by taking the rest as an arg,
807
we must advance to the next element now. */
810
else if (optind == argc)
814
/* 1003.2 specifies the format of this message. */
815
fprintf (stderr, _("%s: option requires an argument -- %c\n"),
819
if (optstring[0] == ':')
826
/* We already incremented `optind' once;
827
increment it again when taking next ARGV-elt as argument. */
828
optarg = argv[optind++];
830
/* optarg is now the argument, see if it's in the
831
table of longopts. */
833
for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
836
/* Test all long options for either exact match
837
or abbreviated matches. */
838
for (p = longopts, option_index = 0; p->name; p++, option_index++)
839
if (!strncmp (p->name, nextchar, nameend - nextchar))
841
if ((unsigned int) (nameend - nextchar) == strlen (p->name))
843
/* Exact match found. */
845
indfound = option_index;
849
else if (pfound == NULL)
851
/* First nonexact match found. */
853
indfound = option_index;
856
/* Second or later nonexact match found. */
862
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
863
argv[0], argv[optind]);
864
nextchar += strlen (nextchar);
870
option_index = indfound;
873
/* Don't test has_arg with >, because some C compilers don't
874
allow it to be used on enums. */
876
optarg = nameend + 1;
880
fprintf (stderr, _("\
790
const struct option *p;
791
const struct option *pfound = NULL;
797
/* This is an option that requires an argument. */
798
if (*nextchar != '\0')
801
/* If we end this ARGV-element by taking the rest as an arg,
802
we must advance to the next element now. */
805
else if (optind == argc)
809
/* 1003.2 specifies the format of this message. */
810
fprintf (stderr, _("%s: option requires an argument -- %c\n"),
814
if (optstring[0] == ':')
821
/* We already incremented `optind' once;
822
increment it again when taking next ARGV-elt as argument. */
823
optarg = argv[optind++];
825
/* optarg is now the argument, see if it's in the
826
table of longopts. */
828
for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
831
/* Test all long options for either exact match
832
or abbreviated matches. */
833
for (p = longopts, option_index = 0; p->name; p++, option_index++)
834
if (!strncmp (p->name, nextchar, nameend - nextchar))
836
if ((unsigned int) (nameend - nextchar) == strlen (p->name))
838
/* Exact match found. */
840
indfound = option_index;
844
else if (pfound == NULL)
846
/* First nonexact match found. */
848
indfound = option_index;
851
/* Second or later nonexact match found. */
857
fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
858
argv[0], argv[optind]);
859
nextchar += strlen (nextchar);
865
option_index = indfound;
868
/* Don't test has_arg with >, because some C compilers don't
869
allow it to be used on enums. */
871
optarg = nameend + 1;
875
fprintf (stderr, _("\
881
876
%s: option `-W %s' doesn't allow an argument\n"),
882
argv[0], pfound->name);
877
argv[0], pfound->name);
884
nextchar += strlen (nextchar);
888
else if (pfound->has_arg == 1)
891
optarg = argv[optind++];
896
_("%s: option `%s' requires an argument\n"),
897
argv[0], argv[optind - 1]);
898
nextchar += strlen (nextchar);
899
return optstring[0] == ':' ? ':' : '?';
902
nextchar += strlen (nextchar);
904
*longind = option_index;
907
*(pfound->flag) = pfound->val;
913
return 'W'; /* Let the application handle it. */
879
nextchar += strlen (nextchar);
883
else if (pfound->has_arg == 1)
886
optarg = argv[optind++];
891
_("%s: option `%s' requires an argument\n"),
892
argv[0], argv[optind - 1]);
893
nextchar += strlen (nextchar);
894
return optstring[0] == ':' ? ':' : '?';
897
nextchar += strlen (nextchar);
899
*longind = option_index;
902
*(pfound->flag) = pfound->val;
908
return 'W'; /* Let the application handle it. */
915
910
if (temp[1] == ':')
919
/* This is an option that accepts an argument optionally. */
920
if (*nextchar != '\0')
931
/* This is an option that requires an argument. */
932
if (*nextchar != '\0')
935
/* If we end this ARGV-element by taking the rest as an arg,
936
we must advance to the next element now. */
939
else if (optind == argc)
943
/* 1003.2 specifies the format of this message. */
945
_("%s: option requires an argument -- %c\n"),
949
if (optstring[0] == ':')
955
/* We already incremented `optind' once;
956
increment it again when taking next ARGV-elt as argument. */
957
optarg = argv[optind++];
914
/* This is an option that accepts an argument optionally. */
915
if (*nextchar != '\0')
926
/* This is an option that requires an argument. */
927
if (*nextchar != '\0')
930
/* If we end this ARGV-element by taking the rest as an arg,
931
we must advance to the next element now. */
934
else if (optind == argc)
938
/* 1003.2 specifies the format of this message. */
940
_("%s: option requires an argument -- %c\n"),
944
if (optstring[0] == ':')
950
/* We already incremented `optind' once;
951
increment it again when taking next ARGV-elt as argument. */
952
optarg = argv[optind++];
1023
1018
c = getopt (argc, argv, "abc:d:0123456789");
1039
if (digit_optind != 0 && digit_optind != this_option_optind)
1040
printf ("digits occur in two different argv-elements.\n");
1041
digit_optind = this_option_optind;
1042
printf ("option %c\n", c);
1046
printf ("option a\n");
1050
printf ("option b\n");
1054
printf ("option c with value `%s'\n", optarg);
1061
printf ("?? getopt returned character code 0%o ??\n", c);
1034
if (digit_optind != 0 && digit_optind != this_option_optind)
1035
printf ("digits occur in two different argv-elements.\n");
1036
digit_optind = this_option_optind;
1037
printf ("option %c\n", c);
1041
printf ("option a\n");
1045
printf ("option b\n");
1049
printf ("option c with value `%s'\n", optarg);
1056
printf ("?? getopt returned character code 0%o ??\n", c);
1065
1060
if (optind < argc)
1067
1062
printf ("non-option ARGV-elements: ");
1068
1063
while (optind < argc)
1069
printf ("%s ", argv[optind++]);
1064
printf ("%s ", argv[optind++]);