2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: getopt.c /main/3 1996/06/19 17:15:14 drk $ */
25 getopt() for those systems that don't have it.
27 Derived from comp.sources.unix/volume3/att_getopt.
28 Modified by James Clark (jjc@jclark.com).
48 #define OPTION_CHAR '-'
51 int getopt(argc, argv, opts)
58 static char switchar = '\0';
65 if (switchar == '\0') {
79 argv[optind][0] != switchar &&
81 argv[optind][0] != OPTION_CHAR) || argv[optind][1] == '\0') {
84 for (i = optind; i < argc; i++)
87 argv[i][0] == switchar ||
89 argv[i][0] == OPTION_CHAR) && argv[i][1] != '\0')
93 #ifdef CASE_INSENSITIVE_OPTIONS
97 if (c != ':' && c != OPTION_CHAR && (cp = strchr(opts, c)) != NULL
98 && cp[1] == ':' && argv[i][2] == 0 && i < argc - 1) {
100 char *temp1 = argv[i];
101 char *temp2 = argv[i+1];
102 for (j = i - 1; j >= optind; j--)
104 argv[optind] = temp1;
105 argv[optind+1] = temp2;
109 char *temp = argv[i];
110 for (j = i - 1; j >= optind; j--)
119 if ((argv[optind][0] == OPTION_CHAR && argv[optind][1] == OPTION_CHAR
120 && argv[optind][2] == '\0')
122 || (argv[optind][0] == switchar && argv[optind][1] == switchar
123 && argv[optind][2] == '\0')
130 optopt = c = argv[optind][sp];
131 #ifdef CASE_INSENSITIVE_OPTIONS
135 #endif /* USE_ISASCII */
136 isupper((unsigned char)c))
137 optopt = c = tolower((unsigned char)c);
138 #endif /* CASE_INSENSITIVE_OPTIONS */
139 if (c == ':' || (cp = strchr(opts, c)) == NULL) {
140 if (argv[optind][++sp] == '\0') {
144 message = ": illegal option -- ";
148 if (argv[optind][sp+1] != '\0')
149 optarg = &argv[optind++][sp+1];
150 else if (++optind >= argc) {
152 message = ": option requires an argument -- ";
156 optarg = argv[optind++];
160 if (argv[optind][++sp] == '\0') {
169 fputs(argv[0], stderr);
170 fputs(message, stderr);
171 fputc(optopt, stderr);
177 #endif /* not HAVE_GETOPT */
182 c-continued-statement-offset: 4