Blame lib/getopt1.c

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