1 /*---------------------------------------------------------------------------*
6 * Copyright 1992-1994, David Gottner
10 * Permission to use, copy, modify, and distribute this software and its
11 * documentation for any purpose and without fee is hereby granted,
12 * provided that the above copyright notice, this permission notice and
13 * the following disclaimer notice appear unmodified in all copies.
15 * I DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL I
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
18 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA, OR PROFITS, WHETHER
19 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 * Nevertheless, I would like to know about bugs in this library or
23 * suggestions for improvment. Send bug reports and feedback to
24 * davegottner@delphi.com.
25 *---------------------------------------------------------------------------*/
38 bool opterr
= TRUE
; /* generate error messages */
39 int optind
= 1; /* index into argv array */
40 char * optarg
= NULL
; /* optional argument */
44 int getopt(int argc
, char *argv
[], char optstring
[])
46 int getopt(int argc
, char *const *argv
, const char *optstring
)
49 static char *opt_ptr
= "";
53 if (*opt_ptr
== '\0') {
55 if (optind
>= argc
|| argv
[optind
][0] != '-' ||
56 argv
[optind
][1] == '\0' /* lone dash */ )
59 else if (strcmp(argv
[optind
], "--") == 0) {
64 opt_ptr
= &argv
[optind
++][1];
67 if ( (option
= *opt_ptr
++) == '\0')
70 if ((ptr
= strchr(optstring
, option
)) == NULL
) {
72 fprintf(stderr
, "Unknown option: -%c\n", option
);
77 if (*(ptr
+ 1) == ':') {
78 if (*opt_ptr
!= '\0') {
87 "Argument expected for the -%c option\n", option
);
91 optarg
= argv
[optind
++];