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 #define _CRT_SECURE_NO_WARNINGS
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include "getopt.h"
43
44
45 //static const char* ID = "$Id: getopt.c,v 1.2 2003/10/26 03:10:20 vindaci Exp $";
46
47
48 char* optarg = NULL;
49 int optind = 0;
50 int opterr = 1;
51 int optopt = '?';
52
53
54 static char** prev_argv = NULL; /* Keep a copy of argv and argc to */
55 static int prev_argc = 0; /* tell if getopt params change */
56 static int argv_index = 0; /* Option we're checking */
57 static int argv_index2 = 0; /* Option argument we're checking */
58 static int opt_offset = 0; /* Index into compounded "-option" */
59 static int dashdash = 0; /* True if "--" option reached */
60 static int nonopt = 0; /* How many nonopts we've found */
61
increment_index()62 static void increment_index()
63 {
64 /* Move onto the next option */
65 if(argv_index < argv_index2)
66 {
67 while(prev_argv[++argv_index] && prev_argv[argv_index][0] != '-'
68 && argv_index < argv_index2+1);
69 }
70 else argv_index++;
71 opt_offset = 1;
72 }
73
74
75 /*
76 * Permutes argv[] so that the argument currently being processed is moved
77 * to the end.
78 */
permute_argv_once()79 static int permute_argv_once()
80 {
81 /* Movability check */
82 if(argv_index + nonopt >= prev_argc) return 1;
83 /* Move the current option to the end, bring the others to front */
84 else
85 {
86 char* tmp = prev_argv[argv_index];
87
88 /* Move the data */
89 memmove(&prev_argv[argv_index], &prev_argv[argv_index+1],
90 sizeof(char**) * (prev_argc - argv_index - 1));
91 prev_argv[prev_argc - 1] = tmp;
92
93 nonopt++;
94 return 0;
95 }
96 }
97
98
getopt(int argc,char ** argv,char * optstr)99 int getopt(int argc, char** argv, char* optstr)
100 {
101 int c = 0;
102
103 /* If we have new argv, reinitialize */
104 if(prev_argv != argv || prev_argc != argc)
105 {
106 /* Initialize variables */
107 prev_argv = argv;
108 prev_argc = argc;
109 argv_index = 1;
110 argv_index2 = 1;
111 opt_offset = 1;
112 dashdash = 0;
113 nonopt = 0;
114 }
115
116 /* Jump point in case we want to ignore the current argv_index */
117 getopt_top:
118
119 /* Misc. initializations */
120 optarg = NULL;
121
122 /* Dash-dash check */
123 if(argv[argv_index] && !strcmp(argv[argv_index], "--"))
124 {
125 dashdash = 1;
126 increment_index();
127 }
128
129 /* If we're at the end of argv, that's it. */
130 if(argv[argv_index] == NULL)
131 {
132 c = -1;
133 }
134 /* Are we looking at a string? Single dash is also a string */
135 else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-"))
136 {
137 /* If we want a string... */
138 if(optstr[0] == '-')
139 {
140 c = 1;
141 optarg = argv[argv_index];
142 increment_index();
143 }
144 /* If we really don't want it (we're in POSIX mode), we're done */
145 else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT"))
146 {
147 c = -1;
148
149 /* Everything else is a non-opt argument */
150 nonopt = argc - argv_index;
151 }
152 /* If we mildly don't want it, then move it back */
153 else
154 {
155 if(!permute_argv_once()) goto getopt_top;
156 else c = -1;
157 }
158 }
159 /* Otherwise we're looking at an option */
160 else
161 {
162 char* opt_ptr = NULL;
163
164 /* Grab the option */
165 c = argv[argv_index][opt_offset++];
166
167 /* Is the option in the optstr? */
168 if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c);
169 else opt_ptr = strchr(optstr, c);
170 /* Invalid argument */
171 if(!opt_ptr)
172 {
173 if(opterr)
174 {
175 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
176 }
177
178 optopt = c;
179 c = '?';
180
181 /* Move onto the next option */
182 increment_index();
183 }
184 /* Option takes argument */
185 else if(opt_ptr[1] == ':')
186 {
187 /* ie, -oARGUMENT, -xxxoARGUMENT, etc. */
188 if(argv[argv_index][opt_offset] != '\0')
189 {
190 optarg = &argv[argv_index][opt_offset];
191 increment_index();
192 }
193 /* ie, -o ARGUMENT (only if it's a required argument) */
194 else if(opt_ptr[2] != ':')
195 {
196 /* One of those "you're not expected to understand this" moment */
197 if(argv_index2 < argv_index) argv_index2 = argv_index;
198 while(argv[++argv_index2] && argv[argv_index2][0] == '-');
199 optarg = argv[argv_index2];
200
201 /* Don't cross into the non-option argument list */
202 if(argv_index2 + nonopt >= prev_argc) optarg = NULL;
203
204 /* Move onto the next option */
205 increment_index();
206 }
207 else
208 {
209 /* Move onto the next option */
210 increment_index();
211 }
212
213 /* In case we got no argument for an option with required argument */
214 if(optarg == NULL && opt_ptr[2] != ':')
215 {
216 optopt = c;
217 c = '?';
218
219 if(opterr)
220 {
221 fprintf(stderr,"%s: option requires an argument -- %c\n",
222 argv[0], optopt);
223 }
224 }
225 }
226 /* Option does not take argument */
227 else
228 {
229 /* Next argv_index */
230 if(argv[argv_index][opt_offset] == '\0')
231 {
232 increment_index();
233 }
234 }
235 }
236
237 /* Calculate optind */
238 if(c == -1)
239 {
240 optind = argc - nonopt;
241 }
242 else
243 {
244 optind = argv_index;
245 }
246
247 return c;
248 }
249
250
251 /* vim:ts=3
252 */
253