]>
Commit | Line | Data |
---|---|---|
a71ece51 | 1 | /*---------------------------------------------------------------------- |
2 | ||
3 | Replacement for Unix "getopt()", for DOS/Windows/etc. | |
4 | ||
5 | getopt.c 1.3 2003/09/17 16:17:59 | |
6 | ||
7 | Copyright (C) 1998, 2003 by David A. Hinds -- All Rights Reserved | |
8 | ||
9 | This file is part of ASPEX. | |
10 | ||
11 | ASPEX is free software; you can redistribute it and/or modify it | |
12 | under the terms of the GNU General Public License as published by | |
13 | the Free Software Foundation; either version 2 of the License, or | |
14 | (at your option) any later version. | |
15 | ||
16 | ASPEX is distributed in the hope that it will be useful, but | |
17 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
19 | General Public License for more details. | |
20 | ||
21 | You should have received a copy of the GNU General Public License | |
22 | along with ASPEX; if not, write to the Free Software Foundation, | |
23 | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
24 | ||
25 | ----------------------------------------------------------------------*/ | |
26 | ||
27 | #include "string.h" | |
28 | #include "stdio.h" | |
29 | #include "getopt.h" | |
30 | ||
31 | char *optarg; | |
32 | int optind = 1, opterr, optopt; | |
33 | int pos = 0; | |
34 | int getopt(int argc, char *argv[], const char *optstring) | |
35 | { | |
36 | //static int pos = 0; | |
37 | char *str; | |
38 | ||
39 | if (pos == 0) { | |
40 | if ((optind >= argc) || (*argv[optind] != '-')) | |
41 | return EOF; | |
42 | pos = 1; | |
43 | if (argv[optind][pos] == '\0') | |
44 | return EOF; | |
45 | } | |
46 | ||
47 | str = strchr(optstring, argv[optind][pos]); | |
48 | if (str == NULL) { | |
49 | optopt = argv[optind][pos]; | |
50 | if (opterr) | |
51 | fprintf(stderr, "%s: illegal option -- %c\n", argv[0], | |
52 | optopt); | |
53 | return '?'; | |
54 | } | |
55 | ||
56 | if (str[1] == ':') { | |
57 | if (argv[optind][pos+1] != '\0') { | |
58 | optarg = &argv[optind][pos+1]; | |
59 | return *str; | |
60 | } | |
61 | optind++; | |
62 | if (optind >= argc) { | |
63 | optopt = *str; | |
64 | if (opterr) | |
65 | fprintf(stderr, "%s: option requires an argument -- %c\n", | |
66 | argv[0], optopt); | |
67 | return '?'; | |
68 | } | |
69 | optarg = argv[optind]; | |
70 | optind++; pos = 0; | |
71 | return *str; | |
72 | } | |
73 | else { | |
74 | pos++; | |
75 | if (argv[optind][pos] == '\0') { | |
76 | optind++; | |
77 | pos = 0; | |
78 | } | |
79 | return *str; | |
80 | } | |
81 | } |