|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 /* |
|
6 * Copyright (c) 1987 Regents of the University of California. |
|
7 * All rights reserved. |
|
8 * |
|
9 * Redistribution and use in source and binary forms are permitted |
|
10 * provided that: (1) source distributions retain this entire copyright |
|
11 * notice and comment, and (2) distributions including binaries display |
|
12 * the following acknowledgement: ``This product includes software |
|
13 * developed by the University of California, Berkeley and its contributors'' |
|
14 * in the documentation or other materials provided with the distribution |
|
15 * and in all advertising materials mentioning features or use of this |
|
16 * software. Neither the name of the University nor the names of its |
|
17 * contributors may be used to endorse or promote products derived |
|
18 * from this software without specific prior written permission. |
|
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
|
20 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
|
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
|
22 */ |
|
23 |
|
24 #if defined(LIBC_SCCS) && !defined(lint) |
|
25 static char sccsid[] = "@(#)getopt.c 4.12 (Berkeley) 6/1/90"; |
|
26 #endif /* LIBC_SCCS and not lint */ |
|
27 |
|
28 #include <stdio.h> |
|
29 #include <string.h> |
|
30 #define index strchr |
|
31 #define rindex strrchr |
|
32 |
|
33 /* |
|
34 * get option letter from argument vector |
|
35 */ |
|
36 int opterr = 1, /* if error message should be printed */ |
|
37 optind = 1, /* index into parent argv vector */ |
|
38 optopt; /* character checked for validity */ |
|
39 char *optarg; /* argument associated with option */ |
|
40 |
|
41 #define BADCH (int)'?' |
|
42 #define EMSG "" |
|
43 |
|
44 int getopt(int nargc, char **nargv, char *ostr) |
|
45 { |
|
46 static char *place = EMSG; /* option letter processing */ |
|
47 register char *oli; /* option letter list index */ |
|
48 char *p; |
|
49 |
|
50 if (!*place) { /* update scanning pointer */ |
|
51 if (optind >= nargc || *(place = nargv[optind]) != '-') { |
|
52 place = EMSG; |
|
53 return(EOF); |
|
54 } |
|
55 if (place[1] && *++place == '-') { /* found "--" */ |
|
56 ++optind; |
|
57 place = EMSG; |
|
58 return(EOF); |
|
59 } |
|
60 } /* option letter okay? */ |
|
61 if ((optopt = (int)*place++) == (int)':' || |
|
62 !(oli = index(ostr, optopt))) { |
|
63 /* |
|
64 * if the user didn't specify '-' as an option, |
|
65 * assume it means EOF. |
|
66 */ |
|
67 if (optopt == (int)'-') |
|
68 return(EOF); |
|
69 if (!*place) |
|
70 ++optind; |
|
71 if (opterr) { |
|
72 if (!(p = rindex(*nargv, '/'))) |
|
73 p = *nargv; |
|
74 else |
|
75 ++p; |
|
76 (void)fprintf(stderr, "%s: illegal option -- %c\n", |
|
77 p, optopt); |
|
78 } |
|
79 return(BADCH); |
|
80 } |
|
81 if (*++oli != ':') { /* don't need argument */ |
|
82 optarg = NULL; |
|
83 if (!*place) |
|
84 ++optind; |
|
85 } |
|
86 else { /* need an argument */ |
|
87 if (*place) /* no white space */ |
|
88 optarg = place; |
|
89 else if (nargc <= ++optind) { /* no arg */ |
|
90 place = EMSG; |
|
91 if (!(p = rindex(*nargv, '/'))) |
|
92 p = *nargv; |
|
93 else |
|
94 ++p; |
|
95 if (opterr) |
|
96 (void)fprintf(stderr, |
|
97 "%s: option requires an argument -- %c\n", |
|
98 p, optopt); |
|
99 return(BADCH); |
|
100 } |
|
101 else /* white space */ |
|
102 optarg = nargv[optind]; |
|
103 place = EMSG; |
|
104 ++optind; |
|
105 } |
|
106 return(optopt); /* dump back option letter */ |
|
107 } |