516{
517 int print_errors =
opterr;
518 if (optstring[0] == ':')
519 print_errors = 0;
520
521 if (argc < 1)
522 return -1;
523
525
527 {
532 }
533
534
535
536
537
538#if defined _LIBC && defined USE_NONOPTION_FLAGS
539# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
540 || (optind < nonoption_flags_len \
541 && __getopt_nonoption_flags[optind] == '1'))
542#else
543# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
544#endif
545
547 {
548
549
550
551
556
558 {
559
560
561
566
567
568
569
573 }
574
575
576
577
578
579
581 {
583
589
591 }
592
593
594
595
597 {
598
599
602 return -1;
603 }
604
605
606
607
609 {
611 return -1;
613 return 1;
614 }
615
616
617
618
620 + (longopts != NULL && argv[
optind][1] ==
'-'));
621 }
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638 if (longopts != NULL
639 && (argv[
optind][1] ==
'-'
641 {
642 char *nameend;
644 const struct option *pfound = NULL;
645 int exact = 0;
646 int ambig = 0;
647 int indfound = -1;
648 int option_index;
649
650 for (nameend =
nextchar; *nameend && *nameend !=
'='; nameend++)
651 ;
652
653
654
655 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
657 {
658 if ((
unsigned int) (nameend -
nextchar)
659 == (
unsigned int) strlen (p->
name))
660 {
661
662 pfound = p;
663 indfound = option_index;
664 exact = 1;
665 break;
666 }
667 else if (pfound == NULL)
668 {
669
670 pfound = p;
671 indfound = option_index;
672 }
673 else if (long_only
677
678 ambig = 1;
679 }
680
681 if (ambig && !exact)
682 {
683 if (print_errors)
684 {
685#if defined _LIBC && defined USE_IN_LIBIO
686 char *buf;
687
688 __asprintf (&buf,
_(
"%s: option `%s' is ambiguous\n"),
690
691 if (_IO_fwide (stderr, 0) > 0)
692 __fwprintf (stderr,
L"%s", buf);
693 else
694 fputs (buf, stderr);
695
697#else
698 fprintf (stderr,
_(
"%s: option `%s' is ambiguous\n"),
700#endif
701 }
705 return '?';
706 }
707
708 if (pfound != NULL)
709 {
710 option_index = indfound;
712 if (*nameend)
713 {
714
715
718 else
719 {
720 if (print_errors)
721 {
722#if defined _LIBC && defined USE_IN_LIBIO
723 char *buf;
724#endif
725
726 if (argv[
optind - 1][1] ==
'-')
727 {
728
729#if defined _LIBC && defined USE_IN_LIBIO
730 __asprintf (&buf,
_(
"\
731%s: option `--%s' doesn't allow an argument\n"),
732 argv[0], pfound->
name);
733#else
734 fprintf (stderr,
_(
"\
735%s: option `--%s' doesn't allow an argument\n"),
736 argv[0], pfound->
name);
737#endif
738 }
739 else
740 {
741
742#if defined _LIBC && defined USE_IN_LIBIO
743 __asprintf (&buf,
_(
"\
744%s: option `%c%s' doesn't allow an argument\n"),
745 argv[0], argv[
optind - 1][0],
747#else
748 fprintf (stderr,
_(
"\
749%s: option `%c%s' doesn't allow an argument\n"),
751#endif
752 }
753
754#if defined _LIBC && defined USE_IN_LIBIO
755 if (_IO_fwide (stderr, 0) > 0)
756 __fwprintf (stderr,
L"%s", buf);
757 else
758 fputs (buf, stderr);
759
761#endif
762 }
763
765
767 return '?';
768 }
769 }
771 {
774 else
775 {
776 if (print_errors)
777 {
778#if defined _LIBC && defined USE_IN_LIBIO
779 char *buf;
780
781 __asprintf (&buf,
782 _(
"%s: option `%s' requires an argument\n"),
783 argv[0], argv[
optind - 1]);
784
785 if (_IO_fwide (stderr, 0) > 0)
786 __fwprintf (stderr,
L"%s", buf);
787 else
788 fputs (buf, stderr);
789
791#else
792 fprintf (stderr,
793 _(
"%s: option `%s' requires an argument\n"),
794 argv[0], argv[
optind - 1]);
795#endif
796 }
799 return optstring[0] == ':' ? ':' : '?';
800 }
801 }
803 if (longind != NULL)
804 *longind = option_index;
806 {
808 return 0;
809 }
811 }
812
813
814
815
816
817 if (!long_only || argv[
optind][1] ==
'-'
819 {
820 if (print_errors)
821 {
822#if defined _LIBC && defined USE_IN_LIBIO
823 char *buf;
824#endif
825
826 if (argv[
optind][1] ==
'-')
827 {
828
829#if defined _LIBC && defined USE_IN_LIBIO
830 __asprintf (&buf,
_(
"%s: unrecognized option `--%s'\n"),
832#else
833 fprintf (stderr,
_(
"%s: unrecognized option `--%s'\n"),
835#endif
836 }
837 else
838 {
839
840#if defined _LIBC && defined USE_IN_LIBIO
841 __asprintf (&buf,
_(
"%s: unrecognized option `%c%s'\n"),
843#else
844 fprintf (stderr,
_(
"%s: unrecognized option `%c%s'\n"),
846#endif
847 }
848
849#if defined _LIBC && defined USE_IN_LIBIO
850 if (_IO_fwide (stderr, 0) > 0)
851 __fwprintf (stderr,
L"%s", buf);
852 else
853 fputs (buf, stderr);
854
856#endif
857 }
861 return '?';
862 }
863 }
864
865
866
867 {
869 char *temp =
my_index (optstring, c);
870
871
874
875 if (temp == NULL || c == ':')
876 {
877 if (print_errors)
878 {
879#if defined _LIBC && defined USE_IN_LIBIO
880 char *buf;
881#endif
882
884 {
885
886#if defined _LIBC && defined USE_IN_LIBIO
887 __asprintf (&buf,
_(
"%s: illegal option -- %c\n"),
888 argv[0], c);
889#else
890 fprintf (stderr,
_(
"%s: illegal option -- %c\n"), argv[0], c);
891#endif
892 }
893 else
894 {
895#if defined _LIBC && defined USE_IN_LIBIO
896 __asprintf (&buf,
_(
"%s: invalid option -- %c\n"),
897 argv[0], c);
898#else
899 fprintf (stderr,
_(
"%s: invalid option -- %c\n"), argv[0], c);
900#endif
901 }
902
903#if defined _LIBC && defined USE_IN_LIBIO
904 if (_IO_fwide (stderr, 0) > 0)
905 __fwprintf (stderr,
L"%s", buf);
906 else
907 fputs (buf, stderr);
908
910#endif
911 }
913 return '?';
914 }
915
916 if (temp[0] == 'W' && temp[1] == ';')
917 {
918 char *nameend;
920 const struct option *pfound = NULL;
921 int exact = 0;
922 int ambig = 0;
923 int indfound = 0;
924 int option_index;
925
926
928 {
930
931
933 }
935 {
936 if (print_errors)
937 {
938
939#if defined _LIBC && defined USE_IN_LIBIO
940 char *buf;
941
942 __asprintf (&buf,
_(
"%s: option requires an argument -- %c\n"),
943 argv[0], c);
944
945 if (_IO_fwide (stderr, 0) > 0)
946 __fwprintf (stderr,
L"%s", buf);
947 else
948 fputs (buf, stderr);
949
951#else
952 fprintf (stderr,
_(
"%s: option requires an argument -- %c\n"),
953 argv[0], c);
954#endif
955 }
957 if (optstring[0] == ':')
959 else
962 }
963 else
964
965
967
968
969
970
971 for (
nextchar = nameend =
optarg; *nameend && *nameend !=
'='; nameend++)
972 ;
973
974
975
976 for (p = longopts, option_index = 0; p->
name; p++, option_index++)
978 {
979 if ((
unsigned int) (nameend -
nextchar) == strlen (p->
name))
980 {
981
982 pfound = p;
983 indfound = option_index;
984 exact = 1;
985 break;
986 }
987 else if (pfound == NULL)
988 {
989
990 pfound = p;
991 indfound = option_index;
992 }
993 else
994
995 ambig = 1;
996 }
997 if (ambig && !exact)
998 {
999 if (print_errors)
1000 {
1001#if defined _LIBC && defined USE_IN_LIBIO
1002 char *buf;
1003
1004 __asprintf (&buf,
_(
"%s: option `-W %s' is ambiguous\n"),
1006
1007 if (_IO_fwide (stderr, 0) > 0)
1008 __fwprintf (stderr,
L"%s", buf);
1009 else
1010 fputs (buf, stderr);
1011
1013#else
1014 fprintf (stderr,
_(
"%s: option `-W %s' is ambiguous\n"),
1016#endif
1017 }
1020 return '?';
1021 }
1022 if (pfound != NULL)
1023 {
1024 option_index = indfound;
1025 if (*nameend)
1026 {
1027
1028
1031 else
1032 {
1033 if (print_errors)
1034 {
1035#if defined _LIBC && defined USE_IN_LIBIO
1036 char *buf;
1037
1038 __asprintf (&buf,
_(
"\
1039%s: option `-W %s' doesn't allow an argument\n"),
1040 argv[0], pfound->
name);
1041
1042 if (_IO_fwide (stderr, 0) > 0)
1043 __fwprintf (stderr,
L"%s", buf);
1044 else
1045 fputs (buf, stderr);
1046
1048#else
1049 fprintf (stderr,
_(
"\
1050%s: option `-W %s' doesn't allow an argument\n"),
1051 argv[0], pfound->
name);
1052#endif
1053 }
1054
1056 return '?';
1057 }
1058 }
1059 else if (pfound->
has_arg == 1)
1060 {
1063 else
1064 {
1065 if (print_errors)
1066 {
1067#if defined _LIBC && defined USE_IN_LIBIO
1068 char *buf;
1069
1070 __asprintf (&buf,
_(
"\
1071%s: option `%s' requires an argument\n"),
1072 argv[0], argv[
optind - 1]);
1073
1074 if (_IO_fwide (stderr, 0) > 0)
1075 __fwprintf (stderr,
L"%s", buf);
1076 else
1077 fputs (buf, stderr);
1078
1080#else
1081 fprintf (stderr,
1082 _(
"%s: option `%s' requires an argument\n"),
1083 argv[0], argv[
optind - 1]);
1084#endif
1085 }
1087 return optstring[0] == ':' ? ':' : '?';
1088 }
1089 }
1091 if (longind != NULL)
1092 *longind = option_index;
1094 {
1095 *(pfound->
flag) = pfound->
val;
1096 return 0;
1097 }
1099 }
1101 return 'W';
1102 }
1103 if (temp[1] == ':')
1104 {
1105 if (temp[2] == ':')
1106 {
1107
1109 {
1112 }
1113 else
1116 }
1117 else
1118 {
1119
1121 {
1123
1124
1126 }
1128 {
1129 if (print_errors)
1130 {
1131
1132#if defined _LIBC && defined USE_IN_LIBIO
1133 char *buf;
1134
1135 __asprintf (&buf,
1136 _(
"%s: option requires an argument -- %c\n"),
1137 argv[0], c);
1138
1139 if (_IO_fwide (stderr, 0) > 0)
1140 __fwprintf (stderr,
L"%s", buf);
1141 else
1142 fputs (buf, stderr);
1143
1145#else
1146 fprintf (stderr,
1147 _(
"%s: option requires an argument -- %c\n"),
1148 argv[0], c);
1149#endif
1150 }
1152 if (optstring[0] == ':')
1154 else
1156 }
1157 else
1158
1159
1162 }
1163 }
1165 }
1166}
int __getopt_initialized
Definition getopt.c:137
static void exchange(char **argv)
Definition getopt.c:302
int optopt
Definition getopt.c:157
static const char * _getopt_initialize(int argc, char *const *argv, const char *optstring)
Definition getopt.c:387
#define _(msgid)
Definition getopt.c:87
static char * my_index(char *str, int chr) const
Definition getopt.c:219
char * optarg
Definition getopt.c:116
int opterr
Definition getopt.c:151
int val
Definition getopt.h:112
int has_arg
Definition getopt.h:110
char * name
Definition getopt.h:106
int * flag
Definition getopt.h:111
#define L(s)
Definition I18N.hpp:18