2 * Copyright (c) 1987 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static char sccsid[] = "@(#)getopt.c 4.12 (Berkeley) 6/1/90";
22 #endif /* LIBC_SCCS and not lint */
28 #define rindex strrchr
31 * get option letter from argument vector
33 int opterr = 1, /* if error message should be printed */
34 optind = 1, /* index into parent argv vector */
35 optopt; /* character checked for validity */
36 char *optarg; /* argument associated with option */
38 #define BADCH (int)'?'
41 getopt(int nargc, char **nargv, char *ostr)
43 static char *place = EMSG; /* option letter processing */
44 register char *oli; /* option letter list index */
47 if (!*place) { /* update scanning pointer */
48 if (optind >= nargc || *(place = nargv[optind]) != '-') {
52 if (place[1] && *++place == '-') { /* found "--" */
57 } /* option letter okay? */
58 if ((optopt = (int)*place++) == (int)':' ||
59 !(oli = index(ostr, optopt))) {
61 * if the user didn't specify '-' as an option,
62 * assume it means EOF.
64 if (optopt == (int)'-')
69 if (!(p = rindex(*nargv, '/')))
73 (void)fprintf(stderr, "%s: illegal option -- %c\n",
78 if (*++oli != ':') { /* don't need argument */
83 else { /* need an argument */
84 if (*place) /* no white space */
86 else if (nargc <= ++optind) { /* no arg */
88 if (!(p = rindex(*nargv, '/')))
94 "%s: option requires an argument -- %c\n",
98 else /* white space */
99 optarg = nargv[optind];
103 return(optopt); /* dump back option letter */