Blame lib/getopt1.c

Packit 8f70b4
/* getopt_long and getopt_long_only entry points for GNU getopt.
Packit 8f70b4
   Copyright (C) 1987-2018 Free Software Foundation, Inc.
Packit 8f70b4
   This file is part of the GNU C Library and is also part of gnulib.
Packit 8f70b4
   Patches to this file should be submitted to both projects.
Packit 8f70b4
Packit 8f70b4
   The GNU C Library is free software; you can redistribute it and/or
Packit 8f70b4
   modify it under the terms of the GNU General Public
Packit 8f70b4
   License as published by the Free Software Foundation; either
Packit 8f70b4
   version 3 of the License, or (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   The GNU C Library is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 8f70b4
   General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public
Packit 8f70b4
   License along with the GNU C Library; if not, see
Packit 8f70b4
   <https://www.gnu.org/licenses/>.  */
Packit 8f70b4

Packit 8f70b4
#ifndef _LIBC
Packit 8f70b4
# include <config.h>
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#include "getopt.h"
Packit 8f70b4
#include "getopt_int.h"
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
Packit 8f70b4
	     const struct option *long_options, int *opt_index)
Packit 8f70b4
{
Packit 8f70b4
  return _getopt_internal (argc, (char **) argv, options, long_options,
Packit 8f70b4
			   opt_index, 0, 0);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
_getopt_long_r (int argc, char **argv, const char *options,
Packit 8f70b4
		const struct option *long_options, int *opt_index,
Packit 8f70b4
		struct _getopt_data *d)
Packit 8f70b4
{
Packit 8f70b4
  return _getopt_internal_r (argc, argv, options, long_options, opt_index,
Packit 8f70b4
			     0, d, 0);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
Packit 8f70b4
   If an option that starts with '-' (not '--') doesn't match a long option,
Packit 8f70b4
   but does match a short option, it is parsed as a short option
Packit 8f70b4
   instead.  */
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
getopt_long_only (int argc, char *__getopt_argv_const *argv,
Packit 8f70b4
		  const char *options,
Packit 8f70b4
		  const struct option *long_options, int *opt_index)
Packit 8f70b4
{
Packit 8f70b4
  return _getopt_internal (argc, (char **) argv, options, long_options,
Packit 8f70b4
			   opt_index, 1, 0);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
_getopt_long_only_r (int argc, char **argv, const char *options,
Packit 8f70b4
		     const struct option *long_options, int *opt_index,
Packit 8f70b4
		     struct _getopt_data *d)
Packit 8f70b4
{
Packit 8f70b4
  return _getopt_internal_r (argc, argv, options, long_options, opt_index,
Packit 8f70b4
			     1, d, 0);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4

Packit 8f70b4
#ifdef TEST
Packit 8f70b4
Packit 8f70b4
#include <stdio.h>
Packit 8f70b4
#include <stdlib.h>
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
main (int argc, char **argv)
Packit 8f70b4
{
Packit 8f70b4
  int c;
Packit 8f70b4
  int digit_optind = 0;
Packit 8f70b4
Packit 8f70b4
  while (1)
Packit 8f70b4
    {
Packit 8f70b4
      int this_option_optind = optind ? optind : 1;
Packit 8f70b4
      int option_index = 0;
Packit 8f70b4
      static const struct option long_options[] =
Packit 8f70b4
      {
Packit 8f70b4
	{"add", 1, 0, 0},
Packit 8f70b4
	{"append", 0, 0, 0},
Packit 8f70b4
	{"delete", 1, 0, 0},
Packit 8f70b4
	{"verbose", 0, 0, 0},
Packit 8f70b4
	{"create", 0, 0, 0},
Packit 8f70b4
	{"file", 1, 0, 0},
Packit 8f70b4
	{0, 0, 0, 0}
Packit 8f70b4
      };
Packit 8f70b4
Packit 8f70b4
      c = getopt_long (argc, argv, "abc:d:0123456789",
Packit 8f70b4
		       long_options, &option_index);
Packit 8f70b4
      if (c == -1)
Packit 8f70b4
	break;
Packit 8f70b4
Packit 8f70b4
      switch (c)
Packit 8f70b4
	{
Packit 8f70b4
	case 0:
Packit 8f70b4
	  printf ("option %s", long_options[option_index].name);
Packit 8f70b4
	  if (optarg)
Packit 8f70b4
	    printf (" with arg %s", optarg);
Packit 8f70b4
	  printf ("\n");
Packit 8f70b4
	  break;
Packit 8f70b4
Packit 8f70b4
	case '0':
Packit 8f70b4
	case '1':
Packit 8f70b4
	case '2':
Packit 8f70b4
	case '3':
Packit 8f70b4
	case '4':
Packit 8f70b4
	case '5':
Packit 8f70b4
	case '6':
Packit 8f70b4
	case '7':
Packit 8f70b4
	case '8':
Packit 8f70b4
	case '9':
Packit 8f70b4
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit 8f70b4
	    printf ("digits occur in two different argv-elements.\n");
Packit 8f70b4
	  digit_optind = this_option_optind;
Packit 8f70b4
	  printf ("option %c\n", c);
Packit 8f70b4
	  break;
Packit 8f70b4
Packit 8f70b4
	case 'a':
Packit 8f70b4
	  printf ("option a\n");
Packit 8f70b4
	  break;
Packit 8f70b4
Packit 8f70b4
	case 'b':
Packit 8f70b4
	  printf ("option b\n");
Packit 8f70b4
	  break;
Packit 8f70b4
Packit 8f70b4
	case 'c':
Packit 8f70b4
	  printf ("option c with value '%s'\n", optarg);
Packit 8f70b4
	  break;
Packit 8f70b4
Packit 8f70b4
	case 'd':
Packit 8f70b4
	  printf ("option d with value '%s'\n", optarg);
Packit 8f70b4
	  break;
Packit 8f70b4
Packit 8f70b4
	case '?':
Packit 8f70b4
	  break;
Packit 8f70b4
Packit 8f70b4
	default:
Packit 8f70b4
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit 8f70b4
	}
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  if (optind < argc)
Packit 8f70b4
    {
Packit 8f70b4
      printf ("non-option ARGV-elements: ");
Packit 8f70b4
      while (optind < argc)
Packit 8f70b4
	printf ("%s ", argv[optind++]);
Packit 8f70b4
      printf ("\n");
Packit 8f70b4
    }
Packit 8f70b4
Packit 8f70b4
  exit (0);
Packit 8f70b4
}
Packit 8f70b4
Packit 8f70b4
#endif /* TEST */