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/. */
6 * Copyright (c) 1987 Regents of the University of California.
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.
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 */
31 #define rindex strrchr
34 * get option letter from argument vector
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 */
41 #define BADCH (int)'?'
44 int getopt(int nargc
, char **nargv
, char *ostr
)
46 static char *place
= EMSG
; /* option letter processing */
47 register char *oli
; /* option letter list index */
50 if (!*place
) { /* update scanning pointer */
51 if (optind
>= nargc
|| *(place
= nargv
[optind
]) != '-') {
55 if (place
[1] && *++place
== '-') { /* found "--" */
60 } /* option letter okay? */
61 if ((optopt
= (int)*place
++) == (int)':' ||
62 !(oli
= index(ostr
, optopt
))) {
64 * if the user didn't specify '-' as an option,
65 * assume it means EOF.
67 if (optopt
== (int)'-')
72 if (!(p
= rindex(*nargv
, '/')))
76 (void)fprintf(stderr
, "%s: illegal option -- %c\n",
81 if (*++oli
!= ':') { /* don't need argument */
86 else { /* need an argument */
87 if (*place
) /* no white space */
89 else if (nargc
<= ++optind
) { /* no arg */
91 if (!(p
= rindex(*nargv
, '/')))
97 "%s: option requires an argument -- %c\n",
101 else /* white space */
102 optarg
= nargv
[optind
];
106 return(optopt
); /* dump back option letter */