Blame portability/getopt1.c

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