Change to the linux kernel coding style
[wmaker-crm.git] / plugins / libwmfun / getopt1.c
Commit [+]AuthorDateLineData
7aadc1ef id2000-12-03 18:58:41 +00001/* getopt_long and getopt_long_only entry points for GNU getopt.
2 Copyright (C) 1987, 88, 89, 90, 91, 92, 1993
3 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option) any
8 later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. */
14\f
7aadc1ef id2000-12-03 18:58:41 +000015#include "getopt.h"
16
17#ifndef __STDC__
18/* This is a separate conditional since some stdc systems
19 reject `defined (const)'. */
20#ifndef const
21#define const
22#endif
23#endif
24
25#include <stdio.h>
26
27/* Comment out all this code if we are using the GNU C Library, and are not
28 actually compiling the library itself. This code is part of the GNU C
29 Library, but also included in many other GNU distributions. Compiling
30 and linking in this code is a waste when using the GNU C library
31 (especially if it is a shared library). Rather than having every GNU
32 program understand `configure --with-gnu-libc' and omit the object files,
33 it is simpler to just do this in the source for each such file. */
34
35#if defined (_LIBC) || !defined (__GNU_LIBRARY__)
36
7aadc1ef id2000-12-03 18:58:41 +000037/* This needs to come after some library #include
38 to get __GNU_LIBRARY__ defined. */
39#ifdef __GNU_LIBRARY__
40#include <stdlib.h>
41#else
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020042char *getenv();
7aadc1ef id2000-12-03 18:58:41 +000043#endif
44
45#ifndef NULL
46#define NULL 0
47#endif
48
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020049int getopt_long(argc, argv, options, long_options, opt_index)
50int argc;
51char *const *argv;
52const char *options;
53const struct option *long_options;
54int *opt_index;
7aadc1ef id2000-12-03 18:58:41 +000055{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020056 return _getopt_internal(argc, argv, options, long_options, opt_index, 0);
7aadc1ef id2000-12-03 18:58:41 +000057}
58
59/* Like getopt_long, but '-' as well as '--' can indicate a long option.
60 If an option that starts with '-' (not '--') doesn't match a long option,
61 but does match a short option, it is parsed as a short option
62 instead. */
63
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020064int getopt_long_only(argc, argv, options, long_options, opt_index)
65int argc;
66char *const *argv;
67const char *options;
68const struct option *long_options;
69int *opt_index;
7aadc1ef id2000-12-03 18:58:41 +000070{
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020071 return _getopt_internal(argc, argv, options, long_options, opt_index, 1);
7aadc1ef id2000-12-03 18:58:41 +000072}
73
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020074#endif /* _LIBC or not __GNU_LIBRARY__. */
7aadc1ef id2000-12-03 18:58:41 +000075\f
76#ifdef TEST
77
78#include <stdio.h>
79
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020080int main(argc, argv)
81int argc;
82char **argv;
7aadc1ef id2000-12-03 18:58:41 +000083{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020084 int c;
85 int digit_optind = 0;
86
87 while (1) {
88 int this_option_optind = optind ? optind : 1;
89 int option_index = 0;
90 static struct option long_options[] = {
91 {"add", 1, 0, 0},
92 {"append", 0, 0, 0},
93 {"delete", 1, 0, 0},
94 {"verbose", 0, 0, 0},
95 {"create", 0, 0, 0},
96 {"file", 1, 0, 0},
97 {0, 0, 0, 0}
98 };
99
100 c = getopt_long(argc, argv, "abc:d:0123456789", long_options, &option_index);
101 if (c == EOF)
102 break;
103
104 switch (c) {
105 case 0:
106 printf("option %s", long_options[option_index].name);
107 if (optarg)
108 printf(" with arg %s", optarg);
109 printf("\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 printf("digits occur in two different argv-elements.\n");
124 digit_optind = this_option_optind;
125 printf("option %c\n", c);
126 break;
127
128 case 'a':
129 printf("option a\n");
130 break;
131
132 case 'b':
133 printf("option b\n");
134 break;
135
136 case 'c':
137 printf("option c with value `%s'\n", optarg);
138 break;
139
140 case 'd':
141 printf("option d with value `%s'\n", optarg);
142 break;
143
144 case '?':
145 break;
146
147 default:
148 printf("?? getopt returned character code 0%o ??\n", c);
149 }
7aadc1ef id2000-12-03 18:58:41 +0000150 }
7aadc1ef id2000-12-03 18:58:41 +0000151
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200152 if (optind < argc) {
153 printf("non-option ARGV-elements: ");
154 while (optind < argc)
155 printf("%s ", argv[optind++]);
156 printf("\n");
157 }
7aadc1ef id2000-12-03 18:58:41 +0000158
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200159 exit(0);
7aadc1ef id2000-12-03 18:58:41 +0000160}
161
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200162#endif /* TEST */