Skip to content

Commit 3a0493e

Browse files
author
Cameron Elliott
committed
unix style getopt()
1 parent eb5dca4 commit 3a0493e

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

getopt.c

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
#include "getopt.h" // make sure you construct the header file as dictated above
3+
4+
/*
5+
* Copyright (c) 1987, 1993, 1994
6+
* The Regents of the University of California. All rights reserved.
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
* 3. All advertising materials mentioning features or use of this software
17+
* must display the following acknowledgement:
18+
* This product includes software developed by the University of
19+
* California, Berkeley and its contributors.
20+
* 4. Neither the name of the University nor the names of its contributors
21+
* may be used to endorse or promote products derived from this software
22+
* without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34+
* SUCH DAMAGE.
35+
*/
36+
37+
#include <string.h>
38+
#include <stdio.h>
39+
40+
int opterr = 1, /* if error message should be printed */
41+
optind = 1, /* index into parent argv vector */
42+
optopt, /* character checked for validity */
43+
optreset; /* reset getopt */
44+
char *optarg; /* argument associated with option */
45+
46+
#define BADCH (int)'?'
47+
#define BADARG (int)':'
48+
#define EMSG ""
49+
50+
/*
51+
* getopt --
52+
* Parse argc/argv argument vector.
53+
*/
54+
int getopt(int nargc, char * const nargv[], const char *ostr)
55+
{
56+
static char *place = EMSG; /* option letter processing */
57+
const char *oli; /* option letter list index */
58+
59+
if (optreset || !*place) { /* update scanning pointer */
60+
optreset = 0;
61+
if (optind >= nargc || *(place = nargv[optind]) != '-') {
62+
place = EMSG;
63+
return (-1);
64+
}
65+
if (place[1] && *++place == '-') { /* found "--" */
66+
++optind;
67+
place = EMSG;
68+
return (-1);
69+
}
70+
} /* option letter okay? */
71+
if ((optopt = (int)*place++) == (int)':' ||
72+
!(oli = strchr(ostr, optopt))) {
73+
/*
74+
* if the user didn't specify '-' as an option,
75+
* assume it means -1.
76+
*/
77+
if (optopt == (int)'-')
78+
return (-1);
79+
if (!*place)
80+
++optind;
81+
if (opterr && *ostr != ':')
82+
(void)printf("illegal option -- %c\n", optopt);
83+
return (BADCH);
84+
}
85+
if (*++oli != ':') { /* don't need argument */
86+
optarg = NULL;
87+
if (!*place)
88+
++optind;
89+
}
90+
else { /* need an argument */
91+
if (*place) /* no white space */
92+
optarg = place;
93+
else if (nargc <= ++optind) { /* no arg */
94+
place = EMSG;
95+
if (*ostr == ':')
96+
return (BADARG);
97+
if (opterr)
98+
(void)printf("option requires an argument -- %c\n", optopt);
99+
return (BADCH);
100+
}
101+
else /* white space */
102+
optarg = nargv[optind];
103+
place = EMSG;
104+
++optind;
105+
}
106+
return (optopt); /* dump back option letter */
107+
}
108+

getopt.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Put this in a separate .h file (called "getopt.h").
2+
// The prototype for the header file is:
3+
4+
#ifndef GETOPT_H
5+
#define GETOPT_H
6+
7+
int getopt(int nargc, char * const nargv[], const char *ostr) ;
8+
char *optarg;
9+
#endif
10+

0 commit comments

Comments
 (0)