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 $
5 * Copyright (c)2002-2003 Mark K. Kim
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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.
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
43 static const char* ID = "$Id$";
52 static char** prev_argv = NULL; /* Keep a copy of argv and argc to */
53 static int prev_argc = 0; /* tell if getopt params change */
54 static int argv_index = 0; /* Option we're checking */
55 static int argv_index2 = 0; /* Option argument we're checking */
56 static int opt_offset = 0; /* Index into compounded "-option" */
57 static int dashdash = 0; /* True if "--" option reached */
58 static int nonopt = 0; /* How many nonopts we've found */
60 static void increment_index()
62 /* Move onto the next option */
63 if(argv_index < argv_index2)
65 while(prev_argv[++argv_index] && prev_argv[argv_index][0] != '-'
66 && argv_index < argv_index2+1);
74 * Permutes argv[] so that the argument currently being processed is moved
77 static int permute_argv_once()
79 /* Movability check */
80 if(argv_index + nonopt >= prev_argc) return 1;
81 /* Move the current option to the end, bring the others to front */
84 char* tmp = prev_argv[argv_index];
87 memmove(&prev_argv[argv_index], &prev_argv[argv_index+1],
88 sizeof(char**) * (prev_argc - argv_index - 1));
89 prev_argv[prev_argc - 1] = tmp;
97 int getopt(int argc, char *const *argv, const char *optstr)
101 /* If we have new argv, reinitialize */
102 if(prev_argv != argv || prev_argc != argc)
104 /* Initialize variables */
114 /* Jump point in case we want to ignore the current argv_index */
117 /* Misc. initializations */
120 /* Dash-dash check */
121 if(argv[argv_index] && !strcmp(argv[argv_index], "--"))
127 /* If we're at the end of argv, that's it. */
128 if(argv[argv_index] == NULL)
132 /* Are we looking at a string? Single dash is also a string */
133 else if(dashdash || argv[argv_index][0] != '-' || !strcmp(argv[argv_index], "-"))
135 /* If we want a string... */
139 optarg = argv[argv_index];
142 /* If we really don't want it (we're in POSIX mode), we're done */
143 else if(optstr[0] == '+' || getenv("POSIXLY_CORRECT"))
147 /* Everything else is a non-opt argument */
148 nonopt = argc - argv_index;
150 /* If we mildly don't want it, then move it back */
153 if(!permute_argv_once()) goto getopt_top;
157 /* Otherwise we're looking at an option */
160 char* opt_ptr = NULL;
162 /* Grab the option */
163 c = argv[argv_index][opt_offset++];
165 /* Is the option in the optstr? */
166 if(optstr[0] == '-') opt_ptr = strchr(optstr+1, c);
167 else opt_ptr = strchr(optstr, c);
168 /* Invalid argument */
173 fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
179 /* Move onto the next option */
182 /* Option takes argument */
183 else if(opt_ptr[1] == ':')
185 /* ie, -oARGUMENT, -xxxoARGUMENT, etc. */
186 if(argv[argv_index][opt_offset] != '\0')
188 optarg = &argv[argv_index][opt_offset];
191 /* ie, -o ARGUMENT (only if it's a required argument) */
192 else if(opt_ptr[2] != ':')
194 /* One of those "you're not expected to understand this" moment */
195 if(argv_index2 < argv_index) argv_index2 = argv_index;
196 while(argv[++argv_index2] && argv[argv_index2][0] == '-');
197 optarg = argv[argv_index2];
199 /* Don't cross into the non-option argument list */
200 if(argv_index2 + nonopt >= prev_argc) optarg = NULL;
202 /* Move onto the next option */
207 /* Move onto the next option */
211 /* In case we got no argument for an option with required argument */
212 if(optarg == NULL && opt_ptr[2] != ':')
219 fprintf(stderr,"%s: option requires an argument -- %c\n",
224 /* Option does not take argument */
227 /* Next argv_index */
228 if(argv[argv_index][opt_offset] == '\0')
235 /* Calculate optind */
238 optind = argc - nonopt;