2 * Copyright 1989 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
7 * Copyright (c) 1987 Regents of the University of California.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that the above copyright notice and this paragraph are
12 * duplicated in all such forms and that any documentation,
13 * advertising materials, and other materials related to such
14 * distribution and use acknowledge that the software was developed
15 * by the University of California, Berkeley. The name of the
16 * University may not be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 #pragma ident "%Z%%M% %I% %E% SMI"
32 * get option letter from argument vector
34 /* See lib/libc/gen/common/optind.c for next 3 definitions. */
35 extern char *optarg
; /* argument associated with option */
36 extern int opterr
; /* if error message should be printed */
37 extern int optind
; /* index into parent argv vector */
38 int optopt
; /* character checked for validity */
41 #define BADCH (int)'?'
45 getopt(int nargc
, char **nargv
, char *ostr
)
47 static char *place
= EMSG
; /* option letter processing */
48 char *oli
; /* option letter list index */
51 if (!*place
) { /* update scanning pointer */
52 if (optind
>= nargc
|| *(place
= nargv
[optind
]) != '-') {
56 if (place
[1] && *++place
== '-') { /* found "--" */
61 } /* option letter okay? */
62 if ((optopt
= (int)*place
++) == (int)':' ||
63 !(oli
= strchr(ostr
, optopt
))) {
66 * For backwards compatibility: don't treat '-' as an
67 * option letter unless caller explicitly asked for it.
69 if (optopt
== (int)'-')
74 if (!(p
= strrchr(*nargv
, '/')))
78 (void)fprintf(stderr
, "%s: illegal option -- %c\n",
83 if (*++oli
!= ':') { /* don't need argument */
87 } else { /* need an argument */
88 if (*place
) /* no white space */
90 else if (nargc
<= ++optind
) { /* no arg */
92 if (!(p
= strrchr(*nargv
, '/')))
98 "%s: option requires an argument -- %c\n",
101 } else /* white space */
102 optarg
= nargv
[optind
];
106 return (optopt
); /* dump back option letter */