1 /*****************************************************************************
2 * getopt.c - competent and free getopt library.
3 * $Header: /cvsroot/freegetopt/freegetopt/getopt.c,v 1.2 2003/10/26 03:10:20 vindaci Exp $
4 *
5 * Copyright (c)2002-2003 Mark K. Kim
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * Neither the original author of this software nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
31 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
34 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
35 * DAMAGE.
36 */
37 #ifndef _CRT_SECURE_NO_WARNINGS
38 #define _CRT_SECURE_NO_WARNINGS
39 #endif
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include "getopt.h"
45
46 /* 2013-01-06 Camilla Berglund <elmindreda@elmindreda.org>
47 *
48 * Only define _CRT_SECURE_NO_WARNINGS if not already defined.
49 */
50 /* 2012-08-12 Lambert Clara <lambert.clara@yahoo.fr>
51 *
52 * Constify third argument of getopt.
53 */
54 /* 2011-07-27 Camilla Berglund <elmindreda@elmindreda.org>
55 *
56 * Added _CRT_SECURE_NO_WARNINGS macro.
57 */
58 /* 2009-10-12 Camilla Berglund <elmindreda@elmindreda.org>
59 *
60 * Removed unused global static variable 'ID'.
61 */
62
63 char* optarg = NULL;
64 int optind = 0;
65 int opterr = 1;
66 int optopt = '?';
67
68
69 static char** prev_argv = NULL; /* Keep a copy of argv and argc to */
70 static int prev_argc = 0; /* tell if getopt params change */
71 static int argv_index = 0; /* Option we're checking */
72 static int argv_index2 = 0; /* Option argument we're checking */
73 static int opt_offset = 0; /* Index into compounded "-option" */
74 static int dashdash = 0; /* True if "--" option reached */
75 static int nonopt = 0; /* How many nonopts we've found */
76
increment_index()77 static void increment_index()
78 {
79 /* Move onto the next option */
80 if(argv_index < argv_index2)
81 {
82 while(prev_argv[++argv_index] && prev_argv[argv_index][0] != '-'
83 && argv_index < argv_index2+1);
84 }
85 else argv_index++;
86 opt_offset = 1;
87 }
88
89
90 /*
91 * Permutes argv[] so that the argument currently being processed is moved
92 * to the end.
93 */
permute_argv_once()94 static int permute_argv_once()
95 {
96 /* Movability check */
97 if(argv_index + nonopt >= prev_argc) return 1;
98 /* Move the current option to the end, bring the others to front */
99 else
100 {
101 char* tmp = prev_argv[argv_index];
102
103 /* Move the data */
104 memmove(&prev_argv[argv_index], &prev_argv[argv_index+1],
105 sizeof(char**) * (prev_argc - argv_index - 1));
106 prev_argv[prev_argc - 1] = tmp;
107
108 nonopt++;
109 return 0;
110 }
111 }
112
113
getopt(int argc,char ** argv,const char * optstr)114 int getopt(int argc, char** argv, const char* optstr)
115 {
116 int c = 0;
117
118 /* If we have new argv, reinitialize */
119 if(prev_argv != argv || prev_argc != argc)
120 {
121 /* Initialize variables */
122 prev_argv = argv;
123 prev_argc = argc;
124 argv_index = 1;
125 argv_index2 = 1;
126 opt_offset = 1;
127 dashdash = 0;
128 nonopt = 0;
129 }
130
131 /* Jump point in case we want to ignore the current argv_index */
132 getopt_top:
133
134 /* Misc. initializations */
135 optarg = NULL;
136
137 /* Dash-dash check */
138 if(argv[argv_index] && !strcmp(argv[argv_index], "--"))
139 {
140 dashdash = 1;
141 increment_index();
142 }
143
144 /* If we're at the end of argv, that's it. */
145 if(argv[argv_index] == NULL)
146 {
147 c = -1;
148 }
149 /* Are we looking at a string? Single dash is also a string */
150 else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-"))
151 {
152 /* If we want a string... */
153 if(optstr[0] == '-')
154 {
155 c = 1;
156 optarg = argv[argv_index];
157 increment_index();
158 }
159 /* If we really don't want it (we're in POSIX mode), we're done */
160 else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT"))
161 {
162 c = -1;
163
164 /* Everything else is a non-opt argument */
165 nonopt = argc - argv_index;
166 }
167 /* If we mildly don't want it, then move it back */
168 else
169 {
170 if(!permute_argv_once()) goto getopt_top;
171 else c = -1;
172 }
173 }
174 /* Otherwise we're looking at an option */
175 else
176 {
177 char* opt_ptr = NULL;
178
179 /* Grab the option */
180 c = argv[argv_index][opt_offset++];
181
182 /* Is the option in the optstr? */
183 if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c);
184 else opt_ptr = strchr(optstr, c);
185 /* Invalid argument */
186 if(!opt_ptr)
187 {
188 if(opterr)
189 {
190 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
191 }
192
193 optopt = c;
194 c = '?';
195
196 /* Move onto the next option */
197 increment_index();
198 }
199 /* Option takes argument */
200 else if(opt_ptr[1] == ':')
201 {
202 /* ie, -oARGUMENT, -xxxoARGUMENT, etc. */
203 if(argv[argv_index][opt_offset] != '\0')
204 {
205 optarg = &argv[argv_index][opt_offset];
206 increment_index();
207 }
208 /* ie, -o ARGUMENT (only if it's a required argument) */
209 else if(opt_ptr[2] != ':')
210 {
211 /* One of those "you're not expected to understand this" moment */
212 if(argv_index2 < argv_index) argv_index2 = argv_index;
213 while(argv[++argv_index2] && argv[argv_index2][0] == '-');
214 optarg = argv[argv_index2];
215
216 /* Don't cross into the non-option argument list */
217 if(argv_index2 + nonopt >= prev_argc) optarg = NULL;
218
219 /* Move onto the next option */
220 increment_index();
221 }
222 else
223 {
224 /* Move onto the next option */
225 increment_index();
226 }
227
228 /* In case we got no argument for an option with required argument */
229 if(optarg == NULL && opt_ptr[2] != ':')
230 {
231 optopt = c;
232 c = '?';
233
234 if(opterr)
235 {
236 fprintf(stderr,"%s: option requires an argument -- %c\n",
237 argv[0], optopt);
238 }
239 }
240 }
241 /* Option does not take argument */
242 else
243 {
244 /* Next argv_index */
245 if(argv[argv_index][opt_offset] == '\0')
246 {
247 increment_index();
248 }
249 }
250 }
251
252 /* Calculate optind */
253 if(c == -1)
254 {
255 optind = argc - nonopt;
256 }
257 else
258 {
259 optind = argv_index;
260 }
261
262 return c;
263 }
264
265
266 /* vim:ts=3
267 */
268