diff options
author | millert <> | 1997-08-20 04:02:18 +0000 |
---|---|---|
committer | millert <> | 1997-08-20 04:02:18 +0000 |
commit | 4eac491513b57fb0e90b92aa5b40f29e9326b233 (patch) | |
tree | 97bbc523a87435947b58a9d65c052d5daf7de3d0 /src | |
parent | 66fba11047b8a3b1be524ba8872525f723bee731 (diff) | |
download | openbsd-4eac491513b57fb0e90b92aa5b40f29e9326b233.tar.gz openbsd-4eac491513b57fb0e90b92aa5b40f29e9326b233.tar.bz2 openbsd-4eac491513b57fb0e90b92aa5b40f29e9326b233.zip |
Move getsubopt(3) to stdlib like lite2 and XPG4.2 say...
Update man page from lite2 while I'm at it.
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/Makefile.inc | 12 | ||||
-rw-r--r-- | src/lib/libc/stdlib/getsubopt.3 | 147 | ||||
-rw-r--r-- | src/lib/libc/stdlib/getsubopt.c | 106 |
3 files changed, 259 insertions, 6 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index fe343587f9..e75fc0d8bf 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -4,10 +4,10 @@ | |||
4 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib | 4 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib |
5 | 5 | ||
6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ | 6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ |
7 | cfree.c exit.c getenv.c getopt.c heapsort.c l64a.c malloc.c merge.c \ | 7 | cfree.c exit.c getenv.c getopt.c getsubopt.c heapsort.c l64a.c \ |
8 | multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ | 8 | malloc.c merge.c multibyte.c putenv.c qsort.c radixsort.c rand.c \ |
9 | setenv.c strtod.c strtol.c strtoq.c strtoul.c strtouq.c system.c \ | 9 | random.c realpath.c setenv.c strtod.c strtol.c strtoq.c strtoul.c \ |
10 | tfind.c tsearch.c \ | 10 | strtouq.c system.c tfind.c tsearch.c \ |
11 | _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ | 11 | _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ |
12 | mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c | 12 | mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c |
13 | 13 | ||
@@ -33,8 +33,8 @@ SRCS+= abs.c div.c labs.c ldiv.c | |||
33 | .endif | 33 | .endif |
34 | 34 | ||
35 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ | 35 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ |
36 | calloc.3 div.3 exit.3 getenv.3 getopt.3 labs.3 ldiv.3 malloc.3 \ | 36 | calloc.3 div.3 exit.3 getenv.3 getopt.3 getsubopt.3 labs.3 ldiv.3 \ |
37 | memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 \ | 37 | malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 \ |
38 | random.3 realpath.3 strtod.3 strtol.3 strtoul.3 system.3 tsearch.3 | 38 | random.3 realpath.3 strtod.3 strtol.3 strtoul.3 system.3 tsearch.3 |
39 | 39 | ||
40 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 | 40 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 |
diff --git a/src/lib/libc/stdlib/getsubopt.3 b/src/lib/libc/stdlib/getsubopt.3 new file mode 100644 index 0000000000..06e089203f --- /dev/null +++ b/src/lib/libc/stdlib/getsubopt.3 | |||
@@ -0,0 +1,147 @@ | |||
1 | .\" $OpenBSD: getsubopt.3,v 1.1 1997/08/20 04:02:17 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1990, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. All advertising materials mentioning features or use of this software | ||
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | ||
20 | .\" without specific prior written permission. | ||
21 | .\" | ||
22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | .\" SUCH DAMAGE. | ||
33 | .\" | ||
34 | .\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93 | ||
35 | .\" | ||
36 | .Dd June 9, 1993 | ||
37 | .Dt GETSUBOPT 3 | ||
38 | .Os | ||
39 | .Sh NAME | ||
40 | .Nm getsubopt | ||
41 | .Nd get sub options from an argument | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Vt extern char *suboptarg | ||
45 | .Ft int | ||
46 | .Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn getsubopt | ||
50 | function | ||
51 | parses a string containing tokens delimited by one or more tab, space or | ||
52 | comma | ||
53 | .Pq Ql \&, | ||
54 | characters. | ||
55 | It is intended for use in parsing groups of option arguments provided | ||
56 | as part of a utility command line. | ||
57 | .Pp | ||
58 | The argument | ||
59 | .Fa optionp | ||
60 | is a pointer to a pointer to the string. | ||
61 | The argument | ||
62 | .Fa tokens | ||
63 | is a pointer to a | ||
64 | .Dv NULL Ns -terminated | ||
65 | array of pointers to strings. | ||
66 | .Pp | ||
67 | The | ||
68 | .Fn getsubopt | ||
69 | function | ||
70 | returns the zero-based offset of the pointer in the | ||
71 | .Fa tokens | ||
72 | array referencing a string which matches the first token | ||
73 | in the string, or, \-1 if the string contains no tokens or | ||
74 | .Fa tokens | ||
75 | does not contain a matching string. | ||
76 | .Pp | ||
77 | If the token is of the form ``name=value'', the location referenced by | ||
78 | .Fa valuep | ||
79 | will be set to point to the start of the ``value'' portion of the token. | ||
80 | .Pp | ||
81 | On return from | ||
82 | .Fn getsubopt , | ||
83 | .Fa optionp | ||
84 | will be set to point to the start of the next token in the string, | ||
85 | or the null at the end of the string if no more tokens are present. | ||
86 | The external variable | ||
87 | .Fa suboptarg | ||
88 | will be set to point to the start of the current token, or | ||
89 | .Dv NULL | ||
90 | if no | ||
91 | tokens were present. | ||
92 | The argument | ||
93 | .Fa valuep | ||
94 | will be set to point to the ``value'' portion of the token, or | ||
95 | .Dv NULL | ||
96 | if no ``value'' portion was present. | ||
97 | .Sh EXAMPLE | ||
98 | .Bd -literal -compact | ||
99 | char *tokens[] = { | ||
100 | #define ONE 0 | ||
101 | "one", | ||
102 | #define TWO 1 | ||
103 | "two", | ||
104 | NULL | ||
105 | }; | ||
106 | |||
107 | \&... | ||
108 | |||
109 | extern char *optarg, *suboptarg; | ||
110 | char *options, *value; | ||
111 | |||
112 | while ((ch = getopt(argc, argv, "ab:")) != \-1) { | ||
113 | switch(ch) { | ||
114 | case 'a': | ||
115 | /* process ``a'' option */ | ||
116 | break; | ||
117 | case 'b': | ||
118 | options = optarg; | ||
119 | while (*options) { | ||
120 | switch(getsubopt(&options, tokens, &value)) { | ||
121 | case ONE: | ||
122 | /* process ``one'' sub option */ | ||
123 | break; | ||
124 | case TWO: | ||
125 | /* process ``two'' sub option */ | ||
126 | if (!value) | ||
127 | error("no value for two"); | ||
128 | i = atoi(value); | ||
129 | break; | ||
130 | case \-1: | ||
131 | if (suboptarg) | ||
132 | error("illegal sub option %s", | ||
133 | suboptarg); | ||
134 | else | ||
135 | error("missing sub option"); | ||
136 | break; | ||
137 | } | ||
138 | break; | ||
139 | } | ||
140 | .Ed | ||
141 | .Sh SEE ALSO | ||
142 | .Xr getopt 3 , | ||
143 | .Xr strsep 3 | ||
144 | .Sh HISTORY | ||
145 | The | ||
146 | .Fn getsubopt | ||
147 | function first appeared in 4.4BSD. | ||
diff --git a/src/lib/libc/stdlib/getsubopt.c b/src/lib/libc/stdlib/getsubopt.c new file mode 100644 index 0000000000..1667a31d7d --- /dev/null +++ b/src/lib/libc/stdlib/getsubopt.c | |||
@@ -0,0 +1,106 @@ | |||
1 | /* $OpenBSD: getsubopt.c,v 1.1 1997/08/20 04:02:17 millert Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1990, 1993 | ||
5 | * The Regents of the University of California. All rights reserved. | ||
6 | * | ||
7 | * Redistribution and use in source and binary forms, with or without | ||
8 | * modification, are permitted provided that the following conditions | ||
9 | * are met: | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer in the | ||
14 | * documentation and/or other materials provided with the distribution. | ||
15 | * 3. All advertising materials mentioning features or use of this software | ||
16 | * must display the following acknowledgement: | ||
17 | * This product includes software developed by the University of | ||
18 | * California, Berkeley and its contributors. | ||
19 | * 4. Neither the name of the University nor the names of its contributors | ||
20 | * may be used to endorse or promote products derived from this software | ||
21 | * without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
33 | * SUCH DAMAGE. | ||
34 | */ | ||
35 | |||
36 | #ifndef lint | ||
37 | #if 0 | ||
38 | static char sccsid[] = "@(#)getsubopt.c 8.1 (Berkeley) 6/4/93"; | ||
39 | #else | ||
40 | static char rcsid[] = "$OpenBSD: getsubopt.c,v 1.1 1997/08/20 04:02:17 millert Exp $"; | ||
41 | #endif | ||
42 | #endif /* not lint */ | ||
43 | |||
44 | #include <unistd.h> | ||
45 | #include <stdlib.h> | ||
46 | #include <string.h> | ||
47 | |||
48 | /* | ||
49 | * The SVID interface to getsubopt provides no way of figuring out which | ||
50 | * part of the suboptions list wasn't matched. This makes error messages | ||
51 | * tricky... The extern variable suboptarg is a pointer to the token | ||
52 | * which didn't match. | ||
53 | */ | ||
54 | char *suboptarg; | ||
55 | |||
56 | int | ||
57 | getsubopt(optionp, tokens, valuep) | ||
58 | register char **optionp, **valuep; | ||
59 | register char * const *tokens; | ||
60 | { | ||
61 | register int cnt; | ||
62 | register char *p; | ||
63 | |||
64 | suboptarg = *valuep = NULL; | ||
65 | |||
66 | if (!optionp || !*optionp) | ||
67 | return(-1); | ||
68 | |||
69 | /* skip leading white-space, commas */ | ||
70 | for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); | ||
71 | |||
72 | if (!*p) { | ||
73 | *optionp = p; | ||
74 | return(-1); | ||
75 | } | ||
76 | |||
77 | /* save the start of the token, and skip the rest of the token. */ | ||
78 | for (suboptarg = p; | ||
79 | *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';); | ||
80 | |||
81 | if (*p) { | ||
82 | /* | ||
83 | * If there's an equals sign, set the value pointer, and | ||
84 | * skip over the value part of the token. Terminate the | ||
85 | * token. | ||
86 | */ | ||
87 | if (*p == '=') { | ||
88 | *p = '\0'; | ||
89 | for (*valuep = ++p; | ||
90 | *p && *p != ',' && *p != ' ' && *p != '\t'; ++p); | ||
91 | if (*p) | ||
92 | *p++ = '\0'; | ||
93 | } else | ||
94 | *p++ = '\0'; | ||
95 | /* Skip any whitespace or commas after this token. */ | ||
96 | for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); | ||
97 | } | ||
98 | |||
99 | /* set optionp for next round. */ | ||
100 | *optionp = p; | ||
101 | |||
102 | for (cnt = 0; *tokens; ++tokens, ++cnt) | ||
103 | if (!strcmp(suboptarg, *tokens)) | ||
104 | return(cnt); | ||
105 | return(-1); | ||
106 | } | ||