1/*
2 * getopt1.c
3 *
4 * Ripped from GLIBC - original copyright follows
5 *
6 * NOTE: Changed to make dependencies work better:
7 *        * <config.h> changed to "config.h"
8 *
9 * $Id: getopt1.c,v 1.1.1.1 2002/06/21 08:51:58 fenix_nl Exp $
10 */
11
12/* getopt_long and getopt_long_only entry points for GNU getopt.
13   Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98
14     Free Software Foundation, Inc.
15   This file is part of the GNU C Library.
16
17   The GNU C Library is free software; you can redistribute it and/or
18   modify it under the terms of the GNU Library General Public License as
19   published by the Free Software Foundation; either version 2 of the
20   License, or (at your option) any later version.
21
22   The GNU C Library is distributed in the hope that it will be useful,
23   but WITHOUT ANY WARRANTY; without even the implied warranty of
24   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25   Library General Public License for more details.
26
27   You should have received a copy of the GNU Library General Public
28   License along with the GNU C Library; see the file COPYING.LIB.  If not,
29   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
30   Boston, MA 02111-1307, USA.  */
31
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
36#include "our_getopt.h"
37
38#if !defined __STDC__ || !__STDC__
39/* This is a separate conditional since some stdc systems
40   reject `defined (const)'.  */
41#ifndef const
42#define const
43#endif
44#endif
45
46#include <stdio.h>
47
48/* Comment out all this code if we are using the GNU C Library, and are not
49   actually compiling the library itself.  This code is part of the GNU C
50   Library, but also included in many other GNU distributions.  Compiling
51   and linking in this code is a waste when using the GNU C library
52   (especially if it is a shared library).  Rather than having every GNU
53   program understand `configure --with-gnu-libc' and omit the object files,
54   it is simpler to just do this in the source for each such file.  */
55
56#define GETOPT_INTERFACE_VERSION 2
57#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
58#include <gnu-versions.h>
59#if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
60#define ELIDE_CODE
61#endif
62#endif
63
64#ifndef ELIDE_CODE
65
66
67/* This needs to come after some library #include
68   to get __GNU_LIBRARY__ defined.  */
69#ifdef __GNU_LIBRARY__
70#include <stdlib.h>
71#endif
72
73#ifndef	NULL
74#define NULL 0
75#endif
76
77int
78getopt_long (argc, argv, options, long_options, opt_index)
79     int argc;
80     char *const *argv;
81     const char *options;
82     const struct option *long_options;
83     int *opt_index;
84{
85  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
86}
87
88/* Like getopt_long, but '-' as well as '--' can indicate a long option.
89   If an option that starts with '-' (not '--') doesn't match a long option,
90   but does match a short option, it is parsed as a short option
91   instead.  */
92
93int
94getopt_long_only (argc, argv, options, long_options, opt_index)
95     int argc;
96     char *const *argv;
97     const char *options;
98     const struct option *long_options;
99     int *opt_index;
100{
101  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
102}
103
104
105#endif	/* Not ELIDE_CODE.  */
106
107#ifdef TEST
108
109#include <stdio.h>
110
111int
112main (argc, argv)
113     int argc;
114     char **argv;
115{
116  int c;
117  int digit_optind = 0;
118
119  while (1)
120    {
121      int this_option_optind = optind ? optind : 1;
122      int option_index = 0;
123      static struct option long_options[] =
124      {
125	{"add", 1, 0, 0},
126	{"append", 0, 0, 0},
127	{"delete", 1, 0, 0},
128	{"verbose", 0, 0, 0},
129	{"create", 0, 0, 0},
130	{"file", 1, 0, 0},
131	{0, 0, 0, 0}
132      };
133
134      c = getopt_long (argc, argv, "abc:d:0123456789",
135		       long_options, &option_index);
136      if (c == -1)
137	break;
138
139      switch (c)
140	{
141	case 0:
142	  printf ("option %s", long_options[option_index].name);
143	  if (optarg)
144	    printf (" with arg %s", optarg);
145	  printf ("\n");
146	  break;
147
148	case '0':
149	case '1':
150	case '2':
151	case '3':
152	case '4':
153	case '5':
154	case '6':
155	case '7':
156	case '8':
157	case '9':
158	  if (digit_optind != 0 && digit_optind != this_option_optind)
159	    printf ("digits occur in two different argv-elements.\n");
160	  digit_optind = this_option_optind;
161	  printf ("option %c\n", c);
162	  break;
163
164	case 'a':
165	  printf ("option a\n");
166	  break;
167
168	case 'b':
169	  printf ("option b\n");
170	  break;
171
172	case 'c':
173	  printf ("option c with value `%s'\n", optarg);
174	  break;
175
176	case 'd':
177	  printf ("option d with value `%s'\n", optarg);
178	  break;
179
180	case '?':
181	  break;
182
183	default:
184	  printf ("?? getopt returned character code 0%o ??\n", c);
185	}
186    }
187
188  if (optind < argc)
189    {
190      printf ("non-option ARGV-elements: ");
191      while (optind < argc)
192	printf ("%s ", argv[optind++]);
193      printf ("\n");
194    }
195
196  exit (0);
197}
198
199#endif /* TEST */
200