]> sjero.net Git - iperf/blob - src/gnu_getopt_long.c
Original 2.0.2 iperf sources
[iperf] / src / gnu_getopt_long.c
1 /* gnu_getopt_long and getopt_long_only entry points for GNU getopt.
2    Copyright (C) 1987,88,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
3
4    This file is part of the GNU C Library.  Its master source is NOT part of
5    the C library, however.  The master source lives in /gd/gnu/lib.
6
7    The GNU C Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The GNU C Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the GNU C Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /*
23  * modified July 9, 1999 by mark gates <mgates@nlanr.net>
24  *          Dec 17, 1999
25  *
26  * renamed all functions and variables by prepending "gnu_"
27  * removed/redid a bunch of stuff under the assumption we're
28  *   using a modern standard C compiler.
29  * renamed file to gnu_getopt_long.c (from gnu_getopt1.c)
30  *
31  * $Id: gnu_getopt_long.c,v 1.1.1.1 2004/05/18 01:50:44 kgibbs Exp $
32  */
33
34 \f
35
36 #include "gnu_getopt.h"
37
38 #include <stdio.h>
39 #include <stdlib.h>
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 int
46 gnu_getopt_long( int argc,
47                  char *const *argv,
48                  const char *options,
49                  const struct option *long_options,
50                  int *opt_index ) {
51     return _gnu_getopt_internal (argc, argv, options, long_options, opt_index, 0);
52 }
53
54 /* Like gnu_getopt_long, but '-' as well as '--' can indicate a long option.
55    If an option that starts with '-' (not '--') doesn't match a long option,
56    but does match a short option, it is parsed as a short option
57    instead.  */
58
59 int
60 gnu_getopt_long_only( int argc,
61                       char *const *argv,
62                       const char *options,
63                       const struct option *long_options,
64                       int *opt_index ) {
65     return _gnu_getopt_internal (argc, argv, options, long_options, opt_index, 1);
66 }
67
68 #ifdef __cplusplus
69 } /* end extern "C" */
70 #endif
71
72 \f
73 #ifdef TEST
74
75     #include <stdio.h>
76
77 int
78 main (argc, argv)
79 int argc;
80 char **argv;
81 {
82 int c;
83 int digit_optind = 0;
84
85 while ( 1 ) {
86     int this_option_optind = gnu_optind ? gnu_optind : 1;
87     int option_index = 0;
88     static struct option long_options[] =
89     {
90         {"add", 1, 0, 0},
91         {"append", 0, 0, 0},
92         {"delete", 1, 0, 0},
93         {"verbose", 0, 0, 0},
94         {"create", 0, 0, 0},
95         {"file", 1, 0, 0},
96         {0, 0, 0, 0}
97     };
98
99     c = gnu_getopt_long (argc, argv, "abc:d:0123456789",
100                          long_options, &option_index);
101     if ( c == -1 )
102         break;
103
104     switch ( c ) {
105         case 0:
106             fprintf ( stderr, "option %s", long_options[option_index].name);
107             if ( gnu_optarg )
108                 fprintf ( stderr, " with arg %s", gnu_optarg);
109             fprintf ( stderr, "\n");
110             break;
111
112         case '0':
113         case '1':
114         case '2':
115         case '3':
116         case '4':
117         case '5':
118         case '6':
119         case '7':
120         case '8':
121         case '9':
122             if ( digit_optind != 0 && digit_optind != this_option_optind )
123                 fprintf ( stderr, "digits occur in two different argv-elements.\n");
124             digit_optind = this_option_optind;
125             fprintf ( stderr, "option %c\n", c);
126             break;
127
128         case 'a':
129             fprintf ( stderr, "option a\n");
130             break;
131
132         case 'b':
133             fprintf ( stderr, "option b\n");
134             break;
135
136         case 'c':
137             fprintf ( stderr, "option c with value `%s'\n", gnu_optarg);
138             break;
139
140         case 'd':
141             fprintf ( stderr, "option d with value `%s'\n", gnu_optarg);
142             break;
143
144         case '?':
145             break;
146
147         default:
148             fprintf ( stderr, "?? gnu_getopt returned character code 0%o ??\n", c);
149     }
150 }
151
152 if ( gnu_optind < argc ) {
153     fprintf ( stderr, "non-option ARGV-elements: ");
154     while ( gnu_optind < argc )
155         fprintf ( stderr, "%s ", argv[gnu_optind++]);
156     fprintf ( stderr, "\n");
157 }
158
159 exit (0);
160 }
161
162 #endif /* TEST */