1 /*
2 * This file is part of the Advance project.
3 *
4 * Copyright (C) 2002 Andrea Mazzoleni
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #if HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #if !HAVE_GETOPT
26
27 /* This source is extracted from the DJGPP LIBC library */
28
29 #define unconst(var, type) ((type)(var))
30
31 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
32 #include <string.h>
33 #include <stdio.h>
34
35 int opterr = 1, optind = 1, optopt = 0;
36 char *optarg = 0;
37
38 #define BADCH (int)'?'
39 #define EMSG ""
40
getopt(int nargc,char * const nargv[],const char * ostr)41 int getopt(int nargc, char *const nargv[], const char *ostr)
42 {
43 static const char *place = EMSG; /* option letter processing */
44 char *oli; /* option letter list index */
45 char *p;
46
47 if (!*place)
48 {
49 if (optind >= nargc || *(place = nargv[optind]) != '-')
50 {
51 place = EMSG;
52 return(EOF);
53 }
54 if (place[1] && *++place == '-')
55 {
56 ++optind;
57 place = EMSG;
58 return(EOF);
59 }
60 }
61
62 if ((optopt = (int)*place++) == (int)':'
63 || !(oli = strchr(ostr, optopt)))
64 {
65 /*
66 * if the user didn't specify '-' as an option,
67 * assume it means EOF.
68 */
69 if (optopt == (int)'-')
70 return EOF;
71 if (!*place)
72 ++optind;
73 if (opterr)
74 {
75 if (!(p = strrchr(*nargv, '/')))
76 p = *nargv;
77 else
78 ++p;
79 fprintf(stderr, "%s: illegal option -- %c\n", p, optopt);
80 }
81 return BADCH;
82 }
83 if (*++oli != ':')
84 { /* don't need argument */
85 optarg = NULL;
86 if (!*place)
87 ++optind;
88 }
89 else
90 { /* need an argument */
91 if (*place) /* no white space */
92 optarg = unconst(place, char *);
93 else if (nargc <= ++optind)
94 { /* no arg */
95 place = EMSG;
96 if (!(p = strrchr(*nargv, '/')))
97 p = *nargv;
98 else
99 ++p;
100 if (opterr)
101 fprintf(stderr, "%s: option requires an argument -- %c\n", p, optopt);
102 return BADCH;
103 }
104 else /* white space */
105 optarg = nargv[optind];
106 place = EMSG;
107 ++optind;
108 }
109 return optopt; /* dump back option letter */
110 }
111
112 #endif
113
114