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(nargc, nargv, ostr)
45 static char *place = EMSG; /* option letter processing */
46 register char *oli; /* option letter list index */
49 if (!*place) { /* update scanning pointer */
50 if (optind >= nargc || *(place = nargv[optind]) != '-') {
54 if (place[1] && *++place == '-') { /* found "--" */
59 } /* option letter okay? */
60 if ((optopt = (int)*place++) == (int)':' ||
61 !(oli = index(ostr, optopt))) {
63 * if the user didn't specify '-' as an option,
64 * assume it means EOF.
66 if (optopt == (int)'-')
71 if (!(p = rindex(*nargv, '/')))
75 (void)fprintf(stderr, "%s: illegal option -- %c\n",
80 if (*++oli != ':') { /* don't need argument */
85 else { /* need an argument */
86 if (*place) /* no white space */
88 else if (nargc <= ++optind) { /* no arg */
90 if (!(p = rindex(*nargv, '/')))
96 "%s: option requires an argument -- %c\n",
100 else /* white space */
101 optarg = nargv[optind];
105 return(optopt); /* dump back option letter */