Blame lib/getopt1.c

Packit 70b277
/* getopt_long and getopt_long_only entry points for GNU getopt.
Packit 70b277
Packit 70b277
   Copyright (C) 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996,
Packit 70b277
   1997, 1998, 2003 Free Software Foundation, Inc.
Packit 70b277
Packit 70b277
   This file is part of the GNU C Library.
Packit 70b277
Packit 70b277
   This program is free software; you can redistribute it and/or modify
Packit 70b277
   it under the terms of the GNU General Public License as published by
Packit 70b277
   the Free Software Foundation; either version 2, or (at your option)
Packit 70b277
   any later version.
Packit 70b277
Packit 70b277
   This program is distributed in the hope that it will be useful,
Packit 70b277
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 70b277
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 70b277
   GNU General Public License for more details.
Packit 70b277
Packit 70b277
   You should have received a copy of the GNU General Public License along
Packit 70b277
   with this program; if not, write to the Free Software Foundation,
Packit 70b277
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.  */
Packit 70b277

Packit 70b277
#ifdef HAVE_CONFIG_H
Packit 70b277
#include <config.h>
Packit 70b277
#endif
Packit 70b277
Packit 70b277
#ifdef _LIBC
Packit 70b277
# include <getopt.h>
Packit 70b277
#else
Packit 70b277
# include "getopt.h"
Packit 70b277
#endif
Packit 70b277
Packit 70b277
#include <stdio.h>
Packit 70b277
Packit 70b277
/* Comment out all this code if we are using the GNU C Library, and are not
Packit 70b277
   actually compiling the library itself.  This code is part of the GNU C
Packit 70b277
   Library, but also included in many other GNU distributions.  Compiling
Packit 70b277
   and linking in this code is a waste when using the GNU C library
Packit 70b277
   (especially if it is a shared library).  Rather than having every GNU
Packit 70b277
   program understand `configure --with-gnu-libc' and omit the object files,
Packit 70b277
   it is simpler to just do this in the source for each such file.  */
Packit 70b277
Packit 70b277
#define GETOPT_INTERFACE_VERSION 2
Packit 70b277
#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
Packit 70b277
#include <gnu-versions.h>
Packit 70b277
#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
Packit 70b277
#define ELIDE_CODE
Packit 70b277
#endif
Packit 70b277
#endif
Packit 70b277
Packit 70b277
#ifndef ELIDE_CODE
Packit 70b277
Packit 70b277
Packit 70b277
/* This needs to come after some library #include
Packit 70b277
   to get __GNU_LIBRARY__ defined.  */
Packit 70b277
#ifdef __GNU_LIBRARY__
Packit 70b277
#include <stdlib.h>
Packit 70b277
#endif
Packit 70b277
Packit 70b277
#ifndef	NULL
Packit 70b277
#define NULL 0
Packit 70b277
#endif
Packit 70b277
Packit 70b277
int
Packit 70b277
getopt_long (int argc,
Packit 70b277
	     char *const *argv,
Packit 70b277
	     const char *options,
Packit 70b277
	     const struct option *long_options,
Packit 70b277
	     int *opt_index)
Packit 70b277
{
Packit 70b277
  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
Packit 70b277
}
Packit 70b277
Packit 70b277
/* Like getopt_long, but '-' as well as '--' can indicate a long option.
Packit 70b277
   If an option that starts with '-' (not '--') doesn't match a long option,
Packit 70b277
   but does match a short option, it is parsed as a short option
Packit 70b277
   instead.  */
Packit 70b277
Packit 70b277
int
Packit 70b277
getopt_long_only (int argc,
Packit 70b277
		  char *const *argv,
Packit 70b277
		  const char *options,
Packit 70b277
		  const struct option *long_options,
Packit 70b277
		  int *opt_index)
Packit 70b277
{
Packit 70b277
  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
Packit 70b277
}
Packit 70b277
Packit 70b277
# ifdef _LIBC
Packit 70b277
libc_hidden_def (getopt_long)
Packit 70b277
libc_hidden_def (getopt_long_only)
Packit 70b277
# endif
Packit 70b277
Packit 70b277
#endif	/* Not ELIDE_CODE.  */
Packit 70b277

Packit 70b277
#ifdef TEST
Packit 70b277
Packit 70b277
#include <stdio.h>
Packit 70b277
Packit 70b277
int
Packit 70b277
main (int argc, char **argv)
Packit 70b277
{
Packit 70b277
  int c;
Packit 70b277
  int digit_optind = 0;
Packit 70b277
Packit 70b277
  while (1)
Packit 70b277
    {
Packit 70b277
      int this_option_optind = optind ? optind : 1;
Packit 70b277
      int option_index = 0;
Packit 70b277
      static struct option long_options[] =
Packit 70b277
      {
Packit 70b277
	{"add", 1, 0, 0},
Packit 70b277
	{"append", 0, 0, 0},
Packit 70b277
	{"delete", 1, 0, 0},
Packit 70b277
	{"verbose", 0, 0, 0},
Packit 70b277
	{"create", 0, 0, 0},
Packit 70b277
	{"file", 1, 0, 0},
Packit 70b277
	{0, 0, 0, 0}
Packit 70b277
      };
Packit 70b277
Packit 70b277
      c = getopt_long (argc, argv, "abc:d:0123456789",
Packit 70b277
		       long_options, &option_index);
Packit 70b277
      if (c == -1)
Packit 70b277
	break;
Packit 70b277
Packit 70b277
      switch (c)
Packit 70b277
	{
Packit 70b277
	case 0:
Packit 70b277
	  printf ("option %s", long_options[option_index].name);
Packit 70b277
	  if (optarg)
Packit 70b277
	    printf (" with arg %s", optarg);
Packit 70b277
	  printf ("\n");
Packit 70b277
	  break;
Packit 70b277
Packit 70b277
	case '0':
Packit 70b277
	case '1':
Packit 70b277
	case '2':
Packit 70b277
	case '3':
Packit 70b277
	case '4':
Packit 70b277
	case '5':
Packit 70b277
	case '6':
Packit 70b277
	case '7':
Packit 70b277
	case '8':
Packit 70b277
	case '9':
Packit 70b277
	  if (digit_optind != 0 && digit_optind != this_option_optind)
Packit 70b277
	    printf ("digits occur in two different argv-elements.\n");
Packit 70b277
	  digit_optind = this_option_optind;
Packit 70b277
	  printf ("option %c\n", c);
Packit 70b277
	  break;
Packit 70b277
Packit 70b277
	case 'a':
Packit 70b277
	  printf ("option a\n");
Packit 70b277
	  break;
Packit 70b277
Packit 70b277
	case 'b':
Packit 70b277
	  printf ("option b\n");
Packit 70b277
	  break;
Packit 70b277
Packit 70b277
	case 'c':
Packit 70b277
	  printf ("option c with value `%s'\n", optarg);
Packit 70b277
	  break;
Packit 70b277
Packit 70b277
	case 'd':
Packit 70b277
	  printf ("option d with value `%s'\n", optarg);
Packit 70b277
	  break;
Packit 70b277
Packit 70b277
	case '?':
Packit 70b277
	  break;
Packit 70b277
Packit 70b277
	default:
Packit 70b277
	  printf ("?? getopt returned character code 0%o ??\n", c);
Packit 70b277
	}
Packit 70b277
    }
Packit 70b277
Packit 70b277
  if (optind < argc)
Packit 70b277
    {
Packit 70b277
      printf ("non-option ARGV-elements: ");
Packit 70b277
      while (optind < argc)
Packit 70b277
	printf ("%s ", argv[optind++]);
Packit 70b277
      printf ("\n");
Packit 70b277
    }
Packit 70b277
Packit 70b277
  exit (0);
Packit 70b277
}
Packit 70b277
Packit 70b277
#endif /* TEST */