Blame lib/getopt1.c

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