typo...
[unix-history] / usr / src / lib / libc / stdlib / getopt.c
CommitLineData
a49db6dd 1/*
cde75055
KB
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
50c7758a
KB
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
a49db6dd
KM
16 */
17
2ce81398 18#if defined(LIBC_SCCS) && !defined(lint)
50c7758a 19static char sccsid[] = "@(#)getopt.c 4.7 (Berkeley) %G%";
aa5d4ab9 20#endif /* LIBC_SCCS and not lint */
a49db6dd
KM
21
22#include <stdio.h>
23
24/*
25 * get option letter from argument vector
26 */
21482c09 27int opterr = 1, /* if error message should be printed */
a49db6dd
KM
28 optind = 1, /* index into parent argv vector */
29 optopt; /* character checked for validity */
30char *optarg; /* argument associated with option */
31
aa5d4ab9
KB
32#define BADCH (int)'?'
33#define EMSG ""
34#define tell(s) { \
86cd40d8
KB
35 if (opterr) { \
36 fputs(*nargv, stderr); \
37 fputs(s, stderr); \
38 fputc(optopt, stderr); \
39 fputc((int)'\n', stderr); \
40 } \
41 return(BADCH); \
42}
a49db6dd 43
86cd40d8 44getopt(nargc, nargv, ostr)
aa5d4ab9
KB
45 int nargc;
46 char **nargv, *ostr;
a49db6dd 47{
aa5d4ab9
KB
48 static char *place = EMSG; /* option letter processing */
49 register char *oli; /* option letter list index */
50 char *index();
a49db6dd 51
86cd40d8 52 if (!*place) { /* update scanning pointer */
aa5d4ab9 53 if (optind >= nargc || *(place = nargv[optind]) != '-')
86cd40d8 54 return(EOF);
aa5d4ab9 55 if (place[1] && *++place == '-') { /* found "--" */
a49db6dd
KM
56 ++optind;
57 return(EOF);
58 }
86cd40d8
KB
59 } /* option letter okay? */
60 if ((optopt = (int)*place++) == (int)':' ||
61 !(oli = index(ostr, optopt))) {
62 if (!*place)
63 ++optind;
a49db6dd
KM
64 tell(": illegal option -- ");
65 }
86cd40d8 66 if (*++oli != ':') { /* don't need argument */
a49db6dd 67 optarg = NULL;
86cd40d8
KB
68 if (!*place)
69 ++optind;
a49db6dd 70 }
86cd40d8
KB
71 else { /* need an argument */
72 if (*place) /* no white space */
73 optarg = place;
a49db6dd
KM
74 else if (nargc <= ++optind) { /* no arg */
75 place = EMSG;
76 tell(": option requires an argument -- ");
77 }
86cd40d8
KB
78 else /* white space */
79 optarg = nargv[optind];
a49db6dd
KM
80 place = EMSG;
81 ++optind;
82 }
86cd40d8 83 return(optopt); /* dump back option letter */
a49db6dd 84}