Rizin
unix-like reverse engineering framework and cli tools
getopt.c File Reference
#include <config.h>
#include "getopt.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "getopt_int.h"

Go to the source code of this file.

Macros

#define _(msgid)   (msgid)
 
#define attribute_hidden
 
#define SWAP_FLAGS(ch1, ch2)
 
#define NONOPTION_P   (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
 

Enumerations

enum  { POSIXLY_CORRECT = 1 }
 

Functions

static void exchange (char **argv, struct _getopt_data *d)
 
static const char * _getopt_initialize (int argc, char **argv, const char *optstring, int posixly_correct, struct _getopt_data *d)
 
int _getopt_internal_r (int argc, char **argv, const char *optstring, const struct option *longopts, int *longind, int long_only, int posixly_correct, struct _getopt_data *d)
 
int _getopt_internal (int argc, char **argv, const char *optstring, const struct option *longopts, int *longind, int long_only, int posixly_correct)
 
int getopt (int argc, char *const *argv, const char *optstring)
 

Variables

char * optarg
 
int optind = 1
 
int opterr = 1
 
int optopt = '?'
 
static struct _getopt_data getopt_data
 

Macro Definition Documentation

◆ _

#define _ (   msgid)    (msgid)

Definition at line 49 of file getopt.c.

◆ attribute_hidden

#define attribute_hidden

Definition at line 56 of file getopt.c.

◆ NONOPTION_P

#define NONOPTION_P   (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')

◆ SWAP_FLAGS

#define SWAP_FLAGS (   ch1,
  ch2 
)

Definition at line 145 of file getopt.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
POSIXLY_CORRECT 

Definition at line 1118 of file getopt.c.

1118 { POSIXLY_CORRECT = 1 };
@ POSIXLY_CORRECT
Definition: getopt.c:1118

Function Documentation

◆ _getopt_initialize()

static const char* _getopt_initialize ( int  argc,
char **  argv,
const char *  optstring,
int  posixly_correct,
struct _getopt_data d 
)
static

Definition at line 239 of file getopt.c.

241 {
242  /* Start processing options with ARGV-element 1 (since ARGV-element 0
243  is the program name); the sequence of previously skipped
244  non-option ARGV-elements is empty. */
245 
246  d->__first_nonopt = d->__last_nonopt = d->optind;
247 
248  d->__nextchar = NULL;
249 
250  d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
251 
252  /* Determine how to handle the ordering of options and nonoptions. */
253 
254  if (optstring[0] == '-')
255  {
256  d->__ordering = RETURN_IN_ORDER;
257  ++optstring;
258  }
259  else if (optstring[0] == '+')
260  {
261  d->__ordering = REQUIRE_ORDER;
262  ++optstring;
263  }
264  else if (d->__posixly_correct)
265  d->__ordering = REQUIRE_ORDER;
266  else
267  d->__ordering = PERMUTE;
268 
269 #if defined _LIBC && defined USE_NONOPTION_FLAGS
270  if (!d->__posixly_correct
271  && argc == __libc_argc && argv == __libc_argv)
272  {
273  if (d->__nonoption_flags_max_len == 0)
274  {
275  if (__getopt_nonoption_flags == NULL
276  || __getopt_nonoption_flags[0] == '\0')
277  d->__nonoption_flags_max_len = -1;
278  else
279  {
280  const char *orig_str = __getopt_nonoption_flags;
281  int len = d->__nonoption_flags_max_len = strlen (orig_str);
282  if (d->__nonoption_flags_max_len < argc)
283  d->__nonoption_flags_max_len = argc;
284  __getopt_nonoption_flags =
285  (char *) malloc (d->__nonoption_flags_max_len);
286  if (__getopt_nonoption_flags == NULL)
287  d->__nonoption_flags_max_len = -1;
288  else
289  memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
290  '\0', d->__nonoption_flags_max_len - len);
291  }
292  }
293  d->__nonoption_flags_len = d->__nonoption_flags_max_len;
294  }
295  else
296  d->__nonoption_flags_len = 0;
297 #endif
298 
299  return optstring;
300 }
size_t len
Definition: 6502dis.c:15
#define NULL
Definition: cris-opc.c:27
return memset(p, 0, total)
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
#define d(i)
Definition: sha256.c:44
static char * posixly_correct
Definition: getopt.c:190
char * getenv()
@ REQUIRE_ORDER
Definition: getopt.c:186
@ RETURN_IN_ORDER
Definition: getopt.c:186
@ PERMUTE
Definition: getopt.c:186

References argv, d, getenv(), len, malloc(), memset(), NULL, PERMUTE, posixly_correct, REQUIRE_ORDER, and RETURN_IN_ORDER.

Referenced by _getopt_internal_r().

◆ _getopt_internal()

int _getopt_internal ( int  argc,
char **  argv,
const char *  optstring,
const struct option longopts,
int longind,
int  long_only,
int  posixly_correct 
)

Definition at line 1094 of file getopt.c.

1097 {
1098  int result;
1099 
1102 
1103  result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1104  long_only, posixly_correct, &getopt_data);
1105 
1109 
1110  return result;
1111 }
char * optarg
Definition: getopt_int.h:41
int optopt
Definition: getopt.c:153
int optind
Definition: getopt.c:127
char * optarg
Definition: getopt.c:112
int opterr
Definition: getopt.c:147
int _getopt_internal_r(int argc, char **argv, const char *optstring, const struct option *longopts, int *longind, int long_only, int posixly_correct, struct _getopt_data *d)
Definition: getopt.c:358
static struct _getopt_data getopt_data
Definition: getopt.c:111

References _getopt_internal_r(), argv, getopt_data, optarg, _getopt_data::optarg, opterr, _getopt_data::opterr, optind, _getopt_data::optind, optopt, _getopt_data::optopt, and posixly_correct.

◆ _getopt_internal_r()

int _getopt_internal_r ( int  argc,
char **  argv,
const char *  optstring,
const struct option longopts,
int longind,
int  long_only,
int  posixly_correct,
struct _getopt_data d 
)

Definition at line 358 of file getopt.c.

361 {
362  int print_errors = d->opterr;
363  if (optstring[0] == ':')
364  print_errors = 0;
365 
366  if (argc < 1)
367  return -1;
368 
369  d->optarg = NULL;
370 
371  if (d->optind == 0 || !d->__initialized)
372  {
373  if (d->optind == 0)
374  d->optind = 1; /* Don't scan ARGV[0], the program name. */
375  optstring = _getopt_initialize (argc, argv, optstring,
376  posixly_correct, d);
377  d->__initialized = 1;
378  }
379 
380  /* Test whether ARGV[optind] points to a non-option argument.
381  Either it does not have option syntax, or there is an environment flag
382  from the shell indicating it is not an option. The later information
383  is only used when the used in the GNU libc. */
384 #if defined _LIBC && defined USE_NONOPTION_FLAGS
385 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0' \
386  || (d->optind < d->__nonoption_flags_len \
387  && __getopt_nonoption_flags[d->optind] == '1'))
388 #else
389 # define NONOPTION_P (argv[d->optind][0] != '-' || argv[d->optind][1] == '\0')
390 #endif
391 
392  if (d->__nextchar == NULL || *d->__nextchar == '\0')
393  {
394  /* Advance to the next ARGV-element. */
395 
396  /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
397  moved back by the user (who may also have changed the arguments). */
398  if (d->__last_nonopt > d->optind)
399  d->__last_nonopt = d->optind;
400  if (d->__first_nonopt > d->optind)
401  d->__first_nonopt = d->optind;
402 
403  if (d->__ordering == PERMUTE)
404  {
405  /* If we have just processed some options following some non-options,
406  exchange them so that the options come first. */
407 
408  if (d->__first_nonopt != d->__last_nonopt
409  && d->__last_nonopt != d->optind)
410  exchange ((char **) argv, d);
411  else if (d->__last_nonopt != d->optind)
412  d->__first_nonopt = d->optind;
413 
414  /* Skip any additional non-options
415  and extend the range of non-options previously skipped. */
416 
417  while (d->optind < argc && NONOPTION_P)
418  d->optind++;
419  d->__last_nonopt = d->optind;
420  }
421 
422  /* The special ARGV-element `--' means premature end of options.
423  Skip it like a null option,
424  then exchange with previous non-options as if it were an option,
425  then skip everything else like a non-option. */
426 
427  if (d->optind != argc && !strcmp (argv[d->optind], "--"))
428  {
429  d->optind++;
430 
431  if (d->__first_nonopt != d->__last_nonopt
432  && d->__last_nonopt != d->optind)
433  exchange ((char **) argv, d);
434  else if (d->__first_nonopt == d->__last_nonopt)
435  d->__first_nonopt = d->optind;
436  d->__last_nonopt = argc;
437 
438  d->optind = argc;
439  }
440 
441  /* If we have done all the ARGV-elements, stop the scan
442  and back over any non-options that we skipped and permuted. */
443 
444  if (d->optind == argc)
445  {
446  /* Set the next-arg-index to point at the non-options
447  that we previously skipped, so the caller will digest them. */
448  if (d->__first_nonopt != d->__last_nonopt)
449  d->optind = d->__first_nonopt;
450  return -1;
451  }
452 
453  /* If we have come to a non-option and did not permute it,
454  either stop the scan or describe it to the caller and pass it by. */
455 
456  if (NONOPTION_P)
457  {
458  if (d->__ordering == REQUIRE_ORDER)
459  return -1;
460  d->optarg = argv[d->optind++];
461  return 1;
462  }
463 
464  /* We have found another option-ARGV-element.
465  Skip the initial punctuation. */
466 
467  d->__nextchar = (argv[d->optind] + 1
468  + (longopts != NULL && argv[d->optind][1] == '-'));
469  }
470 
471  /* Decode the current option-ARGV-element. */
472 
473  /* Check whether the ARGV-element is a long option.
474 
475  If long_only and the ARGV-element has the form "-f", where f is
476  a valid short option, don't consider it an abbreviated form of
477  a long option that starts with f. Otherwise there would be no
478  way to give the -f short option.
479 
480  On the other hand, if there's a long option "fubar" and
481  the ARGV-element is "-fu", do consider that an abbreviation of
482  the long option, just like "--fu", and not "-f" with arg "u".
483 
484  This distinction seems to be the most useful approach. */
485 
486  if (longopts != NULL
487  && (argv[d->optind][1] == '-'
488  || (long_only && (argv[d->optind][2]
489  || !strchr (optstring, argv[d->optind][1])))))
490  {
491  char *nameend;
492  const struct option *p;
493  const struct option *pfound = NULL;
494  int exact = 0;
495  int ambig = 0;
496  int indfound = -1;
497  int option_index;
498 
499  for (nameend = d->__nextchar; *nameend && *nameend != '='; nameend++)
500  /* Do nothing. */ ;
501 
502  /* Test all long options for either exact match
503  or abbreviated matches. */
504  for (p = longopts, option_index = 0; p->name; p++, option_index++)
505  if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
506  {
507  if ((unsigned int) (nameend - d->__nextchar)
508  == (unsigned int) strlen (p->name))
509  {
510  /* Exact match found. */
511  pfound = p;
512  indfound = option_index;
513  exact = 1;
514  break;
515  }
516  else if (pfound == NULL)
517  {
518  /* First nonexact match found. */
519  pfound = p;
520  indfound = option_index;
521  }
522  else if (long_only
523  || pfound->has_arg != p->has_arg
524  || pfound->flag != p->flag
525  || pfound->val != p->val)
526  /* Second or later nonexact match found. */
527  ambig = 1;
528  }
529 
530  if (ambig && !exact)
531  {
532  if (print_errors)
533  {
534 #if defined _LIBC && defined USE_IN_LIBIO
535  char *buf;
536 
537  if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
538  argv[0], argv[d->optind]) >= 0)
539  {
540  _IO_flockfile (stderr);
541 
542  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
543  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
544 
545  __fxprintf (NULL, "%s", buf);
546 
547  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
548  _IO_funlockfile (stderr);
549 
550  free (buf);
551  }
552 #else
553  fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
554  argv[0], argv[d->optind]);
555 #endif
556  }
557  d->__nextchar += strlen (d->__nextchar);
558  d->optind++;
559  d->optopt = 0;
560  return '?';
561  }
562 
563  if (pfound != NULL)
564  {
565  option_index = indfound;
566  d->optind++;
567  if (*nameend)
568  {
569  /* Don't test has_arg with >, because some C compilers don't
570  allow it to be used on enums. */
571  if (pfound->has_arg)
572  d->optarg = nameend + 1;
573  else
574  {
575  if (print_errors)
576  {
577 #if defined _LIBC && defined USE_IN_LIBIO
578  char *buf;
579  int n;
580 #endif
581 
582  if (argv[d->optind - 1][1] == '-')
583  {
584  /* --option */
585 #if defined _LIBC && defined USE_IN_LIBIO
586  n = __asprintf (&buf, _("\
587 %s: option `--%s' doesn't allow an argument\n"),
588  argv[0], pfound->name);
589 #else
590  fprintf (stderr, _("\
591 %s: option `--%s' doesn't allow an argument\n"),
592  argv[0], pfound->name);
593 #endif
594  }
595  else
596  {
597  /* +option or -option */
598 #if defined _LIBC && defined USE_IN_LIBIO
599  n = __asprintf (&buf, _("\
600 %s: option `%c%s' doesn't allow an argument\n"),
601  argv[0], argv[d->optind - 1][0],
602  pfound->name);
603 #else
604  fprintf (stderr, _("\
605 %s: option `%c%s' doesn't allow an argument\n"),
606  argv[0], argv[d->optind - 1][0],
607  pfound->name);
608 #endif
609  }
610 
611 #if defined _LIBC && defined USE_IN_LIBIO
612  if (n >= 0)
613  {
614  _IO_flockfile (stderr);
615 
616  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
617  ((_IO_FILE *) stderr)->_flags2
618  |= _IO_FLAGS2_NOTCANCEL;
619 
620  __fxprintf (NULL, "%s", buf);
621 
622  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
623  _IO_funlockfile (stderr);
624 
625  free (buf);
626  }
627 #endif
628  }
629 
630  d->__nextchar += strlen (d->__nextchar);
631 
632  d->optopt = pfound->val;
633  return '?';
634  }
635  }
636  else if (pfound->has_arg == 1)
637  {
638  if (d->optind < argc)
639  d->optarg = argv[d->optind++];
640  else
641  {
642  if (print_errors)
643  {
644 #if defined _LIBC && defined USE_IN_LIBIO
645  char *buf;
646 
647  if (__asprintf (&buf, _("\
648 %s: option `%s' requires an argument\n"),
649  argv[0], argv[d->optind - 1]) >= 0)
650  {
651  _IO_flockfile (stderr);
652 
653  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
654  ((_IO_FILE *) stderr)->_flags2
655  |= _IO_FLAGS2_NOTCANCEL;
656 
657  __fxprintf (NULL, "%s", buf);
658 
659  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
660  _IO_funlockfile (stderr);
661 
662  free (buf);
663  }
664 #else
665  fprintf (stderr,
666  _("%s: option `%s' requires an argument\n"),
667  argv[0], argv[d->optind - 1]);
668 #endif
669  }
670  d->__nextchar += strlen (d->__nextchar);
671  d->optopt = pfound->val;
672  return optstring[0] == ':' ? ':' : '?';
673  }
674  }
675  d->__nextchar += strlen (d->__nextchar);
676  if (longind != NULL)
677  *longind = option_index;
678  if (pfound->flag)
679  {
680  *(pfound->flag) = pfound->val;
681  return 0;
682  }
683  return pfound->val;
684  }
685 
686  /* Can't find it as a long option. If this is not getopt_long_only,
687  or the option starts with '--' or is not a valid short
688  option, then it's an error.
689  Otherwise interpret it as a short option. */
690  if (!long_only || argv[d->optind][1] == '-'
691  || strchr (optstring, *d->__nextchar) == NULL)
692  {
693  if (print_errors)
694  {
695 #if defined _LIBC && defined USE_IN_LIBIO
696  char *buf;
697  int n;
698 #endif
699 
700  if (argv[d->optind][1] == '-')
701  {
702  /* --option */
703 #if defined _LIBC && defined USE_IN_LIBIO
704  n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
705  argv[0], d->__nextchar);
706 #else
707  fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
708  argv[0], d->__nextchar);
709 #endif
710  }
711  else
712  {
713  /* +option or -option */
714 #if defined _LIBC && defined USE_IN_LIBIO
715  n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
716  argv[0], argv[d->optind][0], d->__nextchar);
717 #else
718  fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
719  argv[0], argv[d->optind][0], d->__nextchar);
720 #endif
721  }
722 
723 #if defined _LIBC && defined USE_IN_LIBIO
724  if (n >= 0)
725  {
726  _IO_flockfile (stderr);
727 
728  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
729  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
730 
731  __fxprintf (NULL, "%s", buf);
732 
733  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
734  _IO_funlockfile (stderr);
735 
736  free (buf);
737  }
738 #endif
739  }
740  d->__nextchar = (char *) "";
741  d->optind++;
742  d->optopt = 0;
743  return '?';
744  }
745  }
746 
747  /* Look at and handle the next short option-character. */
748 
749  {
750  char c = *d->__nextchar++;
751  char *temp = strchr (optstring, c);
752 
753  /* Increment `optind' when we start to process its last character. */
754  if (*d->__nextchar == '\0')
755  ++d->optind;
756 
757  if (temp == NULL || c == ':')
758  {
759  if (print_errors)
760  {
761 #if defined _LIBC && defined USE_IN_LIBIO
762  char *buf;
763  int n;
764 #endif
765 
766  if (d->__posixly_correct)
767  {
768  /* 1003.2 specifies the format of this message. */
769 #if defined _LIBC && defined USE_IN_LIBIO
770  n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
771  argv[0], c);
772 #else
773  fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
774 #endif
775  }
776  else
777  {
778 #if defined _LIBC && defined USE_IN_LIBIO
779  n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
780  argv[0], c);
781 #else
782  fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
783 #endif
784  }
785 
786 #if defined _LIBC && defined USE_IN_LIBIO
787  if (n >= 0)
788  {
789  _IO_flockfile (stderr);
790 
791  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
792  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
793 
794  __fxprintf (NULL, "%s", buf);
795 
796  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
797  _IO_funlockfile (stderr);
798 
799  free (buf);
800  }
801 #endif
802  }
803  d->optopt = c;
804  return '?';
805  }
806  /* Convenience. Treat POSIX -W foo same as long option --foo */
807  if (temp[0] == 'W' && temp[1] == ';')
808  {
809  char *nameend;
810  const struct option *p;
811  const struct option *pfound = NULL;
812  int exact = 0;
813  int ambig = 0;
814  int indfound = 0;
815  int option_index;
816 
817  /* This is an option that requires an argument. */
818  if (*d->__nextchar != '\0')
819  {
820  d->optarg = d->__nextchar;
821  /* If we end this ARGV-element by taking the rest as an arg,
822  we must advance to the next element now. */
823  d->optind++;
824  }
825  else if (d->optind == argc)
826  {
827  if (print_errors)
828  {
829  /* 1003.2 specifies the format of this message. */
830 #if defined _LIBC && defined USE_IN_LIBIO
831  char *buf;
832 
833  if (__asprintf (&buf,
834  _("%s: option requires an argument -- %c\n"),
835  argv[0], c) >= 0)
836  {
837  _IO_flockfile (stderr);
838 
839  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
840  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
841 
842  __fxprintf (NULL, "%s", buf);
843 
844  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
845  _IO_funlockfile (stderr);
846 
847  free (buf);
848  }
849 #else
850  fprintf (stderr, _("%s: option requires an argument -- %c\n"),
851  argv[0], c);
852 #endif
853  }
854  d->optopt = c;
855  if (optstring[0] == ':')
856  c = ':';
857  else
858  c = '?';
859  return c;
860  }
861  else
862  /* We already incremented `d->optind' once;
863  increment it again when taking next ARGV-elt as argument. */
864  d->optarg = argv[d->optind++];
865 
866  /* optarg is now the argument, see if it's in the
867  table of longopts. */
868 
869  for (d->__nextchar = nameend = d->optarg; *nameend && *nameend != '=';
870  nameend++)
871  /* Do nothing. */ ;
872 
873  /* Test all long options for either exact match
874  or abbreviated matches. */
875  for (p = longopts, option_index = 0; p->name; p++, option_index++)
876  if (!strncmp (p->name, d->__nextchar, nameend - d->__nextchar))
877  {
878  if ((unsigned int) (nameend - d->__nextchar) == strlen (p->name))
879  {
880  /* Exact match found. */
881  pfound = p;
882  indfound = option_index;
883  exact = 1;
884  break;
885  }
886  else if (pfound == NULL)
887  {
888  /* First nonexact match found. */
889  pfound = p;
890  indfound = option_index;
891  }
892  else
893  /* Second or later nonexact match found. */
894  ambig = 1;
895  }
896  if (ambig && !exact)
897  {
898  if (print_errors)
899  {
900 #if defined _LIBC && defined USE_IN_LIBIO
901  char *buf;
902 
903  if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
904  argv[0], argv[d->optind]) >= 0)
905  {
906  _IO_flockfile (stderr);
907 
908  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
909  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
910 
911  __fxprintf (NULL, "%s", buf);
912 
913  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
914  _IO_funlockfile (stderr);
915 
916  free (buf);
917  }
918 #else
919  fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
920  argv[0], argv[d->optind]);
921 #endif
922  }
923  d->__nextchar += strlen (d->__nextchar);
924  d->optind++;
925  return '?';
926  }
927  if (pfound != NULL)
928  {
929  option_index = indfound;
930  if (*nameend)
931  {
932  /* Don't test has_arg with >, because some C compilers don't
933  allow it to be used on enums. */
934  if (pfound->has_arg)
935  d->optarg = nameend + 1;
936  else
937  {
938  if (print_errors)
939  {
940 #if defined _LIBC && defined USE_IN_LIBIO
941  char *buf;
942 
943  if (__asprintf (&buf, _("\
944 %s: option `-W %s' doesn't allow an argument\n"),
945  argv[0], pfound->name) >= 0)
946  {
947  _IO_flockfile (stderr);
948 
949  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
950  ((_IO_FILE *) stderr)->_flags2
951  |= _IO_FLAGS2_NOTCANCEL;
952 
953  __fxprintf (NULL, "%s", buf);
954 
955  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
956  _IO_funlockfile (stderr);
957 
958  free (buf);
959  }
960 #else
961  fprintf (stderr, _("\
962 %s: option `-W %s' doesn't allow an argument\n"),
963  argv[0], pfound->name);
964 #endif
965  }
966 
967  d->__nextchar += strlen (d->__nextchar);
968  return '?';
969  }
970  }
971  else if (pfound->has_arg == 1)
972  {
973  if (d->optind < argc)
974  d->optarg = argv[d->optind++];
975  else
976  {
977  if (print_errors)
978  {
979 #if defined _LIBC && defined USE_IN_LIBIO
980  char *buf;
981 
982  if (__asprintf (&buf, _("\
983 %s: option `%s' requires an argument\n"),
984  argv[0], argv[d->optind - 1]) >= 0)
985  {
986  _IO_flockfile (stderr);
987 
988  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
989  ((_IO_FILE *) stderr)->_flags2
990  |= _IO_FLAGS2_NOTCANCEL;
991 
992  __fxprintf (NULL, "%s", buf);
993 
994  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
995  _IO_funlockfile (stderr);
996 
997  free (buf);
998  }
999 #else
1000  fprintf (stderr,
1001  _("%s: option `%s' requires an argument\n"),
1002  argv[0], argv[d->optind - 1]);
1003 #endif
1004  }
1005  d->__nextchar += strlen (d->__nextchar);
1006  return optstring[0] == ':' ? ':' : '?';
1007  }
1008  }
1009  d->__nextchar += strlen (d->__nextchar);
1010  if (longind != NULL)
1011  *longind = option_index;
1012  if (pfound->flag)
1013  {
1014  *(pfound->flag) = pfound->val;
1015  return 0;
1016  }
1017  return pfound->val;
1018  }
1019  d->__nextchar = NULL;
1020  return 'W'; /* Let the application handle it. */
1021  }
1022  if (temp[1] == ':')
1023  {
1024  if (temp[2] == ':')
1025  {
1026  /* This is an option that accepts an argument optionally. */
1027  if (*d->__nextchar != '\0')
1028  {
1029  d->optarg = d->__nextchar;
1030  d->optind++;
1031  }
1032  else
1033  d->optarg = NULL;
1034  d->__nextchar = NULL;
1035  }
1036  else
1037  {
1038  /* This is an option that requires an argument. */
1039  if (*d->__nextchar != '\0')
1040  {
1041  d->optarg = d->__nextchar;
1042  /* If we end this ARGV-element by taking the rest as an arg,
1043  we must advance to the next element now. */
1044  d->optind++;
1045  }
1046  else if (d->optind == argc)
1047  {
1048  if (print_errors)
1049  {
1050  /* 1003.2 specifies the format of this message. */
1051 #if defined _LIBC && defined USE_IN_LIBIO
1052  char *buf;
1053 
1054  if (__asprintf (&buf, _("\
1055 %s: option requires an argument -- %c\n"),
1056  argv[0], c) >= 0)
1057  {
1058  _IO_flockfile (stderr);
1059 
1060  int old_flags2 = ((_IO_FILE *) stderr)->_flags2;
1061  ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL;
1062 
1063  __fxprintf (NULL, "%s", buf);
1064 
1065  ((_IO_FILE *) stderr)->_flags2 = old_flags2;
1066  _IO_funlockfile (stderr);
1067 
1068  free (buf);
1069  }
1070 #else
1071  fprintf (stderr,
1072  _("%s: option requires an argument -- %c\n"),
1073  argv[0], c);
1074 #endif
1075  }
1076  d->optopt = c;
1077  if (optstring[0] == ':')
1078  c = ':';
1079  else
1080  c = '?';
1081  }
1082  else
1083  /* We already incremented `optind' once;
1084  increment it again when taking next ARGV-elt as argument. */
1085  d->optarg = argv[d->optind++];
1086  d->__nextchar = NULL;
1087  }
1088  }
1089  return c;
1090  }
1091 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
void * p
Definition: libc.cpp:67
int n
Definition: mipsasm.c:19
#define c(i)
Definition: sha256.c:43
Definition: getopt.h:84
int val
Definition: getopt.h:94
int * flag
Definition: getopt.h:93
int has_arg
Definition: getopt.h:92
char * name
Definition: getopt.h:88
#define NONOPTION_P
static const char * _getopt_initialize(int argc, char **argv, const char *optstring, int posixly_correct, struct _getopt_data *d)
Definition: getopt.c:239
#define _(msgid)
Definition: getopt.c:49
static void exchange(char **argv, struct _getopt_data *d)
Definition: getopt.c:158

References _, _getopt_initialize(), argv, c, d, exchange(), option::flag, free(), option::has_arg, n, option::name, NONOPTION_P, NULL, p, PERMUTE, posixly_correct, REQUIRE_ORDER, and option::val.

Referenced by _getopt_internal(), _getopt_long_only_r(), and _getopt_long_r().

◆ exchange()

static void exchange ( char **  argv,
struct _getopt_data d 
)
static

Definition at line 158 of file getopt.c.

159 {
160  int bottom = d->__first_nonopt;
161  int middle = d->__last_nonopt;
162  int top = d->optind;
163  char *tem;
164 
165  /* Exchange the shorter segment with the far end of the longer segment.
166  That puts the shorter segment into the right place.
167  It leaves the longer segment in the right place overall,
168  but it consists of two parts that need to be swapped next. */
169 
170 #if defined _LIBC && defined USE_NONOPTION_FLAGS
171  /* First make sure the handling of the `__getopt_nonoption_flags'
172  string can work normally. Our top argument must be in the range
173  of the string. */
174  if (d->__nonoption_flags_len > 0 && top >= d->__nonoption_flags_max_len)
175  {
176  /* We must extend the array. The user plays games with us and
177  presents new arguments. */
178  char *new_str = malloc (top + 1);
179  if (new_str == NULL)
180  d->__nonoption_flags_len = d->__nonoption_flags_max_len = 0;
181  else
182  {
183  memset (__mempcpy (new_str, __getopt_nonoption_flags,
184  d->__nonoption_flags_max_len),
185  '\0', top + 1 - d->__nonoption_flags_max_len);
186  d->__nonoption_flags_max_len = top + 1;
187  __getopt_nonoption_flags = new_str;
188  }
189  }
190 #endif
191 
192  while (top > middle && middle > bottom)
193  {
194  if (top - middle > middle - bottom)
195  {
196  /* Bottom segment is the short one. */
197  int len = middle - bottom;
198  register int i;
199 
200  /* Swap it with the top part of the top segment. */
201  for (i = 0; i < len; i++)
202  {
203  tem = argv[bottom + i];
204  argv[bottom + i] = argv[top - (middle - bottom) + i];
205  argv[top - (middle - bottom) + i] = tem;
206  SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
207  }
208  /* Exclude the moved bottom segment from further swapping. */
209  top -= len;
210  }
211  else
212  {
213  /* Top segment is the short one. */
214  int len = top - middle;
215  register int i;
216 
217  /* Swap it with the bottom part of the bottom segment. */
218  for (i = 0; i < len; i++)
219  {
220  tem = argv[bottom + i];
221  argv[bottom + i] = argv[middle + i];
222  argv[middle + i] = tem;
223  SWAP_FLAGS (bottom + i, middle + i);
224  }
225  /* Exclude the moved top segment from further swapping. */
226  bottom += len;
227  }
228  }
229 
230  /* Update records for the slots the non-options now occupy. */
231 
232  d->__first_nonopt += (d->optind - d->__last_nonopt);
233  d->__last_nonopt = d->optind;
234 }
lzma_index ** i
Definition: index.h:629
#define SWAP_FLAGS(ch1, ch2)
Definition: getopt.c:145

References argv, d, i, len, malloc(), memset(), NULL, and SWAP_FLAGS.

Referenced by _getopt_internal_r().

◆ getopt()

int getopt ( int  argc,
char *const argv,
const char *  optstring 
)

Definition at line 1122 of file getopt.c.

1123 {
1124  return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1125  POSIXLY_CORRECT);
1126 }
int _getopt_internal(int argc, char *const *argv, const char *optstring, const struct option *longopts, int *longind, int long_only)
Definition: getopt.c:514

References _getopt_internal(), argv, NULL, and POSIXLY_CORRECT.

Variable Documentation

◆ getopt_data

struct _getopt_data getopt_data
static

Definition at line 107 of file getopt.c.

Referenced by _getopt_internal().

◆ optarg

char* optarg

Definition at line 81 of file getopt.c.

◆ opterr

int opterr = 1

Definition at line 101 of file getopt.c.

◆ optind

int optind = 1

Definition at line 96 of file getopt.c.

◆ optopt

int optopt = '?'

Definition at line 107 of file getopt.c.

Referenced by main().