diff options
Diffstat (limited to 'src/lib/libc')
269 files changed, 44333 insertions, 0 deletions
diff --git a/src/lib/libc/crypt/Makefile.inc b/src/lib/libc/crypt/Makefile.inc new file mode 100644 index 0000000000..131e1d3f39 --- /dev/null +++ b/src/lib/libc/crypt/Makefile.inc | |||
@@ -0,0 +1,15 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.15 2003/08/12 01:22:17 deraadt Exp $ | ||
2 | |||
3 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/crypt ${LIBCSRCDIR}/crypt | ||
4 | |||
5 | SRCS+= cast.c crypt.c crypt2.c md5crypt.c arc4random.c blowfish.c | ||
6 | SRCS+= bcrypt.c skipjack.c | ||
7 | |||
8 | MAN+= crypt.3 blowfish.3 arc4random.3 | ||
9 | MLINKS+=crypt.3 encrypt.3 crypt.3 setkey.3 crypt.3 des_cipher.3 | ||
10 | MLINKS+=crypt.3 bcrypt_gensalt.3 crypt.3 bcrypt.3 crypt.3 md5crypt.3 | ||
11 | MLINKS+=crypt.3 des_setkey.3 blowfish.3 blf_key.3 blowfish.3 blf_enc.3 | ||
12 | MLINKS+=blowfish.3 blf_dec.3 blowfish.3 blf_ecb_encrypt.3 | ||
13 | MLINKS+=blowfish.3 blf_ecb_decrypt.3 blowfish.3 blf_cbc_encrypt.3 | ||
14 | MLINKS+=blowfish.3 blf_cbc_decrypt.3 | ||
15 | MLINKS+=arc4random.3 arc4random_stir.3 arc4random.3 arc4random_addrandom.3 | ||
diff --git a/src/lib/libc/crypt/arc4random.3 b/src/lib/libc/crypt/arc4random.3 new file mode 100644 index 0000000000..d8e0f8cda6 --- /dev/null +++ b/src/lib/libc/crypt/arc4random.3 | |||
@@ -0,0 +1,110 @@ | |||
1 | .\" $OpenBSD: arc4random.3,v 1.17 2000/12/21 14:07:41 aaron Exp $ | ||
2 | .\" | ||
3 | .\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | ||
4 | .\" 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 Niels Provos. | ||
17 | .\" 4. The name of the author may not be used to endorse or promote products | ||
18 | .\" derived from this software without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
21 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
22 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
23 | .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
24 | .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
25 | .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
26 | .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
27 | .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
28 | .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
29 | .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" Manual page, using -mandoc macros | ||
32 | .\" | ||
33 | .Dd April 15, 1997 | ||
34 | .Dt ARC4RANDOM 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm arc4random , | ||
38 | .Nm arc4random_stir , | ||
39 | .Nm arc4random_addrandom | ||
40 | .Nd arc4 random number generator | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <stdlib.h> | ||
43 | .Ft u_int32_t | ||
44 | .Fn arc4random "void" | ||
45 | .Ft void | ||
46 | .Fn arc4random_stir "void" | ||
47 | .Ft void | ||
48 | .Fn arc4random_addrandom "u_char *dat" "int datlen" | ||
49 | .Sh DESCRIPTION | ||
50 | The | ||
51 | .Fn arc4random | ||
52 | function provides a high quality 32-bit pseudo-random | ||
53 | number very quickly. | ||
54 | .Fn arc4random | ||
55 | seeds itself on a regular basis from the kernel strong random number | ||
56 | subsystem described in | ||
57 | .Xr random 4 . | ||
58 | On each call, an ARC4 generator is used to generate a new result. | ||
59 | The | ||
60 | .Fn arc4random | ||
61 | function uses the ARC4 cipher key stream generator, | ||
62 | which uses 8*8 8 bit S-Boxes. | ||
63 | The S-Boxes can be in about (2**1700) states. | ||
64 | .Pp | ||
65 | .Fn arc4random | ||
66 | fits into a middle ground not covered by other subsystems such as | ||
67 | the strong, slow, and resource expensive random | ||
68 | devices described in | ||
69 | .Xr random 4 | ||
70 | versus the fast but poor quality interfaces described in | ||
71 | .Xr rand 3 , | ||
72 | .Xr random 3 , | ||
73 | and | ||
74 | .Xr drand48 3 . | ||
75 | .Pp | ||
76 | The | ||
77 | .Fn arc4random_stir | ||
78 | function reads data from | ||
79 | .Pa /dev/arandom | ||
80 | and uses it to permute the S-Boxes via | ||
81 | .Fn arc4random_addrandom . | ||
82 | .Pp | ||
83 | There is no need to call | ||
84 | .Fn arc4random_stir | ||
85 | before using | ||
86 | .Fn arc4random , | ||
87 | since | ||
88 | .Fn arc4random | ||
89 | automatically initializes itself. | ||
90 | .Sh SEE ALSO | ||
91 | .Xr rand 3 , | ||
92 | .Xr rand48 3 , | ||
93 | .Xr random 3 | ||
94 | .Sh HISTORY | ||
95 | An algorithm called | ||
96 | .Pa RC4 | ||
97 | was designed by RSA Data Security, Inc. | ||
98 | It was considered a trade secret, but not trademarked. | ||
99 | Because it was a trade secret, it obviously could not be patented. | ||
100 | A clone of this was posted anonymously to USENET and confirmed to | ||
101 | be equivalent by several sources who had access to the original cipher. | ||
102 | Because of the trade secret situation, RSA Data Security, Inc. can do | ||
103 | nothing about the release of the ARC4 algorithm. | ||
104 | Since | ||
105 | .Pa RC4 | ||
106 | used to be a trade secret, the cipher is now referred to as | ||
107 | .Pa ARC4 . | ||
108 | .Pp | ||
109 | These functions first appeared in | ||
110 | .Ox 2.1 . | ||
diff --git a/src/lib/libc/crypt/arc4random.c b/src/lib/libc/crypt/arc4random.c new file mode 100644 index 0000000000..5e3b2925a6 --- /dev/null +++ b/src/lib/libc/crypt/arc4random.c | |||
@@ -0,0 +1,173 @@ | |||
1 | /* $OpenBSD: arc4random.c,v 1.9 2003/08/16 19:07:40 tedu Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Arc4 random number generator for OpenBSD. | ||
5 | * Copyright 1996 David Mazieres <dm@lcs.mit.edu>. | ||
6 | * | ||
7 | * Modification and redistribution in source and binary forms is | ||
8 | * permitted provided that due credit is given to the author and the | ||
9 | * OpenBSD project by leaving this copyright notice intact. | ||
10 | */ | ||
11 | |||
12 | /* | ||
13 | * This code is derived from section 17.1 of Applied Cryptography, | ||
14 | * second edition, which describes a stream cipher allegedly | ||
15 | * compatible with RSA Labs "RC4" cipher (the actual description of | ||
16 | * which is a trade secret). The same algorithm is used as a stream | ||
17 | * cipher called "arcfour" in Tatu Ylonen's ssh package. | ||
18 | * | ||
19 | * Here the stream cipher has been modified always to include the time | ||
20 | * when initializing the state. That makes it impossible to | ||
21 | * regenerate the same random sequence twice, so this can't be used | ||
22 | * for encryption, but will generate good random numbers. | ||
23 | * | ||
24 | * RC4 is a registered trademark of RSA Laboratories. | ||
25 | */ | ||
26 | |||
27 | #include <fcntl.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <unistd.h> | ||
30 | #include <sys/types.h> | ||
31 | #include <sys/param.h> | ||
32 | #include <sys/time.h> | ||
33 | #include <sys/sysctl.h> | ||
34 | |||
35 | #ifdef __GNUC__ | ||
36 | #define inline __inline | ||
37 | #else /* !__GNUC__ */ | ||
38 | #define inline | ||
39 | #endif /* !__GNUC__ */ | ||
40 | |||
41 | struct arc4_stream { | ||
42 | u_int8_t i; | ||
43 | u_int8_t j; | ||
44 | u_int8_t s[256]; | ||
45 | }; | ||
46 | |||
47 | static int rs_initialized; | ||
48 | static struct arc4_stream rs; | ||
49 | static pid_t arc4_stir_pid; | ||
50 | |||
51 | static inline void | ||
52 | arc4_init(struct arc4_stream *as) | ||
53 | { | ||
54 | int n; | ||
55 | |||
56 | for (n = 0; n < 256; n++) | ||
57 | as->s[n] = n; | ||
58 | as->i = 0; | ||
59 | as->j = 0; | ||
60 | } | ||
61 | |||
62 | static inline void | ||
63 | arc4_addrandom(struct arc4_stream *as, u_char *dat, int datlen) | ||
64 | { | ||
65 | int n; | ||
66 | u_int8_t si; | ||
67 | |||
68 | as->i--; | ||
69 | for (n = 0; n < 256; n++) { | ||
70 | as->i = (as->i + 1); | ||
71 | si = as->s[as->i]; | ||
72 | as->j = (as->j + si + dat[n % datlen]); | ||
73 | as->s[as->i] = as->s[as->j]; | ||
74 | as->s[as->j] = si; | ||
75 | } | ||
76 | as->j = as->i; | ||
77 | } | ||
78 | |||
79 | static void | ||
80 | arc4_stir(struct arc4_stream *as) | ||
81 | { | ||
82 | int i, mib[2]; | ||
83 | size_t len; | ||
84 | struct { | ||
85 | struct timeval tv; | ||
86 | u_int rnd[(128 - sizeof(struct timeval)) / sizeof(u_int)]; | ||
87 | } rdat; | ||
88 | |||
89 | gettimeofday(&rdat.tv, NULL); | ||
90 | mib[0] = CTL_KERN; | ||
91 | mib[1] = KERN_ARND; | ||
92 | |||
93 | for (i = 0; i < sizeof(rdat.rnd) / sizeof(u_int); i ++) { | ||
94 | len = sizeof(u_int); | ||
95 | if (sysctl(mib, 2, &rdat.rnd[i], &len, NULL, 0) == -1) | ||
96 | break; | ||
97 | } | ||
98 | |||
99 | arc4_stir_pid = getpid(); | ||
100 | arc4_addrandom(as, (void *) &rdat, sizeof(rdat)); | ||
101 | } | ||
102 | |||
103 | static inline u_int8_t | ||
104 | arc4_getbyte(struct arc4_stream *as) | ||
105 | { | ||
106 | u_int8_t si, sj; | ||
107 | |||
108 | as->i = (as->i + 1); | ||
109 | si = as->s[as->i]; | ||
110 | as->j = (as->j + si); | ||
111 | sj = as->s[as->j]; | ||
112 | as->s[as->i] = sj; | ||
113 | as->s[as->j] = si; | ||
114 | return (as->s[(si + sj) & 0xff]); | ||
115 | } | ||
116 | |||
117 | static inline u_int32_t | ||
118 | arc4_getword(struct arc4_stream *as) | ||
119 | { | ||
120 | u_int32_t val; | ||
121 | val = arc4_getbyte(as) << 24; | ||
122 | val |= arc4_getbyte(as) << 16; | ||
123 | val |= arc4_getbyte(as) << 8; | ||
124 | val |= arc4_getbyte(as); | ||
125 | return val; | ||
126 | } | ||
127 | |||
128 | void | ||
129 | arc4random_stir(void) | ||
130 | { | ||
131 | if (!rs_initialized) { | ||
132 | arc4_init(&rs); | ||
133 | rs_initialized = 1; | ||
134 | } | ||
135 | arc4_stir(&rs); | ||
136 | } | ||
137 | |||
138 | void | ||
139 | arc4random_addrandom(u_char *dat, int datlen) | ||
140 | { | ||
141 | if (!rs_initialized) | ||
142 | arc4random_stir(); | ||
143 | arc4_addrandom(&rs, dat, datlen); | ||
144 | } | ||
145 | |||
146 | u_int32_t | ||
147 | arc4random(void) | ||
148 | { | ||
149 | if (!rs_initialized || arc4_stir_pid != getpid()) | ||
150 | arc4random_stir(); | ||
151 | return arc4_getword(&rs); | ||
152 | } | ||
153 | |||
154 | #if 0 | ||
155 | /*-------- Test code for i386 --------*/ | ||
156 | #include <stdio.h> | ||
157 | #include <machine/pctr.h> | ||
158 | int | ||
159 | main(int argc, char **argv) | ||
160 | { | ||
161 | const int iter = 1000000; | ||
162 | int i; | ||
163 | pctrval v; | ||
164 | |||
165 | v = rdtsc(); | ||
166 | for (i = 0; i < iter; i++) | ||
167 | arc4random(); | ||
168 | v = rdtsc() - v; | ||
169 | v /= iter; | ||
170 | |||
171 | printf("%qd cycles\n", v); | ||
172 | } | ||
173 | #endif | ||
diff --git a/src/lib/libc/crypt/bcrypt.c b/src/lib/libc/crypt/bcrypt.c new file mode 100644 index 0000000000..95251db1cd --- /dev/null +++ b/src/lib/libc/crypt/bcrypt.c | |||
@@ -0,0 +1,331 @@ | |||
1 | /* $OpenBSD: bcrypt.c,v 1.18 2003/08/07 00:28:45 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | ||
5 | * 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 Niels Provos. | ||
18 | * 4. The name of the author may not be used to endorse or promote products | ||
19 | * derived from this software without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
22 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
23 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
24 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
25 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
26 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
30 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | /* This password hashing algorithm was designed by David Mazieres | ||
34 | * <dm@lcs.mit.edu> and works as follows: | ||
35 | * | ||
36 | * 1. state := InitState () | ||
37 | * 2. state := ExpandKey (state, salt, password) 3. | ||
38 | * REPEAT rounds: | ||
39 | * state := ExpandKey (state, 0, salt) | ||
40 | * state := ExpandKey(state, 0, password) | ||
41 | * 4. ctext := "OrpheanBeholderScryDoubt" | ||
42 | * 5. REPEAT 64: | ||
43 | * ctext := Encrypt_ECB (state, ctext); | ||
44 | * 6. RETURN Concatenate (salt, ctext); | ||
45 | * | ||
46 | */ | ||
47 | |||
48 | #if 0 | ||
49 | #include <stdio.h> | ||
50 | #endif | ||
51 | |||
52 | #include <stdio.h> | ||
53 | #include <stdlib.h> | ||
54 | #include <sys/types.h> | ||
55 | #include <string.h> | ||
56 | #include <pwd.h> | ||
57 | #include <blf.h> | ||
58 | |||
59 | /* This implementation is adaptable to current computing power. | ||
60 | * You can have up to 2^31 rounds which should be enough for some | ||
61 | * time to come. | ||
62 | */ | ||
63 | |||
64 | #define BCRYPT_VERSION '2' | ||
65 | #define BCRYPT_MAXSALT 16 /* Precomputation is just so nice */ | ||
66 | #define BCRYPT_BLOCKS 6 /* Ciphertext blocks */ | ||
67 | #define BCRYPT_MINROUNDS 16 /* we have log2(rounds) in salt */ | ||
68 | |||
69 | char *bcrypt_gensalt(u_int8_t); | ||
70 | |||
71 | static void encode_salt(char *, u_int8_t *, u_int16_t, u_int8_t); | ||
72 | static void encode_base64(u_int8_t *, u_int8_t *, u_int16_t); | ||
73 | static void decode_base64(u_int8_t *, u_int16_t, u_int8_t *); | ||
74 | |||
75 | static char encrypted[_PASSWORD_LEN]; | ||
76 | static char gsalt[BCRYPT_MAXSALT * 4 / 3 + 1]; | ||
77 | static char error[] = ":"; | ||
78 | |||
79 | const static u_int8_t Base64Code[] = | ||
80 | "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
81 | |||
82 | const static u_int8_t index_64[128] = { | ||
83 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
84 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
85 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
86 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, | ||
87 | 255, 255, 255, 255, 255, 255, 0, 1, 54, 55, | ||
88 | 56, 57, 58, 59, 60, 61, 62, 63, 255, 255, | ||
89 | 255, 255, 255, 255, 255, 2, 3, 4, 5, 6, | ||
90 | 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, | ||
91 | 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, | ||
92 | 255, 255, 255, 255, 255, 255, 28, 29, 30, | ||
93 | 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, | ||
94 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, | ||
95 | 51, 52, 53, 255, 255, 255, 255, 255 | ||
96 | }; | ||
97 | #define CHAR64(c) ( (c) > 127 ? 255 : index_64[(c)]) | ||
98 | |||
99 | static void | ||
100 | decode_base64(u_int8_t *buffer, u_int16_t len, u_int8_t *data) | ||
101 | { | ||
102 | u_int8_t *bp = buffer; | ||
103 | u_int8_t *p = data; | ||
104 | u_int8_t c1, c2, c3, c4; | ||
105 | while (bp < buffer + len) { | ||
106 | c1 = CHAR64(*p); | ||
107 | c2 = CHAR64(*(p + 1)); | ||
108 | |||
109 | /* Invalid data */ | ||
110 | if (c1 == 255 || c2 == 255) | ||
111 | break; | ||
112 | |||
113 | *bp++ = (c1 << 2) | ((c2 & 0x30) >> 4); | ||
114 | if (bp >= buffer + len) | ||
115 | break; | ||
116 | |||
117 | c3 = CHAR64(*(p + 2)); | ||
118 | if (c3 == 255) | ||
119 | break; | ||
120 | |||
121 | *bp++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2); | ||
122 | if (bp >= buffer + len) | ||
123 | break; | ||
124 | |||
125 | c4 = CHAR64(*(p + 3)); | ||
126 | if (c4 == 255) | ||
127 | break; | ||
128 | *bp++ = ((c3 & 0x03) << 6) | c4; | ||
129 | |||
130 | p += 4; | ||
131 | } | ||
132 | } | ||
133 | |||
134 | static void | ||
135 | encode_salt(char *salt, u_int8_t *csalt, u_int16_t clen, u_int8_t logr) | ||
136 | { | ||
137 | salt[0] = '$'; | ||
138 | salt[1] = BCRYPT_VERSION; | ||
139 | salt[2] = 'a'; | ||
140 | salt[3] = '$'; | ||
141 | |||
142 | snprintf(salt + 4, 4, "%2.2u$", logr); | ||
143 | |||
144 | encode_base64((u_int8_t *) salt + 7, csalt, clen); | ||
145 | } | ||
146 | /* Generates a salt for this version of crypt. | ||
147 | Since versions may change. Keeping this here | ||
148 | seems sensible. | ||
149 | */ | ||
150 | |||
151 | char * | ||
152 | bcrypt_gensalt(u_int8_t log_rounds) | ||
153 | { | ||
154 | u_int8_t csalt[BCRYPT_MAXSALT]; | ||
155 | u_int16_t i; | ||
156 | u_int32_t seed = 0; | ||
157 | |||
158 | for (i = 0; i < BCRYPT_MAXSALT; i++) { | ||
159 | if (i % 4 == 0) | ||
160 | seed = arc4random(); | ||
161 | csalt[i] = seed & 0xff; | ||
162 | seed = seed >> 8; | ||
163 | } | ||
164 | |||
165 | if (log_rounds < 4) | ||
166 | log_rounds = 4; | ||
167 | |||
168 | encode_salt(gsalt, csalt, BCRYPT_MAXSALT, log_rounds); | ||
169 | return gsalt; | ||
170 | } | ||
171 | /* We handle $Vers$log2(NumRounds)$salt+passwd$ | ||
172 | i.e. $2$04$iwouldntknowwhattosayetKdJ6iFtacBqJdKe6aW7ou */ | ||
173 | |||
174 | char * | ||
175 | bcrypt(const char *key, const char *salt) | ||
176 | { | ||
177 | blf_ctx state; | ||
178 | u_int32_t rounds, i, k; | ||
179 | u_int16_t j; | ||
180 | u_int8_t key_len, salt_len, logr, minor; | ||
181 | u_int8_t ciphertext[4 * BCRYPT_BLOCKS] = "OrpheanBeholderScryDoubt"; | ||
182 | u_int8_t csalt[BCRYPT_MAXSALT]; | ||
183 | u_int32_t cdata[BCRYPT_BLOCKS]; | ||
184 | |||
185 | /* Discard "$" identifier */ | ||
186 | salt++; | ||
187 | |||
188 | if (*salt > BCRYPT_VERSION) { | ||
189 | /* How do I handle errors ? Return ':' */ | ||
190 | return error; | ||
191 | } | ||
192 | |||
193 | /* Check for minor versions */ | ||
194 | if (salt[1] != '$') { | ||
195 | switch (salt[1]) { | ||
196 | case 'a': | ||
197 | /* 'ab' should not yield the same as 'abab' */ | ||
198 | minor = salt[1]; | ||
199 | salt++; | ||
200 | break; | ||
201 | default: | ||
202 | return error; | ||
203 | } | ||
204 | } else | ||
205 | minor = 0; | ||
206 | |||
207 | /* Discard version + "$" identifier */ | ||
208 | salt += 2; | ||
209 | |||
210 | if (salt[2] != '$') | ||
211 | /* Out of sync with passwd entry */ | ||
212 | return error; | ||
213 | |||
214 | /* Computer power doesn't increase linear, 2^x should be fine */ | ||
215 | if ((rounds = (u_int32_t) 1 << (logr = atoi(salt))) < BCRYPT_MINROUNDS) | ||
216 | return error; | ||
217 | |||
218 | /* Discard num rounds + "$" identifier */ | ||
219 | salt += 3; | ||
220 | |||
221 | if (strlen(salt) * 3 / 4 < BCRYPT_MAXSALT) | ||
222 | return error; | ||
223 | |||
224 | /* We dont want the base64 salt but the raw data */ | ||
225 | decode_base64(csalt, BCRYPT_MAXSALT, (u_int8_t *) salt); | ||
226 | salt_len = BCRYPT_MAXSALT; | ||
227 | key_len = strlen(key) + (minor >= 'a' ? 1 : 0); | ||
228 | |||
229 | /* Setting up S-Boxes and Subkeys */ | ||
230 | Blowfish_initstate(&state); | ||
231 | Blowfish_expandstate(&state, csalt, salt_len, | ||
232 | (u_int8_t *) key, key_len); | ||
233 | for (k = 0; k < rounds; k++) { | ||
234 | Blowfish_expand0state(&state, (u_int8_t *) key, key_len); | ||
235 | Blowfish_expand0state(&state, csalt, salt_len); | ||
236 | } | ||
237 | |||
238 | /* This can be precomputed later */ | ||
239 | j = 0; | ||
240 | for (i = 0; i < BCRYPT_BLOCKS; i++) | ||
241 | cdata[i] = Blowfish_stream2word(ciphertext, 4 * BCRYPT_BLOCKS, &j); | ||
242 | |||
243 | /* Now do the encryption */ | ||
244 | for (k = 0; k < 64; k++) | ||
245 | blf_enc(&state, cdata, BCRYPT_BLOCKS / 2); | ||
246 | |||
247 | for (i = 0; i < BCRYPT_BLOCKS; i++) { | ||
248 | ciphertext[4 * i + 3] = cdata[i] & 0xff; | ||
249 | cdata[i] = cdata[i] >> 8; | ||
250 | ciphertext[4 * i + 2] = cdata[i] & 0xff; | ||
251 | cdata[i] = cdata[i] >> 8; | ||
252 | ciphertext[4 * i + 1] = cdata[i] & 0xff; | ||
253 | cdata[i] = cdata[i] >> 8; | ||
254 | ciphertext[4 * i + 0] = cdata[i] & 0xff; | ||
255 | } | ||
256 | |||
257 | |||
258 | i = 0; | ||
259 | encrypted[i++] = '$'; | ||
260 | encrypted[i++] = BCRYPT_VERSION; | ||
261 | if (minor) | ||
262 | encrypted[i++] = minor; | ||
263 | encrypted[i++] = '$'; | ||
264 | |||
265 | snprintf(encrypted + i, 4, "%2.2u$", logr); | ||
266 | |||
267 | encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT); | ||
268 | encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext, | ||
269 | 4 * BCRYPT_BLOCKS - 1); | ||
270 | return encrypted; | ||
271 | } | ||
272 | |||
273 | static void | ||
274 | encode_base64(u_int8_t *buffer, u_int8_t *data, u_int16_t len) | ||
275 | { | ||
276 | u_int8_t *bp = buffer; | ||
277 | u_int8_t *p = data; | ||
278 | u_int8_t c1, c2; | ||
279 | while (p < data + len) { | ||
280 | c1 = *p++; | ||
281 | *bp++ = Base64Code[(c1 >> 2)]; | ||
282 | c1 = (c1 & 0x03) << 4; | ||
283 | if (p >= data + len) { | ||
284 | *bp++ = Base64Code[c1]; | ||
285 | break; | ||
286 | } | ||
287 | c2 = *p++; | ||
288 | c1 |= (c2 >> 4) & 0x0f; | ||
289 | *bp++ = Base64Code[c1]; | ||
290 | c1 = (c2 & 0x0f) << 2; | ||
291 | if (p >= data + len) { | ||
292 | *bp++ = Base64Code[c1]; | ||
293 | break; | ||
294 | } | ||
295 | c2 = *p++; | ||
296 | c1 |= (c2 >> 6) & 0x03; | ||
297 | *bp++ = Base64Code[c1]; | ||
298 | *bp++ = Base64Code[c2 & 0x3f]; | ||
299 | } | ||
300 | *bp = '\0'; | ||
301 | } | ||
302 | #if 0 | ||
303 | void | ||
304 | main() | ||
305 | { | ||
306 | char blubber[73]; | ||
307 | char salt[100]; | ||
308 | char *p; | ||
309 | salt[0] = '$'; | ||
310 | salt[1] = BCRYPT_VERSION; | ||
311 | salt[2] = '$'; | ||
312 | |||
313 | snprintf(salt + 3, 4, "%2.2u$", 5); | ||
314 | |||
315 | printf("24 bytes of salt: "); | ||
316 | fgets(salt + 6, 94, stdin); | ||
317 | salt[99] = 0; | ||
318 | printf("72 bytes of password: "); | ||
319 | fpurge(stdin); | ||
320 | fgets(blubber, 73, stdin); | ||
321 | blubber[72] = 0; | ||
322 | |||
323 | p = crypt(blubber, salt); | ||
324 | printf("Passwd entry: %s\n\n", p); | ||
325 | |||
326 | p = bcrypt_gensalt(5); | ||
327 | printf("Generated salt: %s\n", p); | ||
328 | p = crypt(blubber, p); | ||
329 | printf("Passwd entry: %s\n", p); | ||
330 | } | ||
331 | #endif | ||
diff --git a/src/lib/libc/crypt/blowfish.3 b/src/lib/libc/crypt/blowfish.3 new file mode 100644 index 0000000000..44f7eb2bbf --- /dev/null +++ b/src/lib/libc/crypt/blowfish.3 | |||
@@ -0,0 +1,106 @@ | |||
1 | .\" $OpenBSD: blowfish.3,v 1.12 2003/08/28 12:35:00 jmc Exp $ | ||
2 | .\" | ||
3 | .\" Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | ||
4 | .\" 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 Niels Provos. | ||
17 | .\" 4. The name of the author may not be used to endorse or promote products | ||
18 | .\" derived from this software without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
21 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
22 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
23 | .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
24 | .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
25 | .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
26 | .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
27 | .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
28 | .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
29 | .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" Manual page, using -mandoc macros | ||
32 | .\" | ||
33 | .Dd February 13, 1997 | ||
34 | .Dt BLOWFISH 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm blf_key , | ||
38 | .Nm blf_enc , | ||
39 | .Nm blf_dec | ||
40 | .Nd Blowfish encryption | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <blf.h> | ||
43 | .Ft void | ||
44 | .Fn blf_key "blf_ctx *state" "const u_int8_t *key" "u_int16_t keylen" | ||
45 | .Ft void | ||
46 | .Fn blf_enc "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" | ||
47 | .Ft void | ||
48 | .Fn blf_dec "blf_ctx *state" "u_int32_t *data" "u_int16_t datalen" | ||
49 | .Ft void | ||
50 | .Fn blf_ecb_encrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen" | ||
51 | .Ft void | ||
52 | .Fn blf_ecb_decrypt "blf_ctx *state" "u_int8_t *data" "u_int32_t datalen" | ||
53 | .Ft void | ||
54 | .Fn blf_cbc_encrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen" | ||
55 | .Ft void | ||
56 | .Fn blf_cbc_decrypt "blf_ctx *state" "u_int8_t *iv" "u_int8_t *data" "u_int32_t datalen" | ||
57 | .Sh DESCRIPTION | ||
58 | .Pa Blowfish | ||
59 | is a fast unpatented block cipher designed by Bruce Schneier. | ||
60 | It basically consists of a 16 times iterated Feistel network. | ||
61 | The block size is 64 bit and the maximum key size is 448 bit. | ||
62 | .Pp | ||
63 | The | ||
64 | .Fn blf_key | ||
65 | function initializes the 4 8bit S-boxes and the 18 Subkeys with | ||
66 | the hexadecimal digits of Pi. | ||
67 | The key is used for further randomization. | ||
68 | The first argument to | ||
69 | .Fn blf_enc | ||
70 | is the initialized state derived from | ||
71 | .Fn blf_key . | ||
72 | The stream of 32-bit words is encrypted in Electronic Codebook | ||
73 | Mode (ECB) and | ||
74 | .Pa datalen | ||
75 | must be even. | ||
76 | .Fn blf_dec | ||
77 | is used for decrypting Blowfish encrypted blocks. | ||
78 | .Pp | ||
79 | The functions | ||
80 | .Fn blf_ecb_encrypt | ||
81 | and | ||
82 | .Fn blf_ecb_decrypt | ||
83 | are used for encrypting and decrypting octet streams in ECB mode. | ||
84 | The functions | ||
85 | .Fn blf_cbc_encrypt | ||
86 | and | ||
87 | .Fn blf_cbc_decrypt | ||
88 | are used for encrypting and decrypting octet streams in | ||
89 | Cipherblock Chaining Mode (CBC). | ||
90 | .Pp | ||
91 | The functions | ||
92 | .Fn Blowfish_initstate , | ||
93 | .Fn Blowfish_expand0state , | ||
94 | .Fn Blowfish_expandstate , | ||
95 | .Fn Blowfish_encipher | ||
96 | and | ||
97 | .Fn Blowfish_decipher | ||
98 | are used for customization of the | ||
99 | .Pa Blowfish | ||
100 | cipher, e.g., for the blowfish password hashing function. | ||
101 | .Sh SEE ALSO | ||
102 | .Xr passwd 1 , | ||
103 | .Xr crypt 3 , | ||
104 | .Xr passwd 5 | ||
105 | .Sh AUTHORS | ||
106 | .An Niels Provos Aq provos@physnet.uni-hamburg.de | ||
diff --git a/src/lib/libc/crypt/blowfish.c b/src/lib/libc/crypt/blowfish.c new file mode 100644 index 0000000000..12226b5215 --- /dev/null +++ b/src/lib/libc/crypt/blowfish.c | |||
@@ -0,0 +1,694 @@ | |||
1 | /* $OpenBSD: blowfish.c,v 1.17 2003/04/09 21:46:02 markus Exp $ */ | ||
2 | /* | ||
3 | * Blowfish block cipher for OpenBSD | ||
4 | * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Implementation advice by David Mazieres <dm@lcs.mit.edu>. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. All advertising materials mentioning features or use of this software | ||
18 | * must display the following acknowledgement: | ||
19 | * This product includes software developed by Niels Provos. | ||
20 | * 4. The name of the author may not be used to endorse or promote products | ||
21 | * derived from this software without specific prior written permission. | ||
22 | * | ||
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
24 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
25 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
26 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
27 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
28 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
32 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | /* | ||
36 | * This code is derived from section 14.3 and the given source | ||
37 | * in section V of Applied Cryptography, second edition. | ||
38 | * Blowfish is an unpatented fast block cipher designed by | ||
39 | * Bruce Schneier. | ||
40 | */ | ||
41 | |||
42 | #if 0 | ||
43 | #include <stdio.h> /* used for debugging */ | ||
44 | #include <string.h> | ||
45 | #endif | ||
46 | |||
47 | #include <sys/types.h> | ||
48 | #include <blf.h> | ||
49 | |||
50 | #undef inline | ||
51 | #ifdef __GNUC__ | ||
52 | #define inline __inline | ||
53 | #else /* !__GNUC__ */ | ||
54 | #define inline | ||
55 | #endif /* !__GNUC__ */ | ||
56 | |||
57 | /* Function for Feistel Networks */ | ||
58 | |||
59 | #define F(s, x) ((((s)[ (((x)>>24)&0xFF)] \ | ||
60 | + (s)[0x100 + (((x)>>16)&0xFF)]) \ | ||
61 | ^ (s)[0x200 + (((x)>> 8)&0xFF)]) \ | ||
62 | + (s)[0x300 + ( (x) &0xFF)]) | ||
63 | |||
64 | #define BLFRND(s,p,i,j,n) (i ^= F(s,j) ^ (p)[n]) | ||
65 | |||
66 | void | ||
67 | Blowfish_encipher(c, xl, xr) | ||
68 | blf_ctx *c; | ||
69 | u_int32_t *xl; | ||
70 | u_int32_t *xr; | ||
71 | { | ||
72 | u_int32_t Xl; | ||
73 | u_int32_t Xr; | ||
74 | u_int32_t *s = c->S[0]; | ||
75 | u_int32_t *p = c->P; | ||
76 | |||
77 | Xl = *xl; | ||
78 | Xr = *xr; | ||
79 | |||
80 | Xl ^= p[0]; | ||
81 | BLFRND(s, p, Xr, Xl, 1); BLFRND(s, p, Xl, Xr, 2); | ||
82 | BLFRND(s, p, Xr, Xl, 3); BLFRND(s, p, Xl, Xr, 4); | ||
83 | BLFRND(s, p, Xr, Xl, 5); BLFRND(s, p, Xl, Xr, 6); | ||
84 | BLFRND(s, p, Xr, Xl, 7); BLFRND(s, p, Xl, Xr, 8); | ||
85 | BLFRND(s, p, Xr, Xl, 9); BLFRND(s, p, Xl, Xr, 10); | ||
86 | BLFRND(s, p, Xr, Xl, 11); BLFRND(s, p, Xl, Xr, 12); | ||
87 | BLFRND(s, p, Xr, Xl, 13); BLFRND(s, p, Xl, Xr, 14); | ||
88 | BLFRND(s, p, Xr, Xl, 15); BLFRND(s, p, Xl, Xr, 16); | ||
89 | |||
90 | *xl = Xr ^ p[17]; | ||
91 | *xr = Xl; | ||
92 | } | ||
93 | |||
94 | void | ||
95 | Blowfish_decipher(c, xl, xr) | ||
96 | blf_ctx *c; | ||
97 | u_int32_t *xl; | ||
98 | u_int32_t *xr; | ||
99 | { | ||
100 | u_int32_t Xl; | ||
101 | u_int32_t Xr; | ||
102 | u_int32_t *s = c->S[0]; | ||
103 | u_int32_t *p = c->P; | ||
104 | |||
105 | Xl = *xl; | ||
106 | Xr = *xr; | ||
107 | |||
108 | Xl ^= p[17]; | ||
109 | BLFRND(s, p, Xr, Xl, 16); BLFRND(s, p, Xl, Xr, 15); | ||
110 | BLFRND(s, p, Xr, Xl, 14); BLFRND(s, p, Xl, Xr, 13); | ||
111 | BLFRND(s, p, Xr, Xl, 12); BLFRND(s, p, Xl, Xr, 11); | ||
112 | BLFRND(s, p, Xr, Xl, 10); BLFRND(s, p, Xl, Xr, 9); | ||
113 | BLFRND(s, p, Xr, Xl, 8); BLFRND(s, p, Xl, Xr, 7); | ||
114 | BLFRND(s, p, Xr, Xl, 6); BLFRND(s, p, Xl, Xr, 5); | ||
115 | BLFRND(s, p, Xr, Xl, 4); BLFRND(s, p, Xl, Xr, 3); | ||
116 | BLFRND(s, p, Xr, Xl, 2); BLFRND(s, p, Xl, Xr, 1); | ||
117 | |||
118 | *xl = Xr ^ p[0]; | ||
119 | *xr = Xl; | ||
120 | } | ||
121 | |||
122 | void | ||
123 | Blowfish_initstate(c) | ||
124 | blf_ctx *c; | ||
125 | { | ||
126 | |||
127 | /* P-box and S-box tables initialized with digits of Pi */ | ||
128 | |||
129 | static const blf_ctx initstate = | ||
130 | |||
131 | { { | ||
132 | { | ||
133 | 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, | ||
134 | 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, | ||
135 | 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, | ||
136 | 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, | ||
137 | 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, | ||
138 | 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, | ||
139 | 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, | ||
140 | 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, | ||
141 | 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, | ||
142 | 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, | ||
143 | 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, | ||
144 | 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, | ||
145 | 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, | ||
146 | 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, | ||
147 | 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, | ||
148 | 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, | ||
149 | 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, | ||
150 | 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, | ||
151 | 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, | ||
152 | 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, | ||
153 | 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, | ||
154 | 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, | ||
155 | 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, | ||
156 | 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, | ||
157 | 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, | ||
158 | 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, | ||
159 | 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, | ||
160 | 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, | ||
161 | 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, | ||
162 | 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, | ||
163 | 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, | ||
164 | 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, | ||
165 | 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, | ||
166 | 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, | ||
167 | 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, | ||
168 | 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, | ||
169 | 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, | ||
170 | 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, | ||
171 | 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, | ||
172 | 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, | ||
173 | 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, | ||
174 | 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, | ||
175 | 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, | ||
176 | 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, | ||
177 | 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, | ||
178 | 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, | ||
179 | 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, | ||
180 | 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, | ||
181 | 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, | ||
182 | 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, | ||
183 | 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, | ||
184 | 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, | ||
185 | 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, | ||
186 | 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, | ||
187 | 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, | ||
188 | 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, | ||
189 | 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, | ||
190 | 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, | ||
191 | 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, | ||
192 | 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, | ||
193 | 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, | ||
194 | 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, | ||
195 | 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, | ||
196 | 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a}, | ||
197 | { | ||
198 | 0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, | ||
199 | 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, | ||
200 | 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, | ||
201 | 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, | ||
202 | 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, | ||
203 | 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, | ||
204 | 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, | ||
205 | 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, | ||
206 | 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, | ||
207 | 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, | ||
208 | 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, | ||
209 | 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, | ||
210 | 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, | ||
211 | 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, | ||
212 | 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, | ||
213 | 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, | ||
214 | 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, | ||
215 | 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, | ||
216 | 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, | ||
217 | 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, | ||
218 | 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, | ||
219 | 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, | ||
220 | 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, | ||
221 | 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, | ||
222 | 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, | ||
223 | 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, | ||
224 | 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, | ||
225 | 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, | ||
226 | 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, | ||
227 | 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, | ||
228 | 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, | ||
229 | 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, | ||
230 | 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, | ||
231 | 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, | ||
232 | 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, | ||
233 | 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, | ||
234 | 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, | ||
235 | 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, | ||
236 | 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, | ||
237 | 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, | ||
238 | 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, | ||
239 | 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, | ||
240 | 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, | ||
241 | 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, | ||
242 | 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, | ||
243 | 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, | ||
244 | 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, | ||
245 | 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, | ||
246 | 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, | ||
247 | 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, | ||
248 | 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, | ||
249 | 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, | ||
250 | 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, | ||
251 | 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, | ||
252 | 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, | ||
253 | 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, | ||
254 | 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, | ||
255 | 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, | ||
256 | 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, | ||
257 | 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, | ||
258 | 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, | ||
259 | 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, | ||
260 | 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, | ||
261 | 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7}, | ||
262 | { | ||
263 | 0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, | ||
264 | 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, | ||
265 | 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, | ||
266 | 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, | ||
267 | 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, | ||
268 | 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, | ||
269 | 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, | ||
270 | 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, | ||
271 | 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, | ||
272 | 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, | ||
273 | 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, | ||
274 | 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, | ||
275 | 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, | ||
276 | 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, | ||
277 | 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, | ||
278 | 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, | ||
279 | 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, | ||
280 | 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, | ||
281 | 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, | ||
282 | 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, | ||
283 | 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, | ||
284 | 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, | ||
285 | 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, | ||
286 | 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, | ||
287 | 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, | ||
288 | 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, | ||
289 | 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, | ||
290 | 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, | ||
291 | 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, | ||
292 | 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, | ||
293 | 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, | ||
294 | 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, | ||
295 | 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, | ||
296 | 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, | ||
297 | 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, | ||
298 | 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, | ||
299 | 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, | ||
300 | 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, | ||
301 | 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, | ||
302 | 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, | ||
303 | 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, | ||
304 | 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, | ||
305 | 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, | ||
306 | 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, | ||
307 | 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, | ||
308 | 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, | ||
309 | 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, | ||
310 | 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, | ||
311 | 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, | ||
312 | 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, | ||
313 | 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, | ||
314 | 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, | ||
315 | 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, | ||
316 | 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, | ||
317 | 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, | ||
318 | 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, | ||
319 | 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, | ||
320 | 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, | ||
321 | 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, | ||
322 | 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, | ||
323 | 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, | ||
324 | 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, | ||
325 | 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, | ||
326 | 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0}, | ||
327 | { | ||
328 | 0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, | ||
329 | 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, | ||
330 | 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, | ||
331 | 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, | ||
332 | 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, | ||
333 | 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, | ||
334 | 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, | ||
335 | 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, | ||
336 | 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, | ||
337 | 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, | ||
338 | 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, | ||
339 | 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, | ||
340 | 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, | ||
341 | 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, | ||
342 | 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, | ||
343 | 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, | ||
344 | 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, | ||
345 | 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, | ||
346 | 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, | ||
347 | 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, | ||
348 | 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, | ||
349 | 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, | ||
350 | 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, | ||
351 | 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, | ||
352 | 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, | ||
353 | 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, | ||
354 | 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, | ||
355 | 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, | ||
356 | 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, | ||
357 | 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, | ||
358 | 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, | ||
359 | 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, | ||
360 | 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, | ||
361 | 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, | ||
362 | 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, | ||
363 | 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, | ||
364 | 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, | ||
365 | 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, | ||
366 | 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, | ||
367 | 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, | ||
368 | 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, | ||
369 | 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, | ||
370 | 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, | ||
371 | 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, | ||
372 | 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, | ||
373 | 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, | ||
374 | 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, | ||
375 | 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, | ||
376 | 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, | ||
377 | 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, | ||
378 | 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, | ||
379 | 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, | ||
380 | 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, | ||
381 | 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, | ||
382 | 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, | ||
383 | 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, | ||
384 | 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, | ||
385 | 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, | ||
386 | 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, | ||
387 | 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, | ||
388 | 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, | ||
389 | 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, | ||
390 | 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, | ||
391 | 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6} | ||
392 | }, | ||
393 | { | ||
394 | 0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, | ||
395 | 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, | ||
396 | 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, | ||
397 | 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, | ||
398 | 0x9216d5d9, 0x8979fb1b | ||
399 | } }; | ||
400 | |||
401 | *c = initstate; | ||
402 | |||
403 | } | ||
404 | |||
405 | u_int32_t | ||
406 | Blowfish_stream2word(const u_int8_t *data, u_int16_t databytes, u_int16_t *current) | ||
407 | { | ||
408 | u_int8_t i; | ||
409 | u_int16_t j; | ||
410 | u_int32_t temp; | ||
411 | |||
412 | temp = 0x00000000; | ||
413 | j = *current; | ||
414 | |||
415 | for (i = 0; i < 4; i++, j++) { | ||
416 | if (j >= databytes) | ||
417 | j = 0; | ||
418 | temp = (temp << 8) | data[j]; | ||
419 | } | ||
420 | |||
421 | *current = j; | ||
422 | return temp; | ||
423 | } | ||
424 | |||
425 | void | ||
426 | Blowfish_expand0state(blf_ctx *c, const u_int8_t *key, u_int16_t keybytes) | ||
427 | { | ||
428 | u_int16_t i; | ||
429 | u_int16_t j; | ||
430 | u_int16_t k; | ||
431 | u_int32_t temp; | ||
432 | u_int32_t datal; | ||
433 | u_int32_t datar; | ||
434 | |||
435 | j = 0; | ||
436 | for (i = 0; i < BLF_N + 2; i++) { | ||
437 | /* Extract 4 int8 to 1 int32 from keystream */ | ||
438 | temp = Blowfish_stream2word(key, keybytes, &j); | ||
439 | c->P[i] = c->P[i] ^ temp; | ||
440 | } | ||
441 | |||
442 | j = 0; | ||
443 | datal = 0x00000000; | ||
444 | datar = 0x00000000; | ||
445 | for (i = 0; i < BLF_N + 2; i += 2) { | ||
446 | Blowfish_encipher(c, &datal, &datar); | ||
447 | |||
448 | c->P[i] = datal; | ||
449 | c->P[i + 1] = datar; | ||
450 | } | ||
451 | |||
452 | for (i = 0; i < 4; i++) { | ||
453 | for (k = 0; k < 256; k += 2) { | ||
454 | Blowfish_encipher(c, &datal, &datar); | ||
455 | |||
456 | c->S[i][k] = datal; | ||
457 | c->S[i][k + 1] = datar; | ||
458 | } | ||
459 | } | ||
460 | } | ||
461 | |||
462 | |||
463 | void | ||
464 | Blowfish_expandstate(blf_ctx *c, const u_int8_t *data, u_int16_t databytes, | ||
465 | const u_int8_t *key, u_int16_t keybytes) | ||
466 | { | ||
467 | u_int16_t i; | ||
468 | u_int16_t j; | ||
469 | u_int16_t k; | ||
470 | u_int32_t temp; | ||
471 | u_int32_t datal; | ||
472 | u_int32_t datar; | ||
473 | |||
474 | j = 0; | ||
475 | for (i = 0; i < BLF_N + 2; i++) { | ||
476 | /* Extract 4 int8 to 1 int32 from keystream */ | ||
477 | temp = Blowfish_stream2word(key, keybytes, &j); | ||
478 | c->P[i] = c->P[i] ^ temp; | ||
479 | } | ||
480 | |||
481 | j = 0; | ||
482 | datal = 0x00000000; | ||
483 | datar = 0x00000000; | ||
484 | for (i = 0; i < BLF_N + 2; i += 2) { | ||
485 | datal ^= Blowfish_stream2word(data, databytes, &j); | ||
486 | datar ^= Blowfish_stream2word(data, databytes, &j); | ||
487 | Blowfish_encipher(c, &datal, &datar); | ||
488 | |||
489 | c->P[i] = datal; | ||
490 | c->P[i + 1] = datar; | ||
491 | } | ||
492 | |||
493 | for (i = 0; i < 4; i++) { | ||
494 | for (k = 0; k < 256; k += 2) { | ||
495 | datal ^= Blowfish_stream2word(data, databytes, &j); | ||
496 | datar ^= Blowfish_stream2word(data, databytes, &j); | ||
497 | Blowfish_encipher(c, &datal, &datar); | ||
498 | |||
499 | c->S[i][k] = datal; | ||
500 | c->S[i][k + 1] = datar; | ||
501 | } | ||
502 | } | ||
503 | |||
504 | } | ||
505 | |||
506 | void | ||
507 | blf_key(blf_ctx *c, const u_int8_t *k, u_int16_t len) | ||
508 | { | ||
509 | /* Initialize S-boxes and subkeys with Pi */ | ||
510 | Blowfish_initstate(c); | ||
511 | |||
512 | /* Transform S-boxes and subkeys with key */ | ||
513 | Blowfish_expand0state(c, k, len); | ||
514 | } | ||
515 | |||
516 | void | ||
517 | blf_enc(blf_ctx *c, u_int32_t *data, u_int16_t blocks) | ||
518 | { | ||
519 | u_int32_t *d; | ||
520 | u_int16_t i; | ||
521 | |||
522 | d = data; | ||
523 | for (i = 0; i < blocks; i++) { | ||
524 | Blowfish_encipher(c, d, d + 1); | ||
525 | d += 2; | ||
526 | } | ||
527 | } | ||
528 | |||
529 | void | ||
530 | blf_dec(blf_ctx *c, u_int32_t *data, u_int16_t blocks) | ||
531 | { | ||
532 | u_int32_t *d; | ||
533 | u_int16_t i; | ||
534 | |||
535 | d = data; | ||
536 | for (i = 0; i < blocks; i++) { | ||
537 | Blowfish_decipher(c, d, d + 1); | ||
538 | d += 2; | ||
539 | } | ||
540 | } | ||
541 | |||
542 | void | ||
543 | blf_ecb_encrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) | ||
544 | { | ||
545 | u_int32_t l, r; | ||
546 | u_int32_t i; | ||
547 | |||
548 | for (i = 0; i < len; i += 8) { | ||
549 | l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; | ||
550 | r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; | ||
551 | Blowfish_encipher(c, &l, &r); | ||
552 | data[0] = l >> 24 & 0xff; | ||
553 | data[1] = l >> 16 & 0xff; | ||
554 | data[2] = l >> 8 & 0xff; | ||
555 | data[3] = l & 0xff; | ||
556 | data[4] = r >> 24 & 0xff; | ||
557 | data[5] = r >> 16 & 0xff; | ||
558 | data[6] = r >> 8 & 0xff; | ||
559 | data[7] = r & 0xff; | ||
560 | data += 8; | ||
561 | } | ||
562 | } | ||
563 | |||
564 | void | ||
565 | blf_ecb_decrypt(blf_ctx *c, u_int8_t *data, u_int32_t len) | ||
566 | { | ||
567 | u_int32_t l, r; | ||
568 | u_int32_t i; | ||
569 | |||
570 | for (i = 0; i < len; i += 8) { | ||
571 | l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; | ||
572 | r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; | ||
573 | Blowfish_decipher(c, &l, &r); | ||
574 | data[0] = l >> 24 & 0xff; | ||
575 | data[1] = l >> 16 & 0xff; | ||
576 | data[2] = l >> 8 & 0xff; | ||
577 | data[3] = l & 0xff; | ||
578 | data[4] = r >> 24 & 0xff; | ||
579 | data[5] = r >> 16 & 0xff; | ||
580 | data[6] = r >> 8 & 0xff; | ||
581 | data[7] = r & 0xff; | ||
582 | data += 8; | ||
583 | } | ||
584 | } | ||
585 | |||
586 | void | ||
587 | blf_cbc_encrypt(blf_ctx *c, u_int8_t *iv, u_int8_t *data, u_int32_t len) | ||
588 | { | ||
589 | u_int32_t l, r; | ||
590 | u_int32_t i, j; | ||
591 | |||
592 | for (i = 0; i < len; i += 8) { | ||
593 | for (j = 0; j < 8; j++) | ||
594 | data[j] ^= iv[j]; | ||
595 | l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; | ||
596 | r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; | ||
597 | Blowfish_encipher(c, &l, &r); | ||
598 | data[0] = l >> 24 & 0xff; | ||
599 | data[1] = l >> 16 & 0xff; | ||
600 | data[2] = l >> 8 & 0xff; | ||
601 | data[3] = l & 0xff; | ||
602 | data[4] = r >> 24 & 0xff; | ||
603 | data[5] = r >> 16 & 0xff; | ||
604 | data[6] = r >> 8 & 0xff; | ||
605 | data[7] = r & 0xff; | ||
606 | iv = data; | ||
607 | data += 8; | ||
608 | } | ||
609 | } | ||
610 | |||
611 | void | ||
612 | blf_cbc_decrypt(blf_ctx *c, u_int8_t *iva, u_int8_t *data, u_int32_t len) | ||
613 | { | ||
614 | u_int32_t l, r; | ||
615 | u_int8_t *iv; | ||
616 | u_int32_t i, j; | ||
617 | |||
618 | iv = data + len - 16; | ||
619 | data = data + len - 8; | ||
620 | for (i = len - 8; i >= 8; i -= 8) { | ||
621 | l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; | ||
622 | r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; | ||
623 | Blowfish_decipher(c, &l, &r); | ||
624 | data[0] = l >> 24 & 0xff; | ||
625 | data[1] = l >> 16 & 0xff; | ||
626 | data[2] = l >> 8 & 0xff; | ||
627 | data[3] = l & 0xff; | ||
628 | data[4] = r >> 24 & 0xff; | ||
629 | data[5] = r >> 16 & 0xff; | ||
630 | data[6] = r >> 8 & 0xff; | ||
631 | data[7] = r & 0xff; | ||
632 | for (j = 0; j < 8; j++) | ||
633 | data[j] ^= iv[j]; | ||
634 | iv -= 8; | ||
635 | data -= 8; | ||
636 | } | ||
637 | l = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; | ||
638 | r = data[4] << 24 | data[5] << 16 | data[6] << 8 | data[7]; | ||
639 | Blowfish_decipher(c, &l, &r); | ||
640 | data[0] = l >> 24 & 0xff; | ||
641 | data[1] = l >> 16 & 0xff; | ||
642 | data[2] = l >> 8 & 0xff; | ||
643 | data[3] = l & 0xff; | ||
644 | data[4] = r >> 24 & 0xff; | ||
645 | data[5] = r >> 16 & 0xff; | ||
646 | data[6] = r >> 8 & 0xff; | ||
647 | data[7] = r & 0xff; | ||
648 | for (j = 0; j < 8; j++) | ||
649 | data[j] ^= iva[j]; | ||
650 | } | ||
651 | |||
652 | #if 0 | ||
653 | void | ||
654 | report(u_int32_t data[], u_int16_t len) | ||
655 | { | ||
656 | u_int16_t i; | ||
657 | for (i = 0; i < len; i += 2) | ||
658 | printf("Block %0hd: %08lx %08lx.\n", | ||
659 | i / 2, data[i], data[i + 1]); | ||
660 | } | ||
661 | void | ||
662 | main(void) | ||
663 | { | ||
664 | |||
665 | blf_ctx c; | ||
666 | char key[] = "AAAAA"; | ||
667 | char key2[] = "abcdefghijklmnopqrstuvwxyz"; | ||
668 | |||
669 | u_int32_t data[10]; | ||
670 | u_int32_t data2[] = | ||
671 | {0x424c4f57l, 0x46495348l}; | ||
672 | |||
673 | u_int16_t i; | ||
674 | |||
675 | /* First test */ | ||
676 | for (i = 0; i < 10; i++) | ||
677 | data[i] = i; | ||
678 | |||
679 | blf_key(&c, (u_int8_t *) key, 5); | ||
680 | blf_enc(&c, data, 5); | ||
681 | blf_dec(&c, data, 1); | ||
682 | blf_dec(&c, data + 2, 4); | ||
683 | printf("Should read as 0 - 9.\n"); | ||
684 | report(data, 10); | ||
685 | |||
686 | /* Second test */ | ||
687 | blf_key(&c, (u_int8_t *) key2, strlen(key2)); | ||
688 | blf_enc(&c, data2, 1); | ||
689 | printf("\nShould read as: 0x324ed0fe 0xf413a203.\n"); | ||
690 | report(data2, 2); | ||
691 | blf_dec(&c, data2, 1); | ||
692 | report(data2, 2); | ||
693 | } | ||
694 | #endif | ||
diff --git a/src/lib/libc/crypt/cast.c b/src/lib/libc/crypt/cast.c new file mode 100644 index 0000000000..264138f03e --- /dev/null +++ b/src/lib/libc/crypt/cast.c | |||
@@ -0,0 +1,779 @@ | |||
1 | /* $OpenBSD: cast.c,v 1.2 1998/07/21 22:42:03 provos Exp $ */ | ||
2 | /* | ||
3 | * CAST-128 in C | ||
4 | * Written by Steve Reid <sreid@sea-to-sky.net> | ||
5 | * 100% Public Domain - no warranty | ||
6 | * Released 1997.10.11 | ||
7 | */ | ||
8 | |||
9 | #include <sys/types.h> | ||
10 | |||
11 | #include <cast.h> | ||
12 | |||
13 | /* CAST S-Boxes */ | ||
14 | |||
15 | static const u_int32_t cast_sbox1[256] = { | ||
16 | 0x30FB40D4, 0x9FA0FF0B, 0x6BECCD2F, 0x3F258C7A, | ||
17 | 0x1E213F2F, 0x9C004DD3, 0x6003E540, 0xCF9FC949, | ||
18 | 0xBFD4AF27, 0x88BBBDB5, 0xE2034090, 0x98D09675, | ||
19 | 0x6E63A0E0, 0x15C361D2, 0xC2E7661D, 0x22D4FF8E, | ||
20 | 0x28683B6F, 0xC07FD059, 0xFF2379C8, 0x775F50E2, | ||
21 | 0x43C340D3, 0xDF2F8656, 0x887CA41A, 0xA2D2BD2D, | ||
22 | 0xA1C9E0D6, 0x346C4819, 0x61B76D87, 0x22540F2F, | ||
23 | 0x2ABE32E1, 0xAA54166B, 0x22568E3A, 0xA2D341D0, | ||
24 | 0x66DB40C8, 0xA784392F, 0x004DFF2F, 0x2DB9D2DE, | ||
25 | 0x97943FAC, 0x4A97C1D8, 0x527644B7, 0xB5F437A7, | ||
26 | 0xB82CBAEF, 0xD751D159, 0x6FF7F0ED, 0x5A097A1F, | ||
27 | 0x827B68D0, 0x90ECF52E, 0x22B0C054, 0xBC8E5935, | ||
28 | 0x4B6D2F7F, 0x50BB64A2, 0xD2664910, 0xBEE5812D, | ||
29 | 0xB7332290, 0xE93B159F, 0xB48EE411, 0x4BFF345D, | ||
30 | 0xFD45C240, 0xAD31973F, 0xC4F6D02E, 0x55FC8165, | ||
31 | 0xD5B1CAAD, 0xA1AC2DAE, 0xA2D4B76D, 0xC19B0C50, | ||
32 | 0x882240F2, 0x0C6E4F38, 0xA4E4BFD7, 0x4F5BA272, | ||
33 | 0x564C1D2F, 0xC59C5319, 0xB949E354, 0xB04669FE, | ||
34 | 0xB1B6AB8A, 0xC71358DD, 0x6385C545, 0x110F935D, | ||
35 | 0x57538AD5, 0x6A390493, 0xE63D37E0, 0x2A54F6B3, | ||
36 | 0x3A787D5F, 0x6276A0B5, 0x19A6FCDF, 0x7A42206A, | ||
37 | 0x29F9D4D5, 0xF61B1891, 0xBB72275E, 0xAA508167, | ||
38 | 0x38901091, 0xC6B505EB, 0x84C7CB8C, 0x2AD75A0F, | ||
39 | 0x874A1427, 0xA2D1936B, 0x2AD286AF, 0xAA56D291, | ||
40 | 0xD7894360, 0x425C750D, 0x93B39E26, 0x187184C9, | ||
41 | 0x6C00B32D, 0x73E2BB14, 0xA0BEBC3C, 0x54623779, | ||
42 | 0x64459EAB, 0x3F328B82, 0x7718CF82, 0x59A2CEA6, | ||
43 | 0x04EE002E, 0x89FE78E6, 0x3FAB0950, 0x325FF6C2, | ||
44 | 0x81383F05, 0x6963C5C8, 0x76CB5AD6, 0xD49974C9, | ||
45 | 0xCA180DCF, 0x380782D5, 0xC7FA5CF6, 0x8AC31511, | ||
46 | 0x35E79E13, 0x47DA91D0, 0xF40F9086, 0xA7E2419E, | ||
47 | 0x31366241, 0x051EF495, 0xAA573B04, 0x4A805D8D, | ||
48 | 0x548300D0, 0x00322A3C, 0xBF64CDDF, 0xBA57A68E, | ||
49 | 0x75C6372B, 0x50AFD341, 0xA7C13275, 0x915A0BF5, | ||
50 | 0x6B54BFAB, 0x2B0B1426, 0xAB4CC9D7, 0x449CCD82, | ||
51 | 0xF7FBF265, 0xAB85C5F3, 0x1B55DB94, 0xAAD4E324, | ||
52 | 0xCFA4BD3F, 0x2DEAA3E2, 0x9E204D02, 0xC8BD25AC, | ||
53 | 0xEADF55B3, 0xD5BD9E98, 0xE31231B2, 0x2AD5AD6C, | ||
54 | 0x954329DE, 0xADBE4528, 0xD8710F69, 0xAA51C90F, | ||
55 | 0xAA786BF6, 0x22513F1E, 0xAA51A79B, 0x2AD344CC, | ||
56 | 0x7B5A41F0, 0xD37CFBAD, 0x1B069505, 0x41ECE491, | ||
57 | 0xB4C332E6, 0x032268D4, 0xC9600ACC, 0xCE387E6D, | ||
58 | 0xBF6BB16C, 0x6A70FB78, 0x0D03D9C9, 0xD4DF39DE, | ||
59 | 0xE01063DA, 0x4736F464, 0x5AD328D8, 0xB347CC96, | ||
60 | 0x75BB0FC3, 0x98511BFB, 0x4FFBCC35, 0xB58BCF6A, | ||
61 | 0xE11F0ABC, 0xBFC5FE4A, 0xA70AEC10, 0xAC39570A, | ||
62 | 0x3F04442F, 0x6188B153, 0xE0397A2E, 0x5727CB79, | ||
63 | 0x9CEB418F, 0x1CACD68D, 0x2AD37C96, 0x0175CB9D, | ||
64 | 0xC69DFF09, 0xC75B65F0, 0xD9DB40D8, 0xEC0E7779, | ||
65 | 0x4744EAD4, 0xB11C3274, 0xDD24CB9E, 0x7E1C54BD, | ||
66 | 0xF01144F9, 0xD2240EB1, 0x9675B3FD, 0xA3AC3755, | ||
67 | 0xD47C27AF, 0x51C85F4D, 0x56907596, 0xA5BB15E6, | ||
68 | 0x580304F0, 0xCA042CF1, 0x011A37EA, 0x8DBFAADB, | ||
69 | 0x35BA3E4A, 0x3526FFA0, 0xC37B4D09, 0xBC306ED9, | ||
70 | 0x98A52666, 0x5648F725, 0xFF5E569D, 0x0CED63D0, | ||
71 | 0x7C63B2CF, 0x700B45E1, 0xD5EA50F1, 0x85A92872, | ||
72 | 0xAF1FBDA7, 0xD4234870, 0xA7870BF3, 0x2D3B4D79, | ||
73 | 0x42E04198, 0x0CD0EDE7, 0x26470DB8, 0xF881814C, | ||
74 | 0x474D6AD7, 0x7C0C5E5C, 0xD1231959, 0x381B7298, | ||
75 | 0xF5D2F4DB, 0xAB838653, 0x6E2F1E23, 0x83719C9E, | ||
76 | 0xBD91E046, 0x9A56456E, 0xDC39200C, 0x20C8C571, | ||
77 | 0x962BDA1C, 0xE1E696FF, 0xB141AB08, 0x7CCA89B9, | ||
78 | 0x1A69E783, 0x02CC4843, 0xA2F7C579, 0x429EF47D, | ||
79 | 0x427B169C, 0x5AC9F049, 0xDD8F0F00, 0x5C8165BF | ||
80 | }; | ||
81 | |||
82 | static const u_int32_t cast_sbox2[256] = { | ||
83 | 0x1F201094, 0xEF0BA75B, 0x69E3CF7E, 0x393F4380, | ||
84 | 0xFE61CF7A, 0xEEC5207A, 0x55889C94, 0x72FC0651, | ||
85 | 0xADA7EF79, 0x4E1D7235, 0xD55A63CE, 0xDE0436BA, | ||
86 | 0x99C430EF, 0x5F0C0794, 0x18DCDB7D, 0xA1D6EFF3, | ||
87 | 0xA0B52F7B, 0x59E83605, 0xEE15B094, 0xE9FFD909, | ||
88 | 0xDC440086, 0xEF944459, 0xBA83CCB3, 0xE0C3CDFB, | ||
89 | 0xD1DA4181, 0x3B092AB1, 0xF997F1C1, 0xA5E6CF7B, | ||
90 | 0x01420DDB, 0xE4E7EF5B, 0x25A1FF41, 0xE180F806, | ||
91 | 0x1FC41080, 0x179BEE7A, 0xD37AC6A9, 0xFE5830A4, | ||
92 | 0x98DE8B7F, 0x77E83F4E, 0x79929269, 0x24FA9F7B, | ||
93 | 0xE113C85B, 0xACC40083, 0xD7503525, 0xF7EA615F, | ||
94 | 0x62143154, 0x0D554B63, 0x5D681121, 0xC866C359, | ||
95 | 0x3D63CF73, 0xCEE234C0, 0xD4D87E87, 0x5C672B21, | ||
96 | 0x071F6181, 0x39F7627F, 0x361E3084, 0xE4EB573B, | ||
97 | 0x602F64A4, 0xD63ACD9C, 0x1BBC4635, 0x9E81032D, | ||
98 | 0x2701F50C, 0x99847AB4, 0xA0E3DF79, 0xBA6CF38C, | ||
99 | 0x10843094, 0x2537A95E, 0xF46F6FFE, 0xA1FF3B1F, | ||
100 | 0x208CFB6A, 0x8F458C74, 0xD9E0A227, 0x4EC73A34, | ||
101 | 0xFC884F69, 0x3E4DE8DF, 0xEF0E0088, 0x3559648D, | ||
102 | 0x8A45388C, 0x1D804366, 0x721D9BFD, 0xA58684BB, | ||
103 | 0xE8256333, 0x844E8212, 0x128D8098, 0xFED33FB4, | ||
104 | 0xCE280AE1, 0x27E19BA5, 0xD5A6C252, 0xE49754BD, | ||
105 | 0xC5D655DD, 0xEB667064, 0x77840B4D, 0xA1B6A801, | ||
106 | 0x84DB26A9, 0xE0B56714, 0x21F043B7, 0xE5D05860, | ||
107 | 0x54F03084, 0x066FF472, 0xA31AA153, 0xDADC4755, | ||
108 | 0xB5625DBF, 0x68561BE6, 0x83CA6B94, 0x2D6ED23B, | ||
109 | 0xECCF01DB, 0xA6D3D0BA, 0xB6803D5C, 0xAF77A709, | ||
110 | 0x33B4A34C, 0x397BC8D6, 0x5EE22B95, 0x5F0E5304, | ||
111 | 0x81ED6F61, 0x20E74364, 0xB45E1378, 0xDE18639B, | ||
112 | 0x881CA122, 0xB96726D1, 0x8049A7E8, 0x22B7DA7B, | ||
113 | 0x5E552D25, 0x5272D237, 0x79D2951C, 0xC60D894C, | ||
114 | 0x488CB402, 0x1BA4FE5B, 0xA4B09F6B, 0x1CA815CF, | ||
115 | 0xA20C3005, 0x8871DF63, 0xB9DE2FCB, 0x0CC6C9E9, | ||
116 | 0x0BEEFF53, 0xE3214517, 0xB4542835, 0x9F63293C, | ||
117 | 0xEE41E729, 0x6E1D2D7C, 0x50045286, 0x1E6685F3, | ||
118 | 0xF33401C6, 0x30A22C95, 0x31A70850, 0x60930F13, | ||
119 | 0x73F98417, 0xA1269859, 0xEC645C44, 0x52C877A9, | ||
120 | 0xCDFF33A6, 0xA02B1741, 0x7CBAD9A2, 0x2180036F, | ||
121 | 0x50D99C08, 0xCB3F4861, 0xC26BD765, 0x64A3F6AB, | ||
122 | 0x80342676, 0x25A75E7B, 0xE4E6D1FC, 0x20C710E6, | ||
123 | 0xCDF0B680, 0x17844D3B, 0x31EEF84D, 0x7E0824E4, | ||
124 | 0x2CCB49EB, 0x846A3BAE, 0x8FF77888, 0xEE5D60F6, | ||
125 | 0x7AF75673, 0x2FDD5CDB, 0xA11631C1, 0x30F66F43, | ||
126 | 0xB3FAEC54, 0x157FD7FA, 0xEF8579CC, 0xD152DE58, | ||
127 | 0xDB2FFD5E, 0x8F32CE19, 0x306AF97A, 0x02F03EF8, | ||
128 | 0x99319AD5, 0xC242FA0F, 0xA7E3EBB0, 0xC68E4906, | ||
129 | 0xB8DA230C, 0x80823028, 0xDCDEF3C8, 0xD35FB171, | ||
130 | 0x088A1BC8, 0xBEC0C560, 0x61A3C9E8, 0xBCA8F54D, | ||
131 | 0xC72FEFFA, 0x22822E99, 0x82C570B4, 0xD8D94E89, | ||
132 | 0x8B1C34BC, 0x301E16E6, 0x273BE979, 0xB0FFEAA6, | ||
133 | 0x61D9B8C6, 0x00B24869, 0xB7FFCE3F, 0x08DC283B, | ||
134 | 0x43DAF65A, 0xF7E19798, 0x7619B72F, 0x8F1C9BA4, | ||
135 | 0xDC8637A0, 0x16A7D3B1, 0x9FC393B7, 0xA7136EEB, | ||
136 | 0xC6BCC63E, 0x1A513742, 0xEF6828BC, 0x520365D6, | ||
137 | 0x2D6A77AB, 0x3527ED4B, 0x821FD216, 0x095C6E2E, | ||
138 | 0xDB92F2FB, 0x5EEA29CB, 0x145892F5, 0x91584F7F, | ||
139 | 0x5483697B, 0x2667A8CC, 0x85196048, 0x8C4BACEA, | ||
140 | 0x833860D4, 0x0D23E0F9, 0x6C387E8A, 0x0AE6D249, | ||
141 | 0xB284600C, 0xD835731D, 0xDCB1C647, 0xAC4C56EA, | ||
142 | 0x3EBD81B3, 0x230EABB0, 0x6438BC87, 0xF0B5B1FA, | ||
143 | 0x8F5EA2B3, 0xFC184642, 0x0A036B7A, 0x4FB089BD, | ||
144 | 0x649DA589, 0xA345415E, 0x5C038323, 0x3E5D3BB9, | ||
145 | 0x43D79572, 0x7E6DD07C, 0x06DFDF1E, 0x6C6CC4EF, | ||
146 | 0x7160A539, 0x73BFBE70, 0x83877605, 0x4523ECF1 | ||
147 | }; | ||
148 | |||
149 | static const u_int32_t cast_sbox3[256] = { | ||
150 | 0x8DEFC240, 0x25FA5D9F, 0xEB903DBF, 0xE810C907, | ||
151 | 0x47607FFF, 0x369FE44B, 0x8C1FC644, 0xAECECA90, | ||
152 | 0xBEB1F9BF, 0xEEFBCAEA, 0xE8CF1950, 0x51DF07AE, | ||
153 | 0x920E8806, 0xF0AD0548, 0xE13C8D83, 0x927010D5, | ||
154 | 0x11107D9F, 0x07647DB9, 0xB2E3E4D4, 0x3D4F285E, | ||
155 | 0xB9AFA820, 0xFADE82E0, 0xA067268B, 0x8272792E, | ||
156 | 0x553FB2C0, 0x489AE22B, 0xD4EF9794, 0x125E3FBC, | ||
157 | 0x21FFFCEE, 0x825B1BFD, 0x9255C5ED, 0x1257A240, | ||
158 | 0x4E1A8302, 0xBAE07FFF, 0x528246E7, 0x8E57140E, | ||
159 | 0x3373F7BF, 0x8C9F8188, 0xA6FC4EE8, 0xC982B5A5, | ||
160 | 0xA8C01DB7, 0x579FC264, 0x67094F31, 0xF2BD3F5F, | ||
161 | 0x40FFF7C1, 0x1FB78DFC, 0x8E6BD2C1, 0x437BE59B, | ||
162 | 0x99B03DBF, 0xB5DBC64B, 0x638DC0E6, 0x55819D99, | ||
163 | 0xA197C81C, 0x4A012D6E, 0xC5884A28, 0xCCC36F71, | ||
164 | 0xB843C213, 0x6C0743F1, 0x8309893C, 0x0FEDDD5F, | ||
165 | 0x2F7FE850, 0xD7C07F7E, 0x02507FBF, 0x5AFB9A04, | ||
166 | 0xA747D2D0, 0x1651192E, 0xAF70BF3E, 0x58C31380, | ||
167 | 0x5F98302E, 0x727CC3C4, 0x0A0FB402, 0x0F7FEF82, | ||
168 | 0x8C96FDAD, 0x5D2C2AAE, 0x8EE99A49, 0x50DA88B8, | ||
169 | 0x8427F4A0, 0x1EAC5790, 0x796FB449, 0x8252DC15, | ||
170 | 0xEFBD7D9B, 0xA672597D, 0xADA840D8, 0x45F54504, | ||
171 | 0xFA5D7403, 0xE83EC305, 0x4F91751A, 0x925669C2, | ||
172 | 0x23EFE941, 0xA903F12E, 0x60270DF2, 0x0276E4B6, | ||
173 | 0x94FD6574, 0x927985B2, 0x8276DBCB, 0x02778176, | ||
174 | 0xF8AF918D, 0x4E48F79E, 0x8F616DDF, 0xE29D840E, | ||
175 | 0x842F7D83, 0x340CE5C8, 0x96BBB682, 0x93B4B148, | ||
176 | 0xEF303CAB, 0x984FAF28, 0x779FAF9B, 0x92DC560D, | ||
177 | 0x224D1E20, 0x8437AA88, 0x7D29DC96, 0x2756D3DC, | ||
178 | 0x8B907CEE, 0xB51FD240, 0xE7C07CE3, 0xE566B4A1, | ||
179 | 0xC3E9615E, 0x3CF8209D, 0x6094D1E3, 0xCD9CA341, | ||
180 | 0x5C76460E, 0x00EA983B, 0xD4D67881, 0xFD47572C, | ||
181 | 0xF76CEDD9, 0xBDA8229C, 0x127DADAA, 0x438A074E, | ||
182 | 0x1F97C090, 0x081BDB8A, 0x93A07EBE, 0xB938CA15, | ||
183 | 0x97B03CFF, 0x3DC2C0F8, 0x8D1AB2EC, 0x64380E51, | ||
184 | 0x68CC7BFB, 0xD90F2788, 0x12490181, 0x5DE5FFD4, | ||
185 | 0xDD7EF86A, 0x76A2E214, 0xB9A40368, 0x925D958F, | ||
186 | 0x4B39FFFA, 0xBA39AEE9, 0xA4FFD30B, 0xFAF7933B, | ||
187 | 0x6D498623, 0x193CBCFA, 0x27627545, 0x825CF47A, | ||
188 | 0x61BD8BA0, 0xD11E42D1, 0xCEAD04F4, 0x127EA392, | ||
189 | 0x10428DB7, 0x8272A972, 0x9270C4A8, 0x127DE50B, | ||
190 | 0x285BA1C8, 0x3C62F44F, 0x35C0EAA5, 0xE805D231, | ||
191 | 0x428929FB, 0xB4FCDF82, 0x4FB66A53, 0x0E7DC15B, | ||
192 | 0x1F081FAB, 0x108618AE, 0xFCFD086D, 0xF9FF2889, | ||
193 | 0x694BCC11, 0x236A5CAE, 0x12DECA4D, 0x2C3F8CC5, | ||
194 | 0xD2D02DFE, 0xF8EF5896, 0xE4CF52DA, 0x95155B67, | ||
195 | 0x494A488C, 0xB9B6A80C, 0x5C8F82BC, 0x89D36B45, | ||
196 | 0x3A609437, 0xEC00C9A9, 0x44715253, 0x0A874B49, | ||
197 | 0xD773BC40, 0x7C34671C, 0x02717EF6, 0x4FEB5536, | ||
198 | 0xA2D02FFF, 0xD2BF60C4, 0xD43F03C0, 0x50B4EF6D, | ||
199 | 0x07478CD1, 0x006E1888, 0xA2E53F55, 0xB9E6D4BC, | ||
200 | 0xA2048016, 0x97573833, 0xD7207D67, 0xDE0F8F3D, | ||
201 | 0x72F87B33, 0xABCC4F33, 0x7688C55D, 0x7B00A6B0, | ||
202 | 0x947B0001, 0x570075D2, 0xF9BB88F8, 0x8942019E, | ||
203 | 0x4264A5FF, 0x856302E0, 0x72DBD92B, 0xEE971B69, | ||
204 | 0x6EA22FDE, 0x5F08AE2B, 0xAF7A616D, 0xE5C98767, | ||
205 | 0xCF1FEBD2, 0x61EFC8C2, 0xF1AC2571, 0xCC8239C2, | ||
206 | 0x67214CB8, 0xB1E583D1, 0xB7DC3E62, 0x7F10BDCE, | ||
207 | 0xF90A5C38, 0x0FF0443D, 0x606E6DC6, 0x60543A49, | ||
208 | 0x5727C148, 0x2BE98A1D, 0x8AB41738, 0x20E1BE24, | ||
209 | 0xAF96DA0F, 0x68458425, 0x99833BE5, 0x600D457D, | ||
210 | 0x282F9350, 0x8334B362, 0xD91D1120, 0x2B6D8DA0, | ||
211 | 0x642B1E31, 0x9C305A00, 0x52BCE688, 0x1B03588A, | ||
212 | 0xF7BAEFD5, 0x4142ED9C, 0xA4315C11, 0x83323EC5, | ||
213 | 0xDFEF4636, 0xA133C501, 0xE9D3531C, 0xEE353783 | ||
214 | }; | ||
215 | |||
216 | static const u_int32_t cast_sbox4[256] = { | ||
217 | 0x9DB30420, 0x1FB6E9DE, 0xA7BE7BEF, 0xD273A298, | ||
218 | 0x4A4F7BDB, 0x64AD8C57, 0x85510443, 0xFA020ED1, | ||
219 | 0x7E287AFF, 0xE60FB663, 0x095F35A1, 0x79EBF120, | ||
220 | 0xFD059D43, 0x6497B7B1, 0xF3641F63, 0x241E4ADF, | ||
221 | 0x28147F5F, 0x4FA2B8CD, 0xC9430040, 0x0CC32220, | ||
222 | 0xFDD30B30, 0xC0A5374F, 0x1D2D00D9, 0x24147B15, | ||
223 | 0xEE4D111A, 0x0FCA5167, 0x71FF904C, 0x2D195FFE, | ||
224 | 0x1A05645F, 0x0C13FEFE, 0x081B08CA, 0x05170121, | ||
225 | 0x80530100, 0xE83E5EFE, 0xAC9AF4F8, 0x7FE72701, | ||
226 | 0xD2B8EE5F, 0x06DF4261, 0xBB9E9B8A, 0x7293EA25, | ||
227 | 0xCE84FFDF, 0xF5718801, 0x3DD64B04, 0xA26F263B, | ||
228 | 0x7ED48400, 0x547EEBE6, 0x446D4CA0, 0x6CF3D6F5, | ||
229 | 0x2649ABDF, 0xAEA0C7F5, 0x36338CC1, 0x503F7E93, | ||
230 | 0xD3772061, 0x11B638E1, 0x72500E03, 0xF80EB2BB, | ||
231 | 0xABE0502E, 0xEC8D77DE, 0x57971E81, 0xE14F6746, | ||
232 | 0xC9335400, 0x6920318F, 0x081DBB99, 0xFFC304A5, | ||
233 | 0x4D351805, 0x7F3D5CE3, 0xA6C866C6, 0x5D5BCCA9, | ||
234 | 0xDAEC6FEA, 0x9F926F91, 0x9F46222F, 0x3991467D, | ||
235 | 0xA5BF6D8E, 0x1143C44F, 0x43958302, 0xD0214EEB, | ||
236 | 0x022083B8, 0x3FB6180C, 0x18F8931E, 0x281658E6, | ||
237 | 0x26486E3E, 0x8BD78A70, 0x7477E4C1, 0xB506E07C, | ||
238 | 0xF32D0A25, 0x79098B02, 0xE4EABB81, 0x28123B23, | ||
239 | 0x69DEAD38, 0x1574CA16, 0xDF871B62, 0x211C40B7, | ||
240 | 0xA51A9EF9, 0x0014377B, 0x041E8AC8, 0x09114003, | ||
241 | 0xBD59E4D2, 0xE3D156D5, 0x4FE876D5, 0x2F91A340, | ||
242 | 0x557BE8DE, 0x00EAE4A7, 0x0CE5C2EC, 0x4DB4BBA6, | ||
243 | 0xE756BDFF, 0xDD3369AC, 0xEC17B035, 0x06572327, | ||
244 | 0x99AFC8B0, 0x56C8C391, 0x6B65811C, 0x5E146119, | ||
245 | 0x6E85CB75, 0xBE07C002, 0xC2325577, 0x893FF4EC, | ||
246 | 0x5BBFC92D, 0xD0EC3B25, 0xB7801AB7, 0x8D6D3B24, | ||
247 | 0x20C763EF, 0xC366A5FC, 0x9C382880, 0x0ACE3205, | ||
248 | 0xAAC9548A, 0xECA1D7C7, 0x041AFA32, 0x1D16625A, | ||
249 | 0x6701902C, 0x9B757A54, 0x31D477F7, 0x9126B031, | ||
250 | 0x36CC6FDB, 0xC70B8B46, 0xD9E66A48, 0x56E55A79, | ||
251 | 0x026A4CEB, 0x52437EFF, 0x2F8F76B4, 0x0DF980A5, | ||
252 | 0x8674CDE3, 0xEDDA04EB, 0x17A9BE04, 0x2C18F4DF, | ||
253 | 0xB7747F9D, 0xAB2AF7B4, 0xEFC34D20, 0x2E096B7C, | ||
254 | 0x1741A254, 0xE5B6A035, 0x213D42F6, 0x2C1C7C26, | ||
255 | 0x61C2F50F, 0x6552DAF9, 0xD2C231F8, 0x25130F69, | ||
256 | 0xD8167FA2, 0x0418F2C8, 0x001A96A6, 0x0D1526AB, | ||
257 | 0x63315C21, 0x5E0A72EC, 0x49BAFEFD, 0x187908D9, | ||
258 | 0x8D0DBD86, 0x311170A7, 0x3E9B640C, 0xCC3E10D7, | ||
259 | 0xD5CAD3B6, 0x0CAEC388, 0xF73001E1, 0x6C728AFF, | ||
260 | 0x71EAE2A1, 0x1F9AF36E, 0xCFCBD12F, 0xC1DE8417, | ||
261 | 0xAC07BE6B, 0xCB44A1D8, 0x8B9B0F56, 0x013988C3, | ||
262 | 0xB1C52FCA, 0xB4BE31CD, 0xD8782806, 0x12A3A4E2, | ||
263 | 0x6F7DE532, 0x58FD7EB6, 0xD01EE900, 0x24ADFFC2, | ||
264 | 0xF4990FC5, 0x9711AAC5, 0x001D7B95, 0x82E5E7D2, | ||
265 | 0x109873F6, 0x00613096, 0xC32D9521, 0xADA121FF, | ||
266 | 0x29908415, 0x7FBB977F, 0xAF9EB3DB, 0x29C9ED2A, | ||
267 | 0x5CE2A465, 0xA730F32C, 0xD0AA3FE8, 0x8A5CC091, | ||
268 | 0xD49E2CE7, 0x0CE454A9, 0xD60ACD86, 0x015F1919, | ||
269 | 0x77079103, 0xDEA03AF6, 0x78A8565E, 0xDEE356DF, | ||
270 | 0x21F05CBE, 0x8B75E387, 0xB3C50651, 0xB8A5C3EF, | ||
271 | 0xD8EEB6D2, 0xE523BE77, 0xC2154529, 0x2F69EFDF, | ||
272 | 0xAFE67AFB, 0xF470C4B2, 0xF3E0EB5B, 0xD6CC9876, | ||
273 | 0x39E4460C, 0x1FDA8538, 0x1987832F, 0xCA007367, | ||
274 | 0xA99144F8, 0x296B299E, 0x492FC295, 0x9266BEAB, | ||
275 | 0xB5676E69, 0x9BD3DDDA, 0xDF7E052F, 0xDB25701C, | ||
276 | 0x1B5E51EE, 0xF65324E6, 0x6AFCE36C, 0x0316CC04, | ||
277 | 0x8644213E, 0xB7DC59D0, 0x7965291F, 0xCCD6FD43, | ||
278 | 0x41823979, 0x932BCDF6, 0xB657C34D, 0x4EDFD282, | ||
279 | 0x7AE5290C, 0x3CB9536B, 0x851E20FE, 0x9833557E, | ||
280 | 0x13ECF0B0, 0xD3FFB372, 0x3F85C5C1, 0x0AEF7ED2 | ||
281 | }; | ||
282 | |||
283 | static const u_int32_t cast_sbox5[256] = { | ||
284 | 0x7EC90C04, 0x2C6E74B9, 0x9B0E66DF, 0xA6337911, | ||
285 | 0xB86A7FFF, 0x1DD358F5, 0x44DD9D44, 0x1731167F, | ||
286 | 0x08FBF1FA, 0xE7F511CC, 0xD2051B00, 0x735ABA00, | ||
287 | 0x2AB722D8, 0x386381CB, 0xACF6243A, 0x69BEFD7A, | ||
288 | 0xE6A2E77F, 0xF0C720CD, 0xC4494816, 0xCCF5C180, | ||
289 | 0x38851640, 0x15B0A848, 0xE68B18CB, 0x4CAADEFF, | ||
290 | 0x5F480A01, 0x0412B2AA, 0x259814FC, 0x41D0EFE2, | ||
291 | 0x4E40B48D, 0x248EB6FB, 0x8DBA1CFE, 0x41A99B02, | ||
292 | 0x1A550A04, 0xBA8F65CB, 0x7251F4E7, 0x95A51725, | ||
293 | 0xC106ECD7, 0x97A5980A, 0xC539B9AA, 0x4D79FE6A, | ||
294 | 0xF2F3F763, 0x68AF8040, 0xED0C9E56, 0x11B4958B, | ||
295 | 0xE1EB5A88, 0x8709E6B0, 0xD7E07156, 0x4E29FEA7, | ||
296 | 0x6366E52D, 0x02D1C000, 0xC4AC8E05, 0x9377F571, | ||
297 | 0x0C05372A, 0x578535F2, 0x2261BE02, 0xD642A0C9, | ||
298 | 0xDF13A280, 0x74B55BD2, 0x682199C0, 0xD421E5EC, | ||
299 | 0x53FB3CE8, 0xC8ADEDB3, 0x28A87FC9, 0x3D959981, | ||
300 | 0x5C1FF900, 0xFE38D399, 0x0C4EFF0B, 0x062407EA, | ||
301 | 0xAA2F4FB1, 0x4FB96976, 0x90C79505, 0xB0A8A774, | ||
302 | 0xEF55A1FF, 0xE59CA2C2, 0xA6B62D27, 0xE66A4263, | ||
303 | 0xDF65001F, 0x0EC50966, 0xDFDD55BC, 0x29DE0655, | ||
304 | 0x911E739A, 0x17AF8975, 0x32C7911C, 0x89F89468, | ||
305 | 0x0D01E980, 0x524755F4, 0x03B63CC9, 0x0CC844B2, | ||
306 | 0xBCF3F0AA, 0x87AC36E9, 0xE53A7426, 0x01B3D82B, | ||
307 | 0x1A9E7449, 0x64EE2D7E, 0xCDDBB1DA, 0x01C94910, | ||
308 | 0xB868BF80, 0x0D26F3FD, 0x9342EDE7, 0x04A5C284, | ||
309 | 0x636737B6, 0x50F5B616, 0xF24766E3, 0x8ECA36C1, | ||
310 | 0x136E05DB, 0xFEF18391, 0xFB887A37, 0xD6E7F7D4, | ||
311 | 0xC7FB7DC9, 0x3063FCDF, 0xB6F589DE, 0xEC2941DA, | ||
312 | 0x26E46695, 0xB7566419, 0xF654EFC5, 0xD08D58B7, | ||
313 | 0x48925401, 0xC1BACB7F, 0xE5FF550F, 0xB6083049, | ||
314 | 0x5BB5D0E8, 0x87D72E5A, 0xAB6A6EE1, 0x223A66CE, | ||
315 | 0xC62BF3CD, 0x9E0885F9, 0x68CB3E47, 0x086C010F, | ||
316 | 0xA21DE820, 0xD18B69DE, 0xF3F65777, 0xFA02C3F6, | ||
317 | 0x407EDAC3, 0xCBB3D550, 0x1793084D, 0xB0D70EBA, | ||
318 | 0x0AB378D5, 0xD951FB0C, 0xDED7DA56, 0x4124BBE4, | ||
319 | 0x94CA0B56, 0x0F5755D1, 0xE0E1E56E, 0x6184B5BE, | ||
320 | 0x580A249F, 0x94F74BC0, 0xE327888E, 0x9F7B5561, | ||
321 | 0xC3DC0280, 0x05687715, 0x646C6BD7, 0x44904DB3, | ||
322 | 0x66B4F0A3, 0xC0F1648A, 0x697ED5AF, 0x49E92FF6, | ||
323 | 0x309E374F, 0x2CB6356A, 0x85808573, 0x4991F840, | ||
324 | 0x76F0AE02, 0x083BE84D, 0x28421C9A, 0x44489406, | ||
325 | 0x736E4CB8, 0xC1092910, 0x8BC95FC6, 0x7D869CF4, | ||
326 | 0x134F616F, 0x2E77118D, 0xB31B2BE1, 0xAA90B472, | ||
327 | 0x3CA5D717, 0x7D161BBA, 0x9CAD9010, 0xAF462BA2, | ||
328 | 0x9FE459D2, 0x45D34559, 0xD9F2DA13, 0xDBC65487, | ||
329 | 0xF3E4F94E, 0x176D486F, 0x097C13EA, 0x631DA5C7, | ||
330 | 0x445F7382, 0x175683F4, 0xCDC66A97, 0x70BE0288, | ||
331 | 0xB3CDCF72, 0x6E5DD2F3, 0x20936079, 0x459B80A5, | ||
332 | 0xBE60E2DB, 0xA9C23101, 0xEBA5315C, 0x224E42F2, | ||
333 | 0x1C5C1572, 0xF6721B2C, 0x1AD2FFF3, 0x8C25404E, | ||
334 | 0x324ED72F, 0x4067B7FD, 0x0523138E, 0x5CA3BC78, | ||
335 | 0xDC0FD66E, 0x75922283, 0x784D6B17, 0x58EBB16E, | ||
336 | 0x44094F85, 0x3F481D87, 0xFCFEAE7B, 0x77B5FF76, | ||
337 | 0x8C2302BF, 0xAAF47556, 0x5F46B02A, 0x2B092801, | ||
338 | 0x3D38F5F7, 0x0CA81F36, 0x52AF4A8A, 0x66D5E7C0, | ||
339 | 0xDF3B0874, 0x95055110, 0x1B5AD7A8, 0xF61ED5AD, | ||
340 | 0x6CF6E479, 0x20758184, 0xD0CEFA65, 0x88F7BE58, | ||
341 | 0x4A046826, 0x0FF6F8F3, 0xA09C7F70, 0x5346ABA0, | ||
342 | 0x5CE96C28, 0xE176EDA3, 0x6BAC307F, 0x376829D2, | ||
343 | 0x85360FA9, 0x17E3FE2A, 0x24B79767, 0xF5A96B20, | ||
344 | 0xD6CD2595, 0x68FF1EBF, 0x7555442C, 0xF19F06BE, | ||
345 | 0xF9E0659A, 0xEEB9491D, 0x34010718, 0xBB30CAB8, | ||
346 | 0xE822FE15, 0x88570983, 0x750E6249, 0xDA627E55, | ||
347 | 0x5E76FFA8, 0xB1534546, 0x6D47DE08, 0xEFE9E7D4 | ||
348 | }; | ||
349 | |||
350 | static const u_int32_t cast_sbox6[256] = { | ||
351 | 0xF6FA8F9D, 0x2CAC6CE1, 0x4CA34867, 0xE2337F7C, | ||
352 | 0x95DB08E7, 0x016843B4, 0xECED5CBC, 0x325553AC, | ||
353 | 0xBF9F0960, 0xDFA1E2ED, 0x83F0579D, 0x63ED86B9, | ||
354 | 0x1AB6A6B8, 0xDE5EBE39, 0xF38FF732, 0x8989B138, | ||
355 | 0x33F14961, 0xC01937BD, 0xF506C6DA, 0xE4625E7E, | ||
356 | 0xA308EA99, 0x4E23E33C, 0x79CBD7CC, 0x48A14367, | ||
357 | 0xA3149619, 0xFEC94BD5, 0xA114174A, 0xEAA01866, | ||
358 | 0xA084DB2D, 0x09A8486F, 0xA888614A, 0x2900AF98, | ||
359 | 0x01665991, 0xE1992863, 0xC8F30C60, 0x2E78EF3C, | ||
360 | 0xD0D51932, 0xCF0FEC14, 0xF7CA07D2, 0xD0A82072, | ||
361 | 0xFD41197E, 0x9305A6B0, 0xE86BE3DA, 0x74BED3CD, | ||
362 | 0x372DA53C, 0x4C7F4448, 0xDAB5D440, 0x6DBA0EC3, | ||
363 | 0x083919A7, 0x9FBAEED9, 0x49DBCFB0, 0x4E670C53, | ||
364 | 0x5C3D9C01, 0x64BDB941, 0x2C0E636A, 0xBA7DD9CD, | ||
365 | 0xEA6F7388, 0xE70BC762, 0x35F29ADB, 0x5C4CDD8D, | ||
366 | 0xF0D48D8C, 0xB88153E2, 0x08A19866, 0x1AE2EAC8, | ||
367 | 0x284CAF89, 0xAA928223, 0x9334BE53, 0x3B3A21BF, | ||
368 | 0x16434BE3, 0x9AEA3906, 0xEFE8C36E, 0xF890CDD9, | ||
369 | 0x80226DAE, 0xC340A4A3, 0xDF7E9C09, 0xA694A807, | ||
370 | 0x5B7C5ECC, 0x221DB3A6, 0x9A69A02F, 0x68818A54, | ||
371 | 0xCEB2296F, 0x53C0843A, 0xFE893655, 0x25BFE68A, | ||
372 | 0xB4628ABC, 0xCF222EBF, 0x25AC6F48, 0xA9A99387, | ||
373 | 0x53BDDB65, 0xE76FFBE7, 0xE967FD78, 0x0BA93563, | ||
374 | 0x8E342BC1, 0xE8A11BE9, 0x4980740D, 0xC8087DFC, | ||
375 | 0x8DE4BF99, 0xA11101A0, 0x7FD37975, 0xDA5A26C0, | ||
376 | 0xE81F994F, 0x9528CD89, 0xFD339FED, 0xB87834BF, | ||
377 | 0x5F04456D, 0x22258698, 0xC9C4C83B, 0x2DC156BE, | ||
378 | 0x4F628DAA, 0x57F55EC5, 0xE2220ABE, 0xD2916EBF, | ||
379 | 0x4EC75B95, 0x24F2C3C0, 0x42D15D99, 0xCD0D7FA0, | ||
380 | 0x7B6E27FF, 0xA8DC8AF0, 0x7345C106, 0xF41E232F, | ||
381 | 0x35162386, 0xE6EA8926, 0x3333B094, 0x157EC6F2, | ||
382 | 0x372B74AF, 0x692573E4, 0xE9A9D848, 0xF3160289, | ||
383 | 0x3A62EF1D, 0xA787E238, 0xF3A5F676, 0x74364853, | ||
384 | 0x20951063, 0x4576698D, 0xB6FAD407, 0x592AF950, | ||
385 | 0x36F73523, 0x4CFB6E87, 0x7DA4CEC0, 0x6C152DAA, | ||
386 | 0xCB0396A8, 0xC50DFE5D, 0xFCD707AB, 0x0921C42F, | ||
387 | 0x89DFF0BB, 0x5FE2BE78, 0x448F4F33, 0x754613C9, | ||
388 | 0x2B05D08D, 0x48B9D585, 0xDC049441, 0xC8098F9B, | ||
389 | 0x7DEDE786, 0xC39A3373, 0x42410005, 0x6A091751, | ||
390 | 0x0EF3C8A6, 0x890072D6, 0x28207682, 0xA9A9F7BE, | ||
391 | 0xBF32679D, 0xD45B5B75, 0xB353FD00, 0xCBB0E358, | ||
392 | 0x830F220A, 0x1F8FB214, 0xD372CF08, 0xCC3C4A13, | ||
393 | 0x8CF63166, 0x061C87BE, 0x88C98F88, 0x6062E397, | ||
394 | 0x47CF8E7A, 0xB6C85283, 0x3CC2ACFB, 0x3FC06976, | ||
395 | 0x4E8F0252, 0x64D8314D, 0xDA3870E3, 0x1E665459, | ||
396 | 0xC10908F0, 0x513021A5, 0x6C5B68B7, 0x822F8AA0, | ||
397 | 0x3007CD3E, 0x74719EEF, 0xDC872681, 0x073340D4, | ||
398 | 0x7E432FD9, 0x0C5EC241, 0x8809286C, 0xF592D891, | ||
399 | 0x08A930F6, 0x957EF305, 0xB7FBFFBD, 0xC266E96F, | ||
400 | 0x6FE4AC98, 0xB173ECC0, 0xBC60B42A, 0x953498DA, | ||
401 | 0xFBA1AE12, 0x2D4BD736, 0x0F25FAAB, 0xA4F3FCEB, | ||
402 | 0xE2969123, 0x257F0C3D, 0x9348AF49, 0x361400BC, | ||
403 | 0xE8816F4A, 0x3814F200, 0xA3F94043, 0x9C7A54C2, | ||
404 | 0xBC704F57, 0xDA41E7F9, 0xC25AD33A, 0x54F4A084, | ||
405 | 0xB17F5505, 0x59357CBE, 0xEDBD15C8, 0x7F97C5AB, | ||
406 | 0xBA5AC7B5, 0xB6F6DEAF, 0x3A479C3A, 0x5302DA25, | ||
407 | 0x653D7E6A, 0x54268D49, 0x51A477EA, 0x5017D55B, | ||
408 | 0xD7D25D88, 0x44136C76, 0x0404A8C8, 0xB8E5A121, | ||
409 | 0xB81A928A, 0x60ED5869, 0x97C55B96, 0xEAEC991B, | ||
410 | 0x29935913, 0x01FDB7F1, 0x088E8DFA, 0x9AB6F6F5, | ||
411 | 0x3B4CBF9F, 0x4A5DE3AB, 0xE6051D35, 0xA0E1D855, | ||
412 | 0xD36B4CF1, 0xF544EDEB, 0xB0E93524, 0xBEBB8FBD, | ||
413 | 0xA2D762CF, 0x49C92F54, 0x38B5F331, 0x7128A454, | ||
414 | 0x48392905, 0xA65B1DB8, 0x851C97BD, 0xD675CF2F | ||
415 | }; | ||
416 | |||
417 | static const u_int32_t cast_sbox7[256] = { | ||
418 | 0x85E04019, 0x332BF567, 0x662DBFFF, 0xCFC65693, | ||
419 | 0x2A8D7F6F, 0xAB9BC912, 0xDE6008A1, 0x2028DA1F, | ||
420 | 0x0227BCE7, 0x4D642916, 0x18FAC300, 0x50F18B82, | ||
421 | 0x2CB2CB11, 0xB232E75C, 0x4B3695F2, 0xB28707DE, | ||
422 | 0xA05FBCF6, 0xCD4181E9, 0xE150210C, 0xE24EF1BD, | ||
423 | 0xB168C381, 0xFDE4E789, 0x5C79B0D8, 0x1E8BFD43, | ||
424 | 0x4D495001, 0x38BE4341, 0x913CEE1D, 0x92A79C3F, | ||
425 | 0x089766BE, 0xBAEEADF4, 0x1286BECF, 0xB6EACB19, | ||
426 | 0x2660C200, 0x7565BDE4, 0x64241F7A, 0x8248DCA9, | ||
427 | 0xC3B3AD66, 0x28136086, 0x0BD8DFA8, 0x356D1CF2, | ||
428 | 0x107789BE, 0xB3B2E9CE, 0x0502AA8F, 0x0BC0351E, | ||
429 | 0x166BF52A, 0xEB12FF82, 0xE3486911, 0xD34D7516, | ||
430 | 0x4E7B3AFF, 0x5F43671B, 0x9CF6E037, 0x4981AC83, | ||
431 | 0x334266CE, 0x8C9341B7, 0xD0D854C0, 0xCB3A6C88, | ||
432 | 0x47BC2829, 0x4725BA37, 0xA66AD22B, 0x7AD61F1E, | ||
433 | 0x0C5CBAFA, 0x4437F107, 0xB6E79962, 0x42D2D816, | ||
434 | 0x0A961288, 0xE1A5C06E, 0x13749E67, 0x72FC081A, | ||
435 | 0xB1D139F7, 0xF9583745, 0xCF19DF58, 0xBEC3F756, | ||
436 | 0xC06EBA30, 0x07211B24, 0x45C28829, 0xC95E317F, | ||
437 | 0xBC8EC511, 0x38BC46E9, 0xC6E6FA14, 0xBAE8584A, | ||
438 | 0xAD4EBC46, 0x468F508B, 0x7829435F, 0xF124183B, | ||
439 | 0x821DBA9F, 0xAFF60FF4, 0xEA2C4E6D, 0x16E39264, | ||
440 | 0x92544A8B, 0x009B4FC3, 0xABA68CED, 0x9AC96F78, | ||
441 | 0x06A5B79A, 0xB2856E6E, 0x1AEC3CA9, 0xBE838688, | ||
442 | 0x0E0804E9, 0x55F1BE56, 0xE7E5363B, 0xB3A1F25D, | ||
443 | 0xF7DEBB85, 0x61FE033C, 0x16746233, 0x3C034C28, | ||
444 | 0xDA6D0C74, 0x79AAC56C, 0x3CE4E1AD, 0x51F0C802, | ||
445 | 0x98F8F35A, 0x1626A49F, 0xEED82B29, 0x1D382FE3, | ||
446 | 0x0C4FB99A, 0xBB325778, 0x3EC6D97B, 0x6E77A6A9, | ||
447 | 0xCB658B5C, 0xD45230C7, 0x2BD1408B, 0x60C03EB7, | ||
448 | 0xB9068D78, 0xA33754F4, 0xF430C87D, 0xC8A71302, | ||
449 | 0xB96D8C32, 0xEBD4E7BE, 0xBE8B9D2D, 0x7979FB06, | ||
450 | 0xE7225308, 0x8B75CF77, 0x11EF8DA4, 0xE083C858, | ||
451 | 0x8D6B786F, 0x5A6317A6, 0xFA5CF7A0, 0x5DDA0033, | ||
452 | 0xF28EBFB0, 0xF5B9C310, 0xA0EAC280, 0x08B9767A, | ||
453 | 0xA3D9D2B0, 0x79D34217, 0x021A718D, 0x9AC6336A, | ||
454 | 0x2711FD60, 0x438050E3, 0x069908A8, 0x3D7FEDC4, | ||
455 | 0x826D2BEF, 0x4EEB8476, 0x488DCF25, 0x36C9D566, | ||
456 | 0x28E74E41, 0xC2610ACA, 0x3D49A9CF, 0xBAE3B9DF, | ||
457 | 0xB65F8DE6, 0x92AEAF64, 0x3AC7D5E6, 0x9EA80509, | ||
458 | 0xF22B017D, 0xA4173F70, 0xDD1E16C3, 0x15E0D7F9, | ||
459 | 0x50B1B887, 0x2B9F4FD5, 0x625ABA82, 0x6A017962, | ||
460 | 0x2EC01B9C, 0x15488AA9, 0xD716E740, 0x40055A2C, | ||
461 | 0x93D29A22, 0xE32DBF9A, 0x058745B9, 0x3453DC1E, | ||
462 | 0xD699296E, 0x496CFF6F, 0x1C9F4986, 0xDFE2ED07, | ||
463 | 0xB87242D1, 0x19DE7EAE, 0x053E561A, 0x15AD6F8C, | ||
464 | 0x66626C1C, 0x7154C24C, 0xEA082B2A, 0x93EB2939, | ||
465 | 0x17DCB0F0, 0x58D4F2AE, 0x9EA294FB, 0x52CF564C, | ||
466 | 0x9883FE66, 0x2EC40581, 0x763953C3, 0x01D6692E, | ||
467 | 0xD3A0C108, 0xA1E7160E, 0xE4F2DFA6, 0x693ED285, | ||
468 | 0x74904698, 0x4C2B0EDD, 0x4F757656, 0x5D393378, | ||
469 | 0xA132234F, 0x3D321C5D, 0xC3F5E194, 0x4B269301, | ||
470 | 0xC79F022F, 0x3C997E7E, 0x5E4F9504, 0x3FFAFBBD, | ||
471 | 0x76F7AD0E, 0x296693F4, 0x3D1FCE6F, 0xC61E45BE, | ||
472 | 0xD3B5AB34, 0xF72BF9B7, 0x1B0434C0, 0x4E72B567, | ||
473 | 0x5592A33D, 0xB5229301, 0xCFD2A87F, 0x60AEB767, | ||
474 | 0x1814386B, 0x30BCC33D, 0x38A0C07D, 0xFD1606F2, | ||
475 | 0xC363519B, 0x589DD390, 0x5479F8E6, 0x1CB8D647, | ||
476 | 0x97FD61A9, 0xEA7759F4, 0x2D57539D, 0x569A58CF, | ||
477 | 0xE84E63AD, 0x462E1B78, 0x6580F87E, 0xF3817914, | ||
478 | 0x91DA55F4, 0x40A230F3, 0xD1988F35, 0xB6E318D2, | ||
479 | 0x3FFA50BC, 0x3D40F021, 0xC3C0BDAE, 0x4958C24C, | ||
480 | 0x518F36B2, 0x84B1D370, 0x0FEDCE83, 0x878DDADA, | ||
481 | 0xF2A279C7, 0x94E01BE8, 0x90716F4B, 0x954B8AA3 | ||
482 | }; | ||
483 | |||
484 | static const u_int32_t cast_sbox8[256] = { | ||
485 | 0xE216300D, 0xBBDDFFFC, 0xA7EBDABD, 0x35648095, | ||
486 | 0x7789F8B7, 0xE6C1121B, 0x0E241600, 0x052CE8B5, | ||
487 | 0x11A9CFB0, 0xE5952F11, 0xECE7990A, 0x9386D174, | ||
488 | 0x2A42931C, 0x76E38111, 0xB12DEF3A, 0x37DDDDFC, | ||
489 | 0xDE9ADEB1, 0x0A0CC32C, 0xBE197029, 0x84A00940, | ||
490 | 0xBB243A0F, 0xB4D137CF, 0xB44E79F0, 0x049EEDFD, | ||
491 | 0x0B15A15D, 0x480D3168, 0x8BBBDE5A, 0x669DED42, | ||
492 | 0xC7ECE831, 0x3F8F95E7, 0x72DF191B, 0x7580330D, | ||
493 | 0x94074251, 0x5C7DCDFA, 0xABBE6D63, 0xAA402164, | ||
494 | 0xB301D40A, 0x02E7D1CA, 0x53571DAE, 0x7A3182A2, | ||
495 | 0x12A8DDEC, 0xFDAA335D, 0x176F43E8, 0x71FB46D4, | ||
496 | 0x38129022, 0xCE949AD4, 0xB84769AD, 0x965BD862, | ||
497 | 0x82F3D055, 0x66FB9767, 0x15B80B4E, 0x1D5B47A0, | ||
498 | 0x4CFDE06F, 0xC28EC4B8, 0x57E8726E, 0x647A78FC, | ||
499 | 0x99865D44, 0x608BD593, 0x6C200E03, 0x39DC5FF6, | ||
500 | 0x5D0B00A3, 0xAE63AFF2, 0x7E8BD632, 0x70108C0C, | ||
501 | 0xBBD35049, 0x2998DF04, 0x980CF42A, 0x9B6DF491, | ||
502 | 0x9E7EDD53, 0x06918548, 0x58CB7E07, 0x3B74EF2E, | ||
503 | 0x522FFFB1, 0xD24708CC, 0x1C7E27CD, 0xA4EB215B, | ||
504 | 0x3CF1D2E2, 0x19B47A38, 0x424F7618, 0x35856039, | ||
505 | 0x9D17DEE7, 0x27EB35E6, 0xC9AFF67B, 0x36BAF5B8, | ||
506 | 0x09C467CD, 0xC18910B1, 0xE11DBF7B, 0x06CD1AF8, | ||
507 | 0x7170C608, 0x2D5E3354, 0xD4DE495A, 0x64C6D006, | ||
508 | 0xBCC0C62C, 0x3DD00DB3, 0x708F8F34, 0x77D51B42, | ||
509 | 0x264F620F, 0x24B8D2BF, 0x15C1B79E, 0x46A52564, | ||
510 | 0xF8D7E54E, 0x3E378160, 0x7895CDA5, 0x859C15A5, | ||
511 | 0xE6459788, 0xC37BC75F, 0xDB07BA0C, 0x0676A3AB, | ||
512 | 0x7F229B1E, 0x31842E7B, 0x24259FD7, 0xF8BEF472, | ||
513 | 0x835FFCB8, 0x6DF4C1F2, 0x96F5B195, 0xFD0AF0FC, | ||
514 | 0xB0FE134C, 0xE2506D3D, 0x4F9B12EA, 0xF215F225, | ||
515 | 0xA223736F, 0x9FB4C428, 0x25D04979, 0x34C713F8, | ||
516 | 0xC4618187, 0xEA7A6E98, 0x7CD16EFC, 0x1436876C, | ||
517 | 0xF1544107, 0xBEDEEE14, 0x56E9AF27, 0xA04AA441, | ||
518 | 0x3CF7C899, 0x92ECBAE6, 0xDD67016D, 0x151682EB, | ||
519 | 0xA842EEDF, 0xFDBA60B4, 0xF1907B75, 0x20E3030F, | ||
520 | 0x24D8C29E, 0xE139673B, 0xEFA63FB8, 0x71873054, | ||
521 | 0xB6F2CF3B, 0x9F326442, 0xCB15A4CC, 0xB01A4504, | ||
522 | 0xF1E47D8D, 0x844A1BE5, 0xBAE7DFDC, 0x42CBDA70, | ||
523 | 0xCD7DAE0A, 0x57E85B7A, 0xD53F5AF6, 0x20CF4D8C, | ||
524 | 0xCEA4D428, 0x79D130A4, 0x3486EBFB, 0x33D3CDDC, | ||
525 | 0x77853B53, 0x37EFFCB5, 0xC5068778, 0xE580B3E6, | ||
526 | 0x4E68B8F4, 0xC5C8B37E, 0x0D809EA2, 0x398FEB7C, | ||
527 | 0x132A4F94, 0x43B7950E, 0x2FEE7D1C, 0x223613BD, | ||
528 | 0xDD06CAA2, 0x37DF932B, 0xC4248289, 0xACF3EBC3, | ||
529 | 0x5715F6B7, 0xEF3478DD, 0xF267616F, 0xC148CBE4, | ||
530 | 0x9052815E, 0x5E410FAB, 0xB48A2465, 0x2EDA7FA4, | ||
531 | 0xE87B40E4, 0xE98EA084, 0x5889E9E1, 0xEFD390FC, | ||
532 | 0xDD07D35B, 0xDB485694, 0x38D7E5B2, 0x57720101, | ||
533 | 0x730EDEBC, 0x5B643113, 0x94917E4F, 0x503C2FBA, | ||
534 | 0x646F1282, 0x7523D24A, 0xE0779695, 0xF9C17A8F, | ||
535 | 0x7A5B2121, 0xD187B896, 0x29263A4D, 0xBA510CDF, | ||
536 | 0x81F47C9F, 0xAD1163ED, 0xEA7B5965, 0x1A00726E, | ||
537 | 0x11403092, 0x00DA6D77, 0x4A0CDD61, 0xAD1F4603, | ||
538 | 0x605BDFB0, 0x9EEDC364, 0x22EBE6A8, 0xCEE7D28A, | ||
539 | 0xA0E736A0, 0x5564A6B9, 0x10853209, 0xC7EB8F37, | ||
540 | 0x2DE705CA, 0x8951570F, 0xDF09822B, 0xBD691A6C, | ||
541 | 0xAA12E4F2, 0x87451C0F, 0xE0F6A27A, 0x3ADA4819, | ||
542 | 0x4CF1764F, 0x0D771C2B, 0x67CDB156, 0x350D8384, | ||
543 | 0x5938FA0F, 0x42399EF3, 0x36997B07, 0x0E84093D, | ||
544 | 0x4AA93E61, 0x8360D87B, 0x1FA98B0C, 0x1149382C, | ||
545 | 0xE97625A5, 0x0614D1B7, 0x0E25244B, 0x0C768347, | ||
546 | 0x589E8D82, 0x0D2059D1, 0xA466BB1E, 0xF8DA0A82, | ||
547 | 0x04F19130, 0xBA6E4EC0, 0x99265164, 0x1EE7230D, | ||
548 | 0x50B2AD80, 0xEAEE6801, 0x8DB2A283, 0xEA8BF59E | ||
549 | }; | ||
550 | |||
551 | /* Macros to access 8-bit bytes out of a 32-bit word */ | ||
552 | #define U8a(x) ( (u_int8_t) (x>>24) ) | ||
553 | #define U8b(x) ( (u_int8_t) ((x>>16)&255) ) | ||
554 | #define U8c(x) ( (u_int8_t) ((x>>8)&255) ) | ||
555 | #define U8d(x) ( (u_int8_t) ((x)&255) ) | ||
556 | |||
557 | /* Circular left shift */ | ||
558 | #define ROL(x, n) ( ((x)<<(n)) | ((x)>>(32-(n))) ) | ||
559 | |||
560 | /* CAST-128 uses three different round functions */ | ||
561 | #define F1(l, r, i) \ | ||
562 | t = ROL(key->xkey[i] + r, key->xkey[i+16]); \ | ||
563 | l ^= ((cast_sbox1[U8a(t)] ^ cast_sbox2[U8b(t)]) - \ | ||
564 | cast_sbox3[U8c(t)]) + cast_sbox4[U8d(t)]; | ||
565 | #define F2(l, r, i) \ | ||
566 | t = ROL(key->xkey[i] ^ r, key->xkey[i+16]); \ | ||
567 | l ^= ((cast_sbox1[U8a(t)] - cast_sbox2[U8b(t)]) + \ | ||
568 | cast_sbox3[U8c(t)]) ^ cast_sbox4[U8d(t)]; | ||
569 | #define F3(l, r, i) \ | ||
570 | t = ROL(key->xkey[i] - r, key->xkey[i+16]); \ | ||
571 | l ^= ((cast_sbox1[U8a(t)] + cast_sbox2[U8b(t)]) ^ \ | ||
572 | cast_sbox3[U8c(t)]) - cast_sbox4[U8d(t)]; | ||
573 | |||
574 | |||
575 | /***** Encryption Function *****/ | ||
576 | |||
577 | void cast_encrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock) | ||
578 | { | ||
579 | u_int32_t t, l, r; | ||
580 | |||
581 | /* Get inblock into l,r */ | ||
582 | l = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) | | ||
583 | ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3]; | ||
584 | r = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) | | ||
585 | ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7]; | ||
586 | /* Do the work */ | ||
587 | F1(l, r, 0); | ||
588 | F2(r, l, 1); | ||
589 | F3(l, r, 2); | ||
590 | F1(r, l, 3); | ||
591 | F2(l, r, 4); | ||
592 | F3(r, l, 5); | ||
593 | F1(l, r, 6); | ||
594 | F2(r, l, 7); | ||
595 | F3(l, r, 8); | ||
596 | F1(r, l, 9); | ||
597 | F2(l, r, 10); | ||
598 | F3(r, l, 11); | ||
599 | /* Only do full 16 rounds if key length > 80 bits */ | ||
600 | if (key->rounds > 12) { | ||
601 | F1(l, r, 12); | ||
602 | F2(r, l, 13); | ||
603 | F3(l, r, 14); | ||
604 | F1(r, l, 15); | ||
605 | } | ||
606 | /* Put l,r into outblock */ | ||
607 | outblock[0] = U8a(r); | ||
608 | outblock[1] = U8b(r); | ||
609 | outblock[2] = U8c(r); | ||
610 | outblock[3] = U8d(r); | ||
611 | outblock[4] = U8a(l); | ||
612 | outblock[5] = U8b(l); | ||
613 | outblock[6] = U8c(l); | ||
614 | outblock[7] = U8d(l); | ||
615 | /* Wipe clean */ | ||
616 | t = l = r = 0; | ||
617 | } | ||
618 | |||
619 | |||
620 | /***** Decryption Function *****/ | ||
621 | |||
622 | void cast_decrypt(cast_key* key, u_int8_t* inblock, u_int8_t* outblock) | ||
623 | { | ||
624 | u_int32_t t, l, r; | ||
625 | |||
626 | /* Get inblock into l,r */ | ||
627 | r = ((u_int32_t)inblock[0] << 24) | ((u_int32_t)inblock[1] << 16) | | ||
628 | ((u_int32_t)inblock[2] << 8) | (u_int32_t)inblock[3]; | ||
629 | l = ((u_int32_t)inblock[4] << 24) | ((u_int32_t)inblock[5] << 16) | | ||
630 | ((u_int32_t)inblock[6] << 8) | (u_int32_t)inblock[7]; | ||
631 | /* Do the work */ | ||
632 | /* Only do full 16 rounds if key length > 80 bits */ | ||
633 | if (key->rounds > 12) { | ||
634 | F1(r, l, 15); | ||
635 | F3(l, r, 14); | ||
636 | F2(r, l, 13); | ||
637 | F1(l, r, 12); | ||
638 | } | ||
639 | F3(r, l, 11); | ||
640 | F2(l, r, 10); | ||
641 | F1(r, l, 9); | ||
642 | F3(l, r, 8); | ||
643 | F2(r, l, 7); | ||
644 | F1(l, r, 6); | ||
645 | F3(r, l, 5); | ||
646 | F2(l, r, 4); | ||
647 | F1(r, l, 3); | ||
648 | F3(l, r, 2); | ||
649 | F2(r, l, 1); | ||
650 | F1(l, r, 0); | ||
651 | /* Put l,r into outblock */ | ||
652 | outblock[0] = U8a(l); | ||
653 | outblock[1] = U8b(l); | ||
654 | outblock[2] = U8c(l); | ||
655 | outblock[3] = U8d(l); | ||
656 | outblock[4] = U8a(r); | ||
657 | outblock[5] = U8b(r); | ||
658 | outblock[6] = U8c(r); | ||
659 | outblock[7] = U8d(r); | ||
660 | /* Wipe clean */ | ||
661 | t = l = r = 0; | ||
662 | } | ||
663 | |||
664 | |||
665 | /***** Key Schedual *****/ | ||
666 | |||
667 | void cast_setkey(cast_key* key, u_int8_t* rawkey, int keybytes) | ||
668 | { | ||
669 | u_int32_t t[4], z[4], x[4]; | ||
670 | int i; | ||
671 | |||
672 | /* Set number of rounds to 12 or 16, depending on key length */ | ||
673 | key->rounds = (keybytes <= 10 ? 12 : 16); | ||
674 | |||
675 | /* Copy key to workspace x */ | ||
676 | for (i = 0; i < 4; i++) { | ||
677 | x[i] = 0; | ||
678 | if ((i*4+0) < keybytes) x[i] = (u_int32_t)rawkey[i*4+0] << 24; | ||
679 | if ((i*4+1) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+1] << 16; | ||
680 | if ((i*4+2) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+2] << 8; | ||
681 | if ((i*4+3) < keybytes) x[i] |= (u_int32_t)rawkey[i*4+3]; | ||
682 | } | ||
683 | /* Generate 32 subkeys, four at a time */ | ||
684 | for (i = 0; i < 32; i+=4) { | ||
685 | switch (i & 4) { | ||
686 | case 0: | ||
687 | t[0] = z[0] = x[0] ^ cast_sbox5[U8b(x[3])] ^ | ||
688 | cast_sbox6[U8d(x[3])] ^ cast_sbox7[U8a(x[3])] ^ | ||
689 | cast_sbox8[U8c(x[3])] ^ cast_sbox7[U8a(x[2])]; | ||
690 | t[1] = z[1] = x[2] ^ cast_sbox5[U8a(z[0])] ^ | ||
691 | cast_sbox6[U8c(z[0])] ^ cast_sbox7[U8b(z[0])] ^ | ||
692 | cast_sbox8[U8d(z[0])] ^ cast_sbox8[U8c(x[2])]; | ||
693 | t[2] = z[2] = x[3] ^ cast_sbox5[U8d(z[1])] ^ | ||
694 | cast_sbox6[U8c(z[1])] ^ cast_sbox7[U8b(z[1])] ^ | ||
695 | cast_sbox8[U8a(z[1])] ^ cast_sbox5[U8b(x[2])]; | ||
696 | t[3] = z[3] = x[1] ^ cast_sbox5[U8c(z[2])] ^ | ||
697 | cast_sbox6[U8b(z[2])] ^ cast_sbox7[U8d(z[2])] ^ | ||
698 | cast_sbox8[U8a(z[2])] ^ cast_sbox6[U8d(x[2])]; | ||
699 | break; | ||
700 | case 4: | ||
701 | t[0] = x[0] = z[2] ^ cast_sbox5[U8b(z[1])] ^ | ||
702 | cast_sbox6[U8d(z[1])] ^ cast_sbox7[U8a(z[1])] ^ | ||
703 | cast_sbox8[U8c(z[1])] ^ cast_sbox7[U8a(z[0])]; | ||
704 | t[1] = x[1] = z[0] ^ cast_sbox5[U8a(x[0])] ^ | ||
705 | cast_sbox6[U8c(x[0])] ^ cast_sbox7[U8b(x[0])] ^ | ||
706 | cast_sbox8[U8d(x[0])] ^ cast_sbox8[U8c(z[0])]; | ||
707 | t[2] = x[2] = z[1] ^ cast_sbox5[U8d(x[1])] ^ | ||
708 | cast_sbox6[U8c(x[1])] ^ cast_sbox7[U8b(x[1])] ^ | ||
709 | cast_sbox8[U8a(x[1])] ^ cast_sbox5[U8b(z[0])]; | ||
710 | t[3] = x[3] = z[3] ^ cast_sbox5[U8c(x[2])] ^ | ||
711 | cast_sbox6[U8b(x[2])] ^ cast_sbox7[U8d(x[2])] ^ | ||
712 | cast_sbox8[U8a(x[2])] ^ cast_sbox6[U8d(z[0])]; | ||
713 | break; | ||
714 | } | ||
715 | switch (i & 12) { | ||
716 | case 0: | ||
717 | case 12: | ||
718 | key->xkey[i+0] = cast_sbox5[U8a(t[2])] ^ cast_sbox6[U8b(t[2])] ^ | ||
719 | cast_sbox7[U8d(t[1])] ^ cast_sbox8[U8c(t[1])]; | ||
720 | key->xkey[i+1] = cast_sbox5[U8c(t[2])] ^ cast_sbox6[U8d(t[2])] ^ | ||
721 | cast_sbox7[U8b(t[1])] ^ cast_sbox8[U8a(t[1])]; | ||
722 | key->xkey[i+2] = cast_sbox5[U8a(t[3])] ^ cast_sbox6[U8b(t[3])] ^ | ||
723 | cast_sbox7[U8d(t[0])] ^ cast_sbox8[U8c(t[0])]; | ||
724 | key->xkey[i+3] = cast_sbox5[U8c(t[3])] ^ cast_sbox6[U8d(t[3])] ^ | ||
725 | cast_sbox7[U8b(t[0])] ^ cast_sbox8[U8a(t[0])]; | ||
726 | break; | ||
727 | case 4: | ||
728 | case 8: | ||
729 | key->xkey[i+0] = cast_sbox5[U8d(t[0])] ^ cast_sbox6[U8c(t[0])] ^ | ||
730 | cast_sbox7[U8a(t[3])] ^ cast_sbox8[U8b(t[3])]; | ||
731 | key->xkey[i+1] = cast_sbox5[U8b(t[0])] ^ cast_sbox6[U8a(t[0])] ^ | ||
732 | cast_sbox7[U8c(t[3])] ^ cast_sbox8[U8d(t[3])]; | ||
733 | key->xkey[i+2] = cast_sbox5[U8d(t[1])] ^ cast_sbox6[U8c(t[1])] ^ | ||
734 | cast_sbox7[U8a(t[2])] ^ cast_sbox8[U8b(t[2])]; | ||
735 | key->xkey[i+3] = cast_sbox5[U8b(t[1])] ^ cast_sbox6[U8a(t[1])] ^ | ||
736 | cast_sbox7[U8c(t[2])] ^ cast_sbox8[U8d(t[2])]; | ||
737 | break; | ||
738 | } | ||
739 | switch (i & 12) { | ||
740 | case 0: | ||
741 | key->xkey[i+0] ^= cast_sbox5[U8c(z[0])]; | ||
742 | key->xkey[i+1] ^= cast_sbox6[U8c(z[1])]; | ||
743 | key->xkey[i+2] ^= cast_sbox7[U8b(z[2])]; | ||
744 | key->xkey[i+3] ^= cast_sbox8[U8a(z[3])]; | ||
745 | break; | ||
746 | case 4: | ||
747 | key->xkey[i+0] ^= cast_sbox5[U8a(x[2])]; | ||
748 | key->xkey[i+1] ^= cast_sbox6[U8b(x[3])]; | ||
749 | key->xkey[i+2] ^= cast_sbox7[U8d(x[0])]; | ||
750 | key->xkey[i+3] ^= cast_sbox8[U8d(x[1])]; | ||
751 | break; | ||
752 | case 8: | ||
753 | key->xkey[i+0] ^= cast_sbox5[U8b(z[2])]; | ||
754 | key->xkey[i+1] ^= cast_sbox6[U8a(z[3])]; | ||
755 | key->xkey[i+2] ^= cast_sbox7[U8c(z[0])]; | ||
756 | key->xkey[i+3] ^= cast_sbox8[U8c(z[1])]; | ||
757 | break; | ||
758 | case 12: | ||
759 | key->xkey[i+0] ^= cast_sbox5[U8d(x[0])]; | ||
760 | key->xkey[i+1] ^= cast_sbox6[U8d(x[1])]; | ||
761 | key->xkey[i+2] ^= cast_sbox7[U8a(x[2])]; | ||
762 | key->xkey[i+3] ^= cast_sbox8[U8b(x[3])]; | ||
763 | break; | ||
764 | } | ||
765 | if (i >= 16) { | ||
766 | key->xkey[i+0] &= 31; | ||
767 | key->xkey[i+1] &= 31; | ||
768 | key->xkey[i+2] &= 31; | ||
769 | key->xkey[i+3] &= 31; | ||
770 | } | ||
771 | } | ||
772 | /* Wipe clean */ | ||
773 | for (i = 0; i < 4; i++) { | ||
774 | t[i] = x[i] = z[i] = 0; | ||
775 | } | ||
776 | } | ||
777 | |||
778 | /* Made in Canada */ | ||
779 | |||
diff --git a/src/lib/libc/crypt/crypt.3 b/src/lib/libc/crypt/crypt.3 new file mode 100644 index 0000000000..32109dfac3 --- /dev/null +++ b/src/lib/libc/crypt/crypt.3 | |||
@@ -0,0 +1,320 @@ | |||
1 | .\" $OpenBSD: crypt.3,v 1.21 2003/08/07 00:32:12 deraadt Exp $ | ||
2 | .\" | ||
3 | .\" FreeSec: libcrypt | ||
4 | .\" | ||
5 | .\" Copyright (c) 1994 David Burren | ||
6 | .\" 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 | .\" 4. Neither the name of the author nor the names of other contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" Manual page, using -mandoc macros | ||
33 | .\" | ||
34 | .Dd March 9, 1994 | ||
35 | .Dt CRYPT 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm crypt , | ||
39 | .Nm setkey , | ||
40 | .Nm encrypt , | ||
41 | .Nm des_setkey , | ||
42 | .Nm des_cipher | ||
43 | .Nd DES encryption | ||
44 | .Sh SYNOPSIS | ||
45 | .Fd #include <unistd.h> | ||
46 | .Ft char * | ||
47 | .Fn crypt "const char *key" "const char *setting" | ||
48 | .Ft int | ||
49 | .Fn setkey "char *key" | ||
50 | .Ft int | ||
51 | .Fn encrypt "char *block" "int flag" | ||
52 | .Ft int | ||
53 | .Fn des_setkey "const char *key" | ||
54 | .Ft int | ||
55 | .Fn des_cipher "const char *in" "char *out" "int32_t salt" "int count" | ||
56 | .Ft char * | ||
57 | .Fn bcrypt_gensalt "u_int8_t log_rounds" | ||
58 | .Ft char * | ||
59 | .Fn bcrypt "const char *key" "const char *salt" | ||
60 | .Ft char * | ||
61 | .Fn md5crypt "const char *key" "const char *salt" | ||
62 | .Sh DESCRIPTION | ||
63 | The | ||
64 | .Fn crypt | ||
65 | function performs password encryption based on the | ||
66 | .Tn NBS | ||
67 | Data Encryption Standard (DES). | ||
68 | Additional code has been added to deter key search attempts and to use | ||
69 | stronger hashing algorithms. | ||
70 | .Pp | ||
71 | The first argument to | ||
72 | .Fn crypt | ||
73 | is a | ||
74 | .Dv null Ns -terminated | ||
75 | string, typically a user's typed password. | ||
76 | The second is in one of three forms: | ||
77 | if it begins with an underscore | ||
78 | .Pq Ql _ | ||
79 | then an extended format is used | ||
80 | in interpreting both the key and the setting, as outlined below. | ||
81 | If it begins | ||
82 | with a string character | ||
83 | .Pq Ql $ | ||
84 | and a number then a different algorithm is used depending on the number. | ||
85 | At the moment a | ||
86 | .Ql $1 | ||
87 | chooses MD5 hashing and a | ||
88 | .Ql $2 | ||
89 | chooses Blowfish hashing; see below for more information. | ||
90 | .Ss Extended crypt | ||
91 | The | ||
92 | .Ar key | ||
93 | is divided into groups of 8 characters (the last group is null-padded) | ||
94 | and the low-order 7 bits of each character (56 bits per group) are | ||
95 | used to form the DES key as follows: | ||
96 | the first group of 56 bits becomes the initial DES key. | ||
97 | For each additional group, the XOR of the encryption of the current DES | ||
98 | key with itself and the group bits becomes the next DES key. | ||
99 | .Pp | ||
100 | The setting is a 9-character array consisting of an underscore followed | ||
101 | by 4 bytes of iteration count and 4 bytes of salt. | ||
102 | These are encoded as printable characters, 6 bits per character, | ||
103 | least significant character first. | ||
104 | The values 0 to 63 are encoded as | ||
105 | .Dq \&./0-9A-Za-z . | ||
106 | This allows 24 bits for both | ||
107 | .Fa count | ||
108 | and | ||
109 | .Fa salt . | ||
110 | .Ss "MD5" crypt | ||
111 | For | ||
112 | .Tn MD5 | ||
113 | crypt the version number, | ||
114 | .Fa salt | ||
115 | and the hashed password are separated by the | ||
116 | .Ql $ | ||
117 | character. | ||
118 | The maximum length of a password is limited by | ||
119 | the length counter of the MD5 context, which is about | ||
120 | 2**64. | ||
121 | A valid MD5 password entry looks like this: | ||
122 | .Pp | ||
123 | .Dq $1$caeiHQwX$hsKqOjrFRRN6K32OWkCBf1 . | ||
124 | .Pp | ||
125 | The whole MD5 password string is passed as | ||
126 | .Fa setting | ||
127 | for interpretation. | ||
128 | .Ss "Blowfish" crypt | ||
129 | The | ||
130 | .Tn Blowfish | ||
131 | version of crypt has 128 bits of | ||
132 | .Fa salt | ||
133 | in order to make building dictionaries of common passwords space consuming. | ||
134 | The initial state of the | ||
135 | .Tn Blowfish | ||
136 | cipher is expanded using the | ||
137 | .Fa salt | ||
138 | and the | ||
139 | .Fa password | ||
140 | repeating the process a variable number of rounds, which is encoded in | ||
141 | the password string. | ||
142 | The maximum password length is 72. | ||
143 | The final Blowfish password entry is created by encrypting the string | ||
144 | .Pp | ||
145 | .Dq OrpheanBeholderScryDoubt | ||
146 | .Pp | ||
147 | with the | ||
148 | .Tn Blowfish | ||
149 | state 64 times. | ||
150 | .Pp | ||
151 | The version number, the logarithm of the number of rounds and | ||
152 | the concatenation of salt and hashed password are separated by the | ||
153 | .Ql $ | ||
154 | character. | ||
155 | An encoded | ||
156 | .Sq 8 | ||
157 | would specify 256 rounds. | ||
158 | A valid Blowfish password looks like this: | ||
159 | .Pp | ||
160 | .Dq $2a$12$eIAq8PR8sIUnJ1HaohxX2O9x9Qlm2vK97LJ5dsXdmB.eXF42qjchC . | ||
161 | .Pp | ||
162 | The whole Blowfish password string is passed as | ||
163 | .Fa setting | ||
164 | for interpretation. | ||
165 | .Ss "Traditional" crypt | ||
166 | The first 8 bytes of the key are null-padded, and the low-order 7 bits of | ||
167 | each character is used to form the 56-bit | ||
168 | .Tn DES | ||
169 | key. | ||
170 | .Pp | ||
171 | The setting is a 2-character array of the ASCII-encoded salt. | ||
172 | Thus only 12 bits of | ||
173 | .Fa salt | ||
174 | are used. | ||
175 | .Fa count | ||
176 | is set to 25. | ||
177 | .Ss DES Algorithm | ||
178 | The | ||
179 | .Fa salt | ||
180 | introduces disorder in the | ||
181 | .Tn DES | ||
182 | algorithm in one of 16777216 or 4096 possible ways | ||
183 | (i.e., with 24 or 12 bits: if bit | ||
184 | .Em i | ||
185 | of the | ||
186 | .Ar salt | ||
187 | is set, then bits | ||
188 | .Em i | ||
189 | and | ||
190 | .Em i+24 | ||
191 | are swapped in the | ||
192 | .Tn DES | ||
193 | E-box output). | ||
194 | .Pp | ||
195 | The DES key is used to encrypt a 64-bit constant using | ||
196 | .Ar count | ||
197 | iterations of | ||
198 | .Tn DES . | ||
199 | The value returned is a | ||
200 | .Dv null Ns -terminated | ||
201 | string, 20 or 13 bytes (plus null) in length, consisting of the | ||
202 | .Ar setting | ||
203 | followed by the encoded 64-bit encryption. | ||
204 | .Pp | ||
205 | The functions | ||
206 | .Fn encrypt , | ||
207 | .Fn setkey , | ||
208 | .Fn des_setkey , | ||
209 | and | ||
210 | .Fn des_cipher | ||
211 | provide access to the | ||
212 | .Tn DES | ||
213 | algorithm itself. | ||
214 | .Fn setkey | ||
215 | is passed a 64-byte array of binary values (numeric 0 or 1). | ||
216 | A 56-bit key is extracted from this array by dividing the | ||
217 | array into groups of 8, and ignoring the last bit in each group. | ||
218 | That bit is reserved for a byte parity check by DES, but is ignored | ||
219 | by these functions. | ||
220 | .Pp | ||
221 | The | ||
222 | .Fa block | ||
223 | argument to | ||
224 | .Fn encrypt | ||
225 | is also a 64-byte array of binary values. | ||
226 | If the value of | ||
227 | .Fa flag | ||
228 | is 0, | ||
229 | .Fa block | ||
230 | is encrypted otherwise it is decrypted. | ||
231 | The result is returned in the original array | ||
232 | .Fa block | ||
233 | after using the key specified by | ||
234 | .Fn setkey | ||
235 | to process it. | ||
236 | .Pp | ||
237 | The argument to | ||
238 | .Fn des_setkey | ||
239 | is a character array of length 8. | ||
240 | The least significant bit (the parity bit) in each character is ignored, | ||
241 | and the remaining bits are concatenated to form a 56-bit key. | ||
242 | The function | ||
243 | .Fn des_cipher | ||
244 | encrypts (or decrypts if | ||
245 | .Fa count | ||
246 | is negative) the 64-bits stored in the 8 characters at | ||
247 | .Fa in | ||
248 | using | ||
249 | .Xr abs 3 | ||
250 | of | ||
251 | .Fa count | ||
252 | iterations of | ||
253 | .Tn DES | ||
254 | and stores the 64-bit result in the 8 characters at | ||
255 | .Fa out | ||
256 | (which may be the same as | ||
257 | .Fa in ) . | ||
258 | The | ||
259 | .Fa salt | ||
260 | specifies perturbations to the | ||
261 | .Tn DES | ||
262 | E-box output as described above. | ||
263 | .Pp | ||
264 | The function | ||
265 | .Fn crypt | ||
266 | returns a pointer to the encrypted value on success, and | ||
267 | .Dv NULL | ||
268 | on failure. | ||
269 | The functions | ||
270 | .Fn setkey , | ||
271 | .Fn encrypt , | ||
272 | .Fn des_setkey , | ||
273 | and | ||
274 | .Fn des_cipher | ||
275 | return 0 on success and 1 on failure. | ||
276 | .Pp | ||
277 | The | ||
278 | .Fn crypt , | ||
279 | .Fn setkey , | ||
280 | and | ||
281 | .Fn des_setkey | ||
282 | functions all manipulate the same key space. | ||
283 | .Sh SEE ALSO | ||
284 | .Xr login 1 , | ||
285 | .Xr passwd 1 , | ||
286 | .Xr blowfish 3 , | ||
287 | .Xr getpass 3 , | ||
288 | .Xr md5 3 , | ||
289 | .Xr passwd 5 | ||
290 | .Sh HISTORY | ||
291 | A rotor-based | ||
292 | .Fn crypt | ||
293 | function appeared in | ||
294 | .At v3 . | ||
295 | The current style | ||
296 | .Fn crypt | ||
297 | first appeared in | ||
298 | .At v7 . | ||
299 | .Pp | ||
300 | This library (FreeSec 1.0) was developed outside the United States of America | ||
301 | as an unencumbered replacement for the U.S.-only libcrypt encryption | ||
302 | library. | ||
303 | Programs linked against the | ||
304 | .Fn crypt | ||
305 | interface may be exported from the U.S.A. only if they use | ||
306 | .Fn crypt | ||
307 | solely for authentication purposes and avoid use of | ||
308 | the other programmer interfaces listed above. | ||
309 | Special care has been taken | ||
310 | in the library so that programs which only use the | ||
311 | .Fn crypt | ||
312 | interface do not pull in the other components. | ||
313 | .Sh AUTHORS | ||
314 | .An David Burren Aq davidb@werj.com.au | ||
315 | .Sh BUGS | ||
316 | The | ||
317 | .Fn crypt | ||
318 | function returns a pointer to static data, and subsequent calls to | ||
319 | .Fn crypt | ||
320 | will modify the same object. | ||
diff --git a/src/lib/libc/crypt/crypt.c b/src/lib/libc/crypt/crypt.c new file mode 100644 index 0000000000..e0f5e8364a --- /dev/null +++ b/src/lib/libc/crypt/crypt.c | |||
@@ -0,0 +1,700 @@ | |||
1 | /* $OpenBSD: crypt.c,v 1.18 2003/08/12 01:22:17 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * FreeSec: libcrypt | ||
5 | * | ||
6 | * Copyright (c) 1994 David Burren | ||
7 | * All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 4. Neither the name of the author nor the names of other contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * | ||
33 | * | ||
34 | * This is an original implementation of the DES and the crypt(3) interfaces | ||
35 | * by David Burren <davidb@werj.com.au>. | ||
36 | * | ||
37 | * An excellent reference on the underlying algorithm (and related | ||
38 | * algorithms) is: | ||
39 | * | ||
40 | * B. Schneier, Applied Cryptography: protocols, algorithms, | ||
41 | * and source code in C, John Wiley & Sons, 1994. | ||
42 | * | ||
43 | * Note that in that book's description of DES the lookups for the initial, | ||
44 | * pbox, and final permutations are inverted (this has been brought to the | ||
45 | * attention of the author). A list of errata for this book has been | ||
46 | * posted to the sci.crypt newsgroup by the author and is available for FTP. | ||
47 | */ | ||
48 | |||
49 | #if defined(LIBC_SCCS) && !defined(lint) | ||
50 | static char rcsid[] = "$OpenBSD: crypt.c,v 1.18 2003/08/12 01:22:17 deraadt Exp $"; | ||
51 | #endif /* LIBC_SCCS and not lint */ | ||
52 | |||
53 | #include <sys/types.h> | ||
54 | #include <sys/param.h> | ||
55 | #include <pwd.h> | ||
56 | #include <unistd.h> | ||
57 | #include <string.h> | ||
58 | |||
59 | #ifdef DEBUG | ||
60 | # include <stdio.h> | ||
61 | #endif | ||
62 | |||
63 | static const u_char IP[64] = { | ||
64 | 58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, | ||
65 | 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, | ||
66 | 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, | ||
67 | 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 | ||
68 | }; | ||
69 | |||
70 | static u_char inv_key_perm[64]; | ||
71 | static u_char u_key_perm[56]; | ||
72 | static u_char const key_perm[56] = { | ||
73 | 57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, | ||
74 | 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, | ||
75 | 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, | ||
76 | 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 | ||
77 | }; | ||
78 | |||
79 | static const u_char key_shifts[16] = { | ||
80 | 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 | ||
81 | }; | ||
82 | |||
83 | static u_char inv_comp_perm[56]; | ||
84 | static const u_char comp_perm[48] = { | ||
85 | 14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, | ||
86 | 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, | ||
87 | 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, | ||
88 | 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 | ||
89 | }; | ||
90 | |||
91 | /* | ||
92 | * No E box is used, as it's replaced by some ANDs, shifts, and ORs. | ||
93 | */ | ||
94 | |||
95 | static u_char u_sbox[8][64]; | ||
96 | static const u_char sbox[8][64] = { | ||
97 | { | ||
98 | 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, | ||
99 | 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, | ||
100 | 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, | ||
101 | 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 | ||
102 | }, | ||
103 | { | ||
104 | 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, | ||
105 | 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, | ||
106 | 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, | ||
107 | 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 | ||
108 | }, | ||
109 | { | ||
110 | 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, | ||
111 | 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, | ||
112 | 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, | ||
113 | 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 | ||
114 | }, | ||
115 | { | ||
116 | 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, | ||
117 | 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, | ||
118 | 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, | ||
119 | 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 | ||
120 | }, | ||
121 | { | ||
122 | 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, | ||
123 | 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, | ||
124 | 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, | ||
125 | 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 | ||
126 | }, | ||
127 | { | ||
128 | 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, | ||
129 | 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, | ||
130 | 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, | ||
131 | 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 | ||
132 | }, | ||
133 | { | ||
134 | 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, | ||
135 | 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, | ||
136 | 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, | ||
137 | 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 | ||
138 | }, | ||
139 | { | ||
140 | 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, | ||
141 | 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, | ||
142 | 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, | ||
143 | 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 | ||
144 | } | ||
145 | }; | ||
146 | |||
147 | static u_char un_pbox[32]; | ||
148 | static const u_char pbox[32] = { | ||
149 | 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, | ||
150 | 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 | ||
151 | }; | ||
152 | |||
153 | const u_int32_t _des_bits32[32] = | ||
154 | { | ||
155 | 0x80000000, 0x40000000, 0x20000000, 0x10000000, | ||
156 | 0x08000000, 0x04000000, 0x02000000, 0x01000000, | ||
157 | 0x00800000, 0x00400000, 0x00200000, 0x00100000, | ||
158 | 0x00080000, 0x00040000, 0x00020000, 0x00010000, | ||
159 | 0x00008000, 0x00004000, 0x00002000, 0x00001000, | ||
160 | 0x00000800, 0x00000400, 0x00000200, 0x00000100, | ||
161 | 0x00000080, 0x00000040, 0x00000020, 0x00000010, | ||
162 | 0x00000008, 0x00000004, 0x00000002, 0x00000001 | ||
163 | }; | ||
164 | |||
165 | const u_char _des_bits8[8] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; | ||
166 | |||
167 | static u_int32_t saltbits; | ||
168 | static int32_t old_salt; | ||
169 | static const u_int32_t *bits28, *bits24; | ||
170 | static u_char init_perm[64], final_perm[64]; | ||
171 | static u_int32_t en_keysl[16], en_keysr[16]; | ||
172 | static u_int32_t de_keysl[16], de_keysr[16]; | ||
173 | int _des_initialised = 0; | ||
174 | static u_char m_sbox[4][4096]; | ||
175 | static u_int32_t psbox[4][256]; | ||
176 | static u_int32_t ip_maskl[8][256], ip_maskr[8][256]; | ||
177 | static u_int32_t fp_maskl[8][256], fp_maskr[8][256]; | ||
178 | static u_int32_t key_perm_maskl[8][128], key_perm_maskr[8][128]; | ||
179 | static u_int32_t comp_maskl[8][128], comp_maskr[8][128]; | ||
180 | static u_int32_t old_rawkey0, old_rawkey1; | ||
181 | |||
182 | static u_char ascii64[] = | ||
183 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | ||
184 | /* 0000000000111111111122222222223333333333444444444455555555556666 */ | ||
185 | /* 0123456789012345678901234567890123456789012345678901234567890123 */ | ||
186 | |||
187 | static __inline int | ||
188 | ascii_to_bin(char ch) | ||
189 | { | ||
190 | if (ch > 'z') | ||
191 | return(0); | ||
192 | if (ch >= 'a') | ||
193 | return(ch - 'a' + 38); | ||
194 | if (ch > 'Z') | ||
195 | return(0); | ||
196 | if (ch >= 'A') | ||
197 | return(ch - 'A' + 12); | ||
198 | if (ch > '9') | ||
199 | return(0); | ||
200 | if (ch >= '.') | ||
201 | return(ch - '.'); | ||
202 | return(0); | ||
203 | } | ||
204 | |||
205 | void | ||
206 | _des_init(void) | ||
207 | { | ||
208 | int i, j, b, k, inbit, obit; | ||
209 | u_int32_t *p, *il, *ir, *fl, *fr; | ||
210 | |||
211 | old_rawkey0 = old_rawkey1 = 0; | ||
212 | saltbits = 0; | ||
213 | old_salt = 0; | ||
214 | bits24 = (bits28 = _des_bits32 + 4) + 4; | ||
215 | |||
216 | /* | ||
217 | * Invert the S-boxes, reordering the input bits. | ||
218 | */ | ||
219 | for (i = 0; i < 8; i++) | ||
220 | for (j = 0; j < 64; j++) { | ||
221 | b = (j & 0x20) | ((j & 1) << 4) | ((j >> 1) & 0xf); | ||
222 | u_sbox[i][j] = sbox[i][b]; | ||
223 | } | ||
224 | |||
225 | /* | ||
226 | * Convert the inverted S-boxes into 4 arrays of 8 bits. | ||
227 | * Each will handle 12 bits of the S-box input. | ||
228 | */ | ||
229 | for (b = 0; b < 4; b++) | ||
230 | for (i = 0; i < 64; i++) | ||
231 | for (j = 0; j < 64; j++) | ||
232 | m_sbox[b][(i << 6) | j] = | ||
233 | (u_sbox[(b << 1)][i] << 4) | | ||
234 | u_sbox[(b << 1) + 1][j]; | ||
235 | |||
236 | /* | ||
237 | * Set up the initial & final permutations into a useful form, and | ||
238 | * initialise the inverted key permutation. | ||
239 | */ | ||
240 | for (i = 0; i < 64; i++) { | ||
241 | init_perm[final_perm[i] = IP[i] - 1] = i; | ||
242 | inv_key_perm[i] = 255; | ||
243 | } | ||
244 | |||
245 | /* | ||
246 | * Invert the key permutation and initialise the inverted key | ||
247 | * compression permutation. | ||
248 | */ | ||
249 | for (i = 0; i < 56; i++) { | ||
250 | u_key_perm[i] = key_perm[i] - 1; | ||
251 | inv_key_perm[key_perm[i] - 1] = i; | ||
252 | inv_comp_perm[i] = 255; | ||
253 | } | ||
254 | |||
255 | /* | ||
256 | * Invert the key compression permutation. | ||
257 | */ | ||
258 | for (i = 0; i < 48; i++) { | ||
259 | inv_comp_perm[comp_perm[i] - 1] = i; | ||
260 | } | ||
261 | |||
262 | /* | ||
263 | * Set up the OR-mask arrays for the initial and final permutations, | ||
264 | * and for the key initial and compression permutations. | ||
265 | */ | ||
266 | for (k = 0; k < 8; k++) { | ||
267 | for (i = 0; i < 256; i++) { | ||
268 | *(il = &ip_maskl[k][i]) = 0; | ||
269 | *(ir = &ip_maskr[k][i]) = 0; | ||
270 | *(fl = &fp_maskl[k][i]) = 0; | ||
271 | *(fr = &fp_maskr[k][i]) = 0; | ||
272 | for (j = 0; j < 8; j++) { | ||
273 | inbit = 8 * k + j; | ||
274 | if (i & _des_bits8[j]) { | ||
275 | if ((obit = init_perm[inbit]) < 32) | ||
276 | *il |= _des_bits32[obit]; | ||
277 | else | ||
278 | *ir |= _des_bits32[obit-32]; | ||
279 | if ((obit = final_perm[inbit]) < 32) | ||
280 | *fl |= _des_bits32[obit]; | ||
281 | else | ||
282 | *fr |= _des_bits32[obit - 32]; | ||
283 | } | ||
284 | } | ||
285 | } | ||
286 | for (i = 0; i < 128; i++) { | ||
287 | *(il = &key_perm_maskl[k][i]) = 0; | ||
288 | *(ir = &key_perm_maskr[k][i]) = 0; | ||
289 | for (j = 0; j < 7; j++) { | ||
290 | inbit = 8 * k + j; | ||
291 | if (i & _des_bits8[j + 1]) { | ||
292 | if ((obit = inv_key_perm[inbit]) == 255) | ||
293 | continue; | ||
294 | if (obit < 28) | ||
295 | *il |= bits28[obit]; | ||
296 | else | ||
297 | *ir |= bits28[obit - 28]; | ||
298 | } | ||
299 | } | ||
300 | *(il = &comp_maskl[k][i]) = 0; | ||
301 | *(ir = &comp_maskr[k][i]) = 0; | ||
302 | for (j = 0; j < 7; j++) { | ||
303 | inbit = 7 * k + j; | ||
304 | if (i & _des_bits8[j + 1]) { | ||
305 | if ((obit=inv_comp_perm[inbit]) == 255) | ||
306 | continue; | ||
307 | if (obit < 24) | ||
308 | *il |= bits24[obit]; | ||
309 | else | ||
310 | *ir |= bits24[obit - 24]; | ||
311 | } | ||
312 | } | ||
313 | } | ||
314 | } | ||
315 | |||
316 | /* | ||
317 | * Invert the P-box permutation, and convert into OR-masks for | ||
318 | * handling the output of the S-box arrays setup above. | ||
319 | */ | ||
320 | for (i = 0; i < 32; i++) | ||
321 | un_pbox[pbox[i] - 1] = i; | ||
322 | |||
323 | for (b = 0; b < 4; b++) | ||
324 | for (i = 0; i < 256; i++) { | ||
325 | *(p = &psbox[b][i]) = 0; | ||
326 | for (j = 0; j < 8; j++) { | ||
327 | if (i & _des_bits8[j]) | ||
328 | *p |= _des_bits32[un_pbox[8 * b + j]]; | ||
329 | } | ||
330 | } | ||
331 | |||
332 | _des_initialised = 1; | ||
333 | } | ||
334 | |||
335 | void | ||
336 | _des_setup_salt(int32_t salt) | ||
337 | { | ||
338 | u_int32_t obit, saltbit; | ||
339 | int i; | ||
340 | |||
341 | if (salt == old_salt) | ||
342 | return; | ||
343 | old_salt = salt; | ||
344 | |||
345 | saltbits = 0; | ||
346 | saltbit = 1; | ||
347 | obit = 0x800000; | ||
348 | for (i = 0; i < 24; i++) { | ||
349 | if (salt & saltbit) | ||
350 | saltbits |= obit; | ||
351 | saltbit <<= 1; | ||
352 | obit >>= 1; | ||
353 | } | ||
354 | } | ||
355 | |||
356 | int | ||
357 | des_setkey(const char *key) | ||
358 | { | ||
359 | u_int32_t k0, k1, rawkey0, rawkey1; | ||
360 | int shifts, round; | ||
361 | |||
362 | if (!_des_initialised) | ||
363 | _des_init(); | ||
364 | |||
365 | rawkey0 = ntohl(*(u_int32_t *) key); | ||
366 | rawkey1 = ntohl(*(u_int32_t *) (key + 4)); | ||
367 | |||
368 | if ((rawkey0 | rawkey1) | ||
369 | && rawkey0 == old_rawkey0 | ||
370 | && rawkey1 == old_rawkey1) { | ||
371 | /* | ||
372 | * Already setup for this key. | ||
373 | * This optimisation fails on a zero key (which is weak and | ||
374 | * has bad parity anyway) in order to simplify the starting | ||
375 | * conditions. | ||
376 | */ | ||
377 | return(0); | ||
378 | } | ||
379 | old_rawkey0 = rawkey0; | ||
380 | old_rawkey1 = rawkey1; | ||
381 | |||
382 | /* | ||
383 | * Do key permutation and split into two 28-bit subkeys. | ||
384 | */ | ||
385 | k0 = key_perm_maskl[0][rawkey0 >> 25] | ||
386 | | key_perm_maskl[1][(rawkey0 >> 17) & 0x7f] | ||
387 | | key_perm_maskl[2][(rawkey0 >> 9) & 0x7f] | ||
388 | | key_perm_maskl[3][(rawkey0 >> 1) & 0x7f] | ||
389 | | key_perm_maskl[4][rawkey1 >> 25] | ||
390 | | key_perm_maskl[5][(rawkey1 >> 17) & 0x7f] | ||
391 | | key_perm_maskl[6][(rawkey1 >> 9) & 0x7f] | ||
392 | | key_perm_maskl[7][(rawkey1 >> 1) & 0x7f]; | ||
393 | k1 = key_perm_maskr[0][rawkey0 >> 25] | ||
394 | | key_perm_maskr[1][(rawkey0 >> 17) & 0x7f] | ||
395 | | key_perm_maskr[2][(rawkey0 >> 9) & 0x7f] | ||
396 | | key_perm_maskr[3][(rawkey0 >> 1) & 0x7f] | ||
397 | | key_perm_maskr[4][rawkey1 >> 25] | ||
398 | | key_perm_maskr[5][(rawkey1 >> 17) & 0x7f] | ||
399 | | key_perm_maskr[6][(rawkey1 >> 9) & 0x7f] | ||
400 | | key_perm_maskr[7][(rawkey1 >> 1) & 0x7f]; | ||
401 | /* | ||
402 | * Rotate subkeys and do compression permutation. | ||
403 | */ | ||
404 | shifts = 0; | ||
405 | for (round = 0; round < 16; round++) { | ||
406 | u_int32_t t0, t1; | ||
407 | |||
408 | shifts += key_shifts[round]; | ||
409 | |||
410 | t0 = (k0 << shifts) | (k0 >> (28 - shifts)); | ||
411 | t1 = (k1 << shifts) | (k1 >> (28 - shifts)); | ||
412 | |||
413 | de_keysl[15 - round] = | ||
414 | en_keysl[round] = comp_maskl[0][(t0 >> 21) & 0x7f] | ||
415 | | comp_maskl[1][(t0 >> 14) & 0x7f] | ||
416 | | comp_maskl[2][(t0 >> 7) & 0x7f] | ||
417 | | comp_maskl[3][t0 & 0x7f] | ||
418 | | comp_maskl[4][(t1 >> 21) & 0x7f] | ||
419 | | comp_maskl[5][(t1 >> 14) & 0x7f] | ||
420 | | comp_maskl[6][(t1 >> 7) & 0x7f] | ||
421 | | comp_maskl[7][t1 & 0x7f]; | ||
422 | |||
423 | de_keysr[15 - round] = | ||
424 | en_keysr[round] = comp_maskr[0][(t0 >> 21) & 0x7f] | ||
425 | | comp_maskr[1][(t0 >> 14) & 0x7f] | ||
426 | | comp_maskr[2][(t0 >> 7) & 0x7f] | ||
427 | | comp_maskr[3][t0 & 0x7f] | ||
428 | | comp_maskr[4][(t1 >> 21) & 0x7f] | ||
429 | | comp_maskr[5][(t1 >> 14) & 0x7f] | ||
430 | | comp_maskr[6][(t1 >> 7) & 0x7f] | ||
431 | | comp_maskr[7][t1 & 0x7f]; | ||
432 | } | ||
433 | return(0); | ||
434 | } | ||
435 | |||
436 | int | ||
437 | _des_do_des(u_int32_t l_in, u_int32_t r_in, u_int32_t *l_out, u_int32_t *r_out, | ||
438 | int count) | ||
439 | { | ||
440 | /* | ||
441 | * l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format. | ||
442 | */ | ||
443 | u_int32_t l, r, *kl, *kr, *kl1, *kr1; | ||
444 | u_int32_t f, r48l, r48r; | ||
445 | int round; | ||
446 | |||
447 | if (count == 0) { | ||
448 | return(1); | ||
449 | } else if (count > 0) { | ||
450 | /* | ||
451 | * Encrypting | ||
452 | */ | ||
453 | kl1 = en_keysl; | ||
454 | kr1 = en_keysr; | ||
455 | } else { | ||
456 | /* | ||
457 | * Decrypting | ||
458 | */ | ||
459 | count = -count; | ||
460 | kl1 = de_keysl; | ||
461 | kr1 = de_keysr; | ||
462 | } | ||
463 | |||
464 | /* | ||
465 | * Do initial permutation (IP). | ||
466 | */ | ||
467 | l = ip_maskl[0][l_in >> 24] | ||
468 | | ip_maskl[1][(l_in >> 16) & 0xff] | ||
469 | | ip_maskl[2][(l_in >> 8) & 0xff] | ||
470 | | ip_maskl[3][l_in & 0xff] | ||
471 | | ip_maskl[4][r_in >> 24] | ||
472 | | ip_maskl[5][(r_in >> 16) & 0xff] | ||
473 | | ip_maskl[6][(r_in >> 8) & 0xff] | ||
474 | | ip_maskl[7][r_in & 0xff]; | ||
475 | r = ip_maskr[0][l_in >> 24] | ||
476 | | ip_maskr[1][(l_in >> 16) & 0xff] | ||
477 | | ip_maskr[2][(l_in >> 8) & 0xff] | ||
478 | | ip_maskr[3][l_in & 0xff] | ||
479 | | ip_maskr[4][r_in >> 24] | ||
480 | | ip_maskr[5][(r_in >> 16) & 0xff] | ||
481 | | ip_maskr[6][(r_in >> 8) & 0xff] | ||
482 | | ip_maskr[7][r_in & 0xff]; | ||
483 | |||
484 | while (count--) { | ||
485 | /* | ||
486 | * Do each round. | ||
487 | */ | ||
488 | kl = kl1; | ||
489 | kr = kr1; | ||
490 | round = 16; | ||
491 | while (round--) { | ||
492 | /* | ||
493 | * Expand R to 48 bits (simulate the E-box). | ||
494 | */ | ||
495 | r48l = ((r & 0x00000001) << 23) | ||
496 | | ((r & 0xf8000000) >> 9) | ||
497 | | ((r & 0x1f800000) >> 11) | ||
498 | | ((r & 0x01f80000) >> 13) | ||
499 | | ((r & 0x001f8000) >> 15); | ||
500 | |||
501 | r48r = ((r & 0x0001f800) << 7) | ||
502 | | ((r & 0x00001f80) << 5) | ||
503 | | ((r & 0x000001f8) << 3) | ||
504 | | ((r & 0x0000001f) << 1) | ||
505 | | ((r & 0x80000000) >> 31); | ||
506 | /* | ||
507 | * Do salting for crypt() and friends, and | ||
508 | * XOR with the permuted key. | ||
509 | */ | ||
510 | f = (r48l ^ r48r) & saltbits; | ||
511 | r48l ^= f ^ *kl++; | ||
512 | r48r ^= f ^ *kr++; | ||
513 | /* | ||
514 | * Do sbox lookups (which shrink it back to 32 bits) | ||
515 | * and do the pbox permutation at the same time. | ||
516 | */ | ||
517 | f = psbox[0][m_sbox[0][r48l >> 12]] | ||
518 | | psbox[1][m_sbox[1][r48l & 0xfff]] | ||
519 | | psbox[2][m_sbox[2][r48r >> 12]] | ||
520 | | psbox[3][m_sbox[3][r48r & 0xfff]]; | ||
521 | /* | ||
522 | * Now that we've permuted things, complete f(). | ||
523 | */ | ||
524 | f ^= l; | ||
525 | l = r; | ||
526 | r = f; | ||
527 | } | ||
528 | r = l; | ||
529 | l = f; | ||
530 | } | ||
531 | /* | ||
532 | * Do final permutation (inverse of IP). | ||
533 | */ | ||
534 | *l_out = fp_maskl[0][l >> 24] | ||
535 | | fp_maskl[1][(l >> 16) & 0xff] | ||
536 | | fp_maskl[2][(l >> 8) & 0xff] | ||
537 | | fp_maskl[3][l & 0xff] | ||
538 | | fp_maskl[4][r >> 24] | ||
539 | | fp_maskl[5][(r >> 16) & 0xff] | ||
540 | | fp_maskl[6][(r >> 8) & 0xff] | ||
541 | | fp_maskl[7][r & 0xff]; | ||
542 | *r_out = fp_maskr[0][l >> 24] | ||
543 | | fp_maskr[1][(l >> 16) & 0xff] | ||
544 | | fp_maskr[2][(l >> 8) & 0xff] | ||
545 | | fp_maskr[3][l & 0xff] | ||
546 | | fp_maskr[4][r >> 24] | ||
547 | | fp_maskr[5][(r >> 16) & 0xff] | ||
548 | | fp_maskr[6][(r >> 8) & 0xff] | ||
549 | | fp_maskr[7][r & 0xff]; | ||
550 | return(0); | ||
551 | } | ||
552 | |||
553 | int | ||
554 | des_cipher(const char *in, char *out, int32_t salt, int count) | ||
555 | { | ||
556 | u_int32_t l_out, r_out, rawl, rawr; | ||
557 | u_int32_t x[2]; | ||
558 | int retval; | ||
559 | |||
560 | if (!_des_initialised) | ||
561 | _des_init(); | ||
562 | |||
563 | _des_setup_salt(salt); | ||
564 | |||
565 | memcpy(x, in, sizeof x); | ||
566 | rawl = ntohl(x[0]); | ||
567 | rawr = ntohl(x[1]); | ||
568 | retval = _des_do_des(rawl, rawr, &l_out, &r_out, count); | ||
569 | |||
570 | x[0] = htonl(l_out); | ||
571 | x[1] = htonl(r_out); | ||
572 | memcpy(out, x, sizeof x); | ||
573 | return(retval); | ||
574 | } | ||
575 | |||
576 | char * | ||
577 | crypt(const char *key, const char *setting) | ||
578 | { | ||
579 | int i; | ||
580 | u_int32_t count, salt, l, r0, r1, keybuf[2]; | ||
581 | u_char *p, *q; | ||
582 | static u_char output[21]; | ||
583 | extern char *md5crypt(const char *, const char *); | ||
584 | extern char *bcrypt(const char *, const char *); | ||
585 | |||
586 | if (setting[0] == '$') { | ||
587 | switch (setting[1]) { | ||
588 | case '1': | ||
589 | return (md5crypt(key, setting)); | ||
590 | default: | ||
591 | return bcrypt(key, setting); | ||
592 | } | ||
593 | } | ||
594 | |||
595 | if (!_des_initialised) | ||
596 | _des_init(); | ||
597 | |||
598 | /* | ||
599 | * Copy the key, shifting each character up by one bit | ||
600 | * and padding with zeros. | ||
601 | */ | ||
602 | q = (u_char *) keybuf; | ||
603 | while ((q - (u_char *) keybuf) < sizeof(keybuf)) { | ||
604 | if ((*q++ = *key << 1)) | ||
605 | key++; | ||
606 | } | ||
607 | if (des_setkey((u_char *) keybuf)) | ||
608 | return(NULL); | ||
609 | |||
610 | if (*setting == _PASSWORD_EFMT1) { | ||
611 | /* | ||
612 | * "new"-style: | ||
613 | * setting - underscore, 4 bytes of count, 4 bytes of salt | ||
614 | * key - unlimited characters | ||
615 | */ | ||
616 | for (i = 1, count = 0; i < 5; i++) | ||
617 | count |= ascii_to_bin(setting[i]) << (i - 1) * 6; | ||
618 | |||
619 | for (i = 5, salt = 0; i < 9; i++) | ||
620 | salt |= ascii_to_bin(setting[i]) << (i - 5) * 6; | ||
621 | |||
622 | while (*key) { | ||
623 | /* | ||
624 | * Encrypt the key with itself. | ||
625 | */ | ||
626 | if (des_cipher((u_char*)keybuf, (u_char*)keybuf, 0, 1)) | ||
627 | return(NULL); | ||
628 | /* | ||
629 | * And XOR with the next 8 characters of the key. | ||
630 | */ | ||
631 | q = (u_char *) keybuf; | ||
632 | while (((q - (u_char *) keybuf) < sizeof(keybuf)) && | ||
633 | *key) | ||
634 | *q++ ^= *key++ << 1; | ||
635 | |||
636 | if (des_setkey((u_char *) keybuf)) | ||
637 | return(NULL); | ||
638 | } | ||
639 | strlcpy((char *)output, setting, 10); | ||
640 | |||
641 | /* | ||
642 | * Double check that we weren't given a short setting. | ||
643 | * If we were, the above code will probably have created | ||
644 | * weird values for count and salt, but we don't really care. | ||
645 | * Just make sure the output string doesn't have an extra | ||
646 | * NUL in it. | ||
647 | */ | ||
648 | p = output + strlen((const char *)output); | ||
649 | } else { | ||
650 | /* | ||
651 | * "old"-style: | ||
652 | * setting - 2 bytes of salt | ||
653 | * key - up to 8 characters | ||
654 | */ | ||
655 | count = 25; | ||
656 | |||
657 | salt = (ascii_to_bin(setting[1]) << 6) | ||
658 | | ascii_to_bin(setting[0]); | ||
659 | |||
660 | output[0] = setting[0]; | ||
661 | /* | ||
662 | * If the encrypted password that the salt was extracted from | ||
663 | * is only 1 character long, the salt will be corrupted. We | ||
664 | * need to ensure that the output string doesn't have an extra | ||
665 | * NUL in it! | ||
666 | */ | ||
667 | output[1] = setting[1] ? setting[1] : output[0]; | ||
668 | |||
669 | p = output + 2; | ||
670 | } | ||
671 | _des_setup_salt(salt); | ||
672 | |||
673 | /* | ||
674 | * Do it. | ||
675 | */ | ||
676 | if (_des_do_des(0, 0, &r0, &r1, count)) | ||
677 | return(NULL); | ||
678 | /* | ||
679 | * Now encode the result... | ||
680 | */ | ||
681 | l = (r0 >> 8); | ||
682 | *p++ = ascii64[(l >> 18) & 0x3f]; | ||
683 | *p++ = ascii64[(l >> 12) & 0x3f]; | ||
684 | *p++ = ascii64[(l >> 6) & 0x3f]; | ||
685 | *p++ = ascii64[l & 0x3f]; | ||
686 | |||
687 | l = (r0 << 16) | ((r1 >> 16) & 0xffff); | ||
688 | *p++ = ascii64[(l >> 18) & 0x3f]; | ||
689 | *p++ = ascii64[(l >> 12) & 0x3f]; | ||
690 | *p++ = ascii64[(l >> 6) & 0x3f]; | ||
691 | *p++ = ascii64[l & 0x3f]; | ||
692 | |||
693 | l = r1 << 2; | ||
694 | *p++ = ascii64[(l >> 12) & 0x3f]; | ||
695 | *p++ = ascii64[(l >> 6) & 0x3f]; | ||
696 | *p++ = ascii64[l & 0x3f]; | ||
697 | *p = 0; | ||
698 | |||
699 | return((char *)output); | ||
700 | } | ||
diff --git a/src/lib/libc/crypt/crypt2.c b/src/lib/libc/crypt/crypt2.c new file mode 100644 index 0000000000..0c93c12044 --- /dev/null +++ b/src/lib/libc/crypt/crypt2.c | |||
@@ -0,0 +1,108 @@ | |||
1 | /* $OpenBSD: crypt2.c,v 1.1 2003/08/12 01:22:17 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * FreeSec: libcrypt | ||
5 | * | ||
6 | * Copyright (c) 1994 David Burren | ||
7 | * All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 4. Neither the name of the author nor the names of other contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * | ||
33 | * | ||
34 | * This is an original implementation of the DES and the crypt(3) interfaces | ||
35 | * by David Burren <davidb@werj.com.au>. | ||
36 | * | ||
37 | * An excellent reference on the underlying algorithm (and related | ||
38 | * algorithms) is: | ||
39 | * | ||
40 | * B. Schneier, Applied Cryptography: protocols, algorithms, | ||
41 | * and source code in C, John Wiley & Sons, 1994. | ||
42 | * | ||
43 | * Note that in that book's description of DES the lookups for the initial, | ||
44 | * pbox, and final permutations are inverted (this has been brought to the | ||
45 | * attention of the author). A list of errata for this book has been | ||
46 | * posted to the sci.crypt newsgroup by the author and is available for FTP. | ||
47 | */ | ||
48 | |||
49 | #if defined(LIBC_SCCS) && !defined(lint) | ||
50 | static char rcsid[] = "$OpenBSD: crypt2.c,v 1.1 2003/08/12 01:22:17 deraadt Exp $"; | ||
51 | #endif /* LIBC_SCCS and not lint */ | ||
52 | |||
53 | #include <sys/types.h> | ||
54 | #include <sys/param.h> | ||
55 | #include <pwd.h> | ||
56 | #include <unistd.h> | ||
57 | #include <string.h> | ||
58 | |||
59 | #ifdef DEBUG | ||
60 | # include <stdio.h> | ||
61 | #endif | ||
62 | |||
63 | extern const u_char _des_bits8[8]; | ||
64 | extern const u_int32_t _des_bits32[32]; | ||
65 | extern int _des_initialised; | ||
66 | |||
67 | int | ||
68 | setkey(const char *key) | ||
69 | { | ||
70 | int i, j; | ||
71 | u_int32_t packed_keys[2]; | ||
72 | u_char *p; | ||
73 | |||
74 | p = (u_char *) packed_keys; | ||
75 | |||
76 | for (i = 0; i < 8; i++) { | ||
77 | p[i] = 0; | ||
78 | for (j = 0; j < 8; j++) | ||
79 | if (*key++ & 1) | ||
80 | p[i] |= _des_bits8[j]; | ||
81 | } | ||
82 | return(des_setkey(p)); | ||
83 | } | ||
84 | |||
85 | int | ||
86 | encrypt(char *block, int flag) | ||
87 | { | ||
88 | u_int32_t io[2]; | ||
89 | u_char *p; | ||
90 | int i, j, retval; | ||
91 | |||
92 | if (!_des_initialised) | ||
93 | _des_init(); | ||
94 | |||
95 | _des_setup_salt(0); | ||
96 | p = (u_char *)block; | ||
97 | for (i = 0; i < 2; i++) { | ||
98 | io[i] = 0L; | ||
99 | for (j = 0; j < 32; j++) | ||
100 | if (*p++ & 1) | ||
101 | io[i] |= _des_bits32[j]; | ||
102 | } | ||
103 | retval = _des_do_des(io[0], io[1], io, io + 1, flag ? -1 : 1); | ||
104 | for (i = 0; i < 2; i++) | ||
105 | for (j = 0; j < 32; j++) | ||
106 | block[(i << 5) | j] = (io[i] & _des_bits32[j]) ? 1 : 0; | ||
107 | return(retval); | ||
108 | } | ||
diff --git a/src/lib/libc/crypt/md5crypt.c b/src/lib/libc/crypt/md5crypt.c new file mode 100644 index 0000000000..97717e68d5 --- /dev/null +++ b/src/lib/libc/crypt/md5crypt.c | |||
@@ -0,0 +1,153 @@ | |||
1 | /* $OpenBSD: md5crypt.c,v 1.13 2003/08/07 00:30:21 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ---------------------------------------------------------------------------- | ||
5 | * "THE BEER-WARE LICENSE" (Revision 42): | ||
6 | * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you | ||
7 | * can do whatever you want with this stuff. If we meet some day, and you think | ||
8 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp | ||
9 | * ---------------------------------------------------------------------------- | ||
10 | * | ||
11 | * $FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp $ | ||
12 | * | ||
13 | */ | ||
14 | |||
15 | #if defined(LIBC_SCCS) && !defined(lint) | ||
16 | static char rcsid[] = "$OpenBSD: md5crypt.c,v 1.13 2003/08/07 00:30:21 deraadt Exp $"; | ||
17 | #endif /* LIBC_SCCS and not lint */ | ||
18 | |||
19 | #include <unistd.h> | ||
20 | #include <stdio.h> | ||
21 | #include <string.h> | ||
22 | #include <md5.h> | ||
23 | #include <string.h> | ||
24 | |||
25 | static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ | ||
26 | "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | ||
27 | |||
28 | static void to64(char *, u_int32_t, int); | ||
29 | |||
30 | static void | ||
31 | to64(char *s, u_int32_t v, int n) | ||
32 | { | ||
33 | while (--n >= 0) { | ||
34 | *s++ = itoa64[v&0x3f]; | ||
35 | v >>= 6; | ||
36 | } | ||
37 | } | ||
38 | |||
39 | /* | ||
40 | * UNIX password | ||
41 | * | ||
42 | * Use MD5 for what it is best at... | ||
43 | */ | ||
44 | |||
45 | char *md5crypt(const char *pw, const char *salt); | ||
46 | |||
47 | char * | ||
48 | md5crypt(const char *pw, const char *salt) | ||
49 | { | ||
50 | /* | ||
51 | * This string is magic for this algorithm. Having | ||
52 | * it this way, we can get get better later on | ||
53 | */ | ||
54 | static unsigned char *magic = (unsigned char *)"$1$"; | ||
55 | |||
56 | static char passwd[120], *p; | ||
57 | static const unsigned char *sp,*ep; | ||
58 | unsigned char final[16]; | ||
59 | int sl,pl,i; | ||
60 | MD5_CTX ctx,ctx1; | ||
61 | u_int32_t l; | ||
62 | |||
63 | /* Refine the Salt first */ | ||
64 | sp = (const unsigned char *)salt; | ||
65 | |||
66 | /* If it starts with the magic string, then skip that */ | ||
67 | if(!strncmp((const char *)sp,(const char *)magic,strlen((const char *)magic))) | ||
68 | sp += strlen((const char *)magic); | ||
69 | |||
70 | /* It stops at the first '$', max 8 chars */ | ||
71 | for(ep=sp;*ep && *ep != '$' && ep < (sp+8);ep++) | ||
72 | continue; | ||
73 | |||
74 | /* get the length of the true salt */ | ||
75 | sl = ep - sp; | ||
76 | |||
77 | MD5Init(&ctx); | ||
78 | |||
79 | /* The password first, since that is what is most unknown */ | ||
80 | MD5Update(&ctx,(const unsigned char *)pw,strlen(pw)); | ||
81 | |||
82 | /* Then our magic string */ | ||
83 | MD5Update(&ctx,magic,strlen((const char *)magic)); | ||
84 | |||
85 | /* Then the raw salt */ | ||
86 | MD5Update(&ctx,sp,sl); | ||
87 | |||
88 | /* Then just as many characters of the MD5(pw,salt,pw) */ | ||
89 | MD5Init(&ctx1); | ||
90 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
91 | MD5Update(&ctx1,sp,sl); | ||
92 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
93 | MD5Final(final,&ctx1); | ||
94 | for(pl = strlen(pw); pl > 0; pl -= 16) | ||
95 | MD5Update(&ctx,final,pl>16 ? 16 : pl); | ||
96 | |||
97 | /* Don't leave anything around in vm they could use. */ | ||
98 | memset(final,0,sizeof final); | ||
99 | |||
100 | /* Then something really weird... */ | ||
101 | for (i = strlen(pw); i ; i >>= 1) | ||
102 | if(i&1) | ||
103 | MD5Update(&ctx, final, 1); | ||
104 | else | ||
105 | MD5Update(&ctx, (const unsigned char *)pw, 1); | ||
106 | |||
107 | /* Now make the output string */ | ||
108 | snprintf(passwd, sizeof(passwd), "%s%.*s$", (char *)magic, | ||
109 | sl, (const char *)sp); | ||
110 | |||
111 | MD5Final(final,&ctx); | ||
112 | |||
113 | /* | ||
114 | * and now, just to make sure things don't run too fast | ||
115 | * On a 60 Mhz Pentium this takes 34 msec, so you would | ||
116 | * need 30 seconds to build a 1000 entry dictionary... | ||
117 | */ | ||
118 | for(i=0;i<1000;i++) { | ||
119 | MD5Init(&ctx1); | ||
120 | if(i & 1) | ||
121 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
122 | else | ||
123 | MD5Update(&ctx1,final,16); | ||
124 | |||
125 | if(i % 3) | ||
126 | MD5Update(&ctx1,sp,sl); | ||
127 | |||
128 | if(i % 7) | ||
129 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
130 | |||
131 | if(i & 1) | ||
132 | MD5Update(&ctx1,final,16); | ||
133 | else | ||
134 | MD5Update(&ctx1,(const unsigned char *)pw,strlen(pw)); | ||
135 | MD5Final(final,&ctx1); | ||
136 | } | ||
137 | |||
138 | p = passwd + strlen(passwd); | ||
139 | |||
140 | l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4; | ||
141 | l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4; | ||
142 | l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4; | ||
143 | l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4; | ||
144 | l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4; | ||
145 | l = final[11] ; to64(p,l,2); p += 2; | ||
146 | *p = '\0'; | ||
147 | |||
148 | /* Don't leave anything around in vm they could use. */ | ||
149 | memset(final, 0, sizeof final); | ||
150 | |||
151 | return passwd; | ||
152 | } | ||
153 | |||
diff --git a/src/lib/libc/crypt/skipjack.c b/src/lib/libc/crypt/skipjack.c new file mode 100644 index 0000000000..e700f40c39 --- /dev/null +++ b/src/lib/libc/crypt/skipjack.c | |||
@@ -0,0 +1,258 @@ | |||
1 | /* | ||
2 | * Further optimized test implementation of SKIPJACK algorithm | ||
3 | * Mark Tillotson <markt@chaos.org.uk>, 25 June 98 | ||
4 | * Optimizations suit RISC (lots of registers) machine best. | ||
5 | * | ||
6 | * based on unoptimized implementation of | ||
7 | * Panu Rissanen <bande@lut.fi> 960624 | ||
8 | * | ||
9 | * SKIPJACK and KEA Algorithm Specifications | ||
10 | * Version 2.0 | ||
11 | * 29 May 1998 | ||
12 | */ | ||
13 | |||
14 | #include <sys/param.h> | ||
15 | #include <skipjack.h> | ||
16 | #include <stdlib.h> | ||
17 | |||
18 | static const u_int8_t ftable[0x100] = | ||
19 | { | ||
20 | 0xa3, 0xd7, 0x09, 0x83, 0xf8, 0x48, 0xf6, 0xf4, | ||
21 | 0xb3, 0x21, 0x15, 0x78, 0x99, 0xb1, 0xaf, 0xf9, | ||
22 | 0xe7, 0x2d, 0x4d, 0x8a, 0xce, 0x4c, 0xca, 0x2e, | ||
23 | 0x52, 0x95, 0xd9, 0x1e, 0x4e, 0x38, 0x44, 0x28, | ||
24 | 0x0a, 0xdf, 0x02, 0xa0, 0x17, 0xf1, 0x60, 0x68, | ||
25 | 0x12, 0xb7, 0x7a, 0xc3, 0xe9, 0xfa, 0x3d, 0x53, | ||
26 | 0x96, 0x84, 0x6b, 0xba, 0xf2, 0x63, 0x9a, 0x19, | ||
27 | 0x7c, 0xae, 0xe5, 0xf5, 0xf7, 0x16, 0x6a, 0xa2, | ||
28 | 0x39, 0xb6, 0x7b, 0x0f, 0xc1, 0x93, 0x81, 0x1b, | ||
29 | 0xee, 0xb4, 0x1a, 0xea, 0xd0, 0x91, 0x2f, 0xb8, | ||
30 | 0x55, 0xb9, 0xda, 0x85, 0x3f, 0x41, 0xbf, 0xe0, | ||
31 | 0x5a, 0x58, 0x80, 0x5f, 0x66, 0x0b, 0xd8, 0x90, | ||
32 | 0x35, 0xd5, 0xc0, 0xa7, 0x33, 0x06, 0x65, 0x69, | ||
33 | 0x45, 0x00, 0x94, 0x56, 0x6d, 0x98, 0x9b, 0x76, | ||
34 | 0x97, 0xfc, 0xb2, 0xc2, 0xb0, 0xfe, 0xdb, 0x20, | ||
35 | 0xe1, 0xeb, 0xd6, 0xe4, 0xdd, 0x47, 0x4a, 0x1d, | ||
36 | 0x42, 0xed, 0x9e, 0x6e, 0x49, 0x3c, 0xcd, 0x43, | ||
37 | 0x27, 0xd2, 0x07, 0xd4, 0xde, 0xc7, 0x67, 0x18, | ||
38 | 0x89, 0xcb, 0x30, 0x1f, 0x8d, 0xc6, 0x8f, 0xaa, | ||
39 | 0xc8, 0x74, 0xdc, 0xc9, 0x5d, 0x5c, 0x31, 0xa4, | ||
40 | 0x70, 0x88, 0x61, 0x2c, 0x9f, 0x0d, 0x2b, 0x87, | ||
41 | 0x50, 0x82, 0x54, 0x64, 0x26, 0x7d, 0x03, 0x40, | ||
42 | 0x34, 0x4b, 0x1c, 0x73, 0xd1, 0xc4, 0xfd, 0x3b, | ||
43 | 0xcc, 0xfb, 0x7f, 0xab, 0xe6, 0x3e, 0x5b, 0xa5, | ||
44 | 0xad, 0x04, 0x23, 0x9c, 0x14, 0x51, 0x22, 0xf0, | ||
45 | 0x29, 0x79, 0x71, 0x7e, 0xff, 0x8c, 0x0e, 0xe2, | ||
46 | 0x0c, 0xef, 0xbc, 0x72, 0x75, 0x6f, 0x37, 0xa1, | ||
47 | 0xec, 0xd3, 0x8e, 0x62, 0x8b, 0x86, 0x10, 0xe8, | ||
48 | 0x08, 0x77, 0x11, 0xbe, 0x92, 0x4f, 0x24, 0xc5, | ||
49 | 0x32, 0x36, 0x9d, 0xcf, 0xf3, 0xa6, 0xbb, 0xac, | ||
50 | 0x5e, 0x6c, 0xa9, 0x13, 0x57, 0x25, 0xb5, 0xe3, | ||
51 | 0xbd, 0xa8, 0x3a, 0x01, 0x05, 0x59, 0x2a, 0x46 | ||
52 | }; | ||
53 | |||
54 | /* | ||
55 | * For each key byte generate a table to represent the function | ||
56 | * ftable [in ^ keybyte] | ||
57 | * | ||
58 | * These tables used to save an XOR in each stage of the G-function | ||
59 | * the tables are hopefully pointed to by register allocated variables | ||
60 | * k0, k1..k9 | ||
61 | */ | ||
62 | void | ||
63 | subkey_table_gen (u_int8_t *key, u_int8_t **key_tables) | ||
64 | { | ||
65 | int i, k; | ||
66 | |||
67 | for (k = 0; k < 10; k++) { | ||
68 | u_int8_t key_byte = key[k]; | ||
69 | u_int8_t * table = (u_int8_t *) malloc(0x100); | ||
70 | /* XXX */ | ||
71 | |||
72 | key_tables[k] = table; | ||
73 | for (i = 0; i < 0x100; i++) | ||
74 | table[i] = ftable[i ^ key_byte]; | ||
75 | } | ||
76 | } | ||
77 | |||
78 | |||
79 | #define g(k0, k1, k2, k3, ih, il, oh, ol) \ | ||
80 | { \ | ||
81 | oh = k##k0 [il] ^ ih; \ | ||
82 | ol = k##k1 [oh] ^ il; \ | ||
83 | oh = k##k2 [ol] ^ oh; \ | ||
84 | ol = k##k3 [oh] ^ ol; \ | ||
85 | } | ||
86 | |||
87 | #define g0(ih, il, oh, ol) g(0, 1, 2, 3, ih, il, oh, ol) | ||
88 | #define g4(ih, il, oh, ol) g(4, 5, 6, 7, ih, il, oh, ol) | ||
89 | #define g8(ih, il, oh, ol) g(8, 9, 0, 1, ih, il, oh, ol) | ||
90 | #define g2(ih, il, oh, ol) g(2, 3, 4, 5, ih, il, oh, ol) | ||
91 | #define g6(ih, il, oh, ol) g(6, 7, 8, 9, ih, il, oh, ol) | ||
92 | |||
93 | |||
94 | #define g_inv(k0, k1, k2, k3, ih, il, oh, ol) \ | ||
95 | { \ | ||
96 | ol = k##k3 [ih] ^ il; \ | ||
97 | oh = k##k2 [ol] ^ ih; \ | ||
98 | ol = k##k1 [oh] ^ ol; \ | ||
99 | oh = k##k0 [ol] ^ oh; \ | ||
100 | } | ||
101 | |||
102 | |||
103 | #define g0_inv(ih, il, oh, ol) g_inv(0, 1, 2, 3, ih, il, oh, ol) | ||
104 | #define g4_inv(ih, il, oh, ol) g_inv(4, 5, 6, 7, ih, il, oh, ol) | ||
105 | #define g8_inv(ih, il, oh, ol) g_inv(8, 9, 0, 1, ih, il, oh, ol) | ||
106 | #define g2_inv(ih, il, oh, ol) g_inv(2, 3, 4, 5, ih, il, oh, ol) | ||
107 | #define g6_inv(ih, il, oh, ol) g_inv(6, 7, 8, 9, ih, il, oh, ol) | ||
108 | |||
109 | /* optimized version of Skipjack algorithm | ||
110 | * | ||
111 | * the appropriate g-function is inlined for each round | ||
112 | * | ||
113 | * the data movement is minimized by rotating the names of the | ||
114 | * variables w1..w4, not their contents (saves 3 moves per round) | ||
115 | * | ||
116 | * the loops are completely unrolled (needed to staticize choice of g) | ||
117 | * | ||
118 | * compiles to about 470 instructions on a Sparc (gcc -O) | ||
119 | * which is about 58 instructions per byte, 14 per round. | ||
120 | * gcc seems to leave in some unnecessary and with 0xFF operations | ||
121 | * but only in the latter part of the functions. Perhaps it | ||
122 | * runs out of resources to properly optimize long inlined function? | ||
123 | * in theory should get about 11 instructions per round, not 14 | ||
124 | */ | ||
125 | |||
126 | void | ||
127 | skipjack_forwards(u_int8_t *plain, u_int8_t *cipher, u_int8_t **key_tables) | ||
128 | { | ||
129 | u_int8_t wh1 = plain[0]; u_int8_t wl1 = plain[1]; | ||
130 | u_int8_t wh2 = plain[2]; u_int8_t wl2 = plain[3]; | ||
131 | u_int8_t wh3 = plain[4]; u_int8_t wl3 = plain[5]; | ||
132 | u_int8_t wh4 = plain[6]; u_int8_t wl4 = plain[7]; | ||
133 | |||
134 | u_int8_t * k0 = key_tables [0]; | ||
135 | u_int8_t * k1 = key_tables [1]; | ||
136 | u_int8_t * k2 = key_tables [2]; | ||
137 | u_int8_t * k3 = key_tables [3]; | ||
138 | u_int8_t * k4 = key_tables [4]; | ||
139 | u_int8_t * k5 = key_tables [5]; | ||
140 | u_int8_t * k6 = key_tables [6]; | ||
141 | u_int8_t * k7 = key_tables [7]; | ||
142 | u_int8_t * k8 = key_tables [8]; | ||
143 | u_int8_t * k9 = key_tables [9]; | ||
144 | |||
145 | /* first 8 rounds */ | ||
146 | g0 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 1; wh4 ^= wh1; | ||
147 | g4 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 2; wh3 ^= wh4; | ||
148 | g8 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 3; wh2 ^= wh3; | ||
149 | g2 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 4; wh1 ^= wh2; | ||
150 | g6 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 5; wh4 ^= wh1; | ||
151 | g0 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 6; wh3 ^= wh4; | ||
152 | g4 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 7; wh2 ^= wh3; | ||
153 | g8 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 8; wh1 ^= wh2; | ||
154 | |||
155 | /* second 8 rounds */ | ||
156 | wh2 ^= wh1; wl2 ^= wl1 ^ 9 ; g2 (wh1,wl1, wh1,wl1); | ||
157 | wh1 ^= wh4; wl1 ^= wl4 ^ 10; g6 (wh4,wl4, wh4,wl4); | ||
158 | wh4 ^= wh3; wl4 ^= wl3 ^ 11; g0 (wh3,wl3, wh3,wl3); | ||
159 | wh3 ^= wh2; wl3 ^= wl2 ^ 12; g4 (wh2,wl2, wh2,wl2); | ||
160 | wh2 ^= wh1; wl2 ^= wl1 ^ 13; g8 (wh1,wl1, wh1,wl1); | ||
161 | wh1 ^= wh4; wl1 ^= wl4 ^ 14; g2 (wh4,wl4, wh4,wl4); | ||
162 | wh4 ^= wh3; wl4 ^= wl3 ^ 15; g6 (wh3,wl3, wh3,wl3); | ||
163 | wh3 ^= wh2; wl3 ^= wl2 ^ 16; g0 (wh2,wl2, wh2,wl2); | ||
164 | |||
165 | /* third 8 rounds */ | ||
166 | g4 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 17; wh4 ^= wh1; | ||
167 | g8 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 18; wh3 ^= wh4; | ||
168 | g2 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 19; wh2 ^= wh3; | ||
169 | g6 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 20; wh1 ^= wh2; | ||
170 | g0 (wh1,wl1, wh1,wl1); wl4 ^= wl1 ^ 21; wh4 ^= wh1; | ||
171 | g4 (wh4,wl4, wh4,wl4); wl3 ^= wl4 ^ 22; wh3 ^= wh4; | ||
172 | g8 (wh3,wl3, wh3,wl3); wl2 ^= wl3 ^ 23; wh2 ^= wh3; | ||
173 | g2 (wh2,wl2, wh2,wl2); wl1 ^= wl2 ^ 24; wh1 ^= wh2; | ||
174 | |||
175 | /* last 8 rounds */ | ||
176 | wh2 ^= wh1; wl2 ^= wl1 ^ 25; g6 (wh1,wl1, wh1,wl1); | ||
177 | wh1 ^= wh4; wl1 ^= wl4 ^ 26; g0 (wh4,wl4, wh4,wl4); | ||
178 | wh4 ^= wh3; wl4 ^= wl3 ^ 27; g4 (wh3,wl3, wh3,wl3); | ||
179 | wh3 ^= wh2; wl3 ^= wl2 ^ 28; g8 (wh2,wl2, wh2,wl2); | ||
180 | wh2 ^= wh1; wl2 ^= wl1 ^ 29; g2 (wh1,wl1, wh1,wl1); | ||
181 | wh1 ^= wh4; wl1 ^= wl4 ^ 30; g6 (wh4,wl4, wh4,wl4); | ||
182 | wh4 ^= wh3; wl4 ^= wl3 ^ 31; g0 (wh3,wl3, wh3,wl3); | ||
183 | wh3 ^= wh2; wl3 ^= wl2 ^ 32; g4 (wh2,wl2, wh2,wl2); | ||
184 | |||
185 | /* pack into byte vector */ | ||
186 | cipher [0] = wh1; cipher [1] = wl1; | ||
187 | cipher [2] = wh2; cipher [3] = wl2; | ||
188 | cipher [4] = wh3; cipher [5] = wl3; | ||
189 | cipher [6] = wh4; cipher [7] = wl4; | ||
190 | } | ||
191 | |||
192 | |||
193 | void | ||
194 | skipjack_backwards (u_int8_t *cipher, u_int8_t *plain, u_int8_t **key_tables) | ||
195 | { | ||
196 | /* setup 4 16-bit portions */ | ||
197 | u_int8_t wh1 = cipher[0]; u_int8_t wl1 = cipher[1]; | ||
198 | u_int8_t wh2 = cipher[2]; u_int8_t wl2 = cipher[3]; | ||
199 | u_int8_t wh3 = cipher[4]; u_int8_t wl3 = cipher[5]; | ||
200 | u_int8_t wh4 = cipher[6]; u_int8_t wl4 = cipher[7]; | ||
201 | |||
202 | u_int8_t * k0 = key_tables [0]; | ||
203 | u_int8_t * k1 = key_tables [1]; | ||
204 | u_int8_t * k2 = key_tables [2]; | ||
205 | u_int8_t * k3 = key_tables [3]; | ||
206 | u_int8_t * k4 = key_tables [4]; | ||
207 | u_int8_t * k5 = key_tables [5]; | ||
208 | u_int8_t * k6 = key_tables [6]; | ||
209 | u_int8_t * k7 = key_tables [7]; | ||
210 | u_int8_t * k8 = key_tables [8]; | ||
211 | u_int8_t * k9 = key_tables [9]; | ||
212 | |||
213 | /* first 8 rounds */ | ||
214 | g4_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 32; wh3 ^= wh2; | ||
215 | g0_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 31; wh4 ^= wh3; | ||
216 | g6_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 30; wh1 ^= wh4; | ||
217 | g2_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 29; wh2 ^= wh1; | ||
218 | g8_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 28; wh3 ^= wh2; | ||
219 | g4_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 27; wh4 ^= wh3; | ||
220 | g0_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 26; wh1 ^= wh4; | ||
221 | g6_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 25; wh2 ^= wh1; | ||
222 | |||
223 | /* second 8 rounds */ | ||
224 | wh1 ^= wh2; wl1 ^= wl2 ^ 24; g2_inv (wh2,wl2, wh2,wl2); | ||
225 | wh2 ^= wh3; wl2 ^= wl3 ^ 23; g8_inv (wh3,wl3, wh3,wl3); | ||
226 | wh3 ^= wh4; wl3 ^= wl4 ^ 22; g4_inv (wh4,wl4, wh4,wl4); | ||
227 | wh4 ^= wh1; wl4 ^= wl1 ^ 21; g0_inv (wh1,wl1, wh1,wl1); | ||
228 | wh1 ^= wh2; wl1 ^= wl2 ^ 20; g6_inv (wh2,wl2, wh2,wl2); | ||
229 | wh2 ^= wh3; wl2 ^= wl3 ^ 19; g2_inv (wh3,wl3, wh3,wl3); | ||
230 | wh3 ^= wh4; wl3 ^= wl4 ^ 18; g8_inv (wh4,wl4, wh4,wl4); | ||
231 | wh4 ^= wh1; wl4 ^= wl1 ^ 17; g4_inv (wh1,wl1, wh1,wl1); | ||
232 | |||
233 | /* third 8 rounds */ | ||
234 | g0_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 16; wh3 ^= wh2; | ||
235 | g6_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 15; wh4 ^= wh3; | ||
236 | g2_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 14; wh1 ^= wh4; | ||
237 | g8_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 13; wh2 ^= wh1; | ||
238 | g4_inv (wh2,wl2, wh2,wl2); wl3 ^= wl2 ^ 12; wh3 ^= wh2; | ||
239 | g0_inv (wh3,wl3, wh3,wl3); wl4 ^= wl3 ^ 11; wh4 ^= wh3; | ||
240 | g6_inv (wh4,wl4, wh4,wl4); wl1 ^= wl4 ^ 10; wh1 ^= wh4; | ||
241 | g2_inv (wh1,wl1, wh1,wl1); wl2 ^= wl1 ^ 9; wh2 ^= wh1; | ||
242 | |||
243 | /* last 8 rounds */ | ||
244 | wh1 ^= wh2; wl1 ^= wl2 ^ 8; g8_inv (wh2,wl2, wh2,wl2); | ||
245 | wh2 ^= wh3; wl2 ^= wl3 ^ 7; g4_inv (wh3,wl3, wh3,wl3); | ||
246 | wh3 ^= wh4; wl3 ^= wl4 ^ 6; g0_inv (wh4,wl4, wh4,wl4); | ||
247 | wh4 ^= wh1; wl4 ^= wl1 ^ 5; g6_inv (wh1,wl1, wh1,wl1); | ||
248 | wh1 ^= wh2; wl1 ^= wl2 ^ 4; g2_inv (wh2,wl2, wh2,wl2); | ||
249 | wh2 ^= wh3; wl2 ^= wl3 ^ 3; g8_inv (wh3,wl3, wh3,wl3); | ||
250 | wh3 ^= wh4; wl3 ^= wl4 ^ 2; g4_inv (wh4,wl4, wh4,wl4); | ||
251 | wh4 ^= wh1; wl4 ^= wl1 ^ 1; g0_inv (wh1,wl1, wh1,wl1); | ||
252 | |||
253 | /* pack into byte vector */ | ||
254 | plain [0] = wh1; plain [1] = wl1; | ||
255 | plain [2] = wh2; plain [3] = wl2; | ||
256 | plain [4] = wh3; plain [5] = wl3; | ||
257 | plain [6] = wh4; plain [7] = wl4; | ||
258 | } | ||
diff --git a/src/lib/libc/include/namespace.h b/src/lib/libc/include/namespace.h new file mode 100644 index 0000000000..4a51f15ddf --- /dev/null +++ b/src/lib/libc/include/namespace.h | |||
@@ -0,0 +1,18 @@ | |||
1 | /* $OpenBSD: namespace.h,v 1.2 1996/08/19 08:28:08 tholo Exp $ */ | ||
2 | |||
3 | #define catclose _catclose | ||
4 | #define catgets _catgets | ||
5 | #define catopen _catopen | ||
6 | #define err _err | ||
7 | #define errx _errx | ||
8 | #define strtoq _strtoq | ||
9 | #define strtouq _strtouq | ||
10 | #define sys_errlist _sys_errlist | ||
11 | #define sys_nerr _sys_nerr | ||
12 | #define sys_siglist _sys_siglist | ||
13 | #define verr _verr | ||
14 | #define verrx _verrx | ||
15 | #define vwarn _vwarn | ||
16 | #define vwarnx _vwarnx | ||
17 | #define warn _warn | ||
18 | #define warnx _warnx | ||
diff --git a/src/lib/libc/include/thread_private.h b/src/lib/libc/include/thread_private.h new file mode 100644 index 0000000000..4212e7dcb2 --- /dev/null +++ b/src/lib/libc/include/thread_private.h | |||
@@ -0,0 +1,134 @@ | |||
1 | /* $OpenBSD: thread_private.h,v 1.15 2003/01/28 04:58:00 marc Exp $ */ | ||
2 | |||
3 | #ifndef _THREAD_PRIVATE_H_ | ||
4 | #define _THREAD_PRIVATE_H_ | ||
5 | |||
6 | #include <pthread.h> | ||
7 | |||
8 | /* | ||
9 | * This variable is initially 0 when there is exactly one thread. | ||
10 | * It should never decrease. | ||
11 | */ | ||
12 | extern int __isthreaded; | ||
13 | |||
14 | /* | ||
15 | * Weak symbols are used in libc so that the thread library can | ||
16 | * efficiently wrap libc functions. | ||
17 | * | ||
18 | * Use WEAK_NAME(n) to get a libc-private name for n (_weak_n), | ||
19 | * WEAK_ALIAS(n) to generate the weak symbol n pointing to _weak_n, | ||
20 | * WEAK_PROTOTYPE(n) to generate a prototype for _weak_n (based on n). | ||
21 | */ | ||
22 | #define WEAK_NAME(name) __CONCAT(_weak_,name) | ||
23 | #define WEAK_ALIAS(name) __weak_alias(name, WEAK_NAME(name)) | ||
24 | #ifdef __GNUC__ | ||
25 | #define WEAK_PROTOTYPE(name) __typeof__(name) WEAK_NAME(name) | ||
26 | #else | ||
27 | #define WEAK_PROTOTYPE(name) /* typeof() only in gcc */ | ||
28 | #endif | ||
29 | |||
30 | /* | ||
31 | * These macros help in making persistent storage thread-specific. | ||
32 | * Libc makes extensive use of private static data structures | ||
33 | * that hold state across function invocation, and these macros | ||
34 | * are no-ops when running single-threaded. | ||
35 | * | ||
36 | * Linking against the user-thread library causes these macros to | ||
37 | * allocate storage on a per-thread basis. | ||
38 | */ | ||
39 | |||
40 | #define __THREAD_MUTEX_NAME(name) __CONCAT(_libc_storage_mutex_,name) | ||
41 | #define __THREAD_KEY_NAME(name) __CONCAT(_libc_storage_key_,name) | ||
42 | |||
43 | struct _thread_private_key_struct { | ||
44 | pthread_once_t once; | ||
45 | void (*cleanfn)(void *); | ||
46 | pthread_key_t key; | ||
47 | }; | ||
48 | |||
49 | void _libc_private_storage_lock(pthread_mutex_t *); | ||
50 | void _libc_private_storage_unlock(pthread_mutex_t *); | ||
51 | void * _libc_private_storage(volatile struct _thread_private_key_struct *, | ||
52 | void *, size_t, void *); | ||
53 | |||
54 | /* Declare a module mutex. */ | ||
55 | #define _THREAD_PRIVATE_MUTEX(name) \ | ||
56 | static pthread_mutex_t __THREAD_MUTEX_NAME(name) = \ | ||
57 | PTHREAD_MUTEX_INITIALIZER | ||
58 | |||
59 | /* Lock a module mutex against use by any other threads. */ | ||
60 | #define _THREAD_PRIVATE_MUTEX_LOCK(name) \ | ||
61 | _libc_private_storage_lock(&__THREAD_MUTEX_NAME(name)) | ||
62 | |||
63 | /* Unlock a module mutex. */ | ||
64 | #define _THREAD_PRIVATE_MUTEX_UNLOCK(name) \ | ||
65 | _libc_private_storage_unlock(&__THREAD_MUTEX_NAME(name)) | ||
66 | |||
67 | /* Declare a thread-private storage key. */ | ||
68 | #define _THREAD_PRIVATE_KEY(name) \ | ||
69 | static volatile struct _thread_private_key_struct \ | ||
70 | __THREAD_KEY_NAME(name) = { \ | ||
71 | PTHREAD_ONCE_INIT, \ | ||
72 | 0 \ | ||
73 | } | ||
74 | |||
75 | /* | ||
76 | * In threaded mode, return a pointer to thread-private memory of | ||
77 | * the same size as, and (initially) with the same contents as 'storage'. If | ||
78 | * an error occurs, the 'error' parameter is returned. | ||
79 | * In single-threaded mode, no storage is allocated. Instead, a pointer | ||
80 | * to storage is always returned. | ||
81 | * The 'cleanfn' function of the key structure is called to free the storage. | ||
82 | * If 'cleanfn' is NULL, then free() is used. This hook can be useful for | ||
83 | * getting rid of memory leaks. | ||
84 | */ | ||
85 | #define _THREAD_PRIVATE(keyname, storage, error) \ | ||
86 | _libc_private_storage(&__THREAD_KEY_NAME(keyname), \ | ||
87 | &(storage), sizeof (storage), error) | ||
88 | |||
89 | /* | ||
90 | * Keys used to access the per thread instances of resolver global data. | ||
91 | * These are not static as they are referenced in several places. | ||
92 | */ | ||
93 | extern volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res); | ||
94 | #ifdef INET6 | ||
95 | extern volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res_ext); | ||
96 | #endif | ||
97 | |||
98 | /* | ||
99 | * File descriptor locking definitions. | ||
100 | */ | ||
101 | #define FD_READ 0x1 | ||
102 | #define FD_WRITE 0x2 | ||
103 | #define FD_RDWR (FD_READ | FD_WRITE) | ||
104 | |||
105 | #define _FD_LOCK(_fd,_type,_ts) \ | ||
106 | _thread_fd_lock(_fd, _type, _ts, __FILE__, __LINE__) | ||
107 | #define _FD_UNLOCK(_fd,_type) \ | ||
108 | _thread_fd_unlock(_fd, _type, __FILE__, __LINE__) | ||
109 | |||
110 | int _thread_fd_lock(int, int, struct timespec *, const char *, int); | ||
111 | void _thread_fd_unlock(int, int, const char *, int); | ||
112 | |||
113 | /* | ||
114 | * malloc lock/unlock definitions | ||
115 | */ | ||
116 | # define _MALLOC_LOCK() do { \ | ||
117 | if (__isthreaded) \ | ||
118 | _thread_malloc_lock(); \ | ||
119 | } while (0) | ||
120 | # define _MALLOC_UNLOCK() do { \ | ||
121 | if (__isthreaded) \ | ||
122 | _thread_malloc_unlock();\ | ||
123 | } while (0) | ||
124 | # define _MALLOC_LOCK_INIT()do { \ | ||
125 | if (__isthreaded) \ | ||
126 | _thread_malloc_init();\ | ||
127 | } while (0) | ||
128 | |||
129 | |||
130 | void _thread_malloc_init(void); | ||
131 | void _thread_malloc_lock(void); | ||
132 | void _thread_malloc_unlock(void); | ||
133 | |||
134 | #endif /* _THREAD_PRIVATE_H_ */ | ||
diff --git a/src/lib/libc/net/Makefile.inc b/src/lib/libc/net/Makefile.inc new file mode 100644 index 0000000000..805ae5d224 --- /dev/null +++ b/src/lib/libc/net/Makefile.inc | |||
@@ -0,0 +1,85 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.31 2001/08/06 14:40:47 jakob Exp $ | ||
2 | |||
3 | # net sources | ||
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net ${LIBCSRCDIR}/net | ||
5 | |||
6 | CFLAGS+=-DRESOLVSORT | ||
7 | |||
8 | SRCS+= base64.c freeaddrinfo.c gai_strerror.c getaddrinfo.c gethostnamadr.c \ | ||
9 | getifaddrs.c getnameinfo.c getnetbyaddr.c getnetbyname.c getnetent.c \ | ||
10 | getnetnamadr.c getproto.c getprotoent.c getprotoname.c \ | ||
11 | getservbyname.c getservbyport.c getservent.c getrrsetbyname.c \ | ||
12 | herror.c if_indextoname.c if_nameindex.c if_nametoindex.c inet_addr.c \ | ||
13 | inet_lnaof.c inet_makeaddr.c inet_neta.c inet_netof.c inet_network.c \ | ||
14 | inet_net_ntop.c inet_net_pton.c inet_ntoa.c inet_ntop.c inet_pton.c \ | ||
15 | ipx_addr.c ipx_ntoa.c iso_addr.c linkaddr.c ns_addr.c ns_ntoa.c \ | ||
16 | nsap_addr.c \ | ||
17 | net_addrcmp.c \ | ||
18 | rcmd.c rresvport.c recv.c res_comp.c res_data.c res_debug.c \ | ||
19 | res_init.c res_mkquery.c res_query.c res_random.c res_send.c send.c \ | ||
20 | sethostent.c ethers.c rcmdsh.c | ||
21 | |||
22 | # IPv6 | ||
23 | SRCS+= ip6opt.c rthdr.c vars6.c | ||
24 | |||
25 | # machine-dependent net sources | ||
26 | # m-d Makefile.inc must include sources for: | ||
27 | # htonl() htons() ntohl() ntohs() | ||
28 | |||
29 | .include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/net/Makefile.inc" | ||
30 | |||
31 | MAN+= byteorder.3 ethers.3 getaddrinfo.3 gethostbyname.3 getifaddrs.3 \ | ||
32 | getnameinfo.3 getnetent.3 getprotoent.3 getservent.3 inet.3 \ | ||
33 | if_indextoname.3 inet_net.3 iso_addr.3 link_addr.3 ns.3 ipx.3 \ | ||
34 | rcmd.3 rcmdsh.3 resolver.3 net_addrcmp.3 \ | ||
35 | inet6_option_space.3 inet6_rthdr_space.3 \ | ||
36 | getrrsetbyname.3 | ||
37 | |||
38 | MLINKS+=byteorder.3 htonl.3 byteorder.3 htons.3 byteorder.3 ntohl.3 \ | ||
39 | byteorder.3 ntohs.3 byteorder.3 htobe16.3 byteorder.3 htobe32.3 \ | ||
40 | byteorder.3 betoh16.3 byteorder.3 betoh32.3 byteorder.3 htole16.3 \ | ||
41 | byteorder.3 htole32.3 byteorder.3 letoh16.3 byteorder.3 letoh32.3 \ | ||
42 | byteorder.3 swap16.3 byteorder.3 swap32.3 | ||
43 | MLINKS+=ethers.3 ether_aton.3 ethers.3 ether_hostton.3 ethers.3 ether_line.3 \ | ||
44 | ethers.3 ether_ntoa.3 ethers.3 ether_ntohost.3 ethers.3 ether_addr.3 | ||
45 | MLINKS+= getaddrinfo.3 freeaddrinfo.3 getaddrinfo.3 gai_strerror.3 | ||
46 | MLINKS+=gethostbyname.3 endhostent.3 gethostbyname.3 gethostbyaddr.3 \ | ||
47 | gethostbyname.3 sethostent.3 gethostbyname.3 gethostent.3 \ | ||
48 | gethostbyname.3 herror.3 gethostbyname.3 gethostbyname2.3 \ | ||
49 | gethostbyname.3 hstrerror.3 | ||
50 | MLINKS+=getifaddrs.3 freeifaddrs.3 | ||
51 | MLINKS+=getnetent.3 endnetent.3 getnetent.3 getnetbyaddr.3 \ | ||
52 | getnetent.3 getnetbyname.3 getnetent.3 setnetent.3 | ||
53 | MLINKS+=getprotoent.3 endprotoent.3 getprotoent.3 getprotobyname.3 \ | ||
54 | getprotoent.3 getprotobynumber.3 getprotoent.3 setprotoent.3 | ||
55 | MLINKS+=getservent.3 endservent.3 getservent.3 getservbyname.3 \ | ||
56 | getservent.3 getservbyport.3 getservent.3 setservent.3 | ||
57 | MLINKS+= if_indextoname.3 if_nametoindex.3 if_indextoname.3 if_nameindex.3 \ | ||
58 | if_indextoname.3 if_freenameindex.3 | ||
59 | MLINKS+=inet.3 addr.3 inet.3 inet_addr.3 inet.3 inet_aton.3 \ | ||
60 | inet.3 inet_lnaof.3 inet.3 inet_makeaddr.3 inet.3 inet_netof.3 \ | ||
61 | inet.3 inet_network.3 inet.3 inet_ntoa.3 inet.3 network.3 \ | ||
62 | inet.3 ntoa.3 inet.3 inet_ntop.3 inet.3 inet_pton.3 | ||
63 | MLINKS+=inet_net.3 inet_net_ntop.3 inet_net.3 inet_net_pton.3 | ||
64 | MLINKS+=iso_addr.3 iso_ntoa.3 | ||
65 | MLINKS+=link_addr.3 link_ntoa.3 | ||
66 | MLINKS+=ipx.3 ipx_addr.3 ipx.3 ipx_ntoa.3 | ||
67 | MLINKS+=ns.3 ns_addr.3 ns.3 ns_ntoa.3 | ||
68 | MLINKS+=rcmd.3 iruserok.3 rcmd.3 rresvport.3 rcmd.3 ruserok.3 \ | ||
69 | rcmd.3 rresvport_af.3 rcmd.3 rcmd_af.3 rcmd.3 iruserok_sa.3 | ||
70 | MLINKS+=resolver.3 dn_comp.3 resolver.3 dn_expand.3 resolver.3 res_init.3 \ | ||
71 | resolver.3 res_mkquery.3 resolver.3 res_send.3 resolver.3 res_query.3 \ | ||
72 | resolver.3 res_search.3 | ||
73 | MLINKS+=inet6_option_space.3 inet6_option_init.3 \ | ||
74 | inet6_option_space.3 inet6_option_append.3 \ | ||
75 | inet6_option_space.3 inet6_option_alloc.3 \ | ||
76 | inet6_option_space.3 inet6_option_next.3 \ | ||
77 | inet6_option_space.3 inet6_option_find.3 | ||
78 | MLINKS+=inet6_rthdr_space.3 inet6_rthdr_init.3 \ | ||
79 | inet6_rthdr_space.3 inet6_rthdr_add.3 \ | ||
80 | inet6_rthdr_space.3 inet6_rthdr_lasthop.3 \ | ||
81 | inet6_rthdr_space.3 inet6_rthdr_reverse.3 \ | ||
82 | inet6_rthdr_space.3 inet6_rthdr_segments.3 \ | ||
83 | inet6_rthdr_space.3 inet6_rthdr_getaddr.3 \ | ||
84 | inet6_rthdr_space.3 inet6_rthdr_getflags.3 | ||
85 | MLINKS+=getrrsetbyname.3 freerrset.3 | ||
diff --git a/src/lib/libc/net/base64.c b/src/lib/libc/net/base64.c new file mode 100644 index 0000000000..33b6ffdc41 --- /dev/null +++ b/src/lib/libc/net/base64.c | |||
@@ -0,0 +1,318 @@ | |||
1 | /* $OpenBSD: base64.c,v 1.4 2002/01/02 23:00:10 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | /* | ||
21 | * Portions Copyright (c) 1995 by International Business Machines, Inc. | ||
22 | * | ||
23 | * International Business Machines, Inc. (hereinafter called IBM) grants | ||
24 | * permission under its copyrights to use, copy, modify, and distribute this | ||
25 | * Software with or without fee, provided that the above copyright notice and | ||
26 | * all paragraphs of this notice appear in all copies, and that the name of IBM | ||
27 | * not be used in connection with the marketing of any product incorporating | ||
28 | * the Software or modifications thereof, without specific, written prior | ||
29 | * permission. | ||
30 | * | ||
31 | * To the extent it has a right to do so, IBM grants an immunity from suit | ||
32 | * under its patents, if any, for the use, sale or manufacture of products to | ||
33 | * the extent that such products are used for performing Domain Name System | ||
34 | * dynamic updates in TCP/IP networks by means of the Software. No immunity is | ||
35 | * granted for any product per se or for any other function of any product. | ||
36 | * | ||
37 | * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, | ||
38 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
39 | * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, | ||
40 | * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING | ||
41 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN | ||
42 | * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. | ||
43 | */ | ||
44 | |||
45 | #include <sys/types.h> | ||
46 | #include <sys/param.h> | ||
47 | #include <sys/socket.h> | ||
48 | #include <netinet/in.h> | ||
49 | #include <arpa/inet.h> | ||
50 | #include <arpa/nameser.h> | ||
51 | |||
52 | #include <ctype.h> | ||
53 | #include <resolv.h> | ||
54 | #include <stdio.h> | ||
55 | |||
56 | #include <stdlib.h> | ||
57 | #include <string.h> | ||
58 | |||
59 | /* XXX abort illegal in library */ | ||
60 | #define Assert(Cond) if (!(Cond)) abort() | ||
61 | |||
62 | static const char Base64[] = | ||
63 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
64 | static const char Pad64 = '='; | ||
65 | |||
66 | /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) | ||
67 | The following encoding technique is taken from RFC 1521 by Borenstein | ||
68 | and Freed. It is reproduced here in a slightly edited form for | ||
69 | convenience. | ||
70 | |||
71 | A 65-character subset of US-ASCII is used, enabling 6 bits to be | ||
72 | represented per printable character. (The extra 65th character, "=", | ||
73 | is used to signify a special processing function.) | ||
74 | |||
75 | The encoding process represents 24-bit groups of input bits as output | ||
76 | strings of 4 encoded characters. Proceeding from left to right, a | ||
77 | 24-bit input group is formed by concatenating 3 8-bit input groups. | ||
78 | These 24 bits are then treated as 4 concatenated 6-bit groups, each | ||
79 | of which is translated into a single digit in the base64 alphabet. | ||
80 | |||
81 | Each 6-bit group is used as an index into an array of 64 printable | ||
82 | characters. The character referenced by the index is placed in the | ||
83 | output string. | ||
84 | |||
85 | Table 1: The Base64 Alphabet | ||
86 | |||
87 | Value Encoding Value Encoding Value Encoding Value Encoding | ||
88 | 0 A 17 R 34 i 51 z | ||
89 | 1 B 18 S 35 j 52 0 | ||
90 | 2 C 19 T 36 k 53 1 | ||
91 | 3 D 20 U 37 l 54 2 | ||
92 | 4 E 21 V 38 m 55 3 | ||
93 | 5 F 22 W 39 n 56 4 | ||
94 | 6 G 23 X 40 o 57 5 | ||
95 | 7 H 24 Y 41 p 58 6 | ||
96 | 8 I 25 Z 42 q 59 7 | ||
97 | 9 J 26 a 43 r 60 8 | ||
98 | 10 K 27 b 44 s 61 9 | ||
99 | 11 L 28 c 45 t 62 + | ||
100 | 12 M 29 d 46 u 63 / | ||
101 | 13 N 30 e 47 v | ||
102 | 14 O 31 f 48 w (pad) = | ||
103 | 15 P 32 g 49 x | ||
104 | 16 Q 33 h 50 y | ||
105 | |||
106 | Special processing is performed if fewer than 24 bits are available | ||
107 | at the end of the data being encoded. A full encoding quantum is | ||
108 | always completed at the end of a quantity. When fewer than 24 input | ||
109 | bits are available in an input group, zero bits are added (on the | ||
110 | right) to form an integral number of 6-bit groups. Padding at the | ||
111 | end of the data is performed using the '=' character. | ||
112 | |||
113 | Since all base64 input is an integral number of octets, only the | ||
114 | ------------------------------------------------- | ||
115 | following cases can arise: | ||
116 | |||
117 | (1) the final quantum of encoding input is an integral | ||
118 | multiple of 24 bits; here, the final unit of encoded | ||
119 | output will be an integral multiple of 4 characters | ||
120 | with no "=" padding, | ||
121 | (2) the final quantum of encoding input is exactly 8 bits; | ||
122 | here, the final unit of encoded output will be two | ||
123 | characters followed by two "=" padding characters, or | ||
124 | (3) the final quantum of encoding input is exactly 16 bits; | ||
125 | here, the final unit of encoded output will be three | ||
126 | characters followed by one "=" padding character. | ||
127 | */ | ||
128 | |||
129 | int | ||
130 | b64_ntop(src, srclength, target, targsize) | ||
131 | u_char const *src; | ||
132 | size_t srclength; | ||
133 | char *target; | ||
134 | size_t targsize; | ||
135 | { | ||
136 | size_t datalength = 0; | ||
137 | u_char input[3]; | ||
138 | u_char output[4]; | ||
139 | int i; | ||
140 | |||
141 | while (2 < srclength) { | ||
142 | input[0] = *src++; | ||
143 | input[1] = *src++; | ||
144 | input[2] = *src++; | ||
145 | srclength -= 3; | ||
146 | |||
147 | output[0] = input[0] >> 2; | ||
148 | output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); | ||
149 | output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); | ||
150 | output[3] = input[2] & 0x3f; | ||
151 | Assert(output[0] < 64); | ||
152 | Assert(output[1] < 64); | ||
153 | Assert(output[2] < 64); | ||
154 | Assert(output[3] < 64); | ||
155 | |||
156 | if (datalength + 4 > targsize) | ||
157 | return (-1); | ||
158 | target[datalength++] = Base64[output[0]]; | ||
159 | target[datalength++] = Base64[output[1]]; | ||
160 | target[datalength++] = Base64[output[2]]; | ||
161 | target[datalength++] = Base64[output[3]]; | ||
162 | } | ||
163 | |||
164 | /* Now we worry about padding. */ | ||
165 | if (0 != srclength) { | ||
166 | /* Get what's left. */ | ||
167 | input[0] = input[1] = input[2] = '\0'; | ||
168 | for (i = 0; i < srclength; i++) | ||
169 | input[i] = *src++; | ||
170 | |||
171 | output[0] = input[0] >> 2; | ||
172 | output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4); | ||
173 | output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6); | ||
174 | Assert(output[0] < 64); | ||
175 | Assert(output[1] < 64); | ||
176 | Assert(output[2] < 64); | ||
177 | |||
178 | if (datalength + 4 > targsize) | ||
179 | return (-1); | ||
180 | target[datalength++] = Base64[output[0]]; | ||
181 | target[datalength++] = Base64[output[1]]; | ||
182 | if (srclength == 1) | ||
183 | target[datalength++] = Pad64; | ||
184 | else | ||
185 | target[datalength++] = Base64[output[2]]; | ||
186 | target[datalength++] = Pad64; | ||
187 | } | ||
188 | if (datalength >= targsize) | ||
189 | return (-1); | ||
190 | target[datalength] = '\0'; /* Returned value doesn't count \0. */ | ||
191 | return (datalength); | ||
192 | } | ||
193 | |||
194 | /* skips all whitespace anywhere. | ||
195 | converts characters, four at a time, starting at (or after) | ||
196 | src from base - 64 numbers into three 8 bit bytes in the target area. | ||
197 | it returns the number of data bytes stored at the target, or -1 on error. | ||
198 | */ | ||
199 | |||
200 | int | ||
201 | b64_pton(src, target, targsize) | ||
202 | char const *src; | ||
203 | u_char *target; | ||
204 | size_t targsize; | ||
205 | { | ||
206 | int tarindex, state, ch; | ||
207 | char *pos; | ||
208 | |||
209 | state = 0; | ||
210 | tarindex = 0; | ||
211 | |||
212 | while ((ch = *src++) != '\0') { | ||
213 | if (isspace(ch)) /* Skip whitespace anywhere. */ | ||
214 | continue; | ||
215 | |||
216 | if (ch == Pad64) | ||
217 | break; | ||
218 | |||
219 | pos = strchr(Base64, ch); | ||
220 | if (pos == 0) /* A non-base64 character. */ | ||
221 | return (-1); | ||
222 | |||
223 | switch (state) { | ||
224 | case 0: | ||
225 | if (target) { | ||
226 | if (tarindex >= targsize) | ||
227 | return (-1); | ||
228 | target[tarindex] = (pos - Base64) << 2; | ||
229 | } | ||
230 | state = 1; | ||
231 | break; | ||
232 | case 1: | ||
233 | if (target) { | ||
234 | if (tarindex + 1 >= targsize) | ||
235 | return (-1); | ||
236 | target[tarindex] |= (pos - Base64) >> 4; | ||
237 | target[tarindex+1] = ((pos - Base64) & 0x0f) | ||
238 | << 4 ; | ||
239 | } | ||
240 | tarindex++; | ||
241 | state = 2; | ||
242 | break; | ||
243 | case 2: | ||
244 | if (target) { | ||
245 | if (tarindex + 1 >= targsize) | ||
246 | return (-1); | ||
247 | target[tarindex] |= (pos - Base64) >> 2; | ||
248 | target[tarindex+1] = ((pos - Base64) & 0x03) | ||
249 | << 6; | ||
250 | } | ||
251 | tarindex++; | ||
252 | state = 3; | ||
253 | break; | ||
254 | case 3: | ||
255 | if (target) { | ||
256 | if (tarindex >= targsize) | ||
257 | return (-1); | ||
258 | target[tarindex] |= (pos - Base64); | ||
259 | } | ||
260 | tarindex++; | ||
261 | state = 0; | ||
262 | break; | ||
263 | } | ||
264 | } | ||
265 | |||
266 | /* | ||
267 | * We are done decoding Base-64 chars. Let's see if we ended | ||
268 | * on a byte boundary, and/or with erroneous trailing characters. | ||
269 | */ | ||
270 | |||
271 | if (ch == Pad64) { /* We got a pad char. */ | ||
272 | ch = *src++; /* Skip it, get next. */ | ||
273 | switch (state) { | ||
274 | case 0: /* Invalid = in first position */ | ||
275 | case 1: /* Invalid = in second position */ | ||
276 | return (-1); | ||
277 | |||
278 | case 2: /* Valid, means one byte of info */ | ||
279 | /* Skip any number of spaces. */ | ||
280 | for (; ch != '\0'; ch = *src++) | ||
281 | if (!isspace(ch)) | ||
282 | break; | ||
283 | /* Make sure there is another trailing = sign. */ | ||
284 | if (ch != Pad64) | ||
285 | return (-1); | ||
286 | ch = *src++; /* Skip the = */ | ||
287 | /* Fall through to "single trailing =" case. */ | ||
288 | /* FALLTHROUGH */ | ||
289 | |||
290 | case 3: /* Valid, means two bytes of info */ | ||
291 | /* | ||
292 | * We know this char is an =. Is there anything but | ||
293 | * whitespace after it? | ||
294 | */ | ||
295 | for (; ch != '\0'; ch = *src++) | ||
296 | if (!isspace(ch)) | ||
297 | return (-1); | ||
298 | |||
299 | /* | ||
300 | * Now make sure for cases 2 and 3 that the "extra" | ||
301 | * bits that slopped past the last full byte were | ||
302 | * zeros. If we don't check them, they become a | ||
303 | * subliminal channel. | ||
304 | */ | ||
305 | if (target && target[tarindex] != 0) | ||
306 | return (-1); | ||
307 | } | ||
308 | } else { | ||
309 | /* | ||
310 | * We ended by seeing the end of the string. Make sure we | ||
311 | * have no partial bytes lying around. | ||
312 | */ | ||
313 | if (state != 0) | ||
314 | return (-1); | ||
315 | } | ||
316 | |||
317 | return (tarindex); | ||
318 | } | ||
diff --git a/src/lib/libc/net/byteorder.3 b/src/lib/libc/net/byteorder.3 new file mode 100644 index 0000000000..54123455c6 --- /dev/null +++ b/src/lib/libc/net/byteorder.3 | |||
@@ -0,0 +1,206 @@ | |||
1 | .\" $OpenBSD: byteorder.3,v 1.13 2003/09/26 17:06:50 fgsch Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt BYTEORDER 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm htonl , | ||
35 | .Nm htons , | ||
36 | .Nm ntohl , | ||
37 | .Nm ntohs , | ||
38 | .Nm htobe64 , | ||
39 | .Nm htobe32 , | ||
40 | .Nm htobe16 , | ||
41 | .Nm betoh64 , | ||
42 | .Nm betoh32 , | ||
43 | .Nm betoh16 , | ||
44 | .Nm htole64 , | ||
45 | .Nm htole32 , | ||
46 | .Nm htole16 , | ||
47 | .Nm letoh64 , | ||
48 | .Nm letoh32 , | ||
49 | .Nm letoh16 , | ||
50 | .Nm swap64 , | ||
51 | .Nm swap32 , | ||
52 | .Nm swap16 | ||
53 | .Nd convert values between different byte orderings | ||
54 | .Sh SYNOPSIS | ||
55 | .Fd #include <sys/types.h> | ||
56 | .Ft u_int32_t | ||
57 | .Fn htonl "u_int32_t host32" | ||
58 | .Ft u_int16_t | ||
59 | .Fn htons "u_int16_t host16" | ||
60 | .Ft u_int32_t | ||
61 | .Fn ntohl "u_int32_t net32" | ||
62 | .Ft u_int16_t | ||
63 | .Fn ntohs "u_int16_t net16" | ||
64 | .Ft u_int64_t | ||
65 | .Fn htobe64 "u_int64_t host64" | ||
66 | .Ft u_int32_t | ||
67 | .Fn htobe32 "u_int32_t host32" | ||
68 | .Ft u_int16_t | ||
69 | .Fn htobe16 "u_int16_t host16" | ||
70 | .Ft u_int64_t | ||
71 | .Fn betoh64 "u_int64_t big64" | ||
72 | .Ft u_int32_t | ||
73 | .Fn betoh32 "u_int32_t big32" | ||
74 | .Ft u_int16_t | ||
75 | .Fn betoh16 "u_int16_t big16" | ||
76 | .Ft u_int64_t | ||
77 | .Fn htole64 "u_int64_t host64" | ||
78 | .Ft u_int32_t | ||
79 | .Fn htole32 "u_int32_t host32" | ||
80 | .Ft u_int16_t | ||
81 | .Fn htole16 "u_int16_t host16" | ||
82 | .Ft u_int64_t | ||
83 | .Fn letoh64 "u_int64_t little64" | ||
84 | .Ft u_int32_t | ||
85 | .Fn letoh32 "u_int32_t little32" | ||
86 | .Ft u_int16_t | ||
87 | .Fn letoh16 "u_int16_t little16" | ||
88 | .Ft u_int64_t | ||
89 | .Fn swap64 "u_int32_t val64" | ||
90 | .Ft u_int32_t | ||
91 | .Fn swap32 "u_int32_t val32" | ||
92 | .Ft u_int16_t | ||
93 | .Fn swap16 "u_int16_t val16" | ||
94 | .Sh DESCRIPTION | ||
95 | These routines convert 16, 32 and 64-bit quantities between different | ||
96 | byte orderings. | ||
97 | The | ||
98 | .Dq swap | ||
99 | functions reverse the byte ordering of | ||
100 | the given quantity, the others converts either from/to the native | ||
101 | byte order used by the host to/from either little- or big-endian (a.k.a | ||
102 | network) order. | ||
103 | .Pp | ||
104 | Apart from the swap functions, the names can be described by this form: | ||
105 | {src-order}to{dst-order}{size}. | ||
106 | Both {src-order} and {dst-order} can take the following forms: | ||
107 | .Pp | ||
108 | .Bl -tag -width "be " -offset indent -compact | ||
109 | .It h | ||
110 | Host order. | ||
111 | .It n | ||
112 | Network order (big-endian). | ||
113 | .It be | ||
114 | Big-endian (most significant byte first). | ||
115 | .It le | ||
116 | Little-endian (least significant byte first). | ||
117 | .El | ||
118 | .Pp | ||
119 | One of the specified orderings must be | ||
120 | .Sq h . | ||
121 | {size} will take these forms: | ||
122 | .Pp | ||
123 | .Bl -tag -width "32 " -offset indent -compact | ||
124 | .It l | ||
125 | Long (32-bit, used in conjunction with forms involving | ||
126 | .Sq n ) . | ||
127 | .It s | ||
128 | Short (16-bit, used in conjunction with forms involving | ||
129 | .Sq n ) . | ||
130 | .It 16 | ||
131 | 16-bit. | ||
132 | .It 32 | ||
133 | 32-bit. | ||
134 | .It 64 | ||
135 | 64-bit. | ||
136 | .El | ||
137 | .Pp | ||
138 | The swap functions are of the form: swap{size}. | ||
139 | .Pp | ||
140 | Names involving | ||
141 | .Sq n | ||
142 | convert quantities between network | ||
143 | byte order and host byte order. | ||
144 | The last letter | ||
145 | .Pf ( Sq s | ||
146 | or | ||
147 | .Sq l ) | ||
148 | is a mnemonic | ||
149 | for the traditional names for such quantities, | ||
150 | .Li short | ||
151 | and | ||
152 | .Li long , | ||
153 | respectively. | ||
154 | Today, the C concept of | ||
155 | .Li short | ||
156 | and | ||
157 | .Li long | ||
158 | integers need not coincide with this traditional misunderstanding. | ||
159 | On machines which have a byte order which is the same as the network | ||
160 | order, routines are defined as null macros. | ||
161 | .Pp | ||
162 | The functions involving either | ||
163 | .Dq be , | ||
164 | .Dq le , | ||
165 | or | ||
166 | .Dq swap | ||
167 | use the numbers | ||
168 | 16, 32 and 64 for specifying the bitwidth of the quantities they operate on. | ||
169 | Currently all supported architectures are either big- or little-endian | ||
170 | so either the | ||
171 | .Dq be | ||
172 | or | ||
173 | .Dq le | ||
174 | variants are implemented as null macros. | ||
175 | .Pp | ||
176 | The routines mentioned above which have either {src-order} or {dst-order} | ||
177 | set to | ||
178 | .Sq n | ||
179 | are most often used in | ||
180 | conjunction with Internet addresses and ports as returned by | ||
181 | .Xr gethostbyname 3 | ||
182 | and | ||
183 | .Xr getservent 3 . | ||
184 | .Sh SEE ALSO | ||
185 | .Xr gethostbyname 3 , | ||
186 | .Xr getservent 3 | ||
187 | .Sh STANDARDS | ||
188 | The | ||
189 | .Fn htonl , | ||
190 | .Fn htons , | ||
191 | .Fn ntohl , | ||
192 | and | ||
193 | .Fn ntohs | ||
194 | functions conform to | ||
195 | .St -p1003.1 . | ||
196 | The other functions are extensions that should not be used when portability | ||
197 | is required. | ||
198 | .Sh HISTORY | ||
199 | The | ||
200 | .Nm byteorder | ||
201 | functions appeared in | ||
202 | .Bx 4.2 . | ||
203 | .Sh BUGS | ||
204 | On the vax, alpha, i386, and some mips architectures, | ||
205 | bytes are handled backwards from most everyone else in the world. | ||
206 | This is not expected to be fixed in the near future. | ||
diff --git a/src/lib/libc/net/ethers.3 b/src/lib/libc/net/ethers.3 new file mode 100644 index 0000000000..0509c121b8 --- /dev/null +++ b/src/lib/libc/net/ethers.3 | |||
@@ -0,0 +1,119 @@ | |||
1 | .\" $OpenBSD: ethers.3,v 1.14 2001/08/06 10:42:26 mpech Exp $ | ||
2 | .\" | ||
3 | .\" Written by roland@frob.com. Public domain. | ||
4 | .\" | ||
5 | .Dd December 16, 1993 | ||
6 | .Dt ETHERS 3 | ||
7 | .Os | ||
8 | .Sh NAME | ||
9 | .Nm ether_aton , | ||
10 | .Nm ether_ntoa , | ||
11 | .Nm ether_addr , | ||
12 | .Nm ether_ntohost , | ||
13 | .Nm ether_hostton , | ||
14 | .Nm ether_line | ||
15 | .Nd get ethers entry | ||
16 | .Sh SYNOPSIS | ||
17 | .Fd #include <netinet/if_ether.h> | ||
18 | .Ft char * | ||
19 | .Fn ether_ntoa "struct ether_addr *e" | ||
20 | .Ft struct ether_addr * | ||
21 | .Fn ether_aton "char *s" | ||
22 | .Ft int | ||
23 | .Fn ether_ntohost "char *hostname" "struct ether_addr *e" | ||
24 | .Ft int | ||
25 | .Fn ether_hostton "char *hostname" "struct ether_addr *e" | ||
26 | .Ft int | ||
27 | .Fn ether_line "char *l" "struct ether_addr *e" "char *hostname" | ||
28 | .Sh DESCRIPTION | ||
29 | Ethernet addresses are represented by the | ||
30 | following structure: | ||
31 | .Bd -literal -offset indent | ||
32 | struct ether_addr { | ||
33 | u_int8_t ether_addr_octet[6]; | ||
34 | }; | ||
35 | .Ed | ||
36 | .Pp | ||
37 | The | ||
38 | .Fn ether_ntoa | ||
39 | function converts this structure into an | ||
40 | .Tn ASCII | ||
41 | string of the form | ||
42 | .Dq xx:xx:xx:xx:xx:xx , | ||
43 | consisting of 6 hexadecimal numbers separated | ||
44 | by colons. | ||
45 | It returns a pointer to a static buffer that is reused for each call. | ||
46 | The | ||
47 | .Fn ether_aton | ||
48 | converts an | ||
49 | .Tn ASCII | ||
50 | string of the same form and to a structure | ||
51 | containing the 6 octets of the address. | ||
52 | It returns a pointer to a static structure that is reused for each call. | ||
53 | .Pp | ||
54 | The | ||
55 | .Fn ether_ntohost | ||
56 | and | ||
57 | .Fn ether_hostton | ||
58 | functions interrogate the database mapping host names to Ethernet | ||
59 | addresses, | ||
60 | .Pa /etc/ethers . | ||
61 | The | ||
62 | .Fn ether_ntohost | ||
63 | function looks up the given Ethernet address and writes the associated | ||
64 | host name into the character buffer passed. | ||
65 | This buffer should be | ||
66 | .Dv MAXHOSTNAMELEN | ||
67 | characters in size. | ||
68 | The | ||
69 | .Fn ether_hostton | ||
70 | function looks up the given host name and writes the associated | ||
71 | Ethernet address into the structure passed. | ||
72 | Both functions return | ||
73 | zero if they find the requested host name or address, and \-1 if not. | ||
74 | .Pp | ||
75 | Each call reads | ||
76 | .Pa /etc/ethers | ||
77 | from the beginning; if a | ||
78 | .Ql + | ||
79 | appears alone on a line in the file, then | ||
80 | .Fn ether_hostton | ||
81 | will consult the | ||
82 | .Pa ethers.byname | ||
83 | YP map, and | ||
84 | .Fn ether_ntohost | ||
85 | will consult the | ||
86 | .Pa ethers.byaddr | ||
87 | YP map. | ||
88 | .Pp | ||
89 | The | ||
90 | .Fn ether_line | ||
91 | function parses a line from the | ||
92 | .Pa /etc/ethers | ||
93 | file and fills in the passed | ||
94 | .Li struct ether_addr | ||
95 | and character buffer with the Ethernet address and host name on the line. | ||
96 | It returns zero if the line was successfully parsed and \-1 if not. | ||
97 | The character buffer should be | ||
98 | .Dv MAXHOSTNAMELEN | ||
99 | characters in size. | ||
100 | .Sh FILES | ||
101 | .Bl -tag -width /etc/ethers -compact | ||
102 | .It Pa /etc/ethers | ||
103 | .El | ||
104 | .Sh SEE ALSO | ||
105 | .Xr ethers 5 | ||
106 | .Sh HISTORY | ||
107 | The | ||
108 | .Fn ether_ntoa , | ||
109 | .Fn ether_aton , | ||
110 | .Fn ether_ntohost , | ||
111 | .Fn ether_hostton , | ||
112 | and | ||
113 | .Fn ether_line | ||
114 | functions were adopted from SunOS and appeared in | ||
115 | .Nx 0.9 b. | ||
116 | .Sh BUGS | ||
117 | The data space used by these functions is static; if future use | ||
118 | requires the data, it should be copied before any subsequent calls to | ||
119 | these functions overwrite it. | ||
diff --git a/src/lib/libc/net/ethers.c b/src/lib/libc/net/ethers.c new file mode 100644 index 0000000000..899022a622 --- /dev/null +++ b/src/lib/libc/net/ethers.c | |||
@@ -0,0 +1,261 @@ | |||
1 | /* $OpenBSD: ethers.c,v 1.16 2003/06/17 21:56:24 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | /* | ||
20 | * ethers(3) a la Sun. | ||
21 | * Originally Written by Roland McGrath <roland@frob.com> 10/14/93. | ||
22 | * Substantially modified by Todd C. Miller <Todd.Miller@courtesan.com> | ||
23 | */ | ||
24 | |||
25 | #if defined(LIBC_SCCS) && !defined(lint) | ||
26 | static char rcsid[] = "$OpenBSD: ethers.c,v 1.16 2003/06/17 21:56:24 millert Exp $"; | ||
27 | #endif /* LIBC_SCCS and not lint */ | ||
28 | |||
29 | #include <sys/types.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if.h> | ||
32 | #include <netinet/in.h> | ||
33 | #include <netinet/if_ether.h> | ||
34 | #include <sys/param.h> | ||
35 | #include <paths.h> | ||
36 | #include <errno.h> | ||
37 | #include <stdio.h> | ||
38 | #include <stdlib.h> | ||
39 | #include <string.h> | ||
40 | #include <ctype.h> | ||
41 | #ifdef YP | ||
42 | #include <rpcsvc/ypclnt.h> | ||
43 | #endif | ||
44 | |||
45 | #ifndef _PATH_ETHERS | ||
46 | #define _PATH_ETHERS "/etc/ethers" | ||
47 | #endif | ||
48 | |||
49 | static char * _ether_aton(char *, struct ether_addr *); | ||
50 | |||
51 | char * | ||
52 | ether_ntoa(e) | ||
53 | struct ether_addr *e; | ||
54 | { | ||
55 | static char a[] = "xx:xx:xx:xx:xx:xx"; | ||
56 | |||
57 | if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || | ||
58 | e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || | ||
59 | e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) { | ||
60 | errno = EINVAL; | ||
61 | return (NULL); | ||
62 | } | ||
63 | |||
64 | (void)snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x", | ||
65 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
66 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
67 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
68 | |||
69 | return (a); | ||
70 | } | ||
71 | |||
72 | static char * | ||
73 | _ether_aton(s, e) | ||
74 | char *s; | ||
75 | struct ether_addr *e; | ||
76 | { | ||
77 | int i; | ||
78 | long l; | ||
79 | char *pp; | ||
80 | |||
81 | while (isspace(*s)) | ||
82 | s++; | ||
83 | |||
84 | /* expect 6 hex octets separated by ':' or space/NUL if last octet */ | ||
85 | for (i = 0; i < 6; i++) { | ||
86 | l = strtol(s, &pp, 16); | ||
87 | if (pp == s || l > 0xFF || l < 0) | ||
88 | return (NULL); | ||
89 | if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0')))) | ||
90 | return (NULL); | ||
91 | e->ether_addr_octet[i] = (u_char)l; | ||
92 | s = pp + 1; | ||
93 | } | ||
94 | |||
95 | /* return character after the octets ala strtol(3) */ | ||
96 | return (pp); | ||
97 | } | ||
98 | |||
99 | struct ether_addr * | ||
100 | ether_aton(s) | ||
101 | char *s; | ||
102 | { | ||
103 | static struct ether_addr n; | ||
104 | |||
105 | return (_ether_aton(s, &n) ? &n : NULL); | ||
106 | } | ||
107 | |||
108 | int | ||
109 | ether_ntohost(hostname, e) | ||
110 | char *hostname; | ||
111 | struct ether_addr *e; | ||
112 | { | ||
113 | FILE *f; | ||
114 | char buf[BUFSIZ+1], *p; | ||
115 | size_t len; | ||
116 | struct ether_addr try; | ||
117 | #ifdef YP | ||
118 | char trybuf[sizeof("xx:xx:xx:xx:xx:xx")]; | ||
119 | int trylen; | ||
120 | #endif | ||
121 | |||
122 | if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || | ||
123 | e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || | ||
124 | e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) { | ||
125 | errno = EINVAL; | ||
126 | return (-1); | ||
127 | } | ||
128 | |||
129 | #ifdef YP | ||
130 | snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x", | ||
131 | e->ether_addr_octet[0], e->ether_addr_octet[1], | ||
132 | e->ether_addr_octet[2], e->ether_addr_octet[3], | ||
133 | e->ether_addr_octet[4], e->ether_addr_octet[5]); | ||
134 | trylen = strlen(trybuf); | ||
135 | #endif | ||
136 | |||
137 | f = fopen(_PATH_ETHERS, "r"); | ||
138 | if (f == NULL) | ||
139 | return (-1); | ||
140 | while ((p = fgetln(f, &len)) != NULL) { | ||
141 | if (p[len-1] == '\n') | ||
142 | len--; | ||
143 | if (len > sizeof(buf) - 2) | ||
144 | continue; | ||
145 | (void)memcpy(buf, p, len); | ||
146 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
147 | buf[len+1] = '\0'; | ||
148 | #ifdef YP | ||
149 | /* A + in the file means try YP now. */ | ||
150 | if (!strncmp(buf, "+\n", sizeof(buf))) { | ||
151 | char *ypbuf, *ypdom; | ||
152 | int ypbuflen; | ||
153 | |||
154 | if (yp_get_default_domain(&ypdom)) | ||
155 | continue; | ||
156 | if (yp_match(ypdom, "ethers.byaddr", trybuf, | ||
157 | trylen, &ypbuf, &ypbuflen)) | ||
158 | continue; | ||
159 | if (ether_line(ypbuf, &try, hostname) == 0) { | ||
160 | free(ypbuf); | ||
161 | (void)fclose(f); | ||
162 | return (0); | ||
163 | } | ||
164 | free(ypbuf); | ||
165 | continue; | ||
166 | } | ||
167 | #endif | ||
168 | if (ether_line(buf, &try, hostname) == 0 && | ||
169 | memcmp((void *)&try, (void *)e, sizeof(try)) == 0) { | ||
170 | (void)fclose(f); | ||
171 | return (0); | ||
172 | } | ||
173 | } | ||
174 | (void)fclose(f); | ||
175 | errno = ENOENT; | ||
176 | return (-1); | ||
177 | } | ||
178 | |||
179 | int | ||
180 | ether_hostton(hostname, e) | ||
181 | char *hostname; | ||
182 | struct ether_addr *e; | ||
183 | { | ||
184 | FILE *f; | ||
185 | char buf[BUFSIZ+1], *p; | ||
186 | char try[MAXHOSTNAMELEN]; | ||
187 | size_t len; | ||
188 | #ifdef YP | ||
189 | int hostlen = strlen(hostname); | ||
190 | #endif | ||
191 | |||
192 | f = fopen(_PATH_ETHERS, "r"); | ||
193 | if (f==NULL) | ||
194 | return (-1); | ||
195 | |||
196 | while ((p = fgetln(f, &len)) != NULL) { | ||
197 | if (p[len-1] == '\n') | ||
198 | len--; | ||
199 | if (len > sizeof(buf) - 2) | ||
200 | continue; | ||
201 | memcpy(buf, p, len); | ||
202 | buf[len] = '\n'; /* code assumes newlines later on */ | ||
203 | buf[len+1] = '\0'; | ||
204 | #ifdef YP | ||
205 | /* A + in the file means try YP now. */ | ||
206 | if (!strncmp(buf, "+\n", sizeof(buf))) { | ||
207 | char *ypbuf, *ypdom; | ||
208 | int ypbuflen; | ||
209 | |||
210 | if (yp_get_default_domain(&ypdom)) | ||
211 | continue; | ||
212 | if (yp_match(ypdom, "ethers.byname", hostname, hostlen, | ||
213 | &ypbuf, &ypbuflen)) | ||
214 | continue; | ||
215 | if (ether_line(ypbuf, e, try) == 0) { | ||
216 | free(ypbuf); | ||
217 | (void)fclose(f); | ||
218 | return (0); | ||
219 | } | ||
220 | free(ypbuf); | ||
221 | continue; | ||
222 | } | ||
223 | #endif | ||
224 | if (ether_line(buf, e, try) == 0 && strcmp(hostname, try) == 0) { | ||
225 | (void)fclose(f); | ||
226 | return (0); | ||
227 | } | ||
228 | } | ||
229 | (void)fclose(f); | ||
230 | errno = ENOENT; | ||
231 | return (-1); | ||
232 | } | ||
233 | |||
234 | int | ||
235 | ether_line(line, e, hostname) | ||
236 | char *line; | ||
237 | struct ether_addr *e; | ||
238 | char *hostname; | ||
239 | { | ||
240 | char *p; | ||
241 | size_t n; | ||
242 | |||
243 | /* Parse "xx:xx:xx:xx:xx:xx" */ | ||
244 | if ((p = _ether_aton(line, e)) == NULL || (*p != ' ' && *p != '\t')) | ||
245 | goto bad; | ||
246 | |||
247 | /* Now get the hostname */ | ||
248 | while (isspace(*p)) | ||
249 | p++; | ||
250 | if (*p == '\0') | ||
251 | goto bad; | ||
252 | n = strcspn(p, " \t\n"); | ||
253 | if (n >= MAXHOSTNAMELEN) | ||
254 | goto bad; | ||
255 | strlcpy(hostname, p, n + 1); | ||
256 | return (0); | ||
257 | |||
258 | bad: | ||
259 | errno = EINVAL; | ||
260 | return (-1); | ||
261 | } | ||
diff --git a/src/lib/libc/net/freeaddrinfo.c b/src/lib/libc/net/freeaddrinfo.c new file mode 100644 index 0000000000..30fbecb805 --- /dev/null +++ b/src/lib/libc/net/freeaddrinfo.c | |||
@@ -0,0 +1,49 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1996, 1997, 1998, 1999, Craig Metz, All rights reserved. | ||
3 | * | ||
4 | * Redistribution and use in source and binary forms, with or without | ||
5 | * modification, are permitted provided that the following conditions | ||
6 | * are met: | ||
7 | * 1. Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * 2. Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * 3. All advertising materials mentioning features or use of this software | ||
13 | * must display the following acknowledgement: | ||
14 | * This product includes software developed by Craig Metz and | ||
15 | * by other contributors. | ||
16 | * 4. Neither the name of the author nor the names of contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #include <stdlib.h> | ||
34 | #include <netdb.h> | ||
35 | |||
36 | void | ||
37 | freeaddrinfo(ai) | ||
38 | struct addrinfo *ai; | ||
39 | { | ||
40 | struct addrinfo *p; | ||
41 | |||
42 | do { | ||
43 | p = ai; | ||
44 | ai = ai->ai_next; | ||
45 | if (p->ai_canonname) | ||
46 | free(p->ai_canonname); | ||
47 | free((void *)p); | ||
48 | } while (ai); | ||
49 | } | ||
diff --git a/src/lib/libc/net/gai_strerror.c b/src/lib/libc/net/gai_strerror.c new file mode 100644 index 0000000000..a191cb5b28 --- /dev/null +++ b/src/lib/libc/net/gai_strerror.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* $OpenBSD: gai_strerror.c,v 1.5 2001/06/05 02:31:34 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1997-1999, Craig Metz, 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 Craig Metz and | ||
17 | * by other contributors. | ||
18 | * 4. Neither the name of the author nor the names of 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 | |||
35 | /* gai_strerror() v1.38 */ | ||
36 | |||
37 | #include <sys/types.h> | ||
38 | #include <netdb.h> | ||
39 | #include <errno.h> | ||
40 | |||
41 | char * | ||
42 | gai_strerror(int errnum) | ||
43 | { | ||
44 | switch (errnum) { | ||
45 | case 0: | ||
46 | return "no error"; | ||
47 | case EAI_BADFLAGS: | ||
48 | return "invalid value for ai_flags"; | ||
49 | case EAI_NONAME: | ||
50 | return "name or service is not known"; | ||
51 | case EAI_AGAIN: | ||
52 | return "temporary failure in name resolution"; | ||
53 | case EAI_FAIL: | ||
54 | return "non-recoverable failure in name resolution"; | ||
55 | case EAI_NODATA: | ||
56 | return "no address associated with name"; | ||
57 | case EAI_FAMILY: | ||
58 | return "ai_family not supported"; | ||
59 | case EAI_SOCKTYPE: | ||
60 | return "ai_socktype not supported"; | ||
61 | case EAI_SERVICE: | ||
62 | return "service not supported for ai_socktype"; | ||
63 | case EAI_ADDRFAMILY: | ||
64 | return "address family for name not supported"; | ||
65 | case EAI_MEMORY: | ||
66 | return "memory allocation failure"; | ||
67 | case EAI_SYSTEM: | ||
68 | return "system error"; | ||
69 | case EAI_BADHINTS: | ||
70 | return "invalid value for hints"; | ||
71 | case EAI_PROTOCOL: | ||
72 | return "resolved protocol is unknown"; | ||
73 | default: | ||
74 | return "unknown/invalid error"; | ||
75 | } | ||
76 | } | ||
diff --git a/src/lib/libc/net/getaddrinfo.3 b/src/lib/libc/net/getaddrinfo.3 new file mode 100644 index 0000000000..a9004e3207 --- /dev/null +++ b/src/lib/libc/net/getaddrinfo.3 | |||
@@ -0,0 +1,577 @@ | |||
1 | .\" $OpenBSD: getaddrinfo.3,v 1.25 2003/08/08 09:26:02 jmc Exp $ | ||
2 | .\" $KAME: getaddrinfo.3,v 1.29 2001/02/12 09:24:45 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1983, 1987, 1991, 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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95 | ||
32 | .\" | ||
33 | .Dd May 25, 1995 | ||
34 | .Dt GETADDRINFO 3 | ||
35 | .Os | ||
36 | .\" | ||
37 | .Sh NAME | ||
38 | .Nm getaddrinfo , | ||
39 | .Nm freeaddrinfo , | ||
40 | .Nm gai_strerror | ||
41 | .Nd nodename-to-address translation in protocol-independent manner | ||
42 | .\" | ||
43 | .Sh SYNOPSIS | ||
44 | .Fd #include <sys/types.h> | ||
45 | .Fd #include <sys/socket.h> | ||
46 | .Fd #include <netdb.h> | ||
47 | .Ft int | ||
48 | .Fn getaddrinfo "const char *nodename" "const char *servname" \ | ||
49 | "const struct addrinfo *hints" "struct addrinfo **res" | ||
50 | .Ft void | ||
51 | .Fn freeaddrinfo "struct addrinfo *ai" | ||
52 | .Ft "char *" | ||
53 | .Fn gai_strerror "int ecode" | ||
54 | .\" | ||
55 | .Sh DESCRIPTION | ||
56 | The | ||
57 | .Fn getaddrinfo | ||
58 | function is defined for protocol-independent nodename-to-address translation. | ||
59 | It performs the functionality of | ||
60 | .Xr gethostbyname 3 | ||
61 | and | ||
62 | .Xr getservbyname 3 , | ||
63 | but in a more sophisticated manner. | ||
64 | .Pp | ||
65 | The | ||
66 | .Li addrinfo | ||
67 | structure is defined as a result of including the | ||
68 | .Aq Pa netdb.h | ||
69 | header: | ||
70 | .Bd -literal | ||
71 | struct addrinfo { | ||
72 | int ai_flags; /* input flags */ | ||
73 | int ai_family; /* protocol family for socket */ | ||
74 | int ai_socktype; /* socket type */ | ||
75 | int ai_protocol; /* protocol for socket */ | ||
76 | socklen_t ai_addrlen; /* length of socket-address */ | ||
77 | struct sockaddr *ai_addr; /* socket-address for socket */ | ||
78 | char *ai_canonname; /* canonical name for service location */ | ||
79 | struct addrinfo *ai_next; /* pointer to next in list */ | ||
80 | }; | ||
81 | .Ed | ||
82 | .Pp | ||
83 | The | ||
84 | .Fa nodename | ||
85 | and | ||
86 | .Fa servname | ||
87 | arguments are pointers to NUL-terminated strings or | ||
88 | .Dv NULL . | ||
89 | One or both of these two arguments must be a non-null pointer. | ||
90 | In the normal client scenario, both the | ||
91 | .Fa nodename | ||
92 | and | ||
93 | .Fa servname | ||
94 | are specified. | ||
95 | In the normal server scenario, only the | ||
96 | .Fa servname | ||
97 | is specified. | ||
98 | A non-null | ||
99 | .Fa nodename | ||
100 | string can be either a node name or a numeric host address string | ||
101 | (i.e., a dotted-decimal IPv4 address or an IPv6 hex address). | ||
102 | A non-null | ||
103 | .Fa servname | ||
104 | string can be either a service name or a decimal port number. | ||
105 | .Pp | ||
106 | The caller can optionally pass an | ||
107 | .Li addrinfo | ||
108 | structure, pointed to by the third argument, | ||
109 | to provide hints concerning the type of socket that the caller supports. | ||
110 | In this | ||
111 | .Fa hints | ||
112 | structure all members other than | ||
113 | .Fa ai_flags , | ||
114 | .Fa ai_family , | ||
115 | .Fa ai_socktype , | ||
116 | and | ||
117 | .Fa ai_protocol | ||
118 | must be zero or a null pointer. | ||
119 | A value of | ||
120 | .Dv PF_UNSPEC | ||
121 | for | ||
122 | .Fa ai_family | ||
123 | means the caller will accept any protocol family. | ||
124 | A value of 0 for | ||
125 | .Fa ai_socktype | ||
126 | means the caller will accept any socket type. | ||
127 | A value of 0 for | ||
128 | .Fa ai_protocol | ||
129 | means the caller will accept any protocol. | ||
130 | For example, if the caller handles only TCP and not UDP, then the | ||
131 | .Fa ai_socktype | ||
132 | member of the hints structure should be set to | ||
133 | .Dv SOCK_STREAM | ||
134 | when | ||
135 | .Fn getaddrinfo | ||
136 | is called. | ||
137 | If the caller handles only IPv4 and not IPv6, then the | ||
138 | .Fa ai_family | ||
139 | member of the | ||
140 | .Fa hints | ||
141 | structure should be set to | ||
142 | .Dv PF_INET | ||
143 | when | ||
144 | .Fn getaddrinfo | ||
145 | is called. | ||
146 | If the third argument to | ||
147 | .Fn getaddrinfo | ||
148 | is a null pointer, this is the same as if the caller had filled in an | ||
149 | .Li addrinfo | ||
150 | structure initialized to zero with | ||
151 | .Fa ai_family | ||
152 | set to | ||
153 | .Dv PF_UNSPEC . | ||
154 | .Pp | ||
155 | Upon successful return a pointer to a linked list of one or more | ||
156 | .Li addrinfo | ||
157 | structures is returned through the final argument. | ||
158 | The caller can process each | ||
159 | .Li addrinfo | ||
160 | structure in this list by following the | ||
161 | .Fa ai_next | ||
162 | pointer, until a null pointer is encountered. | ||
163 | In each returned | ||
164 | .Li addrinfo | ||
165 | structure the three members | ||
166 | .Fa ai_family , | ||
167 | .Fa ai_socktype , | ||
168 | and | ||
169 | .Fa ai_protocol | ||
170 | are the corresponding arguments for a call to the | ||
171 | .Fn socket | ||
172 | function. | ||
173 | In each | ||
174 | .Li addrinfo | ||
175 | structure the | ||
176 | .Fa ai_addr | ||
177 | member points to a filled-in socket address structure whose length is | ||
178 | specified by the | ||
179 | .Fa ai_addrlen | ||
180 | member. | ||
181 | .Pp | ||
182 | If the | ||
183 | .Dv AI_PASSIVE | ||
184 | bit is set in the | ||
185 | .Fa ai_flags | ||
186 | member of the | ||
187 | .Fa hints | ||
188 | structure, then the caller plans to use the returned socket address | ||
189 | structure in a call to | ||
190 | .Fn bind . | ||
191 | In this case, if the | ||
192 | .Fa nodename | ||
193 | argument is a null pointer, then the IP address portion of the socket | ||
194 | address structure will be set to | ||
195 | .Dv INADDR_ANY | ||
196 | for an IPv4 address or | ||
197 | .Dv IN6ADDR_ANY_INIT | ||
198 | for an IPv6 address. | ||
199 | .Pp | ||
200 | If the | ||
201 | .Dv AI_PASSIVE | ||
202 | bit is not set in the | ||
203 | .Fa ai_flags | ||
204 | member of the | ||
205 | .Fa hints | ||
206 | structure, then the returned socket address structure will be ready for a | ||
207 | call to | ||
208 | .Fn connect | ||
209 | .Pq for a connection-oriented protocol | ||
210 | or either | ||
211 | .Fn connect , | ||
212 | .Fn sendto , | ||
213 | or | ||
214 | .Fn sendmsg | ||
215 | .Pq for a connectionless protocol . | ||
216 | In this case, if the | ||
217 | .Fa nodename | ||
218 | argument is a null pointer, then the IP address portion of the | ||
219 | socket address structure will be set to the loopback address. | ||
220 | .Pp | ||
221 | If the | ||
222 | .Dv AI_CANONNAME | ||
223 | bit is set in the | ||
224 | .Fa ai_flags | ||
225 | member of the | ||
226 | .Fa hints | ||
227 | structure, then upon successful return the | ||
228 | .Fa ai_canonname | ||
229 | member of the first | ||
230 | .Li addrinfo | ||
231 | structure in the linked list will point to a NUL-terminated string | ||
232 | containing the canonical name of the specified | ||
233 | .Fa nodename . | ||
234 | .Pp | ||
235 | If the | ||
236 | .Dv AI_NUMERICHOST | ||
237 | bit is set in the | ||
238 | .Fa ai_flags | ||
239 | member of the | ||
240 | .Fa hints | ||
241 | structure, then a non-null | ||
242 | .Fa nodename | ||
243 | string must be a numeric host address string. | ||
244 | Otherwise an error of | ||
245 | .Dv EAI_NONAME | ||
246 | is returned. | ||
247 | This flag prevents any type of name resolution service (e.g., the DNS) | ||
248 | from being called. | ||
249 | .Pp | ||
250 | The arguments to | ||
251 | .Fn getaddrinfo | ||
252 | must sufficiently be consistent and unambiguous. | ||
253 | Here are pitfall cases you may encounter: | ||
254 | .Bl -bullet | ||
255 | .It | ||
256 | .Fn getaddrinfo | ||
257 | will raise an error if members of the | ||
258 | .Fa hints | ||
259 | structure are not consistent. | ||
260 | For example, for internet address families, | ||
261 | .Fn getaddrinfo | ||
262 | will raise an error if you specify | ||
263 | .Dv SOCK_STREAM | ||
264 | to | ||
265 | .Fa ai_socktype | ||
266 | while you specify | ||
267 | .Dv IPPROTO_UDP | ||
268 | to | ||
269 | .Fa ai_protocol . | ||
270 | .It | ||
271 | If you specify a | ||
272 | .Fa servname | ||
273 | which is defined only for certain | ||
274 | .Fa ai_socktype , | ||
275 | .Fn getaddrinfo | ||
276 | will raise an error because the arguments are not consistent. | ||
277 | For example, | ||
278 | .Fn getaddrinfo | ||
279 | will raise an error if you ask for | ||
280 | .Dq Li tftp | ||
281 | service on | ||
282 | .Dv SOCK_STREAM . | ||
283 | .It | ||
284 | For internet address families, if you specify | ||
285 | .Fa servname | ||
286 | while you set | ||
287 | .Fa ai_socktype | ||
288 | to | ||
289 | .Dv SOCK_RAW , | ||
290 | .Fn getaddrinfo | ||
291 | will raise an error, because service names are not defined for the internet | ||
292 | .Dv SOCK_RAW | ||
293 | space. | ||
294 | .It | ||
295 | If you specify a numeric | ||
296 | .Fa servname , | ||
297 | while leaving | ||
298 | .Fa ai_socktype | ||
299 | and | ||
300 | .Fa ai_protocol | ||
301 | unspecified, | ||
302 | .Fn getaddrinfo | ||
303 | will raise an error. | ||
304 | This is because the numeric | ||
305 | .Fa servname | ||
306 | does not identify any socket type, and | ||
307 | .Fn getaddrinfo | ||
308 | is not allowed to glob the argument in such case. | ||
309 | .El | ||
310 | .Pp | ||
311 | All of the information returned by | ||
312 | .Fn getaddrinfo | ||
313 | is dynamically allocated: | ||
314 | the | ||
315 | .Li addrinfo | ||
316 | structures, the socket address structures, and canonical node name | ||
317 | strings pointed to by the addrinfo structures. | ||
318 | To return this information to the system the function | ||
319 | .Fn freeaddrinfo | ||
320 | is called. | ||
321 | The | ||
322 | .Fa addrinfo | ||
323 | structure pointed to by the | ||
324 | .Fa ai argument | ||
325 | is freed, along with any dynamic storage pointed to by the structure. | ||
326 | This operation is repeated until a | ||
327 | .Dv NULL | ||
328 | .Fa ai_next | ||
329 | pointer is encountered. | ||
330 | .Pp | ||
331 | To aid applications in printing error messages based on the | ||
332 | .Dv EAI_xxx | ||
333 | codes returned by | ||
334 | .Fn getaddrinfo , | ||
335 | .Fn gai_strerror | ||
336 | is defined. | ||
337 | The argument is one of the | ||
338 | .Dv EAI_xxx | ||
339 | values defined earlier and the return value points to a string describing | ||
340 | the error. | ||
341 | If the argument is not one of the | ||
342 | .Dv EAI_xxx | ||
343 | values, the function still returns a pointer to a string whose contents | ||
344 | indicate an unknown error. | ||
345 | .\" | ||
346 | .Ss Extension for scoped IPv6 address | ||
347 | The implementation allows experimental numeric IPv6 address notation with | ||
348 | scope identifier. | ||
349 | By appending the percent character and scope identifier to addresses, | ||
350 | you can fill the | ||
351 | .Li sin6_scope_id | ||
352 | field for addresses. | ||
353 | This would make management of scoped address easier, | ||
354 | and allows cut-and-paste input of scoped address. | ||
355 | .Pp | ||
356 | At this moment the code supports only link-local addresses with the format. | ||
357 | Scope identifier is hardcoded to the name of the hardware interface associated | ||
358 | with the link. | ||
359 | .Po | ||
360 | such as | ||
361 | .Li ne0 | ||
362 | .Pc . | ||
363 | An example is | ||
364 | .Dq Li fe80::1%ne0 , | ||
365 | which means | ||
366 | .Do | ||
367 | .Li fe80::1 | ||
368 | on the link associated with the | ||
369 | .Li ne0 | ||
370 | interface | ||
371 | .Dc . | ||
372 | .Pp | ||
373 | The implementation is still very experimental and non-standard. | ||
374 | The current implementation assumes a one-to-one relationship between | ||
375 | the interface and link, which is not necessarily true from the specification. | ||
376 | .\" | ||
377 | .Sh EXAMPLES | ||
378 | The following code tries to connect to | ||
379 | .Dq Li www.kame.net | ||
380 | service | ||
381 | .Dq Li http . | ||
382 | via stream socket. | ||
383 | It loops through all the addresses available, regardless of address family. | ||
384 | If the destination resolves to an IPv4 address, it will use | ||
385 | .Dv AF_INET | ||
386 | socket. | ||
387 | Similarly, if it resolves to IPv6, | ||
388 | .Dv AF_INET6 | ||
389 | socket is used. | ||
390 | Observe that there is no hardcoded reference to a particular address family. | ||
391 | The code works even if | ||
392 | .Nm getaddrinfo | ||
393 | returns addresses that are not IPv4/v6. | ||
394 | .Bd -literal -offset indent | ||
395 | struct addrinfo hints, *res, *res0; | ||
396 | int error; | ||
397 | int s; | ||
398 | const char *cause = NULL; | ||
399 | |||
400 | memset(&hints, 0, sizeof(hints)); | ||
401 | hints.ai_family = PF_UNSPEC; | ||
402 | hints.ai_socktype = SOCK_STREAM; | ||
403 | error = getaddrinfo("www.kame.net", "http", &hints, &res0); | ||
404 | if (error) { | ||
405 | errx(1, "%s", gai_strerror(error)); | ||
406 | /*NOTREACHED*/ | ||
407 | } | ||
408 | s = -1; | ||
409 | for (res = res0; res; res = res->ai_next) { | ||
410 | s = socket(res->ai_family, res->ai_socktype, | ||
411 | res->ai_protocol); | ||
412 | if (s < 0) { | ||
413 | cause = "socket"; | ||
414 | continue; | ||
415 | } | ||
416 | |||
417 | if (connect(s, res->ai_addr, res->ai_addrlen) < 0) { | ||
418 | cause = "connect"; | ||
419 | close(s); | ||
420 | s = -1; | ||
421 | continue; | ||
422 | } | ||
423 | |||
424 | break; /* okay we got one */ | ||
425 | } | ||
426 | if (s < 0) { | ||
427 | err(1, cause); | ||
428 | /*NOTREACHED*/ | ||
429 | } | ||
430 | freeaddrinfo(res0); | ||
431 | .Ed | ||
432 | .Pp | ||
433 | The following example tries to open a wildcard listening socket onto service | ||
434 | .Dq Li http , | ||
435 | for all the address families available. | ||
436 | .Bd -literal -offset indent | ||
437 | struct addrinfo hints, *res, *res0; | ||
438 | int error; | ||
439 | int s[MAXSOCK]; | ||
440 | int nsock; | ||
441 | const char *cause = NULL; | ||
442 | |||
443 | memset(&hints, 0, sizeof(hints)); | ||
444 | hints.ai_family = PF_UNSPEC; | ||
445 | hints.ai_socktype = SOCK_STREAM; | ||
446 | hints.ai_flags = AI_PASSIVE; | ||
447 | error = getaddrinfo(NULL, "http", &hints, &res0); | ||
448 | if (error) { | ||
449 | errx(1, "%s", gai_strerror(error)); | ||
450 | /*NOTREACHED*/ | ||
451 | } | ||
452 | nsock = 0; | ||
453 | for (res = res0; res && nsock < MAXSOCK; res = res->ai_next) { | ||
454 | s[nsock] = socket(res->ai_family, res->ai_socktype, | ||
455 | res->ai_protocol); | ||
456 | if (s[nsock] < 0) { | ||
457 | cause = "socket"; | ||
458 | continue; | ||
459 | } | ||
460 | |||
461 | if (bind(s[nsock], res->ai_addr, res->ai_addrlen) < 0) { | ||
462 | cause = "bind"; | ||
463 | close(s[nsock]); | ||
464 | continue; | ||
465 | } | ||
466 | (void) listen(s[nsock], 5); | ||
467 | |||
468 | nsock++; | ||
469 | } | ||
470 | if (nsock == 0) { | ||
471 | err(1, cause); | ||
472 | /*NOTREACHED*/ | ||
473 | } | ||
474 | freeaddrinfo(res0); | ||
475 | .Ed | ||
476 | .\" | ||
477 | .Sh DIAGNOSTICS | ||
478 | Error return status from | ||
479 | .Fn getaddrinfo | ||
480 | is zero on success and non-zero on errors. | ||
481 | Non-zero error codes are defined in | ||
482 | .Aq Pa netdb.h , | ||
483 | and as follows: | ||
484 | .Pp | ||
485 | .Bl -tag -width EAI_ADDRFAMILY -compact | ||
486 | .It Dv EAI_ADDRFAMILY | ||
487 | Address family for | ||
488 | .Fa nodename | ||
489 | not supported. | ||
490 | .It Dv EAI_AGAIN | ||
491 | Temporary failure in name resolution. | ||
492 | .It Dv EAI_BADFLAGS | ||
493 | Invalid value for | ||
494 | .Fa ai_flags . | ||
495 | .It Dv EAI_FAIL | ||
496 | Non-recoverable failure in name resolution. | ||
497 | .It Dv EAI_FAMILY | ||
498 | .Fa ai_family | ||
499 | not supported. | ||
500 | .It Dv EAI_MEMORY | ||
501 | Memory allocation failure. | ||
502 | .It Dv EAI_NODATA | ||
503 | No address associated with | ||
504 | .Fa nodename . | ||
505 | .It Dv EAI_NONAME | ||
506 | .Fa nodename | ||
507 | nor | ||
508 | .Fa servname | ||
509 | provided, or not known. | ||
510 | .It Dv EAI_SERVICE | ||
511 | .Fa servname | ||
512 | not supported for | ||
513 | .Fa ai_socktype . | ||
514 | .It Dv EAI_SOCKTYPE | ||
515 | .Fa ai_socktype | ||
516 | not supported. | ||
517 | .It Dv EAI_SYSTEM | ||
518 | System error returned in | ||
519 | .Va errno . | ||
520 | .El | ||
521 | .Pp | ||
522 | If called with proper argument, | ||
523 | .Fn gai_strerror | ||
524 | returns a pointer to a string describing the given error code. | ||
525 | If the argument is not one of the | ||
526 | .Dv EAI_xxx | ||
527 | values, the function still returns a pointer to a string whose contents | ||
528 | indicate an unknown error. | ||
529 | .\" | ||
530 | .Sh SEE ALSO | ||
531 | .Xr gethostbyname 3 , | ||
532 | .Xr getnameinfo 3 , | ||
533 | .Xr getservbyname 3 , | ||
534 | .Xr hosts 5 , | ||
535 | .Xr resolv.conf 5 , | ||
536 | .Xr services 5 , | ||
537 | .Xr hostname 7 , | ||
538 | .Xr named 8 | ||
539 | .Rs | ||
540 | .%A R. Gilligan | ||
541 | .%A S. Thomson | ||
542 | .%A J. Bound | ||
543 | .%A W. Stevens | ||
544 | .%T Basic Socket Interface Extensions for IPv6 | ||
545 | .%R RFC 2553 | ||
546 | .%D March 1999 | ||
547 | .Re | ||
548 | .Rs | ||
549 | .%A Tatsuya Jinmei | ||
550 | .%A Atsushi Onoe | ||
551 | .%T "An Extension of Format for IPv6 Scoped Addresses" | ||
552 | .%R internet draft | ||
553 | .%N draft-ietf-ipngwg-scopedaddr-format-02.txt | ||
554 | .%O work in progress material | ||
555 | .Re | ||
556 | .Rs | ||
557 | .%A Craig Metz | ||
558 | .%T Protocol Independence Using the Sockets API | ||
559 | .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference" | ||
560 | .%D June 2000 | ||
561 | .Re | ||
562 | .\" | ||
563 | .Sh STANDARDS | ||
564 | The | ||
565 | .Fn getaddrinfo | ||
566 | function is defined in IEEE POSIX 1003.1g draft specification, | ||
567 | and documented in | ||
568 | .Dq Basic Socket Interface Extensions for IPv6 | ||
569 | .Pq RFC 2553 . | ||
570 | .\" | ||
571 | .Sh HISTORY | ||
572 | The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit. | ||
573 | .\" | ||
574 | .Sh BUGS | ||
575 | The current implementation is not thread-safe. | ||
576 | .Pp | ||
577 | The text was shamelessly copied from RFC 2553. | ||
diff --git a/src/lib/libc/net/getaddrinfo.c b/src/lib/libc/net/getaddrinfo.c new file mode 100644 index 0000000000..eb7bd7b3b1 --- /dev/null +++ b/src/lib/libc/net/getaddrinfo.c | |||
@@ -0,0 +1,1885 @@ | |||
1 | /* $OpenBSD: getaddrinfo.c,v 1.48 2003/07/21 23:17:53 marc Exp $ */ | ||
2 | /* $KAME: getaddrinfo.c,v 1.31 2000/08/31 17:36:43 itojun Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
6 | * 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. Neither the name of the project nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | /* | ||
34 | * Issues to be discussed: | ||
35 | * - Thread safe-ness must be checked. | ||
36 | * - Return values. There are nonstandard return values defined and used | ||
37 | * in the source code. This is because RFC2553 is silent about which error | ||
38 | * code must be returned for which situation. | ||
39 | * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 | ||
40 | * says to use inet_aton() to convert IPv4 numeric to binary (alows | ||
41 | * classful form as a result). | ||
42 | * current code - disallow classful form for IPv4 (due to use of inet_pton). | ||
43 | * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is | ||
44 | * invalid. | ||
45 | * current code - SEGV on freeaddrinfo(NULL) | ||
46 | * Note: | ||
47 | * - We use getipnodebyname() just for thread-safeness. There's no intent | ||
48 | * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to | ||
49 | * getipnodebyname(). | ||
50 | * - The code filters out AFs that are not supported by the kernel, | ||
51 | * when globbing NULL hostname (to loopback, or wildcard). Is it the right | ||
52 | * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG | ||
53 | * in ai_flags? | ||
54 | * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. | ||
55 | * (1) what should we do against numeric hostname (2) what should we do | ||
56 | * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? | ||
57 | * non-loopback address configured? global address configured? | ||
58 | * - To avoid search order issue, we have a big amount of code duplicate | ||
59 | * from gethnamaddr.c and some other places. The issues that there's no | ||
60 | * lower layer function to lookup "IPv4 or IPv6" record. Calling | ||
61 | * gethostbyname2 from getaddrinfo will end up in wrong search order, as | ||
62 | * follows: | ||
63 | * - The code makes use of following calls when asked to resolver with | ||
64 | * ai_family = PF_UNSPEC: | ||
65 | * getipnodebyname(host, AF_INET6); | ||
66 | * getipnodebyname(host, AF_INET); | ||
67 | * This will result in the following queries if the node is configure to | ||
68 | * prefer /etc/hosts than DNS: | ||
69 | * lookup /etc/hosts for IPv6 address | ||
70 | * lookup DNS for IPv6 address | ||
71 | * lookup /etc/hosts for IPv4 address | ||
72 | * lookup DNS for IPv4 address | ||
73 | * which may not meet people's requirement. | ||
74 | * The right thing to happen is to have underlying layer which does | ||
75 | * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. | ||
76 | * This would result in a bit of code duplicate with _dns_ghbyname() and | ||
77 | * friends. | ||
78 | */ | ||
79 | |||
80 | #ifndef INET6 | ||
81 | #define INET6 | ||
82 | #endif | ||
83 | |||
84 | #include <sys/types.h> | ||
85 | #include <sys/param.h> | ||
86 | #include <sys/socket.h> | ||
87 | #include <net/if.h> | ||
88 | #include <netinet/in.h> | ||
89 | #include <arpa/inet.h> | ||
90 | #include <arpa/nameser.h> | ||
91 | #include <netdb.h> | ||
92 | #include <resolv.h> | ||
93 | #include <string.h> | ||
94 | #include <stdlib.h> | ||
95 | #include <stddef.h> | ||
96 | #include <ctype.h> | ||
97 | #include <unistd.h> | ||
98 | #include <stdio.h> | ||
99 | #include <errno.h> | ||
100 | |||
101 | #include <syslog.h> | ||
102 | #include <stdarg.h> | ||
103 | |||
104 | #ifdef YP | ||
105 | #include <rpc/rpc.h> | ||
106 | #include <rpcsvc/yp.h> | ||
107 | #include <rpcsvc/ypclnt.h> | ||
108 | #include "ypinternal.h" | ||
109 | #endif | ||
110 | |||
111 | #include "thread_private.h" | ||
112 | |||
113 | #define SUCCESS 0 | ||
114 | #define ANY 0 | ||
115 | #define YES 1 | ||
116 | #define NO 0 | ||
117 | |||
118 | static const char in_addrany[] = { 0, 0, 0, 0 }; | ||
119 | static const char in6_addrany[] = { | ||
120 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 | ||
121 | }; | ||
122 | static const char in_loopback[] = { 127, 0, 0, 1 }; | ||
123 | static const char in6_loopback[] = { | ||
124 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 | ||
125 | }; | ||
126 | |||
127 | static const struct afd { | ||
128 | int a_af; | ||
129 | int a_addrlen; | ||
130 | int a_socklen; | ||
131 | int a_off; | ||
132 | const char *a_addrany; | ||
133 | const char *a_loopback; | ||
134 | int a_scoped; | ||
135 | } afdl [] = { | ||
136 | #ifdef INET6 | ||
137 | {PF_INET6, sizeof(struct in6_addr), | ||
138 | sizeof(struct sockaddr_in6), | ||
139 | offsetof(struct sockaddr_in6, sin6_addr), | ||
140 | in6_addrany, in6_loopback, 1}, | ||
141 | #endif | ||
142 | {PF_INET, sizeof(struct in_addr), | ||
143 | sizeof(struct sockaddr_in), | ||
144 | offsetof(struct sockaddr_in, sin_addr), | ||
145 | in_addrany, in_loopback, 0}, | ||
146 | {0, 0, 0, 0, NULL, NULL, 0}, | ||
147 | }; | ||
148 | |||
149 | struct explore { | ||
150 | int e_af; | ||
151 | int e_socktype; | ||
152 | int e_protocol; | ||
153 | const char *e_protostr; | ||
154 | int e_wild; | ||
155 | #define WILD_AF(ex) ((ex)->e_wild & 0x01) | ||
156 | #define WILD_SOCKTYPE(ex) ((ex)->e_wild & 0x02) | ||
157 | #define WILD_PROTOCOL(ex) ((ex)->e_wild & 0x04) | ||
158 | }; | ||
159 | |||
160 | static const struct explore explore[] = { | ||
161 | #if 0 | ||
162 | { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, | ||
163 | #endif | ||
164 | #ifdef INET6 | ||
165 | { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, | ||
166 | { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, | ||
167 | { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, | ||
168 | #endif | ||
169 | { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, | ||
170 | { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, | ||
171 | { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, | ||
172 | { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, | ||
173 | { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, | ||
174 | { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, | ||
175 | { -1, 0, 0, NULL, 0 }, | ||
176 | }; | ||
177 | |||
178 | #ifdef INET6 | ||
179 | #define PTON_MAX 16 | ||
180 | #else | ||
181 | #define PTON_MAX 4 | ||
182 | #endif | ||
183 | |||
184 | #define MAXPACKET (64*1024) | ||
185 | |||
186 | typedef union { | ||
187 | HEADER hdr; | ||
188 | u_char buf[MAXPACKET]; | ||
189 | } querybuf; | ||
190 | |||
191 | struct res_target { | ||
192 | struct res_target *next; | ||
193 | const char *name; /* domain name */ | ||
194 | int qclass, qtype; /* class and type of query */ | ||
195 | u_char *answer; /* buffer to put answer */ | ||
196 | int anslen; /* size of answer buffer */ | ||
197 | int n; /* result length */ | ||
198 | }; | ||
199 | |||
200 | static int str_isnumber(const char *); | ||
201 | static int explore_fqdn(const struct addrinfo *, const char *, | ||
202 | const char *, struct addrinfo **); | ||
203 | static int explore_null(const struct addrinfo *, | ||
204 | const char *, struct addrinfo **); | ||
205 | static int explore_numeric(const struct addrinfo *, const char *, | ||
206 | const char *, struct addrinfo **, const char *); | ||
207 | static int explore_numeric_scope(const struct addrinfo *, const char *, | ||
208 | const char *, struct addrinfo **); | ||
209 | static int get_canonname(const struct addrinfo *, | ||
210 | struct addrinfo *, const char *); | ||
211 | static struct addrinfo *get_ai(const struct addrinfo *, | ||
212 | const struct afd *, const char *); | ||
213 | static int get_portmatch(const struct addrinfo *, const char *); | ||
214 | static int get_port(struct addrinfo *, const char *, int); | ||
215 | static const struct afd *find_afd(int); | ||
216 | #if 0 | ||
217 | static int addrconfig(const struct addrinfo *); | ||
218 | #endif | ||
219 | #ifdef INET6 | ||
220 | static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); | ||
221 | #endif | ||
222 | |||
223 | static void _sethtent(void); | ||
224 | static void _endhtent(void); | ||
225 | static struct addrinfo * _gethtent(const char *, const struct addrinfo *); | ||
226 | static struct addrinfo *_files_getaddrinfo(const char *, | ||
227 | const struct addrinfo *); | ||
228 | |||
229 | #ifdef YP | ||
230 | static struct addrinfo *_yphostent(char *, const struct addrinfo *); | ||
231 | static struct addrinfo *_yp_getaddrinfo(const char *, | ||
232 | const struct addrinfo *); | ||
233 | #endif | ||
234 | |||
235 | static struct addrinfo *getanswer(const querybuf *, int, const char *, int, | ||
236 | const struct addrinfo *); | ||
237 | static int res_queryN(const char *, struct res_target *); | ||
238 | static int res_searchN(const char *, struct res_target *); | ||
239 | static int res_querydomainN(const char *, const char *, struct res_target *); | ||
240 | static struct addrinfo *_dns_getaddrinfo(const char *, const struct addrinfo *); | ||
241 | |||
242 | |||
243 | /* XXX macros that make external reference is BAD. */ | ||
244 | |||
245 | #define GET_AI(ai, afd, addr) \ | ||
246 | do { \ | ||
247 | /* external reference: pai, error, and label free */ \ | ||
248 | (ai) = get_ai(pai, (afd), (addr)); \ | ||
249 | if ((ai) == NULL) { \ | ||
250 | error = EAI_MEMORY; \ | ||
251 | goto free; \ | ||
252 | } \ | ||
253 | } while (/*CONSTCOND*/0) | ||
254 | |||
255 | #define GET_PORT(ai, serv) \ | ||
256 | do { \ | ||
257 | /* external reference: error and label free */ \ | ||
258 | error = get_port((ai), (serv), 0); \ | ||
259 | if (error != 0) \ | ||
260 | goto free; \ | ||
261 | } while (/*CONSTCOND*/0) | ||
262 | |||
263 | #define GET_CANONNAME(ai, str) \ | ||
264 | do { \ | ||
265 | /* external reference: pai, error and label free */ \ | ||
266 | error = get_canonname(pai, (ai), (str)); \ | ||
267 | if (error != 0) \ | ||
268 | goto free; \ | ||
269 | } while (/*CONSTCOND*/0) | ||
270 | |||
271 | #define ERR(err) \ | ||
272 | do { \ | ||
273 | /* external reference: error, and label bad */ \ | ||
274 | error = (err); \ | ||
275 | goto bad; \ | ||
276 | /*NOTREACHED*/ \ | ||
277 | } while (/*CONSTCOND*/0) | ||
278 | |||
279 | #define MATCH_FAMILY(x, y, w) \ | ||
280 | ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) | ||
281 | #define MATCH(x, y, w) \ | ||
282 | ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) | ||
283 | |||
284 | static int | ||
285 | str_isnumber(p) | ||
286 | const char *p; | ||
287 | { | ||
288 | char *ep; | ||
289 | |||
290 | if (*p == '\0') | ||
291 | return NO; | ||
292 | ep = NULL; | ||
293 | errno = 0; | ||
294 | (void)strtoul(p, &ep, 10); | ||
295 | if (errno == 0 && ep && *ep == '\0') | ||
296 | return YES; | ||
297 | else | ||
298 | return NO; | ||
299 | } | ||
300 | |||
301 | int | ||
302 | getaddrinfo(hostname, servname, hints, res) | ||
303 | const char *hostname, *servname; | ||
304 | const struct addrinfo *hints; | ||
305 | struct addrinfo **res; | ||
306 | { | ||
307 | struct addrinfo sentinel; | ||
308 | struct addrinfo *cur; | ||
309 | int error = 0; | ||
310 | struct addrinfo ai; | ||
311 | struct addrinfo ai0; | ||
312 | struct addrinfo *pai; | ||
313 | const struct explore *ex; | ||
314 | |||
315 | memset(&sentinel, 0, sizeof(sentinel)); | ||
316 | cur = &sentinel; | ||
317 | pai = &ai; | ||
318 | pai->ai_flags = 0; | ||
319 | pai->ai_family = PF_UNSPEC; | ||
320 | pai->ai_socktype = ANY; | ||
321 | pai->ai_protocol = ANY; | ||
322 | pai->ai_addrlen = 0; | ||
323 | pai->ai_canonname = NULL; | ||
324 | pai->ai_addr = NULL; | ||
325 | pai->ai_next = NULL; | ||
326 | |||
327 | if (hostname == NULL && servname == NULL) | ||
328 | return EAI_NONAME; | ||
329 | if (hints) { | ||
330 | /* error check for hints */ | ||
331 | if (hints->ai_addrlen || hints->ai_canonname || | ||
332 | hints->ai_addr || hints->ai_next) | ||
333 | ERR(EAI_BADHINTS); /* xxx */ | ||
334 | if (hints->ai_flags & ~AI_MASK) | ||
335 | ERR(EAI_BADFLAGS); | ||
336 | switch (hints->ai_family) { | ||
337 | case PF_UNSPEC: | ||
338 | case PF_INET: | ||
339 | #ifdef INET6 | ||
340 | case PF_INET6: | ||
341 | #endif | ||
342 | break; | ||
343 | default: | ||
344 | ERR(EAI_FAMILY); | ||
345 | } | ||
346 | memcpy(pai, hints, sizeof(*pai)); | ||
347 | |||
348 | /* | ||
349 | * if both socktype/protocol are specified, check if they | ||
350 | * are meaningful combination. | ||
351 | */ | ||
352 | if (pai->ai_socktype != ANY && pai->ai_protocol != ANY) { | ||
353 | for (ex = explore; ex->e_af >= 0; ex++) { | ||
354 | if (pai->ai_family != ex->e_af) | ||
355 | continue; | ||
356 | if (ex->e_socktype == ANY) | ||
357 | continue; | ||
358 | if (ex->e_protocol == ANY) | ||
359 | continue; | ||
360 | if (pai->ai_socktype == ex->e_socktype | ||
361 | && pai->ai_protocol != ex->e_protocol) { | ||
362 | ERR(EAI_BADHINTS); | ||
363 | } | ||
364 | } | ||
365 | } | ||
366 | } | ||
367 | |||
368 | /* | ||
369 | * check for special cases. (1) numeric servname is disallowed if | ||
370 | * socktype/protocol are left unspecified. (2) servname is disallowed | ||
371 | * for raw and other inet{,6} sockets. | ||
372 | */ | ||
373 | if (MATCH_FAMILY(pai->ai_family, PF_INET, 1) | ||
374 | #ifdef PF_INET6 | ||
375 | || MATCH_FAMILY(pai->ai_family, PF_INET6, 1) | ||
376 | #endif | ||
377 | ) { | ||
378 | ai0 = *pai; /* backup *pai */ | ||
379 | |||
380 | if (pai->ai_family == PF_UNSPEC) { | ||
381 | #ifdef PF_INET6 | ||
382 | pai->ai_family = PF_INET6; | ||
383 | #else | ||
384 | pai->ai_family = PF_INET; | ||
385 | #endif | ||
386 | } | ||
387 | error = get_portmatch(pai, servname); | ||
388 | if (error) | ||
389 | ERR(error); | ||
390 | |||
391 | *pai = ai0; | ||
392 | } | ||
393 | |||
394 | ai0 = *pai; | ||
395 | |||
396 | /* NULL hostname, or numeric hostname */ | ||
397 | for (ex = explore; ex->e_af >= 0; ex++) { | ||
398 | *pai = ai0; | ||
399 | |||
400 | /* PF_UNSPEC entries are prepared for DNS queries only */ | ||
401 | if (ex->e_af == PF_UNSPEC) | ||
402 | continue; | ||
403 | |||
404 | if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) | ||
405 | continue; | ||
406 | if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) | ||
407 | continue; | ||
408 | if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) | ||
409 | continue; | ||
410 | |||
411 | if (pai->ai_family == PF_UNSPEC) | ||
412 | pai->ai_family = ex->e_af; | ||
413 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) | ||
414 | pai->ai_socktype = ex->e_socktype; | ||
415 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) | ||
416 | pai->ai_protocol = ex->e_protocol; | ||
417 | |||
418 | if (hostname == NULL) | ||
419 | error = explore_null(pai, servname, &cur->ai_next); | ||
420 | else | ||
421 | error = explore_numeric_scope(pai, hostname, servname, | ||
422 | &cur->ai_next); | ||
423 | |||
424 | if (error) | ||
425 | goto free; | ||
426 | |||
427 | while (cur && cur->ai_next) | ||
428 | cur = cur->ai_next; | ||
429 | } | ||
430 | |||
431 | /* | ||
432 | * XXX | ||
433 | * If numreic representation of AF1 can be interpreted as FQDN | ||
434 | * representation of AF2, we need to think again about the code below. | ||
435 | */ | ||
436 | if (sentinel.ai_next) | ||
437 | goto good; | ||
438 | |||
439 | if (hostname == NULL) | ||
440 | ERR(EAI_NODATA); | ||
441 | if (pai->ai_flags & AI_NUMERICHOST) | ||
442 | ERR(EAI_NONAME); | ||
443 | |||
444 | /* | ||
445 | * hostname as alphabetical name. | ||
446 | * we would like to prefer AF_INET6 than AF_INET, so we'll make a | ||
447 | * outer loop by AFs. | ||
448 | */ | ||
449 | for (ex = explore; ex->e_af >= 0; ex++) { | ||
450 | *pai = ai0; | ||
451 | |||
452 | /* require exact match for family field */ | ||
453 | if (pai->ai_family != ex->e_af) | ||
454 | continue; | ||
455 | |||
456 | if (!MATCH(pai->ai_socktype, ex->e_socktype, | ||
457 | WILD_SOCKTYPE(ex))) { | ||
458 | continue; | ||
459 | } | ||
460 | if (!MATCH(pai->ai_protocol, ex->e_protocol, | ||
461 | WILD_PROTOCOL(ex))) { | ||
462 | continue; | ||
463 | } | ||
464 | |||
465 | if (pai->ai_socktype == ANY && ex->e_socktype != ANY) | ||
466 | pai->ai_socktype = ex->e_socktype; | ||
467 | if (pai->ai_protocol == ANY && ex->e_protocol != ANY) | ||
468 | pai->ai_protocol = ex->e_protocol; | ||
469 | |||
470 | error = explore_fqdn(pai, hostname, servname, | ||
471 | &cur->ai_next); | ||
472 | |||
473 | while (cur && cur->ai_next) | ||
474 | cur = cur->ai_next; | ||
475 | } | ||
476 | |||
477 | /* XXX */ | ||
478 | if (sentinel.ai_next) | ||
479 | error = 0; | ||
480 | |||
481 | if (error) | ||
482 | goto free; | ||
483 | if (error == 0) { | ||
484 | if (sentinel.ai_next) { | ||
485 | good: | ||
486 | *res = sentinel.ai_next; | ||
487 | return SUCCESS; | ||
488 | } else | ||
489 | error = EAI_FAIL; | ||
490 | } | ||
491 | free: | ||
492 | bad: | ||
493 | if (sentinel.ai_next) | ||
494 | freeaddrinfo(sentinel.ai_next); | ||
495 | *res = NULL; | ||
496 | return error; | ||
497 | } | ||
498 | |||
499 | /* | ||
500 | * FQDN hostname, DNS lookup | ||
501 | */ | ||
502 | |||
503 | static int | ||
504 | explore_fqdn(pai, hostname, servname, res) | ||
505 | const struct addrinfo *pai; | ||
506 | const char *hostname; | ||
507 | const char *servname; | ||
508 | struct addrinfo **res; | ||
509 | { | ||
510 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
511 | struct addrinfo *result; | ||
512 | struct addrinfo *cur; | ||
513 | int error = 0; | ||
514 | char lookups[MAXDNSLUS]; | ||
515 | int i; | ||
516 | _THREAD_PRIVATE_MUTEX(_explore_mutex); | ||
517 | |||
518 | result = NULL; | ||
519 | |||
520 | #if 0 | ||
521 | /* | ||
522 | * If AI_ADDRCONFIG is specified, check if we are expected to | ||
523 | * return the address family or not. | ||
524 | * XXX does not handle PF_UNSPEC case, should filter final result | ||
525 | */ | ||
526 | if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && !addrconfig(pai)) { | ||
527 | return 0; | ||
528 | } | ||
529 | #endif | ||
530 | |||
531 | /* | ||
532 | * if the servname does not match socktype/protocol, ignore it. | ||
533 | */ | ||
534 | if (get_portmatch(pai, servname) != 0) { | ||
535 | return 0; | ||
536 | } | ||
537 | |||
538 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
539 | strlcpy(lookups, "f", sizeof lookups); | ||
540 | else { | ||
541 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
542 | if (lookups[0] == '\0') | ||
543 | strlcpy(lookups, "bf", sizeof lookups); | ||
544 | } | ||
545 | |||
546 | /* | ||
547 | * The yp/dns/files getaddrinfo functions are not thread safe. | ||
548 | * Protect them with a mutex. | ||
549 | */ | ||
550 | _THREAD_PRIVATE_MUTEX_LOCK(_explore_mutex); | ||
551 | for (i = 0; i < MAXDNSLUS && result == NULL && lookups[i]; i++) { | ||
552 | switch (lookups[i]) { | ||
553 | #ifdef YP | ||
554 | case 'y': | ||
555 | result = _yp_getaddrinfo(hostname, pai); | ||
556 | break; | ||
557 | #endif | ||
558 | case 'b': | ||
559 | result = _dns_getaddrinfo(hostname, pai); | ||
560 | break; | ||
561 | case 'f': | ||
562 | result = _files_getaddrinfo(hostname, pai); | ||
563 | break; | ||
564 | } | ||
565 | } | ||
566 | _THREAD_PRIVATE_MUTEX_UNLOCK(_explore_mutex); | ||
567 | if (result) { | ||
568 | for (cur = result; cur; cur = cur->ai_next) { | ||
569 | GET_PORT(cur, servname); | ||
570 | /* canonname should be filled already */ | ||
571 | } | ||
572 | *res = result; | ||
573 | return 0; | ||
574 | } else { | ||
575 | /* translate error code */ | ||
576 | switch (h_errno) { | ||
577 | case NETDB_SUCCESS: | ||
578 | error = EAI_FAIL; /*XXX strange */ | ||
579 | break; | ||
580 | case HOST_NOT_FOUND: | ||
581 | error = EAI_NODATA; | ||
582 | break; | ||
583 | case TRY_AGAIN: | ||
584 | error = EAI_AGAIN; | ||
585 | break; | ||
586 | case NO_RECOVERY: | ||
587 | error = EAI_FAIL; | ||
588 | break; | ||
589 | case NO_DATA: | ||
590 | #if NO_ADDRESS != NO_DATA | ||
591 | case NO_ADDRESS: | ||
592 | #endif | ||
593 | error = EAI_NODATA; | ||
594 | break; | ||
595 | default: /* unknown ones */ | ||
596 | error = EAI_FAIL; | ||
597 | break; | ||
598 | } | ||
599 | } | ||
600 | |||
601 | free: | ||
602 | if (result) | ||
603 | freeaddrinfo(result); | ||
604 | return error; | ||
605 | } | ||
606 | |||
607 | /* | ||
608 | * hostname == NULL. | ||
609 | * passive socket -> anyaddr (0.0.0.0 or ::) | ||
610 | * non-passive socket -> localhost (127.0.0.1 or ::1) | ||
611 | */ | ||
612 | static int | ||
613 | explore_null(pai, servname, res) | ||
614 | const struct addrinfo *pai; | ||
615 | const char *servname; | ||
616 | struct addrinfo **res; | ||
617 | { | ||
618 | int s; | ||
619 | const struct afd *afd; | ||
620 | struct addrinfo *cur; | ||
621 | struct addrinfo sentinel; | ||
622 | int error; | ||
623 | |||
624 | *res = NULL; | ||
625 | sentinel.ai_next = NULL; | ||
626 | cur = &sentinel; | ||
627 | |||
628 | /* | ||
629 | * filter out AFs that are not supported by the kernel | ||
630 | * XXX errno? | ||
631 | */ | ||
632 | s = socket(pai->ai_family, SOCK_DGRAM, 0); | ||
633 | if (s < 0) { | ||
634 | if (errno != EMFILE) | ||
635 | return 0; | ||
636 | } else | ||
637 | close(s); | ||
638 | |||
639 | /* | ||
640 | * if the servname does not match socktype/protocol, ignore it. | ||
641 | */ | ||
642 | if (get_portmatch(pai, servname) != 0) | ||
643 | return 0; | ||
644 | |||
645 | afd = find_afd(pai->ai_family); | ||
646 | if (afd == NULL) | ||
647 | return 0; | ||
648 | |||
649 | if (pai->ai_flags & AI_PASSIVE) { | ||
650 | GET_AI(cur->ai_next, afd, afd->a_addrany); | ||
651 | /* xxx meaningless? | ||
652 | * GET_CANONNAME(cur->ai_next, "anyaddr"); | ||
653 | */ | ||
654 | GET_PORT(cur->ai_next, servname); | ||
655 | } else { | ||
656 | GET_AI(cur->ai_next, afd, afd->a_loopback); | ||
657 | /* xxx meaningless? | ||
658 | * GET_CANONNAME(cur->ai_next, "localhost"); | ||
659 | */ | ||
660 | GET_PORT(cur->ai_next, servname); | ||
661 | } | ||
662 | cur = cur->ai_next; | ||
663 | |||
664 | *res = sentinel.ai_next; | ||
665 | return 0; | ||
666 | |||
667 | free: | ||
668 | if (sentinel.ai_next) | ||
669 | freeaddrinfo(sentinel.ai_next); | ||
670 | return error; | ||
671 | } | ||
672 | |||
673 | /* | ||
674 | * numeric hostname | ||
675 | */ | ||
676 | static int | ||
677 | explore_numeric(pai, hostname, servname, res, canonname) | ||
678 | const struct addrinfo *pai; | ||
679 | const char *hostname; | ||
680 | const char *servname; | ||
681 | struct addrinfo **res; | ||
682 | const char *canonname; | ||
683 | { | ||
684 | const struct afd *afd; | ||
685 | struct addrinfo *cur; | ||
686 | struct addrinfo sentinel; | ||
687 | int error; | ||
688 | char pton[PTON_MAX]; | ||
689 | |||
690 | *res = NULL; | ||
691 | sentinel.ai_next = NULL; | ||
692 | cur = &sentinel; | ||
693 | |||
694 | /* | ||
695 | * if the servname does not match socktype/protocol, ignore it. | ||
696 | */ | ||
697 | if (get_portmatch(pai, servname) != 0) | ||
698 | return 0; | ||
699 | |||
700 | afd = find_afd(pai->ai_family); | ||
701 | if (afd == NULL) | ||
702 | return 0; | ||
703 | |||
704 | switch (afd->a_af) { | ||
705 | #if 0 /*X/Open spec*/ | ||
706 | case AF_INET: | ||
707 | if (inet_aton(hostname, (struct in_addr *)pton) == 1) { | ||
708 | if (pai->ai_family == afd->a_af || | ||
709 | pai->ai_family == PF_UNSPEC /*?*/) { | ||
710 | GET_AI(cur->ai_next, afd, pton); | ||
711 | GET_PORT(cur->ai_next, servname); | ||
712 | if ((pai->ai_flags & AI_CANONNAME)) { | ||
713 | /* | ||
714 | * Set the numeric address itself as | ||
715 | * the canonical name, based on a | ||
716 | * clarification in rfc2553bis-03. | ||
717 | */ | ||
718 | GET_CANONNAME(cur->ai_next, canonname); | ||
719 | } | ||
720 | while (cur && cur->ai_next) | ||
721 | cur = cur->ai_next; | ||
722 | } else | ||
723 | ERR(EAI_FAMILY); /*xxx*/ | ||
724 | } | ||
725 | break; | ||
726 | #endif | ||
727 | default: | ||
728 | if (inet_pton(afd->a_af, hostname, pton) == 1) { | ||
729 | if (pai->ai_family == afd->a_af || | ||
730 | pai->ai_family == PF_UNSPEC /*?*/) { | ||
731 | GET_AI(cur->ai_next, afd, pton); | ||
732 | GET_PORT(cur->ai_next, servname); | ||
733 | if ((pai->ai_flags & AI_CANONNAME)) { | ||
734 | /* | ||
735 | * Set the numeric address itself as | ||
736 | * the canonical name, based on a | ||
737 | * clarification in rfc2553bis-03. | ||
738 | */ | ||
739 | GET_CANONNAME(cur->ai_next, canonname); | ||
740 | } | ||
741 | while (cur && cur->ai_next) | ||
742 | cur = cur->ai_next; | ||
743 | } else | ||
744 | ERR(EAI_FAMILY); /*xxx*/ | ||
745 | } | ||
746 | break; | ||
747 | } | ||
748 | |||
749 | *res = sentinel.ai_next; | ||
750 | return 0; | ||
751 | |||
752 | free: | ||
753 | bad: | ||
754 | if (sentinel.ai_next) | ||
755 | freeaddrinfo(sentinel.ai_next); | ||
756 | return error; | ||
757 | } | ||
758 | |||
759 | /* | ||
760 | * numeric hostname with scope | ||
761 | */ | ||
762 | static int | ||
763 | explore_numeric_scope(pai, hostname, servname, res) | ||
764 | const struct addrinfo *pai; | ||
765 | const char *hostname; | ||
766 | const char *servname; | ||
767 | struct addrinfo **res; | ||
768 | { | ||
769 | #if !defined(SCOPE_DELIMITER) || !defined(INET6) | ||
770 | return explore_numeric(pai, hostname, servname, res, hostname); | ||
771 | #else | ||
772 | const struct afd *afd; | ||
773 | struct addrinfo *cur; | ||
774 | int error; | ||
775 | char *cp, *hostname2 = NULL, *scope, *addr; | ||
776 | struct sockaddr_in6 *sin6; | ||
777 | |||
778 | /* | ||
779 | * if the servname does not match socktype/protocol, ignore it. | ||
780 | */ | ||
781 | if (get_portmatch(pai, servname) != 0) | ||
782 | return 0; | ||
783 | |||
784 | afd = find_afd(pai->ai_family); | ||
785 | if (afd == NULL) | ||
786 | return 0; | ||
787 | |||
788 | if (!afd->a_scoped) | ||
789 | return explore_numeric(pai, hostname, servname, res, hostname); | ||
790 | |||
791 | cp = strchr(hostname, SCOPE_DELIMITER); | ||
792 | if (cp == NULL) | ||
793 | return explore_numeric(pai, hostname, servname, res, hostname); | ||
794 | |||
795 | /* | ||
796 | * Handle special case of <scoped_address><delimiter><scope id> | ||
797 | */ | ||
798 | hostname2 = strdup(hostname); | ||
799 | if (hostname2 == NULL) | ||
800 | return EAI_MEMORY; | ||
801 | /* terminate at the delimiter */ | ||
802 | hostname2[cp - hostname] = '\0'; | ||
803 | addr = hostname2; | ||
804 | scope = cp + 1; | ||
805 | |||
806 | error = explore_numeric(pai, addr, servname, res, hostname); | ||
807 | if (error == 0) { | ||
808 | u_int32_t scopeid; | ||
809 | |||
810 | for (cur = *res; cur; cur = cur->ai_next) { | ||
811 | if (cur->ai_family != AF_INET6) | ||
812 | continue; | ||
813 | sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; | ||
814 | if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { | ||
815 | free(hostname2); | ||
816 | return(EAI_NODATA); /* XXX: is return OK? */ | ||
817 | } | ||
818 | sin6->sin6_scope_id = scopeid; | ||
819 | } | ||
820 | } | ||
821 | |||
822 | free(hostname2); | ||
823 | |||
824 | return error; | ||
825 | #endif | ||
826 | } | ||
827 | |||
828 | static int | ||
829 | get_canonname(pai, ai, str) | ||
830 | const struct addrinfo *pai; | ||
831 | struct addrinfo *ai; | ||
832 | const char *str; | ||
833 | { | ||
834 | if ((pai->ai_flags & AI_CANONNAME) != 0) { | ||
835 | ai->ai_canonname = strdup(str); | ||
836 | if (ai->ai_canonname == NULL) | ||
837 | return EAI_MEMORY; | ||
838 | } | ||
839 | return 0; | ||
840 | } | ||
841 | |||
842 | static struct addrinfo * | ||
843 | get_ai(pai, afd, addr) | ||
844 | const struct addrinfo *pai; | ||
845 | const struct afd *afd; | ||
846 | const char *addr; | ||
847 | { | ||
848 | char *p; | ||
849 | struct addrinfo *ai; | ||
850 | |||
851 | ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) | ||
852 | + (afd->a_socklen)); | ||
853 | if (ai == NULL) | ||
854 | return NULL; | ||
855 | |||
856 | memcpy(ai, pai, sizeof(struct addrinfo)); | ||
857 | ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); | ||
858 | memset(ai->ai_addr, 0, (size_t)afd->a_socklen); | ||
859 | ai->ai_addr->sa_len = afd->a_socklen; | ||
860 | ai->ai_addrlen = afd->a_socklen; | ||
861 | ai->ai_addr->sa_family = ai->ai_family = afd->a_af; | ||
862 | p = (char *)(void *)(ai->ai_addr); | ||
863 | memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); | ||
864 | return ai; | ||
865 | } | ||
866 | |||
867 | static int | ||
868 | get_portmatch(ai, servname) | ||
869 | const struct addrinfo *ai; | ||
870 | const char *servname; | ||
871 | { | ||
872 | |||
873 | /* get_port does not touch first argument. when matchonly == 1. */ | ||
874 | /* LINTED const cast */ | ||
875 | return get_port((struct addrinfo *)ai, servname, 1); | ||
876 | } | ||
877 | |||
878 | static int | ||
879 | get_port(ai, servname, matchonly) | ||
880 | struct addrinfo *ai; | ||
881 | const char *servname; | ||
882 | int matchonly; | ||
883 | { | ||
884 | const char *proto; | ||
885 | struct servent *sp; | ||
886 | int port; | ||
887 | int allownumeric; | ||
888 | /* mutex is defined in getnameinfo.c */ | ||
889 | extern pthread_mutex_t __THREAD_MUTEX_NAME(serv_mutex); | ||
890 | |||
891 | if (servname == NULL) | ||
892 | return 0; | ||
893 | switch (ai->ai_family) { | ||
894 | case AF_INET: | ||
895 | #ifdef AF_INET6 | ||
896 | case AF_INET6: | ||
897 | #endif | ||
898 | break; | ||
899 | default: | ||
900 | return 0; | ||
901 | } | ||
902 | |||
903 | switch (ai->ai_socktype) { | ||
904 | case SOCK_RAW: | ||
905 | return EAI_SERVICE; | ||
906 | case SOCK_DGRAM: | ||
907 | case SOCK_STREAM: | ||
908 | allownumeric = 1; | ||
909 | break; | ||
910 | case ANY: | ||
911 | allownumeric = 0; | ||
912 | break; | ||
913 | default: | ||
914 | return EAI_SOCKTYPE; | ||
915 | } | ||
916 | |||
917 | if (str_isnumber(servname)) { | ||
918 | if (!allownumeric) | ||
919 | return EAI_SERVICE; | ||
920 | port = atoi(servname); | ||
921 | if (port < 0 || port > 65535) | ||
922 | return EAI_SERVICE; | ||
923 | port = htons(port); | ||
924 | } else { | ||
925 | switch (ai->ai_socktype) { | ||
926 | case SOCK_DGRAM: | ||
927 | proto = "udp"; | ||
928 | break; | ||
929 | case SOCK_STREAM: | ||
930 | proto = "tcp"; | ||
931 | break; | ||
932 | default: | ||
933 | proto = NULL; | ||
934 | break; | ||
935 | } | ||
936 | |||
937 | _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); | ||
938 | sp = getservbyname(servname, proto); | ||
939 | _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); | ||
940 | if (sp == NULL) | ||
941 | return EAI_SERVICE; | ||
942 | port = sp->s_port; | ||
943 | } | ||
944 | |||
945 | if (!matchonly) { | ||
946 | switch (ai->ai_family) { | ||
947 | case AF_INET: | ||
948 | ((struct sockaddr_in *)(void *) | ||
949 | ai->ai_addr)->sin_port = port; | ||
950 | break; | ||
951 | #ifdef INET6 | ||
952 | case AF_INET6: | ||
953 | ((struct sockaddr_in6 *)(void *) | ||
954 | ai->ai_addr)->sin6_port = port; | ||
955 | break; | ||
956 | #endif | ||
957 | } | ||
958 | } | ||
959 | |||
960 | return 0; | ||
961 | } | ||
962 | |||
963 | static const struct afd * | ||
964 | find_afd(af) | ||
965 | int af; | ||
966 | { | ||
967 | const struct afd *afd; | ||
968 | |||
969 | if (af == PF_UNSPEC) | ||
970 | return NULL; | ||
971 | for (afd = afdl; afd->a_af; afd++) { | ||
972 | if (afd->a_af == af) | ||
973 | return afd; | ||
974 | } | ||
975 | return NULL; | ||
976 | } | ||
977 | |||
978 | #if 0 | ||
979 | /* | ||
980 | * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend | ||
981 | * will take care of it. | ||
982 | * the semantics of AI_ADDRCONFIG is not defined well. we are not sure | ||
983 | * if the code is right or not. | ||
984 | */ | ||
985 | static int | ||
986 | addrconfig(pai) | ||
987 | const struct addrinfo *pai; | ||
988 | { | ||
989 | int s; | ||
990 | |||
991 | /* XXX errno */ | ||
992 | s = socket(pai->ai_family, SOCK_DGRAM, 0); | ||
993 | if (s < 0) | ||
994 | return 0; | ||
995 | close(s); | ||
996 | return 1; | ||
997 | } | ||
998 | #endif | ||
999 | |||
1000 | #ifdef INET6 | ||
1001 | /* convert a string to a scope identifier. XXX: IPv6 specific */ | ||
1002 | static int | ||
1003 | ip6_str2scopeid(scope, sin6, scopeid) | ||
1004 | char *scope; | ||
1005 | struct sockaddr_in6 *sin6; | ||
1006 | u_int32_t *scopeid; | ||
1007 | { | ||
1008 | u_long lscopeid; | ||
1009 | struct in6_addr *a6 = &sin6->sin6_addr; | ||
1010 | char *ep; | ||
1011 | |||
1012 | /* empty scopeid portion is invalid */ | ||
1013 | if (*scope == '\0') | ||
1014 | return -1; | ||
1015 | |||
1016 | if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { | ||
1017 | /* | ||
1018 | * We currently assume a one-to-one mapping between links | ||
1019 | * and interfaces, so we simply use interface indices for | ||
1020 | * like-local scopes. | ||
1021 | */ | ||
1022 | *scopeid = if_nametoindex(scope); | ||
1023 | if (*scopeid == 0) | ||
1024 | goto trynumeric; | ||
1025 | return 0; | ||
1026 | } | ||
1027 | |||
1028 | /* still unclear about literal, allow numeric only - placeholder */ | ||
1029 | if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) | ||
1030 | goto trynumeric; | ||
1031 | if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) | ||
1032 | goto trynumeric; | ||
1033 | else | ||
1034 | goto trynumeric; /* global */ | ||
1035 | |||
1036 | /* try to convert to a numeric id as a last resort */ | ||
1037 | trynumeric: | ||
1038 | errno = 0; | ||
1039 | lscopeid = strtoul(scope, &ep, 10); | ||
1040 | *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); | ||
1041 | if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) | ||
1042 | return 0; | ||
1043 | else | ||
1044 | return -1; | ||
1045 | } | ||
1046 | #endif | ||
1047 | |||
1048 | /* code duplicate with gethnamaddr.c */ | ||
1049 | |||
1050 | static const char AskedForGot[] = | ||
1051 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; | ||
1052 | static FILE *hostf = NULL; | ||
1053 | |||
1054 | static struct addrinfo * | ||
1055 | getanswer(answer, anslen, qname, qtype, pai) | ||
1056 | const querybuf *answer; | ||
1057 | int anslen; | ||
1058 | const char *qname; | ||
1059 | int qtype; | ||
1060 | const struct addrinfo *pai; | ||
1061 | { | ||
1062 | struct addrinfo sentinel, *cur; | ||
1063 | struct addrinfo ai; | ||
1064 | const struct afd *afd; | ||
1065 | char *canonname; | ||
1066 | const HEADER *hp; | ||
1067 | const u_char *cp; | ||
1068 | int n; | ||
1069 | const u_char *eom; | ||
1070 | char *bp, *ep; | ||
1071 | int type, class, ancount, qdcount; | ||
1072 | int haveanswer, had_error; | ||
1073 | char tbuf[MAXDNAME]; | ||
1074 | int (*name_ok)(const char *); | ||
1075 | char hostbuf[8*1024]; | ||
1076 | |||
1077 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1078 | cur = &sentinel; | ||
1079 | |||
1080 | canonname = NULL; | ||
1081 | eom = answer->buf + anslen; | ||
1082 | switch (qtype) { | ||
1083 | case T_A: | ||
1084 | case T_AAAA: | ||
1085 | case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ | ||
1086 | name_ok = res_hnok; | ||
1087 | break; | ||
1088 | default: | ||
1089 | return (NULL); /* XXX should be abort() -- but that is illegal */ | ||
1090 | } | ||
1091 | /* | ||
1092 | * find first satisfactory answer | ||
1093 | */ | ||
1094 | hp = &answer->hdr; | ||
1095 | ancount = ntohs(hp->ancount); | ||
1096 | qdcount = ntohs(hp->qdcount); | ||
1097 | bp = hostbuf; | ||
1098 | ep = hostbuf + sizeof hostbuf; | ||
1099 | cp = answer->buf + HFIXEDSZ; | ||
1100 | if (qdcount != 1) { | ||
1101 | h_errno = NO_RECOVERY; | ||
1102 | return (NULL); | ||
1103 | } | ||
1104 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
1105 | if ((n < 0) || !(*name_ok)(bp)) { | ||
1106 | h_errno = NO_RECOVERY; | ||
1107 | return (NULL); | ||
1108 | } | ||
1109 | cp += n + QFIXEDSZ; | ||
1110 | if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { | ||
1111 | /* res_send() has already verified that the query name is the | ||
1112 | * same as the one we sent; this just gets the expanded name | ||
1113 | * (i.e., with the succeeding search-domain tacked on). | ||
1114 | */ | ||
1115 | n = strlen(bp) + 1; /* for the \0 */ | ||
1116 | if (n >= MAXHOSTNAMELEN) { | ||
1117 | h_errno = NO_RECOVERY; | ||
1118 | return (NULL); | ||
1119 | } | ||
1120 | canonname = bp; | ||
1121 | bp += n; | ||
1122 | /* The qname can be abbreviated, but h_name is now absolute. */ | ||
1123 | qname = canonname; | ||
1124 | } | ||
1125 | haveanswer = 0; | ||
1126 | had_error = 0; | ||
1127 | while (ancount-- > 0 && cp < eom && !had_error) { | ||
1128 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
1129 | if ((n < 0) || !(*name_ok)(bp)) { | ||
1130 | had_error++; | ||
1131 | continue; | ||
1132 | } | ||
1133 | cp += n; /* name */ | ||
1134 | type = _getshort(cp); | ||
1135 | cp += INT16SZ; /* type */ | ||
1136 | class = _getshort(cp); | ||
1137 | cp += INT16SZ + INT32SZ; /* class, TTL */ | ||
1138 | n = _getshort(cp); | ||
1139 | cp += INT16SZ; /* len */ | ||
1140 | if (class != C_IN) { | ||
1141 | /* XXX - debug? syslog? */ | ||
1142 | cp += n; | ||
1143 | continue; /* XXX - had_error++ ? */ | ||
1144 | } | ||
1145 | if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && | ||
1146 | type == T_CNAME) { | ||
1147 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | ||
1148 | if ((n < 0) || !(*name_ok)(tbuf)) { | ||
1149 | had_error++; | ||
1150 | continue; | ||
1151 | } | ||
1152 | cp += n; | ||
1153 | /* Get canonical name. */ | ||
1154 | n = strlen(tbuf) + 1; /* for the \0 */ | ||
1155 | if (n > ep - bp || n >= MAXHOSTNAMELEN) { | ||
1156 | had_error++; | ||
1157 | continue; | ||
1158 | } | ||
1159 | strlcpy(bp, tbuf, ep - bp); | ||
1160 | canonname = bp; | ||
1161 | bp += n; | ||
1162 | continue; | ||
1163 | } | ||
1164 | if (qtype == T_ANY) { | ||
1165 | if (!(type == T_A || type == T_AAAA)) { | ||
1166 | cp += n; | ||
1167 | continue; | ||
1168 | } | ||
1169 | } else if (type != qtype) { | ||
1170 | if (type != T_KEY && type != T_SIG) | ||
1171 | syslog(LOG_NOTICE|LOG_AUTH, | ||
1172 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | ||
1173 | qname, p_class(C_IN), p_type(qtype), | ||
1174 | p_type(type)); | ||
1175 | cp += n; | ||
1176 | continue; /* XXX - had_error++ ? */ | ||
1177 | } | ||
1178 | switch (type) { | ||
1179 | case T_A: | ||
1180 | case T_AAAA: | ||
1181 | if (strcasecmp(canonname, bp) != 0) { | ||
1182 | syslog(LOG_NOTICE|LOG_AUTH, | ||
1183 | AskedForGot, canonname, bp); | ||
1184 | cp += n; | ||
1185 | continue; /* XXX - had_error++ ? */ | ||
1186 | } | ||
1187 | if (type == T_A && n != INADDRSZ) { | ||
1188 | cp += n; | ||
1189 | continue; | ||
1190 | } | ||
1191 | if (type == T_AAAA && n != IN6ADDRSZ) { | ||
1192 | cp += n; | ||
1193 | continue; | ||
1194 | } | ||
1195 | if (type == T_AAAA) { | ||
1196 | struct in6_addr in6; | ||
1197 | memcpy(&in6, cp, IN6ADDRSZ); | ||
1198 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { | ||
1199 | cp += n; | ||
1200 | continue; | ||
1201 | } | ||
1202 | } | ||
1203 | if (!haveanswer) { | ||
1204 | int nn; | ||
1205 | |||
1206 | canonname = bp; | ||
1207 | nn = strlen(bp) + 1; /* for the \0 */ | ||
1208 | bp += nn; | ||
1209 | } | ||
1210 | |||
1211 | /* don't overwrite pai */ | ||
1212 | ai = *pai; | ||
1213 | ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; | ||
1214 | afd = find_afd(ai.ai_family); | ||
1215 | if (afd == NULL) { | ||
1216 | cp += n; | ||
1217 | continue; | ||
1218 | } | ||
1219 | cur->ai_next = get_ai(&ai, afd, (const char *)cp); | ||
1220 | if (cur->ai_next == NULL) | ||
1221 | had_error++; | ||
1222 | while (cur && cur->ai_next) | ||
1223 | cur = cur->ai_next; | ||
1224 | cp += n; | ||
1225 | break; | ||
1226 | default: | ||
1227 | abort(); /* XXX abort illegal in library */ | ||
1228 | } | ||
1229 | if (!had_error) | ||
1230 | haveanswer++; | ||
1231 | } | ||
1232 | if (haveanswer) { | ||
1233 | if (!canonname) | ||
1234 | (void)get_canonname(pai, sentinel.ai_next, qname); | ||
1235 | else | ||
1236 | (void)get_canonname(pai, sentinel.ai_next, canonname); | ||
1237 | h_errno = NETDB_SUCCESS; | ||
1238 | return sentinel.ai_next; | ||
1239 | } | ||
1240 | |||
1241 | h_errno = NO_RECOVERY; | ||
1242 | return NULL; | ||
1243 | } | ||
1244 | |||
1245 | /*ARGSUSED*/ | ||
1246 | static struct addrinfo * | ||
1247 | _dns_getaddrinfo(name, pai) | ||
1248 | const char *name; | ||
1249 | const struct addrinfo *pai; | ||
1250 | { | ||
1251 | struct addrinfo *ai; | ||
1252 | querybuf *buf, *buf2; | ||
1253 | struct addrinfo sentinel, *cur; | ||
1254 | struct res_target q, q2; | ||
1255 | |||
1256 | memset(&q, 0, sizeof(q2)); | ||
1257 | memset(&q2, 0, sizeof(q2)); | ||
1258 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1259 | cur = &sentinel; | ||
1260 | |||
1261 | buf = malloc(sizeof(*buf)); | ||
1262 | if (buf == NULL) { | ||
1263 | h_errno = NETDB_INTERNAL; | ||
1264 | return NULL; | ||
1265 | } | ||
1266 | buf2 = malloc(sizeof(*buf2)); | ||
1267 | if (buf2 == NULL) { | ||
1268 | free(buf); | ||
1269 | h_errno = NETDB_INTERNAL; | ||
1270 | return NULL; | ||
1271 | } | ||
1272 | |||
1273 | switch (pai->ai_family) { | ||
1274 | case AF_UNSPEC: | ||
1275 | /* prefer IPv6 */ | ||
1276 | q.qclass = C_IN; | ||
1277 | q.qtype = T_AAAA; | ||
1278 | q.answer = buf->buf; | ||
1279 | q.anslen = sizeof(buf->buf); | ||
1280 | q.next = &q2; | ||
1281 | q2.qclass = C_IN; | ||
1282 | q2.qtype = T_A; | ||
1283 | q2.answer = buf2->buf; | ||
1284 | q2.anslen = sizeof(buf2->buf); | ||
1285 | break; | ||
1286 | case AF_INET: | ||
1287 | q.qclass = C_IN; | ||
1288 | q.qtype = T_A; | ||
1289 | q.answer = buf->buf; | ||
1290 | q.anslen = sizeof(buf->buf); | ||
1291 | break; | ||
1292 | case AF_INET6: | ||
1293 | q.qclass = C_IN; | ||
1294 | q.qtype = T_AAAA; | ||
1295 | q.answer = buf->buf; | ||
1296 | q.anslen = sizeof(buf->buf); | ||
1297 | break; | ||
1298 | default: | ||
1299 | free(buf); | ||
1300 | free(buf2); | ||
1301 | return NULL; | ||
1302 | } | ||
1303 | if (res_searchN(name, &q) < 0) { | ||
1304 | free(buf); | ||
1305 | free(buf2); | ||
1306 | return NULL; | ||
1307 | } | ||
1308 | ai = getanswer(buf, q.n, q.name, q.qtype, pai); | ||
1309 | if (ai) { | ||
1310 | cur->ai_next = ai; | ||
1311 | while (cur && cur->ai_next) | ||
1312 | cur = cur->ai_next; | ||
1313 | } | ||
1314 | if (q.next) { | ||
1315 | ai = getanswer(buf2, q2.n, q2.name, q2.qtype, pai); | ||
1316 | if (ai) | ||
1317 | cur->ai_next = ai; | ||
1318 | } | ||
1319 | free(buf); | ||
1320 | free(buf2); | ||
1321 | return sentinel.ai_next; | ||
1322 | } | ||
1323 | |||
1324 | static FILE *hostf; | ||
1325 | |||
1326 | static void | ||
1327 | _sethtent() | ||
1328 | { | ||
1329 | if (!hostf) | ||
1330 | hostf = fopen(_PATH_HOSTS, "r" ); | ||
1331 | else | ||
1332 | rewind(hostf); | ||
1333 | } | ||
1334 | |||
1335 | static void | ||
1336 | _endhtent() | ||
1337 | { | ||
1338 | if (hostf) { | ||
1339 | (void) fclose(hostf); | ||
1340 | hostf = NULL; | ||
1341 | } | ||
1342 | } | ||
1343 | |||
1344 | static struct addrinfo * | ||
1345 | _gethtent(name, pai) | ||
1346 | const char *name; | ||
1347 | const struct addrinfo *pai; | ||
1348 | { | ||
1349 | char *p; | ||
1350 | char *cp, *tname, *cname; | ||
1351 | struct addrinfo hints, *res0, *res; | ||
1352 | int error; | ||
1353 | const char *addr; | ||
1354 | char hostbuf[8*1024]; | ||
1355 | |||
1356 | if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) | ||
1357 | return (NULL); | ||
1358 | again: | ||
1359 | if (!(p = fgets(hostbuf, sizeof hostbuf, hostf))) | ||
1360 | return (NULL); | ||
1361 | if (*p == '#') | ||
1362 | goto again; | ||
1363 | if (!(cp = strpbrk(p, "#\n"))) | ||
1364 | goto again; | ||
1365 | *cp = '\0'; | ||
1366 | if (!(cp = strpbrk(p, " \t"))) | ||
1367 | goto again; | ||
1368 | *cp++ = '\0'; | ||
1369 | addr = p; | ||
1370 | /* if this is not something we're looking for, skip it. */ | ||
1371 | cname = NULL; | ||
1372 | while (cp && *cp) { | ||
1373 | if (*cp == ' ' || *cp == '\t') { | ||
1374 | cp++; | ||
1375 | continue; | ||
1376 | } | ||
1377 | if (!cname) | ||
1378 | cname = cp; | ||
1379 | tname = cp; | ||
1380 | if ((cp = strpbrk(cp, " \t")) != NULL) | ||
1381 | *cp++ = '\0'; | ||
1382 | if (strcasecmp(name, tname) == 0) | ||
1383 | goto found; | ||
1384 | } | ||
1385 | goto again; | ||
1386 | |||
1387 | found: | ||
1388 | hints = *pai; | ||
1389 | hints.ai_flags = AI_NUMERICHOST; | ||
1390 | error = getaddrinfo(addr, NULL, &hints, &res0); | ||
1391 | if (error) | ||
1392 | goto again; | ||
1393 | for (res = res0; res; res = res->ai_next) { | ||
1394 | /* cover it up */ | ||
1395 | res->ai_flags = pai->ai_flags; | ||
1396 | |||
1397 | if (pai->ai_flags & AI_CANONNAME) { | ||
1398 | if (get_canonname(pai, res, cname) != 0) { | ||
1399 | freeaddrinfo(res0); | ||
1400 | goto again; | ||
1401 | } | ||
1402 | } | ||
1403 | } | ||
1404 | return res0; | ||
1405 | } | ||
1406 | |||
1407 | /*ARGSUSED*/ | ||
1408 | static struct addrinfo * | ||
1409 | _files_getaddrinfo(name, pai) | ||
1410 | const char *name; | ||
1411 | const struct addrinfo *pai; | ||
1412 | { | ||
1413 | struct addrinfo sentinel, *cur; | ||
1414 | struct addrinfo *p; | ||
1415 | |||
1416 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1417 | cur = &sentinel; | ||
1418 | |||
1419 | _sethtent(); | ||
1420 | while ((p = _gethtent(name, pai)) != NULL) { | ||
1421 | cur->ai_next = p; | ||
1422 | while (cur && cur->ai_next) | ||
1423 | cur = cur->ai_next; | ||
1424 | } | ||
1425 | _endhtent(); | ||
1426 | |||
1427 | return sentinel.ai_next; | ||
1428 | } | ||
1429 | |||
1430 | #ifdef YP | ||
1431 | static char *__ypdomain; | ||
1432 | |||
1433 | /*ARGSUSED*/ | ||
1434 | static struct addrinfo * | ||
1435 | _yphostent(line, pai) | ||
1436 | char *line; | ||
1437 | const struct addrinfo *pai; | ||
1438 | { | ||
1439 | struct addrinfo sentinel, *cur; | ||
1440 | struct addrinfo hints, *res, *res0; | ||
1441 | int error; | ||
1442 | char *p = line; | ||
1443 | const char *addr, *canonname; | ||
1444 | char *nextline; | ||
1445 | char *cp; | ||
1446 | |||
1447 | addr = canonname = NULL; | ||
1448 | |||
1449 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1450 | cur = &sentinel; | ||
1451 | |||
1452 | nextline: | ||
1453 | /* terminate line */ | ||
1454 | cp = strchr(p, '\n'); | ||
1455 | if (cp) { | ||
1456 | *cp++ = '\0'; | ||
1457 | nextline = cp; | ||
1458 | } else | ||
1459 | nextline = NULL; | ||
1460 | |||
1461 | cp = strpbrk(p, " \t"); | ||
1462 | if (cp == NULL) { | ||
1463 | if (canonname == NULL) | ||
1464 | return (NULL); | ||
1465 | else | ||
1466 | goto done; | ||
1467 | } | ||
1468 | *cp++ = '\0'; | ||
1469 | |||
1470 | addr = p; | ||
1471 | |||
1472 | while (cp && *cp) { | ||
1473 | if (*cp == ' ' || *cp == '\t') { | ||
1474 | cp++; | ||
1475 | continue; | ||
1476 | } | ||
1477 | if (!canonname) | ||
1478 | canonname = cp; | ||
1479 | if ((cp = strpbrk(cp, " \t")) != NULL) | ||
1480 | *cp++ = '\0'; | ||
1481 | } | ||
1482 | |||
1483 | hints = *pai; | ||
1484 | hints.ai_flags = AI_NUMERICHOST; | ||
1485 | error = getaddrinfo(addr, NULL, &hints, &res0); | ||
1486 | if (error == 0) { | ||
1487 | for (res = res0; res; res = res->ai_next) { | ||
1488 | /* cover it up */ | ||
1489 | res->ai_flags = pai->ai_flags; | ||
1490 | |||
1491 | if (pai->ai_flags & AI_CANONNAME) | ||
1492 | (void)get_canonname(pai, res, canonname); | ||
1493 | } | ||
1494 | } else | ||
1495 | res0 = NULL; | ||
1496 | if (res0) { | ||
1497 | cur->ai_next = res0; | ||
1498 | while (cur && cur->ai_next) | ||
1499 | cur = cur->ai_next; | ||
1500 | } | ||
1501 | |||
1502 | if (nextline) { | ||
1503 | p = nextline; | ||
1504 | goto nextline; | ||
1505 | } | ||
1506 | |||
1507 | done: | ||
1508 | return sentinel.ai_next; | ||
1509 | } | ||
1510 | |||
1511 | /*ARGSUSED*/ | ||
1512 | static struct addrinfo * | ||
1513 | _yp_getaddrinfo(name, pai) | ||
1514 | const char *name; | ||
1515 | const struct addrinfo *pai; | ||
1516 | { | ||
1517 | struct addrinfo sentinel, *cur; | ||
1518 | struct addrinfo *ai = NULL; | ||
1519 | static char *__ypcurrent; | ||
1520 | int __ypcurrentlen, r; | ||
1521 | |||
1522 | memset(&sentinel, 0, sizeof(sentinel)); | ||
1523 | cur = &sentinel; | ||
1524 | |||
1525 | if (!__ypdomain) { | ||
1526 | if (_yp_check(&__ypdomain) == 0) | ||
1527 | return NULL; | ||
1528 | } | ||
1529 | if (__ypcurrent) | ||
1530 | free(__ypcurrent); | ||
1531 | __ypcurrent = NULL; | ||
1532 | |||
1533 | /* hosts.byname is only for IPv4 (Solaris8) */ | ||
1534 | if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { | ||
1535 | r = yp_match(__ypdomain, "hosts.byname", name, | ||
1536 | (int)strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
1537 | if (r == 0) { | ||
1538 | struct addrinfo ai4; | ||
1539 | |||
1540 | ai4 = *pai; | ||
1541 | ai4.ai_family = AF_INET; | ||
1542 | ai = _yphostent(__ypcurrent, &ai4); | ||
1543 | if (ai) { | ||
1544 | cur->ai_next = ai; | ||
1545 | while (cur && cur->ai_next) | ||
1546 | cur = cur->ai_next; | ||
1547 | } | ||
1548 | } | ||
1549 | } | ||
1550 | |||
1551 | /* ipnodes.byname can hold both IPv4/v6 */ | ||
1552 | r = yp_match(__ypdomain, "ipnodes.byname", name, | ||
1553 | (int)strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
1554 | if (r == 0) { | ||
1555 | ai = _yphostent(__ypcurrent, pai); | ||
1556 | if (ai) { | ||
1557 | cur->ai_next = ai; | ||
1558 | while (cur && cur->ai_next) | ||
1559 | cur = cur->ai_next; | ||
1560 | } | ||
1561 | } | ||
1562 | |||
1563 | return sentinel.ai_next; | ||
1564 | } | ||
1565 | #endif | ||
1566 | |||
1567 | |||
1568 | /* resolver logic */ | ||
1569 | |||
1570 | extern const char *__hostalias(const char *); | ||
1571 | extern int h_errno; | ||
1572 | extern int res_opt(int, u_char *, int, int); | ||
1573 | |||
1574 | /* | ||
1575 | * Formulate a normal query, send, and await answer. | ||
1576 | * Returned answer is placed in supplied buffer "answer". | ||
1577 | * Perform preliminary check of answer, returning success only | ||
1578 | * if no error is indicated and the answer count is nonzero. | ||
1579 | * Return the size of the response on success, -1 on error. | ||
1580 | * Error number is left in h_errno. | ||
1581 | * | ||
1582 | * Caller must parse answer and determine whether it answers the question. | ||
1583 | */ | ||
1584 | static int | ||
1585 | res_queryN(name, target) | ||
1586 | const char *name; /* domain name */ | ||
1587 | struct res_target *target; | ||
1588 | { | ||
1589 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
1590 | u_char *buf; | ||
1591 | HEADER *hp; | ||
1592 | int n; | ||
1593 | struct res_target *t; | ||
1594 | int rcode; | ||
1595 | int ancount; | ||
1596 | |||
1597 | buf = malloc(MAXPACKET); | ||
1598 | if (buf == NULL) { | ||
1599 | h_errno = NETDB_INTERNAL; | ||
1600 | return (-1); | ||
1601 | } | ||
1602 | |||
1603 | rcode = NOERROR; | ||
1604 | ancount = 0; | ||
1605 | |||
1606 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
1607 | h_errno = NETDB_INTERNAL; | ||
1608 | free(buf); | ||
1609 | return (-1); | ||
1610 | } | ||
1611 | |||
1612 | for (t = target; t; t = t->next) { | ||
1613 | int class, type; | ||
1614 | u_char *answer; | ||
1615 | int anslen; | ||
1616 | |||
1617 | hp = (HEADER *)(void *)t->answer; | ||
1618 | hp->rcode = NOERROR; /* default */ | ||
1619 | |||
1620 | /* make it easier... */ | ||
1621 | class = t->qclass; | ||
1622 | type = t->qtype; | ||
1623 | answer = t->answer; | ||
1624 | anslen = t->anslen; | ||
1625 | #ifdef DEBUG | ||
1626 | if (_resp->options & RES_DEBUG) | ||
1627 | printf(";; res_query(%s, %d, %d)\n", name, class, type); | ||
1628 | #endif | ||
1629 | |||
1630 | n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, | ||
1631 | buf, MAXPACKET); | ||
1632 | if (n > 0 && (_resp->options & RES_USE_EDNS0) != 0) | ||
1633 | n = res_opt(n, buf, MAXPACKET, anslen); | ||
1634 | if (n <= 0) { | ||
1635 | #ifdef DEBUG | ||
1636 | if (_resp->options & RES_DEBUG) | ||
1637 | printf(";; res_query: mkquery failed\n"); | ||
1638 | #endif | ||
1639 | h_errno = NO_RECOVERY; | ||
1640 | free(buf); | ||
1641 | return (n); | ||
1642 | } | ||
1643 | n = res_send(buf, n, answer, anslen); | ||
1644 | #if 0 | ||
1645 | if (n < 0) { | ||
1646 | #ifdef DEBUG | ||
1647 | if (_resp->options & RES_DEBUG) | ||
1648 | printf(";; res_query: send error\n"); | ||
1649 | #endif | ||
1650 | h_errno = TRY_AGAIN; | ||
1651 | free(buf); | ||
1652 | return (n); | ||
1653 | } | ||
1654 | #endif | ||
1655 | |||
1656 | if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { | ||
1657 | rcode = hp->rcode; /* record most recent error */ | ||
1658 | #ifdef DEBUG | ||
1659 | if (_resp->options & RES_DEBUG) | ||
1660 | printf(";; rcode = %u, ancount=%u\n", hp->rcode, | ||
1661 | ntohs(hp->ancount)); | ||
1662 | #endif | ||
1663 | continue; | ||
1664 | } | ||
1665 | |||
1666 | ancount += ntohs(hp->ancount); | ||
1667 | |||
1668 | t->n = n; | ||
1669 | } | ||
1670 | |||
1671 | if (ancount == 0) { | ||
1672 | switch (rcode) { | ||
1673 | case NXDOMAIN: | ||
1674 | h_errno = HOST_NOT_FOUND; | ||
1675 | break; | ||
1676 | case SERVFAIL: | ||
1677 | h_errno = TRY_AGAIN; | ||
1678 | break; | ||
1679 | case NOERROR: | ||
1680 | h_errno = NO_DATA; | ||
1681 | break; | ||
1682 | case FORMERR: | ||
1683 | case NOTIMP: | ||
1684 | case REFUSED: | ||
1685 | default: | ||
1686 | h_errno = NO_RECOVERY; | ||
1687 | break; | ||
1688 | } | ||
1689 | free(buf); | ||
1690 | return (-1); | ||
1691 | } | ||
1692 | free(buf); | ||
1693 | return (ancount); | ||
1694 | } | ||
1695 | |||
1696 | /* | ||
1697 | * Formulate a normal query, send, and retrieve answer in supplied buffer. | ||
1698 | * Return the size of the response on success, -1 on error. | ||
1699 | * If enabled, implement search rules until answer or unrecoverable failure | ||
1700 | * is detected. Error code, if any, is left in h_errno. | ||
1701 | */ | ||
1702 | static int | ||
1703 | res_searchN(name, target) | ||
1704 | const char *name; /* domain name */ | ||
1705 | struct res_target *target; | ||
1706 | { | ||
1707 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
1708 | const char *cp, * const *domain; | ||
1709 | HEADER *hp = (HEADER *)(void *)target->answer; /*XXX*/ | ||
1710 | u_int dots; | ||
1711 | int trailing_dot, ret, saved_herrno; | ||
1712 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; | ||
1713 | |||
1714 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
1715 | h_errno = NETDB_INTERNAL; | ||
1716 | return (-1); | ||
1717 | } | ||
1718 | |||
1719 | errno = 0; | ||
1720 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ | ||
1721 | dots = 0; | ||
1722 | for (cp = name; *cp; cp++) | ||
1723 | dots += (*cp == '.'); | ||
1724 | trailing_dot = 0; | ||
1725 | if (cp > name && *--cp == '.') | ||
1726 | trailing_dot++; | ||
1727 | |||
1728 | /* | ||
1729 | * if there aren't any dots, it could be a user-level alias | ||
1730 | */ | ||
1731 | if (!dots && (cp = __hostalias(name)) != NULL) | ||
1732 | return (res_queryN(cp, target)); | ||
1733 | |||
1734 | /* | ||
1735 | * If there are dots in the name already, let's just give it a try | ||
1736 | * 'as is'. The threshold can be set with the "ndots" option. | ||
1737 | */ | ||
1738 | saved_herrno = -1; | ||
1739 | if (dots >= _resp->ndots) { | ||
1740 | ret = res_querydomainN(name, NULL, target); | ||
1741 | if (ret > 0) | ||
1742 | return (ret); | ||
1743 | saved_herrno = h_errno; | ||
1744 | tried_as_is++; | ||
1745 | } | ||
1746 | |||
1747 | /* | ||
1748 | * We do at least one level of search if | ||
1749 | * - there is no dot and RES_DEFNAME is set, or | ||
1750 | * - there is at least one dot, there is no trailing dot, | ||
1751 | * and RES_DNSRCH is set. | ||
1752 | */ | ||
1753 | if ((!dots && (_resp->options & RES_DEFNAMES)) || | ||
1754 | (dots && !trailing_dot && (_resp->options & RES_DNSRCH))) { | ||
1755 | int done = 0; | ||
1756 | |||
1757 | for (domain = (const char * const *)_resp->dnsrch; | ||
1758 | *domain && !done; | ||
1759 | domain++) { | ||
1760 | |||
1761 | ret = res_querydomainN(name, *domain, target); | ||
1762 | if (ret > 0) | ||
1763 | return (ret); | ||
1764 | |||
1765 | /* | ||
1766 | * If no server present, give up. | ||
1767 | * If name isn't found in this domain, | ||
1768 | * keep trying higher domains in the search list | ||
1769 | * (if that's enabled). | ||
1770 | * On a NO_DATA error, keep trying, otherwise | ||
1771 | * a wildcard entry of another type could keep us | ||
1772 | * from finding this entry higher in the domain. | ||
1773 | * If we get some other error (negative answer or | ||
1774 | * server failure), then stop searching up, | ||
1775 | * but try the input name below in case it's | ||
1776 | * fully-qualified. | ||
1777 | */ | ||
1778 | if (errno == ECONNREFUSED) { | ||
1779 | h_errno = TRY_AGAIN; | ||
1780 | return (-1); | ||
1781 | } | ||
1782 | |||
1783 | switch (h_errno) { | ||
1784 | case NO_DATA: | ||
1785 | got_nodata++; | ||
1786 | /* FALLTHROUGH */ | ||
1787 | case HOST_NOT_FOUND: | ||
1788 | /* keep trying */ | ||
1789 | break; | ||
1790 | case TRY_AGAIN: | ||
1791 | if (hp->rcode == SERVFAIL) { | ||
1792 | /* try next search element, if any */ | ||
1793 | got_servfail++; | ||
1794 | break; | ||
1795 | } | ||
1796 | /* FALLTHROUGH */ | ||
1797 | default: | ||
1798 | /* anything else implies that we're done */ | ||
1799 | done++; | ||
1800 | } | ||
1801 | /* | ||
1802 | * if we got here for some reason other than DNSRCH, | ||
1803 | * we only wanted one iteration of the loop, so stop. | ||
1804 | */ | ||
1805 | if (!(_resp->options & RES_DNSRCH)) | ||
1806 | done++; | ||
1807 | } | ||
1808 | } | ||
1809 | |||
1810 | /* | ||
1811 | * if we have not already tried the name "as is", do that now. | ||
1812 | * note that we do this regardless of how many dots were in the | ||
1813 | * name or whether it ends with a dot. | ||
1814 | */ | ||
1815 | if (!tried_as_is) { | ||
1816 | ret = res_querydomainN(name, NULL, target); | ||
1817 | if (ret > 0) | ||
1818 | return (ret); | ||
1819 | } | ||
1820 | |||
1821 | /* | ||
1822 | * if we got here, we didn't satisfy the search. | ||
1823 | * if we did an initial full query, return that query's h_errno | ||
1824 | * (note that we wouldn't be here if that query had succeeded). | ||
1825 | * else if we ever got a nodata, send that back as the reason. | ||
1826 | * else send back meaningless h_errno, that being the one from | ||
1827 | * the last DNSRCH we did. | ||
1828 | */ | ||
1829 | if (saved_herrno != -1) | ||
1830 | h_errno = saved_herrno; | ||
1831 | else if (got_nodata) | ||
1832 | h_errno = NO_DATA; | ||
1833 | else if (got_servfail) | ||
1834 | h_errno = TRY_AGAIN; | ||
1835 | return (-1); | ||
1836 | } | ||
1837 | |||
1838 | /* | ||
1839 | * Perform a call on res_query on the concatenation of name and domain, | ||
1840 | * removing a trailing dot from name if domain is NULL. | ||
1841 | */ | ||
1842 | static int | ||
1843 | res_querydomainN(name, domain, target) | ||
1844 | const char *name, *domain; | ||
1845 | struct res_target *target; | ||
1846 | { | ||
1847 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
1848 | char nbuf[MAXDNAME]; | ||
1849 | const char *longname = nbuf; | ||
1850 | size_t n, d; | ||
1851 | |||
1852 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
1853 | h_errno = NETDB_INTERNAL; | ||
1854 | return (-1); | ||
1855 | } | ||
1856 | #ifdef DEBUG | ||
1857 | if (_resp->options & RES_DEBUG) | ||
1858 | printf(";; res_querydomain(%s, %s)\n", | ||
1859 | name, domain?domain:"<Nil>"); | ||
1860 | #endif | ||
1861 | if (domain == NULL) { | ||
1862 | /* | ||
1863 | * Check for trailing '.'; | ||
1864 | * copy without '.' if present. | ||
1865 | */ | ||
1866 | n = strlen(name); | ||
1867 | if (n >= MAXDNAME) { | ||
1868 | h_errno = NO_RECOVERY; | ||
1869 | return (-1); | ||
1870 | } | ||
1871 | if (n > 0 && name[--n] == '.') { | ||
1872 | strlcpy(nbuf, name, n + 1); | ||
1873 | } else | ||
1874 | longname = name; | ||
1875 | } else { | ||
1876 | n = strlen(name); | ||
1877 | d = strlen(domain); | ||
1878 | if (n + d + 1 >= MAXDNAME) { | ||
1879 | h_errno = NO_RECOVERY; | ||
1880 | return (-1); | ||
1881 | } | ||
1882 | snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); | ||
1883 | } | ||
1884 | return (res_queryN(longname, target)); | ||
1885 | } | ||
diff --git a/src/lib/libc/net/gethostbyname.3 b/src/lib/libc/net/gethostbyname.3 new file mode 100644 index 0000000000..1a627f403b --- /dev/null +++ b/src/lib/libc/net/gethostbyname.3 | |||
@@ -0,0 +1,297 @@ | |||
1 | .\" $OpenBSD: gethostbyname.3,v 1.20 2003/06/30 16:54:53 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 1987, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd March 13, 1997 | ||
31 | .Dt GETHOSTBYNAME 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm gethostbyname , | ||
35 | .Nm gethostbyname2 , | ||
36 | .Nm gethostbyaddr , | ||
37 | .Nm gethostent , | ||
38 | .Nm sethostent , | ||
39 | .Nm endhostent , | ||
40 | .Nm hstrerror , | ||
41 | .Nm herror | ||
42 | .Nd get network host entry | ||
43 | .Sh SYNOPSIS | ||
44 | .Fd #include <netdb.h> | ||
45 | .Fd extern int h_errno; | ||
46 | .Ft struct hostent * | ||
47 | .Fn gethostbyname "const char *name" | ||
48 | .Ft struct hostent * | ||
49 | .Fn gethostbyname2 "const char *name" "int af" | ||
50 | .Ft struct hostent * | ||
51 | .Fn gethostbyaddr "const char *addr" "int len" "int af" | ||
52 | .Ft struct hostent * | ||
53 | .Fn gethostent void | ||
54 | .Ft void | ||
55 | .Fn sethostent "int stayopen" | ||
56 | .Ft void | ||
57 | .Fn endhostent void | ||
58 | .Ft void | ||
59 | .Fn herror "const char *string" | ||
60 | .Ft const char * | ||
61 | .Fn hstrerror "int err" | ||
62 | .Sh DESCRIPTION | ||
63 | The | ||
64 | .Fn gethostbyname , | ||
65 | .Fn gethostbyname2 | ||
66 | and | ||
67 | .Fn gethostbyaddr | ||
68 | functions each return a pointer to an object with the following structure | ||
69 | describing an internet host referenced by name or by address, respectively. | ||
70 | This structure contains either information obtained from the name server (i.e., | ||
71 | .Xr resolver 3 | ||
72 | and | ||
73 | .Xr named 8 ) , | ||
74 | broken-out fields from a line in | ||
75 | .Pa /etc/hosts , | ||
76 | or database entries supplied by the | ||
77 | .Xr yp 8 | ||
78 | system. | ||
79 | .Xr resolv.conf 5 | ||
80 | describes how the particular database is chosen. | ||
81 | .Bd -literal | ||
82 | struct hostent { | ||
83 | char *h_name; /* official name of host */ | ||
84 | char **h_aliases; /* alias list */ | ||
85 | int h_addrtype; /* host address type */ | ||
86 | int h_length; /* length of address */ | ||
87 | char **h_addr_list; /* list of addresses from name server */ | ||
88 | }; | ||
89 | #define h_addr h_addr_list[0] /* address, for backward compatibility */ | ||
90 | .Ed | ||
91 | .Pp | ||
92 | The members of this structure are: | ||
93 | .Bl -tag -width h_addr_list | ||
94 | .It Fa h_name | ||
95 | Official name of the host. | ||
96 | .It Fa h_aliases | ||
97 | A NULL-terminated array of alternate names for the host. | ||
98 | .It Fa h_addrtype | ||
99 | The type of address being returned. | ||
100 | .It Fa h_length | ||
101 | The length, in bytes, of the address. | ||
102 | .It Fa h_addr_list | ||
103 | A zero-terminated array of network addresses for the host. | ||
104 | Host addresses are returned in network byte order. | ||
105 | .It Fa h_addr | ||
106 | The first address in | ||
107 | .Fa h_addr_list ; | ||
108 | this is for backward compatibility. | ||
109 | .El | ||
110 | .Pp | ||
111 | The function | ||
112 | .Fn gethostbyname | ||
113 | will search for the named host in the current domain and its parents | ||
114 | using the search lookup semantics detailed in | ||
115 | .Xr resolv.conf 5 | ||
116 | and | ||
117 | .Xr hostname 7 . | ||
118 | .Pp | ||
119 | .Fn gethostbyname2 | ||
120 | is an advanced form of | ||
121 | .Fn gethostbyname | ||
122 | which allows lookups in address families other than | ||
123 | .Dv AF_INET . | ||
124 | Currently, the only supported address family besides | ||
125 | .Dv AF_INET | ||
126 | is | ||
127 | .Dv AF_INET6 . | ||
128 | .Pp | ||
129 | The | ||
130 | .Fn gethostbyaddr | ||
131 | function will search for the specified address of length | ||
132 | .Fa len | ||
133 | in the address family | ||
134 | .Fa af . | ||
135 | The only address family currently supported is | ||
136 | .Dv AF_INET . | ||
137 | .Pp | ||
138 | The | ||
139 | .Fn sethostent | ||
140 | function may be used to request the use of a connected | ||
141 | .Tn TCP | ||
142 | socket for queries. | ||
143 | If the | ||
144 | .Fa stayopen | ||
145 | flag is non-zero, | ||
146 | this sets the option to send all queries to the name server using | ||
147 | .Tn TCP | ||
148 | and to retain the connection after each call to | ||
149 | .Fn gethostbyname | ||
150 | or | ||
151 | .Fn gethostbyaddr . | ||
152 | Otherwise, queries are performed using | ||
153 | .Tn UDP | ||
154 | datagrams. | ||
155 | .Pp | ||
156 | The | ||
157 | .Fn endhostent | ||
158 | function closes the | ||
159 | .Tn TCP | ||
160 | connection. | ||
161 | .Pp | ||
162 | The | ||
163 | .Fn herror | ||
164 | function prints an error message describing the failure. | ||
165 | If its argument | ||
166 | .Fa string | ||
167 | is non-null, | ||
168 | it is prepended to the message string and separated from it by a colon | ||
169 | .Pq Ql \&: | ||
170 | and a space. | ||
171 | The error message is printed with a trailing newline. | ||
172 | The contents of the error message is the same as that returned by | ||
173 | .Fn hstrerror | ||
174 | with argument | ||
175 | .Fa h_errno . | ||
176 | .Sh ENVIRONMENT | ||
177 | .Bl -tag -width HOSTALIASES | ||
178 | .It HOSTALIASES | ||
179 | A file containing local host aliases. | ||
180 | See | ||
181 | .Xr hostname 7 | ||
182 | for more information. | ||
183 | .It RES_OPTIONS | ||
184 | A list of options to override the resolver's internal defaults. | ||
185 | See | ||
186 | .Xr resolver 3 | ||
187 | for more information. | ||
188 | .El | ||
189 | .Sh FILES | ||
190 | .Bl -tag -width /etc/resolv.conf -compact | ||
191 | .It Pa /etc/hosts | ||
192 | .It Pa /etc/resolv.conf | ||
193 | .El | ||
194 | .Sh DIAGNOSTICS | ||
195 | Error return status from | ||
196 | .Fn gethostbyname , | ||
197 | .Fn gethostbyname2 , | ||
198 | and | ||
199 | .Fn gethostbyaddr | ||
200 | is indicated by return of a null pointer. | ||
201 | The external integer | ||
202 | .Va h_errno | ||
203 | may then be checked to see whether this is a temporary failure | ||
204 | or an invalid or unknown host. | ||
205 | .Pp | ||
206 | The variable | ||
207 | .Va h_errno | ||
208 | can have the following values: | ||
209 | .Bl -tag -width HOST_NOT_FOUND | ||
210 | .It Dv HOST_NOT_FOUND | ||
211 | No such host is known. | ||
212 | .It Dv TRY_AGAIN | ||
213 | This is usually a temporary error | ||
214 | and means that the local server did not receive | ||
215 | a response from an authoritative server. | ||
216 | A retry at some later time may succeed. | ||
217 | .It Dv NO_RECOVERY | ||
218 | Some unexpected server failure was encountered. | ||
219 | This is a non-recoverable error. | ||
220 | .It Dv NO_DATA | ||
221 | The requested name is valid but does not have an IP address; | ||
222 | this is not a temporary error. | ||
223 | This means that the name is known to the name server but there is no address | ||
224 | associated with this name. | ||
225 | Another type of request to the name server using this domain name | ||
226 | will result in an answer; | ||
227 | for example, a mail-forwarder may be registered for this domain. | ||
228 | .It Dv NETDB_INTERNAL | ||
229 | An internal error occurred. | ||
230 | This may occurs when an address family other than | ||
231 | .It Dv AF_INET | ||
232 | or | ||
233 | .It Dv AF_INET6 | ||
234 | is specified or when a resource is unable to be allocated. | ||
235 | the | ||
236 | .It Dv NETDB_SUCCESS | ||
237 | The function completed successfully. | ||
238 | .El | ||
239 | .Sh SEE ALSO | ||
240 | .Xr getaddrinfo 3 , | ||
241 | .Xr getnameinfo 3 , | ||
242 | .Xr resolver 3 , | ||
243 | .Xr hosts 5 , | ||
244 | .Xr resolv.conf 5 , | ||
245 | .Xr hostname 7 , | ||
246 | .Xr named 8 | ||
247 | .Sh HISTORY | ||
248 | The | ||
249 | .Fn herror | ||
250 | function appeared in | ||
251 | .Bx 4.3 . | ||
252 | The | ||
253 | .Fn endhostent , | ||
254 | .Fn gethostbyaddr , | ||
255 | .Fn gethostbyname , | ||
256 | .Fn gethostent , | ||
257 | and | ||
258 | .Fn sethostent | ||
259 | functions appeared in | ||
260 | .Bx 4.2 . | ||
261 | .Sh CAVEATS | ||
262 | If the search routines in | ||
263 | .Xr resolv.conf 5 | ||
264 | decide to read the | ||
265 | .Pa /etc/hosts | ||
266 | file, | ||
267 | .Fn gethostent | ||
268 | and other functions will | ||
269 | read the next line of the file, | ||
270 | re-opening the file if necessary. | ||
271 | .Pp | ||
272 | The | ||
273 | .Fn sethostent | ||
274 | function opens and/or rewinds the file | ||
275 | .Pa /etc/hosts . | ||
276 | If the | ||
277 | .Fa stayopen | ||
278 | argument is non-zero, the file will not be closed after each call to | ||
279 | .Fn gethostbyname , | ||
280 | .Fn gethostbyname2 , | ||
281 | or | ||
282 | .Fn gethostbyaddr . | ||
283 | .Pp | ||
284 | The | ||
285 | .Fn endhostent | ||
286 | function closes the file. | ||
287 | .Sh BUGS | ||
288 | These functions use static data storage; | ||
289 | if the data is needed for future use, it should be | ||
290 | copied before any subsequent calls overwrite it. | ||
291 | Only the Internet | ||
292 | address formats are currently understood. | ||
293 | .Pp | ||
294 | YP does not support any address families other than | ||
295 | .Dv AF_INET | ||
296 | and uses | ||
297 | the traditional database format. | ||
diff --git a/src/lib/libc/net/gethostnamadr.c b/src/lib/libc/net/gethostnamadr.c new file mode 100644 index 0000000000..ee4af36191 --- /dev/null +++ b/src/lib/libc/net/gethostnamadr.c | |||
@@ -0,0 +1,1148 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1985, 1988, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | * - | ||
29 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
30 | * | ||
31 | * Permission to use, copy, modify, and distribute this software for any | ||
32 | * purpose with or without fee is hereby granted, provided that the above | ||
33 | * copyright notice and this permission notice appear in all copies, and that | ||
34 | * the name of Digital Equipment Corporation not be used in advertising or | ||
35 | * publicity pertaining to distribution of the document or software without | ||
36 | * specific, written prior permission. | ||
37 | * | ||
38 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
39 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
40 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
41 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
42 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
43 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
44 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
45 | * SOFTWARE. | ||
46 | * - | ||
47 | * --Copyright-- | ||
48 | */ | ||
49 | |||
50 | #if defined(LIBC_SCCS) && !defined(lint) | ||
51 | static const char rcsid[] = "$OpenBSD: gethostnamadr.c,v 1.59 2003/10/06 19:18:09 millert Exp $"; | ||
52 | #endif /* LIBC_SCCS and not lint */ | ||
53 | |||
54 | #include <sys/param.h> | ||
55 | #include <sys/socket.h> | ||
56 | #include <netinet/in.h> | ||
57 | #include <arpa/inet.h> | ||
58 | #include <arpa/nameser.h> | ||
59 | #include <netdb.h> | ||
60 | #include <resolv.h> | ||
61 | #include <stdio.h> | ||
62 | #include <ctype.h> | ||
63 | #include <errno.h> | ||
64 | #include <string.h> | ||
65 | #include <syslog.h> | ||
66 | #include <stdlib.h> | ||
67 | #ifdef YP | ||
68 | #include <rpc/rpc.h> | ||
69 | #include <rpcsvc/yp.h> | ||
70 | #include <rpcsvc/ypclnt.h> | ||
71 | #include "ypinternal.h" | ||
72 | #endif | ||
73 | #include "thread_private.h" | ||
74 | |||
75 | #define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ | ||
76 | |||
77 | #define MAXALIASES 35 | ||
78 | #define MAXADDRS 35 | ||
79 | |||
80 | static char *h_addr_ptrs[MAXADDRS + 1]; | ||
81 | |||
82 | #ifdef YP | ||
83 | static char *__ypdomain; | ||
84 | #endif | ||
85 | |||
86 | static struct hostent host; | ||
87 | static char *host_aliases[MAXALIASES]; | ||
88 | static char hostbuf[BUFSIZ+1]; | ||
89 | static union { | ||
90 | struct in_addr _host_in_addr; | ||
91 | u_char _host_addr[16]; /* IPv4 or IPv6 */ | ||
92 | } _host_addr_u; | ||
93 | #define host_addr _host_addr_u._host_addr | ||
94 | static FILE *hostf = NULL; | ||
95 | static int stayopen = 0; | ||
96 | |||
97 | static void map_v4v6_address(const char *src, char *dst); | ||
98 | static void map_v4v6_hostent(struct hostent *hp, char **bp, char *); | ||
99 | |||
100 | #ifdef RESOLVSORT | ||
101 | static void addrsort(char **, int); | ||
102 | #endif | ||
103 | |||
104 | int _hokchar(const char *); | ||
105 | |||
106 | static const char AskedForGot[] = | ||
107 | "gethostby*.getanswer: asked for \"%s\", got \"%s\""; | ||
108 | |||
109 | #define MAXPACKET (64*1024) | ||
110 | |||
111 | typedef union { | ||
112 | HEADER hdr; | ||
113 | u_char buf[MAXPACKET]; | ||
114 | } querybuf; | ||
115 | |||
116 | typedef union { | ||
117 | int32_t al; | ||
118 | char ac; | ||
119 | } align; | ||
120 | |||
121 | static struct hostent *getanswer(const querybuf *, int, const char *, int); | ||
122 | |||
123 | extern int h_errno; | ||
124 | |||
125 | int | ||
126 | _hokchar(const char *p) | ||
127 | { | ||
128 | char c; | ||
129 | |||
130 | /* | ||
131 | * Many people do not obey RFC 822 and 1035. The valid | ||
132 | * characters are a-z, A-Z, 0-9, '-' and . But the others | ||
133 | * tested for below can happen, and we must be more permissive | ||
134 | * than the resolver until those idiots clean up their act. | ||
135 | * We let '/' through, but not '..' | ||
136 | */ | ||
137 | while ((c = *p++)) { | ||
138 | if (('a' <= c && c <= 'z') || | ||
139 | ('A' <= c && c <= 'Z') || | ||
140 | ('0' <= c && c <= '9')) | ||
141 | continue; | ||
142 | if (strchr("-_/", c)) | ||
143 | continue; | ||
144 | if (c == '.' && *p != '.') | ||
145 | continue; | ||
146 | return 0; | ||
147 | } | ||
148 | return 1; | ||
149 | } | ||
150 | |||
151 | static struct hostent * | ||
152 | getanswer(const querybuf *answer, int anslen, const char *qname, int qtype) | ||
153 | { | ||
154 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
155 | const HEADER *hp; | ||
156 | const u_char *cp, *eom; | ||
157 | char tbuf[MAXDNAME]; | ||
158 | char *bp, **ap, **hap, *ep; | ||
159 | int type, class, ancount, qdcount, n; | ||
160 | int haveanswer, had_error, toobig = 0; | ||
161 | const char *tname; | ||
162 | int (*name_ok)(const char *); | ||
163 | |||
164 | tname = qname; | ||
165 | host.h_name = NULL; | ||
166 | eom = answer->buf + anslen; | ||
167 | switch (qtype) { | ||
168 | case T_A: | ||
169 | case T_AAAA: | ||
170 | #ifdef USE_RESOLV_NAME_OK | ||
171 | name_ok = res_hnok; | ||
172 | break; | ||
173 | #endif | ||
174 | case T_PTR: | ||
175 | #ifdef USE_RESOLV_NAME_OK | ||
176 | name_ok = res_dnok; | ||
177 | #else | ||
178 | name_ok = _hokchar; | ||
179 | #endif | ||
180 | break; | ||
181 | default: | ||
182 | return (NULL); | ||
183 | } | ||
184 | /* | ||
185 | * find first satisfactory answer | ||
186 | */ | ||
187 | hp = &answer->hdr; | ||
188 | ancount = ntohs(hp->ancount); | ||
189 | qdcount = ntohs(hp->qdcount); | ||
190 | bp = hostbuf; | ||
191 | ep = hostbuf + sizeof hostbuf; | ||
192 | cp = answer->buf + HFIXEDSZ; | ||
193 | if (qdcount != 1) { | ||
194 | h_errno = NO_RECOVERY; | ||
195 | return (NULL); | ||
196 | } | ||
197 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
198 | if ((n < 0) || !(*name_ok)(bp)) { | ||
199 | h_errno = NO_RECOVERY; | ||
200 | return (NULL); | ||
201 | } | ||
202 | cp += n + QFIXEDSZ; | ||
203 | if (qtype == T_A || qtype == T_AAAA) { | ||
204 | /* res_send() has already verified that the query name is the | ||
205 | * same as the one we sent; this just gets the expanded name | ||
206 | * (i.e., with the succeeding search-domain tacked on). | ||
207 | */ | ||
208 | n = strlen(bp) + 1; /* for the \0 */ | ||
209 | host.h_name = bp; | ||
210 | bp += n; | ||
211 | /* The qname can be abbreviated, but h_name is now absolute. */ | ||
212 | qname = host.h_name; | ||
213 | } | ||
214 | ap = host_aliases; | ||
215 | *ap = NULL; | ||
216 | host.h_aliases = host_aliases; | ||
217 | hap = h_addr_ptrs; | ||
218 | *hap = NULL; | ||
219 | host.h_addr_list = h_addr_ptrs; | ||
220 | haveanswer = 0; | ||
221 | had_error = 0; | ||
222 | while (ancount-- > 0 && cp < eom && !had_error) { | ||
223 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
224 | if ((n < 0) || !(*name_ok)(bp)) { | ||
225 | had_error++; | ||
226 | continue; | ||
227 | } | ||
228 | cp += n; /* name */ | ||
229 | if (cp >= eom) | ||
230 | break; | ||
231 | type = _getshort(cp); | ||
232 | cp += INT16SZ; /* type */ | ||
233 | if (cp >= eom) | ||
234 | break; | ||
235 | class = _getshort(cp); | ||
236 | cp += INT16SZ + INT32SZ; /* class, TTL */ | ||
237 | if (cp >= eom) | ||
238 | break; | ||
239 | n = _getshort(cp); | ||
240 | cp += INT16SZ; /* len */ | ||
241 | if (cp >= eom) | ||
242 | break; | ||
243 | if (type == T_SIG) { | ||
244 | /* XXX - ignore signatures as we don't use them yet */ | ||
245 | cp += n; | ||
246 | continue; | ||
247 | } | ||
248 | if (class != C_IN) { | ||
249 | /* XXX - debug? syslog? */ | ||
250 | cp += n; | ||
251 | continue; /* XXX - had_error++ ? */ | ||
252 | } | ||
253 | if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { | ||
254 | if (ap >= &host_aliases[MAXALIASES-1]) | ||
255 | continue; | ||
256 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | ||
257 | if ((n < 0) || !(*name_ok)(tbuf)) { | ||
258 | had_error++; | ||
259 | continue; | ||
260 | } | ||
261 | cp += n; | ||
262 | /* Store alias. */ | ||
263 | *ap++ = bp; | ||
264 | n = strlen(bp) + 1; /* for the \0 */ | ||
265 | bp += n; | ||
266 | /* Get canonical name. */ | ||
267 | n = strlen(tbuf) + 1; /* for the \0 */ | ||
268 | if (n > ep - bp) { | ||
269 | had_error++; | ||
270 | continue; | ||
271 | } | ||
272 | strlcpy(bp, tbuf, ep - bp); | ||
273 | host.h_name = bp; | ||
274 | bp += n; | ||
275 | continue; | ||
276 | } | ||
277 | if (qtype == T_PTR && type == T_CNAME) { | ||
278 | n = dn_expand(answer->buf, eom, cp, tbuf, sizeof tbuf); | ||
279 | #ifdef USE_RESOLV_NAME_OK | ||
280 | if ((n < 0) || !res_hnok(tbuf)) { | ||
281 | #else | ||
282 | if ((n < 0) || !_hokchar(tbuf)) { | ||
283 | #endif | ||
284 | had_error++; | ||
285 | continue; | ||
286 | } | ||
287 | cp += n; | ||
288 | /* Get canonical name. */ | ||
289 | n = strlen(tbuf) + 1; /* for the \0 */ | ||
290 | if (n > ep - bp) { | ||
291 | had_error++; | ||
292 | continue; | ||
293 | } | ||
294 | strlcpy(bp, tbuf, ep - bp); | ||
295 | tname = bp; | ||
296 | bp += n; | ||
297 | continue; | ||
298 | } | ||
299 | if (type != qtype) { | ||
300 | syslog(LOG_NOTICE|LOG_AUTH, | ||
301 | "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", | ||
302 | qname, p_class(C_IN), p_type(qtype), | ||
303 | p_type(type)); | ||
304 | cp += n; | ||
305 | continue; /* XXX - had_error++ ? */ | ||
306 | } | ||
307 | switch (type) { | ||
308 | case T_PTR: | ||
309 | if (strcasecmp(tname, bp) != 0) { | ||
310 | syslog(LOG_NOTICE|LOG_AUTH, | ||
311 | AskedForGot, qname, bp); | ||
312 | cp += n; | ||
313 | continue; /* XXX - had_error++ ? */ | ||
314 | } | ||
315 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
316 | #ifdef USE_RESOLV_NAME_OK | ||
317 | if ((n < 0) || !res_hnok(bp)) { | ||
318 | #else | ||
319 | if ((n < 0) || !_hokchar(bp)) { | ||
320 | #endif | ||
321 | had_error++; | ||
322 | break; | ||
323 | } | ||
324 | #if MULTI_PTRS_ARE_ALIASES | ||
325 | cp += n; | ||
326 | if (!haveanswer) | ||
327 | host.h_name = bp; | ||
328 | else if (ap < &host_aliases[MAXALIASES-1]) | ||
329 | *ap++ = bp; | ||
330 | else | ||
331 | n = -1; | ||
332 | if (n != -1) { | ||
333 | n = strlen(bp) + 1; /* for the \0 */ | ||
334 | bp += n; | ||
335 | } | ||
336 | break; | ||
337 | #else | ||
338 | host.h_name = bp; | ||
339 | if (_resp->options & RES_USE_INET6) { | ||
340 | n = strlen(bp) + 1; /* for the \0 */ | ||
341 | bp += n; | ||
342 | map_v4v6_hostent(&host, &bp, ep); | ||
343 | } | ||
344 | h_errno = NETDB_SUCCESS; | ||
345 | return (&host); | ||
346 | #endif | ||
347 | case T_A: | ||
348 | case T_AAAA: | ||
349 | if (strcasecmp(host.h_name, bp) != 0) { | ||
350 | syslog(LOG_NOTICE|LOG_AUTH, | ||
351 | AskedForGot, host.h_name, bp); | ||
352 | cp += n; | ||
353 | continue; /* XXX - had_error++ ? */ | ||
354 | } | ||
355 | if (n != host.h_length) { | ||
356 | cp += n; | ||
357 | continue; | ||
358 | } | ||
359 | if (type == T_AAAA) { | ||
360 | struct in6_addr in6; | ||
361 | memcpy(&in6, cp, IN6ADDRSZ); | ||
362 | if (IN6_IS_ADDR_V4MAPPED(&in6)) { | ||
363 | cp += n; | ||
364 | continue; | ||
365 | } | ||
366 | } | ||
367 | if (!haveanswer) { | ||
368 | int nn; | ||
369 | |||
370 | host.h_name = bp; | ||
371 | nn = strlen(bp) + 1; /* for the \0 */ | ||
372 | bp += nn; | ||
373 | } | ||
374 | |||
375 | bp += sizeof(align) - ((u_long)bp % sizeof(align)); | ||
376 | |||
377 | if (bp + n >= &hostbuf[sizeof hostbuf]) { | ||
378 | #ifdef DEBUG | ||
379 | if (_resp->options & RES_DEBUG) | ||
380 | printf("size (%d) too big\n", n); | ||
381 | #endif | ||
382 | had_error++; | ||
383 | continue; | ||
384 | } | ||
385 | if (hap >= &h_addr_ptrs[MAXADDRS-1]) { | ||
386 | if (!toobig++) | ||
387 | #ifdef DEBUG | ||
388 | if (_resp->options & RES_DEBUG) | ||
389 | printf("Too many addresses (%d)\n", MAXADDRS); | ||
390 | #endif | ||
391 | cp += n; | ||
392 | continue; | ||
393 | } | ||
394 | bcopy(cp, *hap++ = bp, n); | ||
395 | bp += n; | ||
396 | cp += n; | ||
397 | break; | ||
398 | } | ||
399 | if (!had_error) | ||
400 | haveanswer++; | ||
401 | } | ||
402 | if (haveanswer) { | ||
403 | *ap = NULL; | ||
404 | *hap = NULL; | ||
405 | # if defined(RESOLVSORT) | ||
406 | /* | ||
407 | * Note: we sort even if host can take only one address | ||
408 | * in its return structures - should give it the "best" | ||
409 | * address in that case, not some random one | ||
410 | */ | ||
411 | if (_resp->nsort && haveanswer > 1 && qtype == T_A) | ||
412 | addrsort(h_addr_ptrs, haveanswer); | ||
413 | # endif /*RESOLVSORT*/ | ||
414 | if (!host.h_name) { | ||
415 | n = strlen(qname) + 1; /* for the \0 */ | ||
416 | if (n > ep - bp) | ||
417 | goto try_again; | ||
418 | strlcpy(bp, qname, ep - bp); | ||
419 | host.h_name = bp; | ||
420 | bp += n; | ||
421 | } | ||
422 | if (_resp->options & RES_USE_INET6) | ||
423 | map_v4v6_hostent(&host, &bp, ep); | ||
424 | h_errno = NETDB_SUCCESS; | ||
425 | return (&host); | ||
426 | } | ||
427 | try_again: | ||
428 | h_errno = TRY_AGAIN; | ||
429 | return (NULL); | ||
430 | } | ||
431 | |||
432 | #ifdef notyet | ||
433 | /* | ||
434 | * XXX This is an extremely bogus implementation. | ||
435 | * | ||
436 | * FreeBSD has this interface: | ||
437 | * int gethostbyaddr_r(const char *addr, int len, int type, | ||
438 | * struct hostent *result, struct hostent_data *buffer) | ||
439 | */ | ||
440 | |||
441 | struct hostent * | ||
442 | gethostbyname_r(const char *name, struct hostent *hp, char *buf, int buflen, | ||
443 | int *errorp) | ||
444 | { | ||
445 | struct hostent *res; | ||
446 | |||
447 | res = gethostbyname(name); | ||
448 | *errorp = h_errno; | ||
449 | if (res == NULL) | ||
450 | return NULL; | ||
451 | memcpy(hp, res, sizeof *hp); /* XXX not sufficient */ | ||
452 | return hp; | ||
453 | } | ||
454 | |||
455 | /* | ||
456 | * XXX This is an extremely bogus implementation. | ||
457 | */ | ||
458 | struct hostent * | ||
459 | gethostbyaddr_r(const char *addr, int len, int af, struct hostent *he, | ||
460 | char *buf, int buflen, int *errorp) | ||
461 | { | ||
462 | struct hostent * res; | ||
463 | |||
464 | res = gethostbyaddr(addr, len, af); | ||
465 | *errorp = h_errno; | ||
466 | if (res == NULL) | ||
467 | return NULL; | ||
468 | memcpy(he, res, sizeof *he); /* XXX not sufficient */ | ||
469 | return he; | ||
470 | } | ||
471 | |||
472 | /* XXX RFC2133 expects a gethostbyname2_r() -- unimplemented */ | ||
473 | #endif | ||
474 | |||
475 | struct hostent * | ||
476 | gethostbyname(const char *name) | ||
477 | { | ||
478 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
479 | struct hostent *hp; | ||
480 | extern struct hostent *_gethtbyname2(const char *, int); | ||
481 | |||
482 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
483 | hp = _gethtbyname2(name, AF_INET); | ||
484 | |||
485 | else if (_resp->options & RES_USE_INET6) { | ||
486 | hp = gethostbyname2(name, AF_INET6); | ||
487 | if (hp == NULL) | ||
488 | hp = gethostbyname2(name, AF_INET); | ||
489 | } | ||
490 | else | ||
491 | hp = gethostbyname2(name, AF_INET); | ||
492 | return hp; | ||
493 | } | ||
494 | |||
495 | struct hostent * | ||
496 | gethostbyname2(const char *name, int af) | ||
497 | { | ||
498 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
499 | querybuf *buf; | ||
500 | const char *cp; | ||
501 | char *bp, *ep; | ||
502 | int n, size, type, i; | ||
503 | struct hostent *hp; | ||
504 | char lookups[MAXDNSLUS]; | ||
505 | extern struct hostent *_gethtbyname2(const char *, int); | ||
506 | extern struct hostent *_yp_gethtbyname(const char *); | ||
507 | |||
508 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
509 | return (_gethtbyname2(name, af)); | ||
510 | |||
511 | switch (af) { | ||
512 | case AF_INET: | ||
513 | size = INADDRSZ; | ||
514 | type = T_A; | ||
515 | break; | ||
516 | case AF_INET6: | ||
517 | size = IN6ADDRSZ; | ||
518 | type = T_AAAA; | ||
519 | break; | ||
520 | default: | ||
521 | h_errno = NETDB_INTERNAL; | ||
522 | errno = EAFNOSUPPORT; | ||
523 | return (NULL); | ||
524 | } | ||
525 | |||
526 | host.h_addrtype = af; | ||
527 | host.h_length = size; | ||
528 | |||
529 | /* | ||
530 | * if there aren't any dots, it could be a user-level alias. | ||
531 | * this is also done in res_query() since we are not the only | ||
532 | * function that looks up host names. | ||
533 | */ | ||
534 | if (!strchr(name, '.') && (cp = __hostalias(name))) | ||
535 | name = cp; | ||
536 | |||
537 | /* | ||
538 | * disallow names consisting only of digits/dots, unless | ||
539 | * they end in a dot. | ||
540 | */ | ||
541 | if (isdigit(name[0])) | ||
542 | for (cp = name;; ++cp) { | ||
543 | if (!*cp) { | ||
544 | if (*--cp == '.') | ||
545 | break; | ||
546 | /* | ||
547 | * All-numeric, no dot at the end. | ||
548 | * Fake up a hostent as if we'd actually | ||
549 | * done a lookup. | ||
550 | */ | ||
551 | if (inet_pton(af, name, host_addr) <= 0) { | ||
552 | h_errno = HOST_NOT_FOUND; | ||
553 | return (NULL); | ||
554 | } | ||
555 | strlcpy(hostbuf, name, MAXHOSTNAMELEN); | ||
556 | bp = hostbuf + MAXHOSTNAMELEN; | ||
557 | ep = hostbuf + sizeof(hostbuf); | ||
558 | host.h_name = hostbuf; | ||
559 | host.h_aliases = host_aliases; | ||
560 | host_aliases[0] = NULL; | ||
561 | h_addr_ptrs[0] = (char *)host_addr; | ||
562 | h_addr_ptrs[1] = NULL; | ||
563 | host.h_addr_list = h_addr_ptrs; | ||
564 | if (_resp->options & RES_USE_INET6) | ||
565 | map_v4v6_hostent(&host, &bp, ep); | ||
566 | h_errno = NETDB_SUCCESS; | ||
567 | return (&host); | ||
568 | } | ||
569 | if (!isdigit(*cp) && *cp != '.') | ||
570 | break; | ||
571 | } | ||
572 | if ((isxdigit(name[0]) && strchr(name, ':') != NULL) || | ||
573 | name[0] == ':') | ||
574 | for (cp = name;; ++cp) { | ||
575 | if (!*cp) { | ||
576 | if (*--cp == '.') | ||
577 | break; | ||
578 | /* | ||
579 | * All-IPv6-legal, no dot at the end. | ||
580 | * Fake up a hostent as if we'd actually | ||
581 | * done a lookup. | ||
582 | */ | ||
583 | if (inet_pton(af, name, host_addr) <= 0) { | ||
584 | h_errno = HOST_NOT_FOUND; | ||
585 | return (NULL); | ||
586 | } | ||
587 | strlcpy(hostbuf, name, MAXHOSTNAMELEN); | ||
588 | bp = hostbuf + MAXHOSTNAMELEN; | ||
589 | ep = hostbuf + sizeof(hostbuf); | ||
590 | host.h_name = hostbuf; | ||
591 | host.h_aliases = host_aliases; | ||
592 | host_aliases[0] = NULL; | ||
593 | h_addr_ptrs[0] = (char *)host_addr; | ||
594 | h_addr_ptrs[1] = NULL; | ||
595 | host.h_addr_list = h_addr_ptrs; | ||
596 | h_errno = NETDB_SUCCESS; | ||
597 | return (&host); | ||
598 | } | ||
599 | if (!isxdigit(*cp) && *cp != ':' && *cp != '.') | ||
600 | break; | ||
601 | } | ||
602 | |||
603 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
604 | if (lookups[0] == '\0') | ||
605 | strlcpy(lookups, "bf", sizeof lookups); | ||
606 | |||
607 | hp = (struct hostent *)NULL; | ||
608 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | ||
609 | switch (lookups[i]) { | ||
610 | #ifdef YP | ||
611 | case 'y': | ||
612 | /* YP only supports AF_INET. */ | ||
613 | if (af == AF_INET) | ||
614 | hp = _yp_gethtbyname(name); | ||
615 | break; | ||
616 | #endif | ||
617 | case 'b': | ||
618 | buf = malloc(sizeof(*buf)); | ||
619 | if (buf == NULL) | ||
620 | break; | ||
621 | if ((n = res_search(name, C_IN, type, buf->buf, | ||
622 | sizeof(buf->buf))) < 0) { | ||
623 | free(buf); | ||
624 | #ifdef DEBUG | ||
625 | if (_resp->options & RES_DEBUG) | ||
626 | printf("res_search failed\n"); | ||
627 | #endif | ||
628 | break; | ||
629 | } | ||
630 | hp = getanswer(buf, n, name, type); | ||
631 | free(buf); | ||
632 | break; | ||
633 | case 'f': | ||
634 | hp = _gethtbyname2(name, af); | ||
635 | break; | ||
636 | } | ||
637 | } | ||
638 | /* XXX h_errno not correct in all cases... */ | ||
639 | return (hp); | ||
640 | } | ||
641 | |||
642 | struct hostent * | ||
643 | gethostbyaddr(const char *addr, int len, int af) | ||
644 | { | ||
645 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
646 | const u_char *uaddr = (const u_char *)addr; | ||
647 | int n, size, i; | ||
648 | querybuf *buf; | ||
649 | struct hostent *hp; | ||
650 | char qbuf[MAXDNAME+1], *qp, *ep; | ||
651 | char lookups[MAXDNSLUS]; | ||
652 | struct hostent *res; | ||
653 | extern struct hostent *_gethtbyaddr(const char *, int, int); | ||
654 | extern struct hostent *_yp_gethtbyaddr(const char *); | ||
655 | |||
656 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
657 | res = _gethtbyaddr(addr, len, af); | ||
658 | return (res); | ||
659 | } | ||
660 | |||
661 | if (af == AF_INET6 && len == IN6ADDRSZ && | ||
662 | (IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)uaddr) || | ||
663 | IN6_IS_ADDR_SITELOCAL((struct in6_addr *)uaddr))) { | ||
664 | h_errno = HOST_NOT_FOUND; | ||
665 | return (NULL); | ||
666 | } | ||
667 | if (af == AF_INET6 && len == IN6ADDRSZ && | ||
668 | (IN6_IS_ADDR_V4MAPPED((struct in6_addr *)uaddr) || | ||
669 | IN6_IS_ADDR_V4COMPAT((struct in6_addr *)uaddr))) { | ||
670 | /* Unmap. */ | ||
671 | addr += IN6ADDRSZ - INADDRSZ; | ||
672 | uaddr += IN6ADDRSZ - INADDRSZ; | ||
673 | af = AF_INET; | ||
674 | len = INADDRSZ; | ||
675 | } | ||
676 | switch (af) { | ||
677 | case AF_INET: | ||
678 | size = INADDRSZ; | ||
679 | break; | ||
680 | case AF_INET6: | ||
681 | size = IN6ADDRSZ; | ||
682 | break; | ||
683 | default: | ||
684 | errno = EAFNOSUPPORT; | ||
685 | h_errno = NETDB_INTERNAL; | ||
686 | return (NULL); | ||
687 | } | ||
688 | if (size != len) { | ||
689 | errno = EINVAL; | ||
690 | h_errno = NETDB_INTERNAL; | ||
691 | return (NULL); | ||
692 | } | ||
693 | ep = qbuf + sizeof(qbuf); | ||
694 | switch (af) { | ||
695 | case AF_INET: | ||
696 | (void) snprintf(qbuf, sizeof qbuf, "%u.%u.%u.%u.in-addr.arpa", | ||
697 | (uaddr[3] & 0xff), (uaddr[2] & 0xff), | ||
698 | (uaddr[1] & 0xff), (uaddr[0] & 0xff)); | ||
699 | break; | ||
700 | case AF_INET6: | ||
701 | qp = qbuf; | ||
702 | for (n = IN6ADDRSZ - 1; n >= 0; n--) { | ||
703 | i = snprintf(qp, ep - qp, "%x.%x.", | ||
704 | uaddr[n] & 0xf, (uaddr[n] >> 4) & 0xf); | ||
705 | if (i <= 0) { | ||
706 | errno = EINVAL; | ||
707 | h_errno = NETDB_INTERNAL; | ||
708 | return (NULL); | ||
709 | } | ||
710 | qp += i; | ||
711 | } | ||
712 | break; | ||
713 | } | ||
714 | |||
715 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
716 | if (lookups[0] == '\0') | ||
717 | strlcpy(lookups, "bf", sizeof lookups); | ||
718 | |||
719 | hp = (struct hostent *)NULL; | ||
720 | for (i = 0; i < MAXDNSLUS && hp == NULL && lookups[i]; i++) { | ||
721 | switch (lookups[i]) { | ||
722 | #ifdef YP | ||
723 | case 'y': | ||
724 | /* YP only supports AF_INET. */ | ||
725 | if (af == AF_INET) | ||
726 | hp = _yp_gethtbyaddr(addr); | ||
727 | break; | ||
728 | #endif | ||
729 | case 'b': | ||
730 | if (af == AF_INET6) | ||
731 | strlcpy(qp, "ip6.arpa", ep - qp); | ||
732 | buf = malloc(sizeof(*buf)); | ||
733 | if (!buf) | ||
734 | break; | ||
735 | n = res_query(qbuf, C_IN, T_PTR, buf->buf, | ||
736 | sizeof(buf->buf)); | ||
737 | if (n < 0 && af == AF_INET6) { | ||
738 | strlcpy(qp, "ip6.int", ep - qp); | ||
739 | n = res_query(qbuf, C_IN, T_PTR, | ||
740 | buf->buf, sizeof(buf->buf)); | ||
741 | } | ||
742 | if (n < 0) { | ||
743 | free(buf); | ||
744 | #ifdef DEBUG | ||
745 | if (_resp->options & RES_DEBUG) | ||
746 | printf("res_query failed\n"); | ||
747 | #endif | ||
748 | break; | ||
749 | } | ||
750 | if (!(hp = getanswer(buf, n, qbuf, T_PTR))) { | ||
751 | free(buf); | ||
752 | break; | ||
753 | } | ||
754 | free(buf); | ||
755 | hp->h_addrtype = af; | ||
756 | hp->h_length = len; | ||
757 | bcopy(addr, host_addr, len); | ||
758 | h_addr_ptrs[0] = (char *)host_addr; | ||
759 | h_addr_ptrs[1] = NULL; | ||
760 | if (af == AF_INET && (_resp->options & RES_USE_INET6)) { | ||
761 | map_v4v6_address((char*)host_addr, | ||
762 | (char*)host_addr); | ||
763 | hp->h_addrtype = AF_INET6; | ||
764 | hp->h_length = IN6ADDRSZ; | ||
765 | } | ||
766 | h_errno = NETDB_SUCCESS; | ||
767 | break; | ||
768 | case 'f': | ||
769 | hp = _gethtbyaddr(addr, len, af); | ||
770 | break; | ||
771 | } | ||
772 | } | ||
773 | /* XXX h_errno not correct in all cases... */ | ||
774 | return (hp); | ||
775 | } | ||
776 | |||
777 | void | ||
778 | _sethtent(int f) | ||
779 | { | ||
780 | if (hostf == NULL) | ||
781 | hostf = fopen(_PATH_HOSTS, "r" ); | ||
782 | else | ||
783 | rewind(hostf); | ||
784 | stayopen = f; | ||
785 | } | ||
786 | |||
787 | void | ||
788 | _endhtent(void) | ||
789 | { | ||
790 | if (hostf && !stayopen) { | ||
791 | (void) fclose(hostf); | ||
792 | hostf = NULL; | ||
793 | } | ||
794 | } | ||
795 | |||
796 | struct hostent * | ||
797 | _gethtent(void) | ||
798 | { | ||
799 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
800 | char *p, *cp, **q; | ||
801 | int af; | ||
802 | size_t len; | ||
803 | |||
804 | if (!hostf && !(hostf = fopen(_PATH_HOSTS, "r" ))) { | ||
805 | h_errno = NETDB_INTERNAL; | ||
806 | return (NULL); | ||
807 | } | ||
808 | again: | ||
809 | if ((p = fgetln(hostf, &len)) == NULL) { | ||
810 | h_errno = HOST_NOT_FOUND; | ||
811 | return (NULL); | ||
812 | } | ||
813 | if (p[len-1] == '\n') | ||
814 | len--; | ||
815 | if (len >= sizeof(hostbuf) || len == 0) | ||
816 | goto again; | ||
817 | p = memcpy(hostbuf, p, len); | ||
818 | hostbuf[len] = '\0'; | ||
819 | if (*p == '#') | ||
820 | goto again; | ||
821 | if ((cp = strchr(p, '#'))) | ||
822 | *cp = '\0'; | ||
823 | if (!(cp = strpbrk(p, " \t"))) | ||
824 | goto again; | ||
825 | *cp++ = '\0'; | ||
826 | if (inet_pton(AF_INET6, p, host_addr) > 0) { | ||
827 | af = AF_INET6; | ||
828 | len = IN6ADDRSZ; | ||
829 | } else if (inet_pton(AF_INET, p, host_addr) > 0) { | ||
830 | if (_resp->options & RES_USE_INET6) { | ||
831 | map_v4v6_address((char*)host_addr, (char*)host_addr); | ||
832 | af = AF_INET6; | ||
833 | len = IN6ADDRSZ; | ||
834 | } else { | ||
835 | af = AF_INET; | ||
836 | len = INADDRSZ; | ||
837 | } | ||
838 | } else { | ||
839 | goto again; | ||
840 | } | ||
841 | /* if this is not something we're looking for, skip it. */ | ||
842 | if (host.h_addrtype != af) | ||
843 | goto again; | ||
844 | if (host.h_length != len) | ||
845 | goto again; | ||
846 | h_addr_ptrs[0] = (char *)host_addr; | ||
847 | h_addr_ptrs[1] = NULL; | ||
848 | host.h_addr_list = h_addr_ptrs; | ||
849 | host.h_length = len; | ||
850 | host.h_addrtype = af; | ||
851 | while (*cp == ' ' || *cp == '\t') | ||
852 | cp++; | ||
853 | host.h_name = cp; | ||
854 | q = host.h_aliases = host_aliases; | ||
855 | if ((cp = strpbrk(cp, " \t"))) | ||
856 | *cp++ = '\0'; | ||
857 | while (cp && *cp) { | ||
858 | if (*cp == ' ' || *cp == '\t') { | ||
859 | cp++; | ||
860 | continue; | ||
861 | } | ||
862 | if (q < &host_aliases[MAXALIASES - 1]) | ||
863 | *q++ = cp; | ||
864 | if ((cp = strpbrk(cp, " \t"))) | ||
865 | *cp++ = '\0'; | ||
866 | } | ||
867 | *q = NULL; | ||
868 | if (_resp->options & RES_USE_INET6) { | ||
869 | char *bp = hostbuf; | ||
870 | char *ep = hostbuf + sizeof hostbuf; | ||
871 | |||
872 | map_v4v6_hostent(&host, &bp, ep); | ||
873 | } | ||
874 | h_errno = NETDB_SUCCESS; | ||
875 | return (&host); | ||
876 | } | ||
877 | |||
878 | struct hostent * | ||
879 | _gethtbyname(const char *name) | ||
880 | { | ||
881 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
882 | struct hostent *hp; | ||
883 | extern struct hostent *_gethtbyname2(const char *, int); | ||
884 | |||
885 | if (_resp->options & RES_USE_INET6) { | ||
886 | hp = _gethtbyname2(name, AF_INET6); | ||
887 | if (hp) | ||
888 | return (hp); | ||
889 | } | ||
890 | return (_gethtbyname2(name, AF_INET)); | ||
891 | } | ||
892 | |||
893 | struct hostent * | ||
894 | _gethtbyname2(const char *name, int af) | ||
895 | { | ||
896 | struct hostent *p; | ||
897 | char **cp; | ||
898 | |||
899 | _sethtent(0); | ||
900 | while ((p = _gethtent())) { | ||
901 | if (p->h_addrtype != af) | ||
902 | continue; | ||
903 | if (strcasecmp(p->h_name, name) == 0) | ||
904 | break; | ||
905 | for (cp = p->h_aliases; *cp != 0; cp++) | ||
906 | if (strcasecmp(*cp, name) == 0) | ||
907 | goto found; | ||
908 | } | ||
909 | found: | ||
910 | _endhtent(); | ||
911 | return (p); | ||
912 | } | ||
913 | |||
914 | struct hostent * | ||
915 | _gethtbyaddr(const char *addr, int len, int af) | ||
916 | { | ||
917 | struct hostent *p; | ||
918 | |||
919 | host.h_length = len; | ||
920 | host.h_addrtype = af; | ||
921 | |||
922 | _sethtent(0); | ||
923 | while ((p = _gethtent())) | ||
924 | if (p->h_addrtype == af && !bcmp(p->h_addr, addr, len)) | ||
925 | break; | ||
926 | _endhtent(); | ||
927 | return (p); | ||
928 | } | ||
929 | |||
930 | #ifdef YP | ||
931 | struct hostent * | ||
932 | _yphostent(char *line) | ||
933 | { | ||
934 | static struct in_addr host_addrs[MAXADDRS]; | ||
935 | char *p = line; | ||
936 | char *cp, **q; | ||
937 | char **hap; | ||
938 | struct in_addr *buf; | ||
939 | int more; | ||
940 | |||
941 | host.h_name = NULL; | ||
942 | host.h_addr_list = h_addr_ptrs; | ||
943 | host.h_length = INADDRSZ; | ||
944 | host.h_addrtype = AF_INET; | ||
945 | hap = h_addr_ptrs; | ||
946 | buf = host_addrs; | ||
947 | q = host.h_aliases = host_aliases; | ||
948 | |||
949 | nextline: | ||
950 | /* check for host_addrs overflow */ | ||
951 | if (buf >= &host_addrs[sizeof(host_addrs) / sizeof(host_addrs[0])]) | ||
952 | goto done; | ||
953 | |||
954 | more = 0; | ||
955 | cp = strpbrk(p, " \t"); | ||
956 | if (cp == NULL) | ||
957 | goto done; | ||
958 | *cp++ = '\0'; | ||
959 | |||
960 | *hap++ = (char *)buf; | ||
961 | (void) inet_aton(p, buf++); | ||
962 | |||
963 | while (*cp == ' ' || *cp == '\t') | ||
964 | cp++; | ||
965 | p = cp; | ||
966 | cp = strpbrk(p, " \t\n"); | ||
967 | if (cp != NULL) { | ||
968 | if (*cp == '\n') | ||
969 | more = 1; | ||
970 | *cp++ = '\0'; | ||
971 | } | ||
972 | if (!host.h_name) | ||
973 | host.h_name = p; | ||
974 | else if (strcmp(host.h_name, p)==0) | ||
975 | ; | ||
976 | else if (q < &host_aliases[MAXALIASES - 1]) | ||
977 | *q++ = p; | ||
978 | p = cp; | ||
979 | if (more) | ||
980 | goto nextline; | ||
981 | |||
982 | while (cp && *cp) { | ||
983 | if (*cp == ' ' || *cp == '\t') { | ||
984 | cp++; | ||
985 | continue; | ||
986 | } | ||
987 | if (*cp == '\n') { | ||
988 | cp++; | ||
989 | goto nextline; | ||
990 | } | ||
991 | if (q < &host_aliases[MAXALIASES - 1]) | ||
992 | *q++ = cp; | ||
993 | cp = strpbrk(cp, " \t"); | ||
994 | if (cp != NULL) | ||
995 | *cp++ = '\0'; | ||
996 | } | ||
997 | done: | ||
998 | if (host.h_name == NULL) | ||
999 | return (NULL); | ||
1000 | *q = NULL; | ||
1001 | *hap = NULL; | ||
1002 | return (&host); | ||
1003 | } | ||
1004 | |||
1005 | struct hostent * | ||
1006 | _yp_gethtbyaddr(const char *addr) | ||
1007 | { | ||
1008 | struct hostent *hp = (struct hostent *)NULL; | ||
1009 | static char *__ypcurrent; | ||
1010 | int __ypcurrentlen, r; | ||
1011 | char name[sizeof("xxx.xxx.xxx.xxx")]; | ||
1012 | |||
1013 | if (!__ypdomain) { | ||
1014 | if (_yp_check(&__ypdomain) == 0) | ||
1015 | return (hp); | ||
1016 | } | ||
1017 | snprintf(name, sizeof name, "%u.%u.%u.%u", | ||
1018 | ((unsigned)addr[0] & 0xff), ((unsigned)addr[1] & 0xff), | ||
1019 | ((unsigned)addr[2] & 0xff), ((unsigned)addr[3] & 0xff)); | ||
1020 | if (__ypcurrent) | ||
1021 | free(__ypcurrent); | ||
1022 | __ypcurrent = NULL; | ||
1023 | r = yp_match(__ypdomain, "hosts.byaddr", name, | ||
1024 | strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
1025 | if (r==0) | ||
1026 | hp = _yphostent(__ypcurrent); | ||
1027 | if (hp==NULL) | ||
1028 | h_errno = HOST_NOT_FOUND; | ||
1029 | return (hp); | ||
1030 | } | ||
1031 | |||
1032 | struct hostent * | ||
1033 | _yp_gethtbyname(const char *name) | ||
1034 | { | ||
1035 | struct hostent *hp = (struct hostent *)NULL; | ||
1036 | static char *__ypcurrent; | ||
1037 | int __ypcurrentlen, r; | ||
1038 | |||
1039 | if (strlen(name) >= MAXHOSTNAMELEN) | ||
1040 | return (NULL); | ||
1041 | if (!__ypdomain) { | ||
1042 | if (_yp_check(&__ypdomain) == 0) | ||
1043 | return (hp); | ||
1044 | } | ||
1045 | if (__ypcurrent) | ||
1046 | free(__ypcurrent); | ||
1047 | __ypcurrent = NULL; | ||
1048 | r = yp_match(__ypdomain, "hosts.byname", name, | ||
1049 | strlen(name), &__ypcurrent, &__ypcurrentlen); | ||
1050 | if (r == 0) | ||
1051 | hp = _yphostent(__ypcurrent); | ||
1052 | if (hp == NULL) | ||
1053 | h_errno = HOST_NOT_FOUND; | ||
1054 | return (hp); | ||
1055 | } | ||
1056 | #endif | ||
1057 | |||
1058 | static void | ||
1059 | map_v4v6_address(const char *src, char *dst) | ||
1060 | { | ||
1061 | u_char *p = (u_char *)dst; | ||
1062 | char tmp[INADDRSZ]; | ||
1063 | int i; | ||
1064 | |||
1065 | /* Stash a temporary copy so our caller can update in place. */ | ||
1066 | bcopy(src, tmp, INADDRSZ); | ||
1067 | /* Mark this ipv6 addr as a mapped ipv4. */ | ||
1068 | for (i = 0; i < 10; i++) | ||
1069 | *p++ = 0x00; | ||
1070 | *p++ = 0xff; | ||
1071 | *p++ = 0xff; | ||
1072 | /* Retrieve the saved copy and we're done. */ | ||
1073 | bcopy(tmp, (void*)p, INADDRSZ); | ||
1074 | } | ||
1075 | |||
1076 | static void | ||
1077 | map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) | ||
1078 | { | ||
1079 | char **ap; | ||
1080 | |||
1081 | if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) | ||
1082 | return; | ||
1083 | hp->h_addrtype = AF_INET6; | ||
1084 | hp->h_length = IN6ADDRSZ; | ||
1085 | for (ap = hp->h_addr_list; *ap; ap++) { | ||
1086 | int i = sizeof(align) - ((u_long)*bpp % sizeof(align)); | ||
1087 | |||
1088 | if (ep - *bpp < (i + IN6ADDRSZ)) { | ||
1089 | /* Out of memory. Truncate address list here. XXX */ | ||
1090 | *ap = NULL; | ||
1091 | return; | ||
1092 | } | ||
1093 | *bpp += i; | ||
1094 | map_v4v6_address(*ap, *bpp); | ||
1095 | *ap = *bpp; | ||
1096 | *bpp += IN6ADDRSZ; | ||
1097 | } | ||
1098 | } | ||
1099 | |||
1100 | struct hostent * | ||
1101 | gethostent(void) | ||
1102 | { | ||
1103 | return (_gethtent()); | ||
1104 | } | ||
1105 | |||
1106 | #ifdef RESOLVSORT | ||
1107 | static void | ||
1108 | addrsort(char **ap, int num) | ||
1109 | { | ||
1110 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
1111 | int i, j; | ||
1112 | char **p; | ||
1113 | short aval[MAXADDRS]; | ||
1114 | int needsort = 0; | ||
1115 | |||
1116 | p = ap; | ||
1117 | for (i = 0; i < num; i++, p++) { | ||
1118 | for (j = 0 ; (unsigned)j < _resp->nsort; j++) | ||
1119 | if (_resp->sort_list[j].addr.s_addr == | ||
1120 | (((struct in_addr *)(*p))->s_addr & | ||
1121 | _resp->sort_list[j].mask)) | ||
1122 | break; | ||
1123 | aval[i] = j; | ||
1124 | if (needsort == 0 && i > 0 && j < aval[i-1]) | ||
1125 | needsort = i; | ||
1126 | } | ||
1127 | if (!needsort) | ||
1128 | return; | ||
1129 | |||
1130 | while (needsort < num) { | ||
1131 | for (j = needsort - 1; j >= 0; j--) { | ||
1132 | if (aval[j] > aval[j+1]) { | ||
1133 | char *hp; | ||
1134 | |||
1135 | i = aval[j]; | ||
1136 | aval[j] = aval[j+1]; | ||
1137 | aval[j+1] = i; | ||
1138 | |||
1139 | hp = ap[j]; | ||
1140 | ap[j] = ap[j+1]; | ||
1141 | ap[j+1] = hp; | ||
1142 | } else | ||
1143 | break; | ||
1144 | } | ||
1145 | needsort++; | ||
1146 | } | ||
1147 | } | ||
1148 | #endif | ||
diff --git a/src/lib/libc/net/getifaddrs.3 b/src/lib/libc/net/getifaddrs.3 new file mode 100644 index 0000000000..9287b3b734 --- /dev/null +++ b/src/lib/libc/net/getifaddrs.3 | |||
@@ -0,0 +1,159 @@ | |||
1 | .\" $OpenBSD: getifaddrs.3,v 1.10 2003/05/30 21:37:59 jmc Exp $ | ||
2 | .\" BSDI getifaddrs.3,v 2.5 2000/02/23 14:51:59 dab Exp | ||
3 | .\" | ||
4 | .\" Copyright (c) 1995, 1999 | ||
5 | .\" Berkeley Software Design, Inc. 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 | .\" | ||
13 | .\" THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
14 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
15 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
16 | .\" ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
17 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
18 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
19 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
20 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
21 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
22 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
23 | .\" SUCH DAMAGE. | ||
24 | .Dd October 12, 1995 | ||
25 | .Dt GETIFADDRS 3 | ||
26 | .Os | ||
27 | .Sh NAME | ||
28 | .Nm getifaddrs | ||
29 | .Nd get interface addresses | ||
30 | .Sh SYNOPSIS | ||
31 | .Fd #include <sys/types.h> | ||
32 | .Fd #include <sys/socket.h> | ||
33 | .Fd #include <ifaddrs.h> | ||
34 | .Ft int | ||
35 | .Fn getifaddrs "struct ifaddrs **ifap" | ||
36 | .Ft void | ||
37 | .Fn freeifaddrs "struct ifaddrs *ifap" | ||
38 | .Sh DESCRIPTION | ||
39 | The | ||
40 | .Fn getifaddrs | ||
41 | function stores a reference to a linked list of the network interfaces | ||
42 | on the local machine in the memory referenced by | ||
43 | .Fa ifap . | ||
44 | The list consists of | ||
45 | .Nm ifaddrs | ||
46 | structures, as defined in the include file | ||
47 | .Aq Pa ifaddrs.h . | ||
48 | The | ||
49 | .Nm ifaddrs | ||
50 | structure contains at least the following entries: | ||
51 | .Bd -literal | ||
52 | struct ifaddrs *ifa_next; /* Pointer to next struct */ | ||
53 | char *ifa_name; /* Interface name */ | ||
54 | u_int ifa_flags; /* Interface flags */ | ||
55 | struct sockaddr *ifa_addr; /* Interface address */ | ||
56 | struct sockaddr *ifa_netmask; /* Interface netmask */ | ||
57 | struct sockaddr *ifa_broadaddr; /* Interface broadcast address */ | ||
58 | struct sockaddr *ifa_dstaddr; /* P2P interface destination */ | ||
59 | void *ifa_data; /* Address specific data */ | ||
60 | .Ed | ||
61 | .Pp | ||
62 | .Bl -tag -width Ds | ||
63 | .It Fa ifa_next | ||
64 | Contains a pointer to the next structure on the list. | ||
65 | This field is set to | ||
66 | .Dv NULL | ||
67 | in last structure on the list. | ||
68 | .It Fa ifa_name | ||
69 | Contains the interface name. | ||
70 | .It Fa ifa_flags | ||
71 | Contains the interface flags, as set by | ||
72 | .Xr ifconfig 8 . | ||
73 | .It Fa ifa_addr | ||
74 | References either the address of the interface or the link level | ||
75 | address of the interface, if one exists, otherwise it is | ||
76 | .Dv NULL . | ||
77 | (The | ||
78 | .Fa sa_family | ||
79 | field of the | ||
80 | .Fa ifa_addr | ||
81 | field should be consulted to determine the format of the | ||
82 | .Fa ifa_addr | ||
83 | address.) | ||
84 | .It Fa ifa_netmask | ||
85 | References the netmask associated with | ||
86 | .Fa ifa_addr , | ||
87 | if one is set, otherwise it is | ||
88 | .Dv NULL . | ||
89 | .It Fa ifa_broadaddr | ||
90 | This field, which should only be referenced for non-P2P interfaces, | ||
91 | references the broadcast address associated with | ||
92 | .Fa ifa_addr , | ||
93 | if one exists, otherwise it is | ||
94 | .Dv NULL . | ||
95 | .It Fa ifa_dstaddr | ||
96 | References the destination address on a P2P interface, | ||
97 | if one exists, otherwise it is | ||
98 | .Dv NULL . | ||
99 | .It Fa ifa_data | ||
100 | References address family specific data. | ||
101 | For | ||
102 | .Dv AF_LINK | ||
103 | addresses it contains a pointer to the | ||
104 | .Li struct if_data | ||
105 | (as defined in include file | ||
106 | .Aq Pa net/if.h ) | ||
107 | which contains various interface attributes and statistics. | ||
108 | For all other address families, it contains a pointer to the | ||
109 | .Li struct ifa_data | ||
110 | (as defined in include file | ||
111 | .Aq Pa net/if.h ) | ||
112 | which contains per-address interface statistics. | ||
113 | .El | ||
114 | .Pp | ||
115 | The data returned by | ||
116 | .Fn getifaddrs | ||
117 | is dynamically allocated and should be freed using | ||
118 | .Fn freeifaddrs | ||
119 | when no longer needed. | ||
120 | .Sh RETURN VALUES | ||
121 | Upon successful completion, a value of 0 is returned. | ||
122 | Otherwise, a value of \-1 is returned and | ||
123 | .Va errno | ||
124 | is set to indicate the error. | ||
125 | .Sh ERRORS | ||
126 | The | ||
127 | .Fn getifaddrs | ||
128 | may fail and set | ||
129 | .Va errno | ||
130 | for any of the errors specified for the library routines | ||
131 | .Xr ioctl 2 , | ||
132 | .Xr socket 2 , | ||
133 | .Xr malloc 3 , | ||
134 | or | ||
135 | .Xr sysctl 3 . | ||
136 | .Sh SEE ALSO | ||
137 | .Xr ioctl 2 , | ||
138 | .Xr socket 2 , | ||
139 | .Xr sysctl 3 , | ||
140 | .Xr networking 4 , | ||
141 | .Xr ifconfig 8 | ||
142 | .Sh HISTORY | ||
143 | The | ||
144 | .Fn getifaddrs | ||
145 | function first appeared in BSDI BSD/OS. | ||
146 | The function is supplied on | ||
147 | .Ox | ||
148 | since | ||
149 | .Ox 2.7 . | ||
150 | .Sh BUGS | ||
151 | If both | ||
152 | .Aq Pa net/if.h | ||
153 | and | ||
154 | .Aq Pa ifaddrs.h | ||
155 | are being included, | ||
156 | .Aq Pa net/if.h | ||
157 | .Em must | ||
158 | be included before | ||
159 | .Aq Pa ifaddrs.h . | ||
diff --git a/src/lib/libc/net/getifaddrs.c b/src/lib/libc/net/getifaddrs.c new file mode 100644 index 0000000000..0db89f6c19 --- /dev/null +++ b/src/lib/libc/net/getifaddrs.c | |||
@@ -0,0 +1,277 @@ | |||
1 | /* $OpenBSD: getifaddrs.c,v 1.9 2002/08/09 06:12:25 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1995, 1999 | ||
5 | * Berkeley Software Design, Inc. 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 | * | ||
13 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
14 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
16 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
17 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
18 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
19 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
20 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
21 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
22 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
23 | * SUCH DAMAGE. | ||
24 | * | ||
25 | * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp | ||
26 | */ | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/ioctl.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if.h> | ||
32 | #include <sys/param.h> | ||
33 | #include <net/route.h> | ||
34 | #include <sys/sysctl.h> | ||
35 | #include <net/if_dl.h> | ||
36 | |||
37 | #include <errno.h> | ||
38 | #include <ifaddrs.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <string.h> | ||
41 | #include <unistd.h> | ||
42 | |||
43 | #define SALIGN (sizeof(long) - 1) | ||
44 | #define SA_RLEN(sa) ((sa)->sa_len ? (((sa)->sa_len + SALIGN) & ~SALIGN) : (SALIGN + 1)) | ||
45 | |||
46 | int | ||
47 | getifaddrs(struct ifaddrs **pif) | ||
48 | { | ||
49 | int icnt = 1; | ||
50 | int dcnt = 0; | ||
51 | int ncnt = 0; | ||
52 | int mib[6]; | ||
53 | size_t needed; | ||
54 | char *buf; | ||
55 | char *next; | ||
56 | struct ifaddrs *cif = 0; | ||
57 | char *p, *p0; | ||
58 | struct rt_msghdr *rtm; | ||
59 | struct if_msghdr *ifm; | ||
60 | struct ifa_msghdr *ifam; | ||
61 | struct sockaddr_dl *dl; | ||
62 | struct sockaddr *sa; | ||
63 | u_short index = 0; | ||
64 | size_t len, alen; | ||
65 | struct ifaddrs *ifa, *ift; | ||
66 | int i; | ||
67 | char *data; | ||
68 | char *names; | ||
69 | |||
70 | mib[0] = CTL_NET; | ||
71 | mib[1] = PF_ROUTE; | ||
72 | mib[2] = 0; /* protocol */ | ||
73 | mib[3] = 0; /* wildcard address family */ | ||
74 | mib[4] = NET_RT_IFLIST; | ||
75 | mib[5] = 0; /* no flags */ | ||
76 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) | ||
77 | return (-1); | ||
78 | if ((buf = malloc(needed)) == NULL) | ||
79 | return (-1); | ||
80 | if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { | ||
81 | free(buf); | ||
82 | return (-1); | ||
83 | } | ||
84 | |||
85 | for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { | ||
86 | rtm = (struct rt_msghdr *)next; | ||
87 | if (rtm->rtm_version != RTM_VERSION) | ||
88 | continue; | ||
89 | switch (rtm->rtm_type) { | ||
90 | case RTM_IFINFO: | ||
91 | ifm = (struct if_msghdr *)rtm; | ||
92 | if (ifm->ifm_addrs & RTA_IFP) { | ||
93 | index = ifm->ifm_index; | ||
94 | ++icnt; | ||
95 | dl = (struct sockaddr_dl *)(ifm + 1); | ||
96 | dcnt += SA_RLEN((struct sockaddr *)dl) + | ||
97 | ALIGNBYTES; | ||
98 | dcnt += sizeof(ifm->ifm_data); | ||
99 | ncnt += dl->sdl_nlen + 1; | ||
100 | } else | ||
101 | index = 0; | ||
102 | break; | ||
103 | |||
104 | case RTM_NEWADDR: | ||
105 | ifam = (struct ifa_msghdr *)rtm; | ||
106 | if (index && ifam->ifam_index != index) | ||
107 | abort(); /* XXX abort illegal in library */ | ||
108 | |||
109 | #define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD) | ||
110 | if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) | ||
111 | break; | ||
112 | p = (char *)(ifam + 1); | ||
113 | ++icnt; | ||
114 | /* Scan to look for length of address */ | ||
115 | alen = 0; | ||
116 | for (p0 = p, i = 0; i < RTAX_MAX; i++) { | ||
117 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
118 | == 0) | ||
119 | continue; | ||
120 | sa = (struct sockaddr *)p; | ||
121 | len = SA_RLEN(sa); | ||
122 | if (i == RTAX_IFA) { | ||
123 | alen = len; | ||
124 | break; | ||
125 | } | ||
126 | p += len; | ||
127 | } | ||
128 | for (p = p0, i = 0; i < RTAX_MAX; i++) { | ||
129 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
130 | == 0) | ||
131 | continue; | ||
132 | sa = (struct sockaddr *)p; | ||
133 | len = SA_RLEN(sa); | ||
134 | if (i == RTAX_NETMASK && sa->sa_len == 0) | ||
135 | dcnt += alen; | ||
136 | else | ||
137 | dcnt += len; | ||
138 | p += len; | ||
139 | } | ||
140 | break; | ||
141 | } | ||
142 | } | ||
143 | |||
144 | if (icnt + dcnt + ncnt == 1) { | ||
145 | *pif = NULL; | ||
146 | free(buf); | ||
147 | return (0); | ||
148 | } | ||
149 | data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt); | ||
150 | if (data == NULL) { | ||
151 | free(buf); | ||
152 | return(-1); | ||
153 | } | ||
154 | |||
155 | ifa = (struct ifaddrs *)data; | ||
156 | data += sizeof(struct ifaddrs) * icnt; | ||
157 | names = data + dcnt; | ||
158 | |||
159 | memset(ifa, 0, sizeof(struct ifaddrs) * icnt); | ||
160 | ift = ifa; | ||
161 | |||
162 | index = 0; | ||
163 | for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { | ||
164 | rtm = (struct rt_msghdr *)next; | ||
165 | if (rtm->rtm_version != RTM_VERSION) | ||
166 | continue; | ||
167 | switch (rtm->rtm_type) { | ||
168 | case RTM_IFINFO: | ||
169 | ifm = (struct if_msghdr *)rtm; | ||
170 | if (ifm->ifm_addrs & RTA_IFP) { | ||
171 | index = ifm->ifm_index; | ||
172 | dl = (struct sockaddr_dl *)(ifm + 1); | ||
173 | |||
174 | cif = ift; | ||
175 | ift->ifa_name = names; | ||
176 | ift->ifa_flags = (int)ifm->ifm_flags; | ||
177 | memcpy(names, dl->sdl_data, dl->sdl_nlen); | ||
178 | names[dl->sdl_nlen] = 0; | ||
179 | names += dl->sdl_nlen + 1; | ||
180 | |||
181 | ift->ifa_addr = (struct sockaddr *)data; | ||
182 | memcpy(data, dl, | ||
183 | ((struct sockaddr *)dl)->sa_len); | ||
184 | data += SA_RLEN((struct sockaddr *)dl); | ||
185 | |||
186 | /* ifm_data needs to be aligned */ | ||
187 | ift->ifa_data = data = (void *)ALIGN(data); | ||
188 | memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data)); | ||
189 | data += sizeof(ifm->ifm_data); | ||
190 | |||
191 | ift = (ift->ifa_next = ift + 1); | ||
192 | } else | ||
193 | index = 0; | ||
194 | break; | ||
195 | |||
196 | case RTM_NEWADDR: | ||
197 | ifam = (struct ifa_msghdr *)rtm; | ||
198 | if (index && ifam->ifam_index != index) | ||
199 | abort(); /* XXX abort illegal in library */ | ||
200 | |||
201 | if (index == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) | ||
202 | break; | ||
203 | ift->ifa_name = cif->ifa_name; | ||
204 | ift->ifa_flags = cif->ifa_flags; | ||
205 | ift->ifa_data = NULL; | ||
206 | p = (char *)(ifam + 1); | ||
207 | /* Scan to look for length of address */ | ||
208 | alen = 0; | ||
209 | for (p0 = p, i = 0; i < RTAX_MAX; i++) { | ||
210 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
211 | == 0) | ||
212 | continue; | ||
213 | sa = (struct sockaddr *)p; | ||
214 | len = SA_RLEN(sa); | ||
215 | if (i == RTAX_IFA) { | ||
216 | alen = len; | ||
217 | break; | ||
218 | } | ||
219 | p += len; | ||
220 | } | ||
221 | for (p = p0, i = 0; i < RTAX_MAX; i++) { | ||
222 | if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) | ||
223 | == 0) | ||
224 | continue; | ||
225 | sa = (struct sockaddr *)p; | ||
226 | len = SA_RLEN(sa); | ||
227 | switch (i) { | ||
228 | case RTAX_IFA: | ||
229 | ift->ifa_addr = (struct sockaddr *)data; | ||
230 | memcpy(data, p, len); | ||
231 | data += len; | ||
232 | break; | ||
233 | |||
234 | case RTAX_NETMASK: | ||
235 | ift->ifa_netmask = | ||
236 | (struct sockaddr *)data; | ||
237 | if (sa->sa_len == 0) { | ||
238 | memset(data, 0, alen); | ||
239 | data += alen; | ||
240 | break; | ||
241 | } | ||
242 | memcpy(data, p, len); | ||
243 | data += len; | ||
244 | break; | ||
245 | |||
246 | case RTAX_BRD: | ||
247 | ift->ifa_broadaddr = | ||
248 | (struct sockaddr *)data; | ||
249 | memcpy(data, p, len); | ||
250 | data += len; | ||
251 | break; | ||
252 | } | ||
253 | p += len; | ||
254 | } | ||
255 | |||
256 | |||
257 | ift = (ift->ifa_next = ift + 1); | ||
258 | break; | ||
259 | } | ||
260 | } | ||
261 | |||
262 | free(buf); | ||
263 | if (--ift >= ifa) { | ||
264 | ift->ifa_next = NULL; | ||
265 | *pif = ifa; | ||
266 | } else { | ||
267 | *pif = NULL; | ||
268 | free(ifa); | ||
269 | } | ||
270 | return (0); | ||
271 | } | ||
272 | |||
273 | void | ||
274 | freeifaddrs(struct ifaddrs *ifp) | ||
275 | { | ||
276 | free(ifp); | ||
277 | } | ||
diff --git a/src/lib/libc/net/getnameinfo.3 b/src/lib/libc/net/getnameinfo.3 new file mode 100644 index 0000000000..3b3a0fd290 --- /dev/null +++ b/src/lib/libc/net/getnameinfo.3 | |||
@@ -0,0 +1,348 @@ | |||
1 | .\" $OpenBSD: getnameinfo.3,v 1.27 2003/08/28 10:16:38 jmc Exp $ | ||
2 | .\" $KAME: getnameinfo.3,v 1.20 2001/01/05 13:37:37 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1983, 1987, 1991, 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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" From: @(#)gethostbyname.3 8.4 (Berkeley) 5/25/95 | ||
32 | .\" | ||
33 | .Dd May 25, 1995 | ||
34 | .Dt GETNAMEINFO 3 | ||
35 | .Os | ||
36 | .\" | ||
37 | .Sh NAME | ||
38 | .Nm getnameinfo | ||
39 | .Nd address-to-nodename translation in protocol-independent manner | ||
40 | .\" | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <sys/types.h> | ||
43 | .Fd #include <sys/socket.h> | ||
44 | .Fd #include <netdb.h> | ||
45 | .Ft int | ||
46 | .Fn getnameinfo "const struct sockaddr *sa" "socklen_t salen" \ | ||
47 | "char *host" "size_t hostlen" "char *serv" "size_t servlen" "int flags" | ||
48 | .\" | ||
49 | .Sh DESCRIPTION | ||
50 | The | ||
51 | .Fn getnameinfo | ||
52 | function is defined for protocol-independent address-to-nodename translation. | ||
53 | Its functionality is a reverse conversion of | ||
54 | .Xr getaddrinfo 3 , | ||
55 | and implements similar functionality to | ||
56 | .Xr gethostbyaddr 3 | ||
57 | and | ||
58 | .Xr getservbyport 3 | ||
59 | in a more sophisticated manner. | ||
60 | .Pp | ||
61 | This function looks up an IP address and port number provided by the | ||
62 | caller in the DNS and system-specific database, and returns text | ||
63 | strings for both in buffers provided by the caller. | ||
64 | The function indicates successful completion by a zero return value; | ||
65 | a non-zero return value indicates failure. | ||
66 | .Pp | ||
67 | The first argument, | ||
68 | .Fa sa , | ||
69 | points to either a | ||
70 | .Li sockaddr_in | ||
71 | structure (for IPv4) or a | ||
72 | .Li sockaddr_in6 | ||
73 | structure (for IPv6) that holds the IP address and port number. | ||
74 | The | ||
75 | .Fa salen | ||
76 | argument gives the length of the | ||
77 | .Li sockaddr_in | ||
78 | or | ||
79 | .Li sockaddr_in6 | ||
80 | structure. | ||
81 | .Pp | ||
82 | The function returns the nodename associated with the IP address in | ||
83 | the buffer pointed to by the | ||
84 | .Fa host | ||
85 | argument. | ||
86 | The caller provides the size of this buffer via the | ||
87 | .Fa hostlen | ||
88 | argument. | ||
89 | The service name associated with the port number is returned in the buffer | ||
90 | pointed to by | ||
91 | .Fa serv , | ||
92 | and the | ||
93 | .Fa servlen | ||
94 | argument gives the length of this buffer. | ||
95 | The caller specifies not to return either string by providing a zero | ||
96 | value for the | ||
97 | .Fa hostlen | ||
98 | or | ||
99 | .Fa servlen | ||
100 | arguments. | ||
101 | Otherwise, the caller must provide buffers large enough to hold the | ||
102 | nodename and the service name, including the terminating null characters. | ||
103 | .Pp | ||
104 | Unfortunately most systems do not provide constants that specify the | ||
105 | maximum size of either a fully-qualified domain name or a service name. | ||
106 | Therefore to aid the application in allocating buffers for these two | ||
107 | returned strings the following constants are defined in | ||
108 | .Aq Pa netdb.h : | ||
109 | .Bd -literal -offset | ||
110 | #define NI_MAXHOST MAXHOSTNAMELEN | ||
111 | #define NI_MAXSERV 32 | ||
112 | .Ed | ||
113 | .Pp | ||
114 | The first value is actually defined as the constant | ||
115 | .Dv MAXDNAME | ||
116 | in recent versions of BIND's | ||
117 | .Aq Pa arpa/nameser.h | ||
118 | header (older versions of BIND define this constant to be 256) | ||
119 | and the second is a guess based on the services listed in the current | ||
120 | Assigned Numbers RFC. | ||
121 | .Pp | ||
122 | The final argument is a | ||
123 | .Fa flag | ||
124 | that changes the default actions of this function. | ||
125 | By default the fully-qualified domain name (FQDN) for the host is | ||
126 | looked up in the DNS and returned. | ||
127 | If the flag bit | ||
128 | .Dv NI_NOFQDN | ||
129 | is set, only the nodename portion of the FQDN is returned for local hosts. | ||
130 | .Pp | ||
131 | If the | ||
132 | .Fa flag | ||
133 | bit | ||
134 | .Dv NI_NUMERICHOST | ||
135 | is set, or if the host's name cannot be located in the DNS, | ||
136 | the numeric form of the host's address is returned instead of its name | ||
137 | .Po | ||
138 | e.g., by calling | ||
139 | .Fn inet_ntop | ||
140 | instead of | ||
141 | .Fn gethostbyaddr | ||
142 | .Pc . | ||
143 | If the | ||
144 | .Fa flag | ||
145 | bit | ||
146 | .Dv NI_NAMEREQD | ||
147 | is set, an error is returned if the host's name cannot be located in the DNS. | ||
148 | .Pp | ||
149 | If the flag bit | ||
150 | .Dv NI_NUMERICSERV | ||
151 | is set, the numeric form of the service address is returned | ||
152 | .Pq e.g., its port number | ||
153 | instead of its name. | ||
154 | The two | ||
155 | .Dv NI_NUMERICxxx | ||
156 | flags are required to support the | ||
157 | .Fl n | ||
158 | flag that many commands provide. | ||
159 | .Pp | ||
160 | A fifth flag bit, | ||
161 | .Dv NI_DGRAM , | ||
162 | specifies that the service is a datagram service, and causes | ||
163 | .Fn getservbyport | ||
164 | to be called with a second argument of | ||
165 | .Qq udp | ||
166 | instead of its default of | ||
167 | .Qq tcp . | ||
168 | This is required for the few ports (512-514) | ||
169 | that have different services for UDP and TCP. | ||
170 | .Pp | ||
171 | These | ||
172 | .Dv NI_xxx | ||
173 | flags are defined in | ||
174 | .Aq Pa netdb.h . | ||
175 | .\" | ||
176 | .Ss Extension for scoped IPv6 address | ||
177 | The implementation allows experimental numeric IPv6 address notation with | ||
178 | scope identifier. | ||
179 | IPv6 link-local address will appear as a string like | ||
180 | .Dq Li fe80::1%ne0 . | ||
181 | Refer to | ||
182 | .Xr getaddrinfo 3 | ||
183 | for the notation. | ||
184 | .\" | ||
185 | .Sh EXAMPLES | ||
186 | The following code tries to get a numeric hostname, and service name, | ||
187 | for given socket address. | ||
188 | Observe that there is no hardcoded reference to a particular address family. | ||
189 | .Bd -literal -offset indent | ||
190 | struct sockaddr *sa; /* input */ | ||
191 | char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; | ||
192 | |||
193 | if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf, | ||
194 | sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { | ||
195 | errx(1, "could not get numeric hostname"); | ||
196 | /*NOTREACHED*/ | ||
197 | } | ||
198 | printf("host=%s, serv=%s\en", hbuf, sbuf); | ||
199 | .Ed | ||
200 | .Pp | ||
201 | The following version checks if the socket address has reverse address mapping. | ||
202 | .Bd -literal -offset indent | ||
203 | struct sockaddr *sa; /* input */ | ||
204 | char hbuf[NI_MAXHOST]; | ||
205 | |||
206 | if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0, | ||
207 | NI_NAMEREQD)) { | ||
208 | errx(1, "could not resolve hostname"); | ||
209 | /*NOTREACHED*/ | ||
210 | } | ||
211 | printf("host=%s\en", hbuf); | ||
212 | .Ed | ||
213 | .\" | ||
214 | .Sh DIAGNOSTICS | ||
215 | The function indicates successful completion by a zero return value; | ||
216 | a non-zero return value indicates failure. | ||
217 | Error codes are as below: | ||
218 | .Bl -tag -width Er | ||
219 | .It Dv EAI_AGAIN | ||
220 | The name could not be resolved at this time. | ||
221 | Future attempts may succeed. | ||
222 | .It Dv EAI_BADFLAGS | ||
223 | The flags had an invalid value. | ||
224 | .It Dv EAI_FAIL | ||
225 | A non-recoverable error occurred. | ||
226 | .It Dv EAI_FAMILY | ||
227 | The address family was not recognized or the address length was invalid | ||
228 | for the specified family. | ||
229 | .It Dv EAI_MEMORY | ||
230 | There was a memory allocation failure. | ||
231 | .It Dv EAI_NONAME | ||
232 | The name does not resolve for the supplied parameters. | ||
233 | .Dv NI_NAMEREQD | ||
234 | is set and the host's name cannot be located, | ||
235 | or both nodename and servname were null. | ||
236 | .It Dv EAI_SYSTEM | ||
237 | A system error occurred. | ||
238 | The error code can be found in errno. | ||
239 | .El | ||
240 | .\" | ||
241 | .Sh SEE ALSO | ||
242 | .Xr getaddrinfo 3 , | ||
243 | .Xr gethostbyaddr 3 , | ||
244 | .Xr getservbyport 3 , | ||
245 | .Xr hosts 5 , | ||
246 | .Xr resolv.conf 5 , | ||
247 | .Xr services 5 , | ||
248 | .Xr hostname 7 , | ||
249 | .Xr named 8 | ||
250 | .Rs | ||
251 | .%A R. Gilligan | ||
252 | .%A S. Thomson | ||
253 | .%A J. Bound | ||
254 | .%A W. Stevens | ||
255 | .%T Basic Socket Interface Extensions for IPv6 | ||
256 | .%R RFC 2553 | ||
257 | .%D March 1999 | ||
258 | .Re | ||
259 | .Rs | ||
260 | .%A Tatsuya Jinmei | ||
261 | .%A Atsushi Onoe | ||
262 | .%T "An Extension of Format for IPv6 Scoped Addresses" | ||
263 | .%R internet draft | ||
264 | .%N draft-ietf-ipngwg-scopedaddr-format-02.txt | ||
265 | .%O work in progress material | ||
266 | .Re | ||
267 | .Rs | ||
268 | .%A Craig Metz | ||
269 | .%T Protocol Independence Using the Sockets API | ||
270 | .%B "Proceedings of the freenix track: 2000 USENIX annual technical conference" | ||
271 | .%D June 2000 | ||
272 | .Re | ||
273 | .\" | ||
274 | .Sh STANDARDS | ||
275 | The | ||
276 | .Fn getnameinfo | ||
277 | function is defined in IEEE POSIX 1003.1g draft specification, | ||
278 | and documented in | ||
279 | .Dq Basic Socket Interface Extensions for IPv6 | ||
280 | .Pq RFC 2553 . | ||
281 | .\" | ||
282 | .Sh HISTORY | ||
283 | The implementation first appeared in WIDE Hydrangea IPv6 protocol stack kit. | ||
284 | .\" | ||
285 | .Sh CAVEATS | ||
286 | .Fn getnameinfo | ||
287 | returns both numeric and FQDN notation of the address specified in | ||
288 | .Fa sa . | ||
289 | There is no return value that indicates if the string returned in | ||
290 | .Fa host | ||
291 | is a result of binary to numeric-text translation (like | ||
292 | .Xr inet_ntop 3 ) , | ||
293 | or the result of DNS reverse lookup. | ||
294 | Therefore, malicious parties could set up a PTR record as below: | ||
295 | .Bd -literal -offset indent | ||
296 | 1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1 | ||
297 | .Ed | ||
298 | .Pp | ||
299 | and trick the caller of | ||
300 | .Fn getnameinfo | ||
301 | into believing that | ||
302 | .Fa sa | ||
303 | is | ||
304 | .Li 10.1.1.1 | ||
305 | when it actually is | ||
306 | .Li 127.0.0.1 . | ||
307 | .Pp | ||
308 | To prevent such attacks, the use of | ||
309 | .Dv NI_NAMEREQD | ||
310 | is recommended when you use the result of | ||
311 | .Fn getnameinfo | ||
312 | for access control purposes: | ||
313 | .Bd -literal -offset indent | ||
314 | struct sockaddr *sa; | ||
315 | socklen_t salen; | ||
316 | char addr[NI_MAXHOST]; | ||
317 | struct addrinfo hints, *res; | ||
318 | int error; | ||
319 | |||
320 | error = getnameinfo(sa, salen, addr, sizeof(addr), | ||
321 | NULL, 0, NI_NAMEREQD); | ||
322 | if (error == 0) { | ||
323 | memset(&hints, 0, sizeof(hints)); | ||
324 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
325 | hints.ai_flags = AI_NUMERICHOST; | ||
326 | if (getaddrinfo(addr, "0", &hints, &res) == 0) { | ||
327 | /* malicious PTR record */ | ||
328 | freeaddrinfo(res); | ||
329 | printf("bogus PTR record\\n"); | ||
330 | return -1; | ||
331 | } | ||
332 | /* addr is FQDN as a result of PTR lookup */ | ||
333 | } else { | ||
334 | /* addr is numeric string */ | ||
335 | error = getnameinfo(sa, salen, addr, sizeof(addr), | ||
336 | NULL, 0, NI_NUMERICHOST); | ||
337 | } | ||
338 | .Ed | ||
339 | .\" | ||
340 | .Sh BUGS | ||
341 | The current implementation is not thread-safe. | ||
342 | .Pp | ||
343 | The text was shamelessly copied from RFC 2553. | ||
344 | .Pp | ||
345 | .Ox | ||
346 | intentionally uses a different | ||
347 | .Dv NI_MAXHOST | ||
348 | value from what RFC 2553 suggests, to avoid buffer length handling mistakes. | ||
diff --git a/src/lib/libc/net/getnameinfo.c b/src/lib/libc/net/getnameinfo.c new file mode 100644 index 0000000000..da5df507ab --- /dev/null +++ b/src/lib/libc/net/getnameinfo.c | |||
@@ -0,0 +1,372 @@ | |||
1 | /* $OpenBSD: getnameinfo.c,v 1.27 2003/07/21 23:17:53 marc Exp $ */ | ||
2 | /* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
6 | * 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. Neither the name of the project nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | /* | ||
34 | * Issues to be discussed: | ||
35 | * - Thread safe-ness must be checked | ||
36 | * - RFC2553 says that we should raise error on short buffer. X/Open says | ||
37 | * we need to truncate the result. We obey RFC2553 (and X/Open should be | ||
38 | * modified). ipngwg rough consensus seems to follow RFC2553. | ||
39 | * - What is "local" in NI_FQDN? | ||
40 | * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. | ||
41 | * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if | ||
42 | * sin6_scope_id is filled - standardization status? | ||
43 | * XXX breaks backward compat for code that expects no scopeid. | ||
44 | * beware on merge. | ||
45 | */ | ||
46 | |||
47 | #ifndef INET6 | ||
48 | #define INET6 | ||
49 | #endif | ||
50 | |||
51 | #include <sys/types.h> | ||
52 | #include <sys/socket.h> | ||
53 | #include <net/if.h> | ||
54 | #include <netinet/in.h> | ||
55 | #include <arpa/inet.h> | ||
56 | #include <arpa/nameser.h> | ||
57 | #include <netdb.h> | ||
58 | #include <resolv.h> | ||
59 | #include <string.h> | ||
60 | #include <stddef.h> | ||
61 | |||
62 | #include "thread_private.h" | ||
63 | |||
64 | static const struct afd { | ||
65 | int a_af; | ||
66 | int a_addrlen; | ||
67 | int a_socklen; | ||
68 | int a_off; | ||
69 | } afdl [] = { | ||
70 | #ifdef INET6 | ||
71 | {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), | ||
72 | offsetof(struct sockaddr_in6, sin6_addr)}, | ||
73 | #endif | ||
74 | {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), | ||
75 | offsetof(struct sockaddr_in, sin_addr)}, | ||
76 | {0, 0, 0}, | ||
77 | }; | ||
78 | |||
79 | struct sockinet { | ||
80 | u_char si_len; | ||
81 | u_char si_family; | ||
82 | u_short si_port; | ||
83 | }; | ||
84 | |||
85 | #ifdef INET6 | ||
86 | static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, | ||
87 | size_t, int); | ||
88 | static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int); | ||
89 | #endif | ||
90 | |||
91 | /* | ||
92 | * this mutex is also used by get_port in getaddrinfo.c | ||
93 | */ | ||
94 | pthread_mutex_t __THREAD_MUTEX_NAME(serv_mutex) = PTHREAD_MUTEX_INITIALIZER; | ||
95 | |||
96 | int | ||
97 | getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) | ||
98 | const struct sockaddr *sa; | ||
99 | socklen_t salen; | ||
100 | char *host; | ||
101 | size_t hostlen; | ||
102 | char *serv; | ||
103 | size_t servlen; | ||
104 | int flags; | ||
105 | { | ||
106 | const struct afd *afd; | ||
107 | struct servent *sp; | ||
108 | struct hostent *hp; | ||
109 | u_short port; | ||
110 | int family, i; | ||
111 | const char *addr; | ||
112 | u_int32_t v4a; | ||
113 | int h_error; | ||
114 | char numserv[512]; | ||
115 | char numaddr[512]; | ||
116 | |||
117 | if (sa == NULL) | ||
118 | return EAI_FAIL; | ||
119 | |||
120 | if (sa->sa_len != salen) | ||
121 | return EAI_FAIL; | ||
122 | |||
123 | family = sa->sa_family; | ||
124 | for (i = 0; afdl[i].a_af; i++) | ||
125 | if (afdl[i].a_af == family) { | ||
126 | afd = &afdl[i]; | ||
127 | goto found; | ||
128 | } | ||
129 | return EAI_FAMILY; | ||
130 | |||
131 | found: | ||
132 | if (salen != afd->a_socklen) | ||
133 | return EAI_FAIL; | ||
134 | |||
135 | /* network byte order */ | ||
136 | port = ((const struct sockinet *)sa)->si_port; | ||
137 | addr = (const char *)sa + afd->a_off; | ||
138 | |||
139 | if (serv == NULL || servlen == 0) { | ||
140 | /* | ||
141 | * do nothing in this case. | ||
142 | * in case you are wondering if "&&" is more correct than | ||
143 | * "||" here: rfc2553bis-03 says that serv == NULL OR | ||
144 | * servlen == 0 means that the caller does not want the result. | ||
145 | */ | ||
146 | } else { | ||
147 | if (flags & NI_NUMERICSERV) | ||
148 | sp = NULL; | ||
149 | else { | ||
150 | _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); | ||
151 | sp = getservbyport(port, | ||
152 | (flags & NI_DGRAM) ? "udp" : "tcp"); | ||
153 | _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); | ||
154 | } | ||
155 | if (sp) { | ||
156 | if (strlen(sp->s_name) + 1 > servlen) | ||
157 | return EAI_MEMORY; | ||
158 | strlcpy(serv, sp->s_name, servlen); | ||
159 | } else { | ||
160 | snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); | ||
161 | if (strlen(numserv) + 1 > servlen) | ||
162 | return EAI_MEMORY; | ||
163 | strlcpy(serv, numserv, servlen); | ||
164 | } | ||
165 | } | ||
166 | |||
167 | switch (sa->sa_family) { | ||
168 | case AF_INET: | ||
169 | v4a = (u_int32_t) | ||
170 | ntohl(((const struct sockaddr_in *)sa)->sin_addr.s_addr); | ||
171 | if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) | ||
172 | flags |= NI_NUMERICHOST; | ||
173 | v4a >>= IN_CLASSA_NSHIFT; | ||
174 | if (v4a == 0) | ||
175 | flags |= NI_NUMERICHOST; | ||
176 | break; | ||
177 | #ifdef INET6 | ||
178 | case AF_INET6: | ||
179 | { | ||
180 | const struct sockaddr_in6 *sin6; | ||
181 | sin6 = (const struct sockaddr_in6 *)sa; | ||
182 | switch (sin6->sin6_addr.s6_addr[0]) { | ||
183 | case 0x00: | ||
184 | if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) | ||
185 | ; | ||
186 | else if (IN6_IS_ADDR_LOOPBACK(&sin6->sin6_addr)) | ||
187 | ; | ||
188 | else | ||
189 | flags |= NI_NUMERICHOST; | ||
190 | break; | ||
191 | default: | ||
192 | if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { | ||
193 | flags |= NI_NUMERICHOST; | ||
194 | } | ||
195 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) | ||
196 | flags |= NI_NUMERICHOST; | ||
197 | break; | ||
198 | } | ||
199 | } | ||
200 | break; | ||
201 | #endif | ||
202 | } | ||
203 | if (host == NULL || hostlen == 0) { | ||
204 | /* | ||
205 | * do nothing in this case. | ||
206 | * in case you are wondering if "&&" is more correct than | ||
207 | * "||" here: rfc2553bis-03 says that host == NULL or | ||
208 | * hostlen == 0 means that the caller does not want the result. | ||
209 | */ | ||
210 | } else if (flags & NI_NUMERICHOST) { | ||
211 | int numaddrlen; | ||
212 | |||
213 | /* NUMERICHOST and NAMEREQD conflicts with each other */ | ||
214 | if (flags & NI_NAMEREQD) | ||
215 | return EAI_NONAME; | ||
216 | |||
217 | switch(afd->a_af) { | ||
218 | #ifdef INET6 | ||
219 | case AF_INET6: | ||
220 | { | ||
221 | int error; | ||
222 | |||
223 | if ((error = ip6_parsenumeric(sa, addr, host, | ||
224 | hostlen, flags)) != 0) | ||
225 | return(error); | ||
226 | break; | ||
227 | } | ||
228 | #endif | ||
229 | default: | ||
230 | if (inet_ntop(afd->a_af, addr, numaddr, sizeof(numaddr)) | ||
231 | == NULL) | ||
232 | return EAI_SYSTEM; | ||
233 | numaddrlen = strlen(numaddr); | ||
234 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | ||
235 | return EAI_MEMORY; | ||
236 | strlcpy(host, numaddr, hostlen); | ||
237 | break; | ||
238 | } | ||
239 | } else { | ||
240 | _THREAD_PRIVATE_MUTEX_LOCK(serv_mutex); | ||
241 | hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); | ||
242 | _THREAD_PRIVATE_MUTEX_UNLOCK(serv_mutex); | ||
243 | h_error = h_errno; | ||
244 | |||
245 | if (hp) { | ||
246 | #if 0 | ||
247 | /* | ||
248 | * commented out, since "for local host" is not | ||
249 | * implemented here - see RFC2553 p30 | ||
250 | */ | ||
251 | if (flags & NI_NOFQDN) { | ||
252 | char *p; | ||
253 | p = strchr(hp->h_name, '.'); | ||
254 | if (p) | ||
255 | *p = '\0'; | ||
256 | } | ||
257 | #endif | ||
258 | if (strlen(hp->h_name) + 1 > hostlen) { | ||
259 | return EAI_MEMORY; | ||
260 | } | ||
261 | strlcpy(host, hp->h_name, hostlen); | ||
262 | } else { | ||
263 | if (flags & NI_NAMEREQD) | ||
264 | return EAI_NONAME; | ||
265 | switch(afd->a_af) { | ||
266 | #ifdef INET6 | ||
267 | case AF_INET6: | ||
268 | { | ||
269 | int error; | ||
270 | |||
271 | if ((error = ip6_parsenumeric(sa, addr, host, | ||
272 | hostlen, | ||
273 | flags)) != 0) | ||
274 | return(error); | ||
275 | break; | ||
276 | } | ||
277 | #endif | ||
278 | default: | ||
279 | if (inet_ntop(afd->a_af, addr, host, | ||
280 | hostlen) == NULL) | ||
281 | return EAI_SYSTEM; | ||
282 | break; | ||
283 | } | ||
284 | } | ||
285 | } | ||
286 | return(0); | ||
287 | } | ||
288 | |||
289 | #ifdef INET6 | ||
290 | static int | ||
291 | ip6_parsenumeric(sa, addr, host, hostlen, flags) | ||
292 | const struct sockaddr *sa; | ||
293 | const char *addr; | ||
294 | char *host; | ||
295 | size_t hostlen; | ||
296 | int flags; | ||
297 | { | ||
298 | int numaddrlen; | ||
299 | char numaddr[512]; | ||
300 | |||
301 | if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) == NULL) | ||
302 | return EAI_SYSTEM; | ||
303 | |||
304 | numaddrlen = strlen(numaddr); | ||
305 | if (numaddrlen + 1 > hostlen) /* don't forget terminator */ | ||
306 | return EAI_MEMORY; | ||
307 | strlcpy(host, numaddr, hostlen); | ||
308 | |||
309 | if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) { | ||
310 | char zonebuf[MAXHOSTNAMELEN]; | ||
311 | int zonelen; | ||
312 | |||
313 | zonelen = ip6_sa2str( | ||
314 | (const struct sockaddr_in6 *)(const void *)sa, | ||
315 | zonebuf, sizeof(zonebuf), flags); | ||
316 | if (zonelen < 0) | ||
317 | return EAI_MEMORY; | ||
318 | if (zonelen + 1 + numaddrlen + 1 > hostlen) | ||
319 | return EAI_MEMORY; | ||
320 | |||
321 | /* construct <numeric-addr><delim><zoneid> */ | ||
322 | memcpy(host + numaddrlen + 1, zonebuf, | ||
323 | (size_t)zonelen); | ||
324 | host[numaddrlen] = SCOPE_DELIMITER; | ||
325 | host[numaddrlen + 1 + zonelen] = '\0'; | ||
326 | } | ||
327 | |||
328 | return 0; | ||
329 | } | ||
330 | |||
331 | /* ARGSUSED */ | ||
332 | static int | ||
333 | ip6_sa2str(sa6, buf, bufsiz, flags) | ||
334 | const struct sockaddr_in6 *sa6; | ||
335 | char *buf; | ||
336 | size_t bufsiz; | ||
337 | int flags; | ||
338 | { | ||
339 | unsigned int ifindex; | ||
340 | const struct in6_addr *a6; | ||
341 | int n; | ||
342 | |||
343 | ifindex = (unsigned int)sa6->sin6_scope_id; | ||
344 | a6 = &sa6->sin6_addr; | ||
345 | |||
346 | #ifdef notdef | ||
347 | if ((flags & NI_NUMERICSCOPE) != 0) { | ||
348 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | ||
349 | if (n < 0 || n >= bufsiz) | ||
350 | return -1; | ||
351 | else | ||
352 | return n; | ||
353 | } | ||
354 | #endif | ||
355 | |||
356 | /* if_indextoname() does not take buffer size. not a good api... */ | ||
357 | if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && | ||
358 | bufsiz >= IF_NAMESIZE) { | ||
359 | char *p = if_indextoname(ifindex, buf); | ||
360 | if (p) { | ||
361 | return(strlen(p)); | ||
362 | } | ||
363 | } | ||
364 | |||
365 | /* last resort */ | ||
366 | n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); | ||
367 | if (n < 0 || n >= bufsiz) | ||
368 | return -1; | ||
369 | else | ||
370 | return n; | ||
371 | } | ||
372 | #endif /* INET6 */ | ||
diff --git a/src/lib/libc/net/getnetbyaddr.c b/src/lib/libc/net/getnetbyaddr.c new file mode 100644 index 0000000000..29365d4735 --- /dev/null +++ b/src/lib/libc/net/getnetbyaddr.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getnetbyaddr.c,v 1.6 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <netdb.h> | ||
35 | |||
36 | extern int _net_stayopen; | ||
37 | |||
38 | struct netent * | ||
39 | _getnetbyaddr(net, type) | ||
40 | register in_addr_t net; | ||
41 | register int type; | ||
42 | { | ||
43 | register struct netent *p; | ||
44 | |||
45 | setnetent(_net_stayopen); | ||
46 | while ((p = getnetent())) | ||
47 | if (p->n_addrtype == type && p->n_net == net) | ||
48 | break; | ||
49 | if (!_net_stayopen) | ||
50 | endnetent(); | ||
51 | return (p); | ||
52 | } | ||
diff --git a/src/lib/libc/net/getnetbyname.c b/src/lib/libc/net/getnetbyname.c new file mode 100644 index 0000000000..04ab7b6332 --- /dev/null +++ b/src/lib/libc/net/getnetbyname.c | |||
@@ -0,0 +1,58 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getnetbyname.c,v 1.6 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <netdb.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | extern int _net_stayopen; | ||
38 | |||
39 | struct netent * | ||
40 | _getnetbyname(name) | ||
41 | register const char *name; | ||
42 | { | ||
43 | register struct netent *p; | ||
44 | register char **cp; | ||
45 | |||
46 | setnetent(_net_stayopen); | ||
47 | while ((p = getnetent())) { | ||
48 | if (strcasecmp(p->n_name, name) == 0) | ||
49 | break; | ||
50 | for (cp = p->n_aliases; *cp != 0; cp++) | ||
51 | if (strcasecmp(*cp, name) == 0) | ||
52 | goto found; | ||
53 | } | ||
54 | found: | ||
55 | if (!_net_stayopen) | ||
56 | endnetent(); | ||
57 | return (p); | ||
58 | } | ||
diff --git a/src/lib/libc/net/getnetent.3 b/src/lib/libc/net/getnetent.3 new file mode 100644 index 0000000000..7cfb22a01d --- /dev/null +++ b/src/lib/libc/net/getnetent.3 | |||
@@ -0,0 +1,140 @@ | |||
1 | .\" $OpenBSD: getnetent.3,v 1.12 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd March 13, 1997 | ||
31 | .Dt GETNETENT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getnetent , | ||
35 | .Nm getnetbyaddr , | ||
36 | .Nm getnetbyname , | ||
37 | .Nm setnetent , | ||
38 | .Nm endnetent | ||
39 | .Nd get network entry | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <netdb.h> | ||
42 | .Ft struct netent * | ||
43 | .Fn getnetent "void" | ||
44 | .Ft struct netent * | ||
45 | .Fn getnetbyname "char *name" | ||
46 | .Ft struct netent * | ||
47 | .Fn getnetbyaddr "in_addr_t net" "int type" | ||
48 | .Ft void | ||
49 | .Fn setnetent "int stayopen" | ||
50 | .Ft void | ||
51 | .Fn endnetent "void" | ||
52 | .Sh DESCRIPTION | ||
53 | The | ||
54 | .Fn getnetent , | ||
55 | .Fn getnetbyname , | ||
56 | and | ||
57 | .Fn getnetbyaddr | ||
58 | functions each return a pointer to an object with the following structure | ||
59 | containing the broken-out fields of a line in the network database, | ||
60 | .Pa /etc/networks . | ||
61 | .Bd -literal -offset indent | ||
62 | struct netent { | ||
63 | char *n_name; /* official name of net */ | ||
64 | char **n_aliases; /* alias list */ | ||
65 | int n_addrtype; /* net number type */ | ||
66 | in_addr_t n_net; /* net number */ | ||
67 | }; | ||
68 | .Ed | ||
69 | .Pp | ||
70 | The members of this structure are: | ||
71 | .Bl -tag -width n_addrtype | ||
72 | .It Fa n_name | ||
73 | The official name of the network. | ||
74 | .It Fa n_aliases | ||
75 | A zero-terminated list of alternate names for the network. | ||
76 | .It Fa n_addrtype | ||
77 | The type of the network number returned; currently only | ||
78 | .Dv AF_INET . | ||
79 | .It Fa n_net | ||
80 | The network number. | ||
81 | Network numbers are returned in machine byte order. | ||
82 | .El | ||
83 | .Pp | ||
84 | The | ||
85 | .Fn getnetent | ||
86 | function reads the next line of the file, opening the file if necessary. | ||
87 | .Pp | ||
88 | The | ||
89 | .Fn setnetent | ||
90 | function opens and rewinds the file. | ||
91 | If the | ||
92 | .Fa stayopen | ||
93 | flag is non-zero, | ||
94 | the net database will not be closed after each call to | ||
95 | .Fn getnetbyname | ||
96 | or | ||
97 | .Fn getnetbyaddr . | ||
98 | .Pp | ||
99 | The | ||
100 | .Fn endnetent | ||
101 | function closes the file. | ||
102 | .Pp | ||
103 | The | ||
104 | .Fn getnetbyname | ||
105 | and | ||
106 | .Fn getnetbyaddr | ||
107 | functions search the domain name server if the system is configured to use one. | ||
108 | If the search fails, or no name server is configured, they sequentially | ||
109 | search from the beginning of the file until a matching net name or | ||
110 | net address and type is found, or until | ||
111 | .Dv EOF | ||
112 | is encountered. | ||
113 | Network numbers are supplied in host order. | ||
114 | .Sh FILES | ||
115 | .Bl -tag -width /etc/networks -compact | ||
116 | .It Pa /etc/networks | ||
117 | .El | ||
118 | .Sh DIAGNOSTICS | ||
119 | Null pointer (0) returned on | ||
120 | .Dv EOF | ||
121 | or error. | ||
122 | .Sh SEE ALSO | ||
123 | .Xr resolver 3 , | ||
124 | .Xr networks 5 | ||
125 | .Sh HISTORY | ||
126 | The | ||
127 | .Fn getnetent , | ||
128 | .Fn getnetbyaddr , | ||
129 | .Fn getnetbyname , | ||
130 | .Fn setnetent , | ||
131 | and | ||
132 | .Fn endnetent | ||
133 | functions appeared in | ||
134 | .Bx 4.2 . | ||
135 | .Sh BUGS | ||
136 | The data space used by these functions is static; if future use | ||
137 | requires the data, it should be copied before any subsequent calls | ||
138 | to these functions overwrite it. | ||
139 | Only Internet network numbers are currently understood. | ||
140 | Expecting network numbers to fit in no more than 32 bits is naive. | ||
diff --git a/src/lib/libc/net/getnetent.c b/src/lib/libc/net/getnetent.c new file mode 100644 index 0000000000..4c3b38e7d8 --- /dev/null +++ b/src/lib/libc/net/getnetent.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getnetent.c,v 1.9 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | #include <netinet/in.h> | ||
37 | #include <arpa/inet.h> | ||
38 | #include <netdb.h> | ||
39 | #include <stdio.h> | ||
40 | #include <string.h> | ||
41 | |||
42 | #define MAXALIASES 35 | ||
43 | |||
44 | static FILE *netf; | ||
45 | static char line[BUFSIZ+1]; | ||
46 | static struct netent net; | ||
47 | static char *net_aliases[MAXALIASES]; | ||
48 | int _net_stayopen; | ||
49 | |||
50 | void | ||
51 | setnetent(f) | ||
52 | int f; | ||
53 | { | ||
54 | if (netf == NULL) | ||
55 | netf = fopen(_PATH_NETWORKS, "r" ); | ||
56 | else | ||
57 | rewind(netf); | ||
58 | _net_stayopen |= f; | ||
59 | } | ||
60 | |||
61 | void | ||
62 | endnetent() | ||
63 | { | ||
64 | if (netf) { | ||
65 | fclose(netf); | ||
66 | netf = NULL; | ||
67 | } | ||
68 | _net_stayopen = 0; | ||
69 | } | ||
70 | |||
71 | struct netent * | ||
72 | getnetent() | ||
73 | { | ||
74 | char *p, *cp, **q; | ||
75 | size_t len; | ||
76 | |||
77 | if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "r" )) == NULL) | ||
78 | return (NULL); | ||
79 | again: | ||
80 | if ((p = fgetln(netf, &len)) == NULL) | ||
81 | return (NULL); | ||
82 | if (p[len-1] == '\n') | ||
83 | len--; | ||
84 | if (len >= sizeof(line) || len == 0) | ||
85 | goto again; | ||
86 | p = memcpy(line, p, len); | ||
87 | line[len] = '\0'; | ||
88 | if (*p == '#') | ||
89 | goto again; | ||
90 | if ((cp = strchr(p, '#')) != NULL) | ||
91 | *cp = '\0'; | ||
92 | net.n_name = p; | ||
93 | if (strlen(net.n_name) >= MAXHOSTNAMELEN-1) | ||
94 | net.n_name[MAXHOSTNAMELEN-1] = '\0'; | ||
95 | cp = strpbrk(p, " \t"); | ||
96 | if (cp == NULL) | ||
97 | goto again; | ||
98 | *cp++ = '\0'; | ||
99 | while (*cp == ' ' || *cp == '\t') | ||
100 | cp++; | ||
101 | p = strpbrk(cp, " \t"); | ||
102 | if (p != NULL) | ||
103 | *p++ = '\0'; | ||
104 | net.n_net = inet_network(cp); | ||
105 | net.n_addrtype = AF_INET; | ||
106 | q = net.n_aliases = net_aliases; | ||
107 | if (p != NULL) | ||
108 | cp = p; | ||
109 | while (cp && *cp) { | ||
110 | if (*cp == ' ' || *cp == '\t') { | ||
111 | cp++; | ||
112 | continue; | ||
113 | } | ||
114 | if (q < &net_aliases[MAXALIASES - 1]) { | ||
115 | *q++ = cp; | ||
116 | if (strlen(cp) >= MAXHOSTNAMELEN-1) | ||
117 | cp[MAXHOSTNAMELEN-1] = '\0'; | ||
118 | } | ||
119 | cp = strpbrk(cp, " \t"); | ||
120 | if (cp != NULL) | ||
121 | *cp++ = '\0'; | ||
122 | } | ||
123 | *q = NULL; | ||
124 | return (&net); | ||
125 | } | ||
diff --git a/src/lib/libc/net/getnetnamadr.c b/src/lib/libc/net/getnetnamadr.c new file mode 100644 index 0000000000..149216ffd8 --- /dev/null +++ b/src/lib/libc/net/getnetnamadr.c | |||
@@ -0,0 +1,400 @@ | |||
1 | /* $OpenBSD: getnetnamadr.c,v 1.23 2003/06/03 21:09:00 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1997, Jason Downs. 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 | * | ||
15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS | ||
16 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
17 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
18 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, | ||
19 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
20 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
21 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
22 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
23 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
24 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
25 | * SUCH DAMAGE. | ||
26 | */ | ||
27 | /* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro | ||
28 | * Dep. Matematica Universidade de Coimbra, Portugal, Europe | ||
29 | * | ||
30 | * Permission to use, copy, modify, and distribute this software for any | ||
31 | * purpose with or without fee is hereby granted, provided that the above | ||
32 | * copyright notice and this permission notice appear in all copies. | ||
33 | */ | ||
34 | /* | ||
35 | * Copyright (c) 1983, 1993 | ||
36 | * The Regents of the University of California. All rights reserved. | ||
37 | * | ||
38 | * Redistribution and use in source and binary forms, with or without | ||
39 | * modification, are permitted provided that the following conditions | ||
40 | * are met: | ||
41 | * 1. Redistributions of source code must retain the above copyright | ||
42 | * notice, this list of conditions and the following disclaimer. | ||
43 | * 2. Redistributions in binary form must reproduce the above copyright | ||
44 | * notice, this list of conditions and the following disclaimer in the | ||
45 | * documentation and/or other materials provided with the distribution. | ||
46 | * 3. Neither the name of the University nor the names of its contributors | ||
47 | * may be used to endorse or promote products derived from this software | ||
48 | * without specific prior written permission. | ||
49 | * | ||
50 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
51 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
52 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
53 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
54 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
55 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
56 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
57 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
58 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
59 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
60 | * SUCH DAMAGE. | ||
61 | */ | ||
62 | |||
63 | #if defined(LIBC_SCCS) && !defined(lint) | ||
64 | #if 0 | ||
65 | static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; | ||
66 | static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03"; | ||
67 | static char rcsid[] = "$From: getnetnamadr.c,v 8.7 1996/08/05 08:31:35 vixie Exp $"; | ||
68 | #else | ||
69 | static char rcsid[] = "$OpenBSD: getnetnamadr.c,v 1.23 2003/06/03 21:09:00 deraadt Exp $"; | ||
70 | #endif | ||
71 | #endif /* LIBC_SCCS and not lint */ | ||
72 | |||
73 | #include <sys/types.h> | ||
74 | #include <sys/param.h> | ||
75 | #include <sys/socket.h> | ||
76 | #include <netinet/in.h> | ||
77 | #include <arpa/inet.h> | ||
78 | #include <arpa/nameser.h> | ||
79 | |||
80 | #include <stdio.h> | ||
81 | #include <netdb.h> | ||
82 | #include <resolv.h> | ||
83 | #include <ctype.h> | ||
84 | #include <errno.h> | ||
85 | #include <string.h> | ||
86 | #include <stdlib.h> | ||
87 | |||
88 | #include "thread_private.h" | ||
89 | |||
90 | extern int h_errno; | ||
91 | |||
92 | struct netent *_getnetbyaddr(in_addr_t net, int type); | ||
93 | struct netent *_getnetbyname(const char *name); | ||
94 | |||
95 | int _hokchar(const char *); | ||
96 | |||
97 | #define BYADDR 0 | ||
98 | #define BYNAME 1 | ||
99 | #define MAXALIASES 35 | ||
100 | |||
101 | #define MAXPACKET (64*1024) | ||
102 | |||
103 | typedef union { | ||
104 | HEADER hdr; | ||
105 | u_char buf[MAXPACKET]; | ||
106 | } querybuf; | ||
107 | |||
108 | typedef union { | ||
109 | long al; | ||
110 | char ac; | ||
111 | } align; | ||
112 | |||
113 | static struct netent * | ||
114 | getnetanswer(answer, anslen, net_i) | ||
115 | querybuf *answer; | ||
116 | int anslen; | ||
117 | int net_i; | ||
118 | { | ||
119 | |||
120 | register HEADER *hp; | ||
121 | register u_char *cp; | ||
122 | register int n; | ||
123 | u_char *eom; | ||
124 | int type, class, ancount, qdcount, haveanswer, i, nchar; | ||
125 | char aux1[MAXHOSTNAMELEN], aux2[MAXHOSTNAMELEN], ans[MAXHOSTNAMELEN]; | ||
126 | char *in, *st, *pauxt, *bp, **ap, *ep; | ||
127 | char *paux1 = &aux1[0], *paux2 = &aux2[0]; | ||
128 | static struct netent net_entry; | ||
129 | static char *net_aliases[MAXALIASES], netbuf[BUFSIZ+1]; | ||
130 | |||
131 | /* | ||
132 | * find first satisfactory answer | ||
133 | * | ||
134 | * answer --> +------------+ ( MESSAGE ) | ||
135 | * | Header | | ||
136 | * +------------+ | ||
137 | * | Question | the question for the name server | ||
138 | * +------------+ | ||
139 | * | Answer | RRs answering the question | ||
140 | * +------------+ | ||
141 | * | Authority | RRs pointing toward an authority | ||
142 | * | Additional | RRs holding additional information | ||
143 | * +------------+ | ||
144 | */ | ||
145 | eom = answer->buf + anslen; | ||
146 | hp = &answer->hdr; | ||
147 | ancount = ntohs(hp->ancount); /* #/records in the answer section */ | ||
148 | qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ | ||
149 | bp = netbuf; | ||
150 | ep = netbuf + sizeof(netbuf); | ||
151 | cp = answer->buf + HFIXEDSZ; | ||
152 | if (!qdcount) { | ||
153 | if (hp->aa) | ||
154 | h_errno = HOST_NOT_FOUND; | ||
155 | else | ||
156 | h_errno = TRY_AGAIN; | ||
157 | return (NULL); | ||
158 | } | ||
159 | while (qdcount-- > 0) { | ||
160 | n = __dn_skipname(cp, eom); | ||
161 | if (n < 0 || (cp + n + QFIXEDSZ) > eom) { | ||
162 | h_errno = NO_RECOVERY; | ||
163 | return(NULL); | ||
164 | } | ||
165 | cp += n + QFIXEDSZ; | ||
166 | } | ||
167 | ap = net_aliases; | ||
168 | *ap = NULL; | ||
169 | net_entry.n_aliases = net_aliases; | ||
170 | haveanswer = 0; | ||
171 | while (--ancount >= 0 && cp < eom) { | ||
172 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
173 | #ifdef USE_RESOLV_NAME_OK | ||
174 | if ((n < 0) || !res_dnok(bp)) | ||
175 | #else | ||
176 | if ((n < 0) || !_hokchar(bp)) | ||
177 | #endif | ||
178 | break; | ||
179 | cp += n; | ||
180 | ans[0] = '\0'; | ||
181 | strlcpy(&ans[0], bp, sizeof ans); | ||
182 | GETSHORT(type, cp); | ||
183 | GETSHORT(class, cp); | ||
184 | cp += INT32SZ; /* TTL */ | ||
185 | GETSHORT(n, cp); | ||
186 | if (class == C_IN && type == T_PTR) { | ||
187 | n = dn_expand(answer->buf, eom, cp, bp, ep - bp); | ||
188 | #ifdef USE_RESOLV_NAME_OK | ||
189 | if ((n < 0) || !res_hnok(bp)) | ||
190 | #else | ||
191 | if ((n < 0) || !_hokchar(bp)) | ||
192 | #endif | ||
193 | { | ||
194 | cp += n; | ||
195 | return (NULL); | ||
196 | } | ||
197 | cp += n; | ||
198 | if ((ap + 2) < &net_aliases[MAXALIASES]) { | ||
199 | *ap++ = bp; | ||
200 | bp += strlen(bp) + 1; | ||
201 | net_entry.n_addrtype = | ||
202 | (class == C_IN) ? AF_INET : AF_UNSPEC; | ||
203 | haveanswer++; | ||
204 | } | ||
205 | } | ||
206 | } | ||
207 | if (haveanswer) { | ||
208 | *ap = NULL; | ||
209 | switch (net_i) { | ||
210 | case BYADDR: | ||
211 | net_entry.n_name = *net_entry.n_aliases; | ||
212 | net_entry.n_net = 0L; | ||
213 | break; | ||
214 | case BYNAME: | ||
215 | ap = net_entry.n_aliases; | ||
216 | next_alias: | ||
217 | in = *ap++; | ||
218 | if (in == NULL) { | ||
219 | h_errno = HOST_NOT_FOUND; | ||
220 | return (NULL); | ||
221 | } | ||
222 | net_entry.n_name = ans; | ||
223 | aux2[0] = '\0'; | ||
224 | for (i = 0; i < 4; i++) { | ||
225 | for (st = in, nchar = 0; | ||
226 | isdigit((unsigned char)*st); | ||
227 | st++, nchar++) | ||
228 | ; | ||
229 | if (*st != '.' || nchar == 0 || nchar > 3) | ||
230 | goto next_alias; | ||
231 | if (i != 0) | ||
232 | nchar++; | ||
233 | strlcpy(paux1, in, nchar+1); | ||
234 | strlcat(paux1, paux2, MAXHOSTNAMELEN); | ||
235 | pauxt = paux2; | ||
236 | paux2 = paux1; | ||
237 | paux1 = pauxt; | ||
238 | in = ++st; | ||
239 | } | ||
240 | if (strcasecmp(in, "IN-ADDR.ARPA") != 0) | ||
241 | goto next_alias; | ||
242 | net_entry.n_net = inet_network(paux2); | ||
243 | break; | ||
244 | } | ||
245 | net_entry.n_aliases++; | ||
246 | return (&net_entry); | ||
247 | } | ||
248 | h_errno = TRY_AGAIN; | ||
249 | return (NULL); | ||
250 | } | ||
251 | |||
252 | struct netent * | ||
253 | getnetbyaddr(net, net_type) | ||
254 | register in_addr_t net; | ||
255 | register int net_type; | ||
256 | { | ||
257 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
258 | unsigned int netbr[4]; | ||
259 | int nn, anslen; | ||
260 | querybuf *buf; | ||
261 | char qbuf[MAXDNAME]; | ||
262 | in_addr_t net2; | ||
263 | struct netent *net_entry = NULL; | ||
264 | char lookups[MAXDNSLUS]; | ||
265 | int i; | ||
266 | |||
267 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
268 | return(_getnetbyaddr(net, net_type)); | ||
269 | |||
270 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
271 | if (lookups[0] == '\0') | ||
272 | strlcpy(lookups, "bf", sizeof lookups); | ||
273 | |||
274 | for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { | ||
275 | switch (lookups[i]) { | ||
276 | #ifdef YP | ||
277 | case 'y': | ||
278 | /* There is no YP support. */ | ||
279 | break; | ||
280 | #endif /* YP */ | ||
281 | case 'b': | ||
282 | if (net_type != AF_INET) | ||
283 | break; /* DNS only supports AF_INET? */ | ||
284 | |||
285 | for (nn = 4, net2 = net; net2; net2 >>= 8) | ||
286 | netbr[--nn] = net2 & 0xff; | ||
287 | switch (nn) { | ||
288 | case 3: /* Class A */ | ||
289 | snprintf(qbuf, sizeof(qbuf), | ||
290 | "0.0.0.%u.in-addr.arpa", netbr[3]); | ||
291 | break; | ||
292 | case 2: /* Class B */ | ||
293 | snprintf(qbuf, sizeof(qbuf), | ||
294 | "0.0.%u.%u.in-addr.arpa", | ||
295 | netbr[3], netbr[2]); | ||
296 | break; | ||
297 | case 1: /* Class C */ | ||
298 | snprintf(qbuf, sizeof(qbuf), | ||
299 | "0.%u.%u.%u.in-addr.arpa", | ||
300 | netbr[3], netbr[2], netbr[1]); | ||
301 | break; | ||
302 | case 0: /* Class D - E */ | ||
303 | snprintf(qbuf, sizeof(qbuf), | ||
304 | "%u.%u.%u.%u.in-addr.arpa", | ||
305 | netbr[3], netbr[2], netbr[1], netbr[0]); | ||
306 | break; | ||
307 | } | ||
308 | buf = malloc(sizeof(*buf)); | ||
309 | if (buf == NULL) | ||
310 | break; | ||
311 | anslen = res_query(qbuf, C_IN, T_PTR, buf->buf, | ||
312 | sizeof(buf->buf)); | ||
313 | if (anslen < 0) { | ||
314 | free(buf); | ||
315 | #ifdef DEBUG | ||
316 | if (_resp->options & RES_DEBUG) | ||
317 | printf("res_query failed\n"); | ||
318 | #endif | ||
319 | break; | ||
320 | } | ||
321 | net_entry = getnetanswer(buf, anslen, BYADDR); | ||
322 | free(buf); | ||
323 | if (net_entry != NULL) { | ||
324 | unsigned u_net = net; /* maybe net should be unsigned ? */ | ||
325 | |||
326 | /* Strip trailing zeros */ | ||
327 | while ((u_net & 0xff) == 0 && u_net != 0) | ||
328 | u_net >>= 8; | ||
329 | net_entry->n_net = u_net; | ||
330 | return (net_entry); | ||
331 | } | ||
332 | break; | ||
333 | case 'f': | ||
334 | net_entry = _getnetbyaddr(net, net_type); | ||
335 | if (net_entry != NULL) | ||
336 | return (net_entry); | ||
337 | } | ||
338 | } | ||
339 | |||
340 | /* Nothing matched. */ | ||
341 | return (NULL); | ||
342 | } | ||
343 | |||
344 | struct netent * | ||
345 | getnetbyname(net) | ||
346 | register const char *net; | ||
347 | { | ||
348 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
349 | int anslen; | ||
350 | querybuf *buf; | ||
351 | char qbuf[MAXDNAME]; | ||
352 | struct netent *net_entry = NULL; | ||
353 | char lookups[MAXDNSLUS]; | ||
354 | int i; | ||
355 | |||
356 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
357 | return (_getnetbyname(net)); | ||
358 | |||
359 | bcopy(_resp->lookups, lookups, sizeof lookups); | ||
360 | if (lookups[0] == '\0') | ||
361 | strlcpy(lookups, "bf", sizeof lookups); | ||
362 | |||
363 | for (i = 0; i < MAXDNSLUS && lookups[i]; i++) { | ||
364 | switch (lookups[i]) { | ||
365 | #ifdef YP | ||
366 | case 'y': | ||
367 | /* There is no YP support. */ | ||
368 | break; | ||
369 | #endif /* YP */ | ||
370 | case 'b': | ||
371 | strlcpy(qbuf, net, sizeof qbuf); | ||
372 | buf = malloc(sizeof(*buf)); | ||
373 | if (buf == NULL) | ||
374 | break; | ||
375 | anslen = res_search(qbuf, C_IN, T_PTR, buf->buf, | ||
376 | sizeof(buf->buf)); | ||
377 | if (anslen < 0) { | ||
378 | free(buf); | ||
379 | #ifdef DEBUG | ||
380 | if (_resp->options & RES_DEBUG) | ||
381 | printf("res_query failed\n"); | ||
382 | #endif | ||
383 | break; | ||
384 | } | ||
385 | net_entry = getnetanswer(buf, anslen, BYNAME); | ||
386 | free(buf); | ||
387 | if (net_entry != NULL) | ||
388 | return (net_entry); | ||
389 | break; | ||
390 | case 'f': | ||
391 | net_entry = _getnetbyname(net); | ||
392 | if (net_entry != NULL) | ||
393 | return (net_entry); | ||
394 | break; | ||
395 | } | ||
396 | } | ||
397 | |||
398 | /* Nothing matched. */ | ||
399 | return (NULL); | ||
400 | } | ||
diff --git a/src/lib/libc/net/getproto.c b/src/lib/libc/net/getproto.c new file mode 100644 index 0000000000..dee089d5c2 --- /dev/null +++ b/src/lib/libc/net/getproto.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getproto.c,v 1.4 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <netdb.h> | ||
35 | |||
36 | extern int _proto_stayopen; | ||
37 | |||
38 | struct protoent * | ||
39 | getprotobynumber(proto) | ||
40 | register int proto; | ||
41 | { | ||
42 | register struct protoent *p; | ||
43 | |||
44 | setprotoent(_proto_stayopen); | ||
45 | while ((p = getprotoent())) | ||
46 | if (p->p_proto == proto) | ||
47 | break; | ||
48 | if (!_proto_stayopen) | ||
49 | endprotoent(); | ||
50 | return (p); | ||
51 | } | ||
diff --git a/src/lib/libc/net/getprotoent.3 b/src/lib/libc/net/getprotoent.3 new file mode 100644 index 0000000000..be257966a2 --- /dev/null +++ b/src/lib/libc/net/getprotoent.3 | |||
@@ -0,0 +1,130 @@ | |||
1 | .\" $OpenBSD: getprotoent.3,v 1.9 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt GETPROTOENT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getprotoent , | ||
35 | .Nm getprotobynumber , | ||
36 | .Nm getprotobyname , | ||
37 | .Nm setprotoent , | ||
38 | .Nm endprotoent | ||
39 | .Nd get protocol entry | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <netdb.h> | ||
42 | .Ft struct protoent * | ||
43 | .Fn getprotoent "void" | ||
44 | .Ft struct protoent * | ||
45 | .Fn getprotobyname "char *name" | ||
46 | .Ft struct protoent * | ||
47 | .Fn getprotobynumber "int proto" | ||
48 | .Ft void | ||
49 | .Fn setprotoent "int stayopen" | ||
50 | .Ft void | ||
51 | .Fn endprotoent "void" | ||
52 | .Sh DESCRIPTION | ||
53 | The | ||
54 | .Fn getprotoent , | ||
55 | .Fn getprotobyname , | ||
56 | and | ||
57 | .Fn getprotobynumber | ||
58 | functions each return a pointer to an object with the following structure | ||
59 | containing the broken-out fields of a line in the network protocol database, | ||
60 | .Pa /etc/protocols . | ||
61 | .Bd -literal -offset indent | ||
62 | .Pp | ||
63 | struct protoent { | ||
64 | char *p_name; /* official name of protocol */ | ||
65 | char **p_aliases; /* alias list */ | ||
66 | int p_proto; /* protocol number */ | ||
67 | }; | ||
68 | .Ed | ||
69 | .Pp | ||
70 | The members of this structure are: | ||
71 | .Bl -tag -width p_aliases | ||
72 | .It Fa p_name | ||
73 | The official name of the protocol. | ||
74 | .It Fa p_aliases | ||
75 | A zero-terminated list of alternate names for the protocol. | ||
76 | .It Fa p_proto | ||
77 | The protocol number. | ||
78 | .El | ||
79 | .Pp | ||
80 | The | ||
81 | .Fn getprotoent | ||
82 | function reads the next line of the file, opening the file if necessary. | ||
83 | .Pp | ||
84 | The | ||
85 | .Fn setprotoent | ||
86 | function opens and rewinds the file. | ||
87 | If the | ||
88 | .Fa stayopen | ||
89 | flag is non-zero, | ||
90 | the net database will not be closed after each call to | ||
91 | .Fn getprotobyname | ||
92 | or | ||
93 | .Fn getprotobynumber . | ||
94 | .Pp | ||
95 | The | ||
96 | .Fn endprotoent | ||
97 | function closes the file. | ||
98 | .Pp | ||
99 | The | ||
100 | .Fn getprotobyname | ||
101 | and | ||
102 | .Fn getprotobynumber | ||
103 | functions sequentially search from the beginning of the file until a | ||
104 | matching protocol name or protocol number is found, or until | ||
105 | .Dv EOF | ||
106 | is encountered. | ||
107 | .Sh RETURN VALUES | ||
108 | Null pointer (0) returned on | ||
109 | .Dv EOF | ||
110 | or error. | ||
111 | .Sh FILES | ||
112 | .Bl -tag -width /etc/protocols -compact | ||
113 | .It Pa /etc/protocols | ||
114 | .El | ||
115 | .Sh SEE ALSO | ||
116 | .Xr protocols 5 | ||
117 | .Sh HISTORY | ||
118 | The | ||
119 | .Fn getprotoent , | ||
120 | .Fn getprotobynumber , | ||
121 | .Fn getprotobyname , | ||
122 | .Fn setprotoent , | ||
123 | and | ||
124 | .Fn endprotoent | ||
125 | functions appeared in | ||
126 | .Bx 4.2 . | ||
127 | .Sh BUGS | ||
128 | These functions use a static data space; if the data is needed for future use, | ||
129 | it should be copied before any subsequent calls overwrite it. | ||
130 | Only the Internet protocols are currently understood. | ||
diff --git a/src/lib/libc/net/getprotoent.c b/src/lib/libc/net/getprotoent.c new file mode 100644 index 0000000000..b2bdd2e164 --- /dev/null +++ b/src/lib/libc/net/getprotoent.c | |||
@@ -0,0 +1,123 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getprotoent.c,v 1.5 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | #include <netdb.h> | ||
37 | #include <stdio.h> | ||
38 | #include <stdlib.h> | ||
39 | #include <string.h> | ||
40 | |||
41 | #define MAXALIASES 35 | ||
42 | |||
43 | static FILE *protof = NULL; | ||
44 | static char line[BUFSIZ+1]; | ||
45 | static struct protoent proto; | ||
46 | static char *proto_aliases[MAXALIASES]; | ||
47 | int _proto_stayopen; | ||
48 | |||
49 | void | ||
50 | setprotoent(f) | ||
51 | int f; | ||
52 | { | ||
53 | if (protof == NULL) | ||
54 | protof = fopen(_PATH_PROTOCOLS, "r" ); | ||
55 | else | ||
56 | rewind(protof); | ||
57 | _proto_stayopen |= f; | ||
58 | } | ||
59 | |||
60 | void | ||
61 | endprotoent() | ||
62 | { | ||
63 | if (protof) { | ||
64 | fclose(protof); | ||
65 | protof = NULL; | ||
66 | } | ||
67 | _proto_stayopen = 0; | ||
68 | } | ||
69 | |||
70 | struct protoent * | ||
71 | getprotoent() | ||
72 | { | ||
73 | char *p, *cp, **q, *endp; | ||
74 | long l; | ||
75 | size_t len; | ||
76 | |||
77 | if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL) | ||
78 | return (NULL); | ||
79 | again: | ||
80 | if ((p = fgetln(protof, &len)) == NULL) | ||
81 | return (NULL); | ||
82 | if (p[len-1] == '\n') | ||
83 | len--; | ||
84 | if (len >= sizeof(line) || len == 0) | ||
85 | goto again; | ||
86 | p = memcpy(line, p, len); | ||
87 | line[len] = '\0'; | ||
88 | if (*p == '#') | ||
89 | goto again; | ||
90 | if ((cp = strchr(p, '#')) != NULL) | ||
91 | *cp = '\0'; | ||
92 | proto.p_name = p; | ||
93 | cp = strpbrk(p, " \t"); | ||
94 | if (cp == NULL) | ||
95 | goto again; | ||
96 | *cp++ = '\0'; | ||
97 | while (*cp == ' ' || *cp == '\t') | ||
98 | cp++; | ||
99 | p = strpbrk(cp, " \t"); | ||
100 | if (p != NULL) | ||
101 | *p++ = '\0'; | ||
102 | l = strtol(cp, &endp, 10); | ||
103 | if (endp == cp || *endp != '\0' || l < 0 || l >= INT_MAX) | ||
104 | goto again; | ||
105 | proto.p_proto = l; | ||
106 | q = proto.p_aliases = proto_aliases; | ||
107 | if (p != NULL) { | ||
108 | cp = p; | ||
109 | while (cp && *cp) { | ||
110 | if (*cp == ' ' || *cp == '\t') { | ||
111 | cp++; | ||
112 | continue; | ||
113 | } | ||
114 | if (q < &proto_aliases[MAXALIASES - 1]) | ||
115 | *q++ = cp; | ||
116 | cp = strpbrk(cp, " \t"); | ||
117 | if (cp != NULL) | ||
118 | *cp++ = '\0'; | ||
119 | } | ||
120 | } | ||
121 | *q = NULL; | ||
122 | return (&proto); | ||
123 | } | ||
diff --git a/src/lib/libc/net/getprotoname.c b/src/lib/libc/net/getprotoname.c new file mode 100644 index 0000000000..4742a60a04 --- /dev/null +++ b/src/lib/libc/net/getprotoname.c | |||
@@ -0,0 +1,58 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getprotoname.c,v 1.4 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <netdb.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | extern int _proto_stayopen; | ||
38 | |||
39 | struct protoent * | ||
40 | getprotobyname(name) | ||
41 | register const char *name; | ||
42 | { | ||
43 | register struct protoent *p; | ||
44 | register char **cp; | ||
45 | |||
46 | setprotoent(_proto_stayopen); | ||
47 | while ((p = getprotoent())) { | ||
48 | if (strcmp(p->p_name, name) == 0) | ||
49 | break; | ||
50 | for (cp = p->p_aliases; *cp != 0; cp++) | ||
51 | if (strcmp(*cp, name) == 0) | ||
52 | goto found; | ||
53 | } | ||
54 | found: | ||
55 | if (!_proto_stayopen) | ||
56 | endprotoent(); | ||
57 | return (p); | ||
58 | } | ||
diff --git a/src/lib/libc/net/getrrsetbyname.3 b/src/lib/libc/net/getrrsetbyname.3 new file mode 100644 index 0000000000..a786ff8747 --- /dev/null +++ b/src/lib/libc/net/getrrsetbyname.3 | |||
@@ -0,0 +1,165 @@ | |||
1 | .\" $OpenBSD: getrrsetbyname.3,v 1.11 2003/05/30 21:37:59 jmc Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (C) 2000, 2001 Internet Software Consortium. | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM | ||
10 | .\" DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | ||
11 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL | ||
12 | .\" INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
13 | .\" INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING | ||
14 | .\" FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | ||
15 | .\" NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | ||
16 | .\" WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | .\" | ||
18 | .Dd October 18, 2000 | ||
19 | .Dt GETRRSETBYNAME 3 | ||
20 | .Os | ||
21 | .Sh NAME | ||
22 | .Nm getrrsetbyname | ||
23 | .Nd retrieve DNS records | ||
24 | .Sh SYNOPSIS | ||
25 | .Fd #include <netdb.h> | ||
26 | .Ft int | ||
27 | .Fn getrrsetbyname "const char *hostname" "unsigned int rdclass" \ | ||
28 | "unsigned int rdtype" "unsigned int flags" "struct rrsetinfo **res" | ||
29 | .Ft int | ||
30 | .Fn freerrset "struct rrsetinfo **rrset" | ||
31 | .Sh DESCRIPTION | ||
32 | .Fn getrrsetbyname | ||
33 | gets a set of resource records associated with a | ||
34 | .Fa hostname , | ||
35 | .Fa class | ||
36 | and | ||
37 | .Fa type . | ||
38 | .Fa hostname | ||
39 | is a pointer a to null-terminated string. | ||
40 | The | ||
41 | .Fa flags | ||
42 | field is currently unused and must be zero. | ||
43 | .Pp | ||
44 | After a successful call to | ||
45 | .Fn getrrsetbyname , | ||
46 | .Fa *res | ||
47 | is a pointer to an | ||
48 | .Li rrsetinfo | ||
49 | structure, containing a list of one or more | ||
50 | .Li rdatainfo | ||
51 | structures containing resource records and potentially another list of | ||
52 | .Li rdatainfo | ||
53 | structures containing SIG resource records associated with those records. | ||
54 | The members | ||
55 | .Li rri_rdclass | ||
56 | and | ||
57 | .Li rri_rdtype | ||
58 | are copied from the parameters. | ||
59 | .Li rri_ttl | ||
60 | and | ||
61 | .Li rri_name | ||
62 | are properties of the obtained rrset. | ||
63 | The resource records contained in | ||
64 | .Li rri_rdatas | ||
65 | and | ||
66 | .Li rri_sigs | ||
67 | are in uncompressed DNS wire format. | ||
68 | Properties of the rdataset are represented in the | ||
69 | .Li rri_flags | ||
70 | bitfield. | ||
71 | If the | ||
72 | .Dv RRSET_VALIDATED | ||
73 | bit is set, the data has been DNSSEC | ||
74 | validated and the signatures verified. | ||
75 | .Pp | ||
76 | The following structures are used: | ||
77 | .Bd -literal -offset | ||
78 | struct rdatainfo { | ||
79 | unsigned int rdi_length; /* length of data */ | ||
80 | unsigned char *rdi_data; /* record data */ | ||
81 | }; | ||
82 | |||
83 | struct rrsetinfo { | ||
84 | unsigned int rri_flags; /* RRSET_VALIDATED ... */ | ||
85 | unsigned int rri_rdclass; /* class number */ | ||
86 | unsigned int rri_rdtype; /* RR type number */ | ||
87 | unsigned int rri_ttl; /* time to live */ | ||
88 | unsigned int rri_nrdatas; /* size of rdatas array */ | ||
89 | unsigned int rri_nsigs; /* size of sigs array */ | ||
90 | char *rri_name; /* canonical name */ | ||
91 | struct rdatainfo *rri_rdatas; /* individual records */ | ||
92 | struct rdatainfo *rri_sigs; /* individual signatures */ | ||
93 | }; | ||
94 | .Ed | ||
95 | .Pp | ||
96 | All of the information returned by | ||
97 | .Fn getrrsetbyname | ||
98 | is dynamically allocated: the | ||
99 | .Li rrsetinfo | ||
100 | and | ||
101 | .Li rdatainfo | ||
102 | structures, | ||
103 | and the canonical host name strings pointed to by the | ||
104 | .Li rrsetinfo | ||
105 | structure. | ||
106 | Memory allocated for the dynamically allocated structures created by | ||
107 | a successful call to | ||
108 | .Fn getrrsetbyname | ||
109 | is released by | ||
110 | .Fn freerrset . | ||
111 | .Li rrset | ||
112 | is a pointer to a | ||
113 | .Li struct rrset | ||
114 | created by a call to | ||
115 | .Fn getrrsetbyname . | ||
116 | .Pp | ||
117 | If the EDNS0 option is activated in | ||
118 | .Xr resolv.conf 5 , | ||
119 | .Fn getrrsetbyname | ||
120 | will request DNSSEC authentication using the EDNS0 DNSSEC OK (DO) bit. | ||
121 | .Sh RETURN VALUES | ||
122 | .Fn getrrsetbyname | ||
123 | returns zero on success, and one of the following error | ||
124 | codes if an error occurred: | ||
125 | .Pp | ||
126 | .Bl -tag -width ERRSET_NOMEMORY -compact | ||
127 | .It Dv ERRSET_NONAME | ||
128 | the name does not exist | ||
129 | .It Dv ERRSET_NODATA | ||
130 | the name exists, but does not have data of the desired type | ||
131 | .It Dv ERRSET_NOMEMORY | ||
132 | memory could not be allocated | ||
133 | .It Dv ERRSET_INVAL | ||
134 | a parameter is invalid | ||
135 | .It Dv ERRSET_FAIL | ||
136 | other failure | ||
137 | .El | ||
138 | .Sh SEE ALSO | ||
139 | .Xr resolver 3 , | ||
140 | .Xr resolv.conf 5 , | ||
141 | .Xr named 8 | ||
142 | .Sh HISTORY | ||
143 | .Fn getrrsetbyname | ||
144 | first appeared in | ||
145 | .Ox 3.0 . | ||
146 | The API first appeared in ISC BIND version 9. | ||
147 | .Sh AUTHORS | ||
148 | .An Jakob Schlyter Aq jakob@openbsd.org | ||
149 | .Sh CAVEATS | ||
150 | The | ||
151 | .Dv RRSET_VALIDATED | ||
152 | flag in | ||
153 | .Li rri_flags | ||
154 | is set if the AD (authenticated data) bit in the DNS answer is | ||
155 | set. | ||
156 | This flag | ||
157 | .Em should not | ||
158 | be trusted unless the transport between the nameserver and the resolver | ||
159 | is secure (e.g. IPsec, trusted network, loopback communication). | ||
160 | .Sh BUGS | ||
161 | The data in | ||
162 | .Li *rdi_data | ||
163 | should be returned in uncompressed wire format. | ||
164 | Currently, the data is in compressed format and the caller can't | ||
165 | uncompress since it doesn't have the full message. | ||
diff --git a/src/lib/libc/net/getrrsetbyname.c b/src/lib/libc/net/getrrsetbyname.c new file mode 100644 index 0000000000..f00ac2b021 --- /dev/null +++ b/src/lib/libc/net/getrrsetbyname.c | |||
@@ -0,0 +1,512 @@ | |||
1 | /* $OpenBSD: getrrsetbyname.c,v 1.7 2003/03/07 07:34:14 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2001 Jakob Schlyter. 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 | * | ||
10 | * 1. Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * | ||
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 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
27 | */ | ||
28 | |||
29 | /* | ||
30 | * Portions Copyright (c) 1999-2001 Internet Software Consortium. | ||
31 | * | ||
32 | * Permission to use, copy, modify, and distribute this software for any | ||
33 | * purpose with or without fee is hereby granted, provided that the above | ||
34 | * copyright notice and this permission notice appear in all copies. | ||
35 | * | ||
36 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM | ||
37 | * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL | ||
38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL | ||
39 | * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
40 | * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING | ||
41 | * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, | ||
42 | * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION | ||
43 | * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
44 | */ | ||
45 | |||
46 | #include <sys/types.h> | ||
47 | #include <netinet/in.h> | ||
48 | #include <arpa/nameser.h> | ||
49 | #include <netdb.h> | ||
50 | #include <resolv.h> | ||
51 | #include <stdlib.h> | ||
52 | #include <string.h> | ||
53 | |||
54 | #include "thread_private.h" | ||
55 | |||
56 | #define ANSWER_BUFFER_SIZE 1024*64 | ||
57 | |||
58 | struct dns_query { | ||
59 | char *name; | ||
60 | u_int16_t type; | ||
61 | u_int16_t class; | ||
62 | struct dns_query *next; | ||
63 | }; | ||
64 | |||
65 | struct dns_rr { | ||
66 | char *name; | ||
67 | u_int16_t type; | ||
68 | u_int16_t class; | ||
69 | u_int16_t ttl; | ||
70 | u_int16_t size; | ||
71 | void *rdata; | ||
72 | struct dns_rr *next; | ||
73 | }; | ||
74 | |||
75 | struct dns_response { | ||
76 | HEADER header; | ||
77 | struct dns_query *query; | ||
78 | struct dns_rr *answer; | ||
79 | struct dns_rr *authority; | ||
80 | struct dns_rr *additional; | ||
81 | }; | ||
82 | |||
83 | static struct dns_response *parse_dns_response(const u_char *, int); | ||
84 | static struct dns_query *parse_dns_qsection(const u_char *, int, | ||
85 | const u_char **, int); | ||
86 | static struct dns_rr *parse_dns_rrsection(const u_char *, int, const u_char **, | ||
87 | int); | ||
88 | |||
89 | static void free_dns_query(struct dns_query *); | ||
90 | static void free_dns_rr(struct dns_rr *); | ||
91 | static void free_dns_response(struct dns_response *); | ||
92 | |||
93 | static int count_dns_rr(struct dns_rr *, u_int16_t, u_int16_t); | ||
94 | |||
95 | int | ||
96 | getrrsetbyname(const char *hostname, unsigned int rdclass, | ||
97 | unsigned int rdtype, unsigned int flags, | ||
98 | struct rrsetinfo **res) | ||
99 | { | ||
100 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
101 | int result; | ||
102 | struct rrsetinfo *rrset = NULL; | ||
103 | struct dns_response *response; | ||
104 | struct dns_rr *rr; | ||
105 | struct rdatainfo *rdata; | ||
106 | int length; | ||
107 | unsigned int index_ans, index_sig; | ||
108 | u_char answer[ANSWER_BUFFER_SIZE]; | ||
109 | |||
110 | /* check for invalid class and type */ | ||
111 | if (rdclass > 0xffff || rdtype > 0xffff) { | ||
112 | result = ERRSET_INVAL; | ||
113 | goto fail; | ||
114 | } | ||
115 | |||
116 | /* don't allow queries of class or type ANY */ | ||
117 | if (rdclass == 0xff || rdtype == 0xff) { | ||
118 | result = ERRSET_INVAL; | ||
119 | goto fail; | ||
120 | } | ||
121 | |||
122 | /* don't allow flags yet, unimplemented */ | ||
123 | if (flags) { | ||
124 | result = ERRSET_INVAL; | ||
125 | goto fail; | ||
126 | } | ||
127 | |||
128 | /* initialize resolver */ | ||
129 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
130 | result = ERRSET_FAIL; | ||
131 | goto fail; | ||
132 | } | ||
133 | |||
134 | #ifdef DEBUG | ||
135 | _resp->options |= RES_DEBUG; | ||
136 | #endif /* DEBUG */ | ||
137 | |||
138 | #ifdef RES_USE_DNSSEC | ||
139 | /* turn on DNSSEC if EDNS0 is configured */ | ||
140 | if (_resp->options & RES_USE_EDNS0) | ||
141 | _resp->options |= RES_USE_DNSSEC; | ||
142 | #endif /* RES_USE_DNSEC */ | ||
143 | |||
144 | /* make query */ | ||
145 | length = res_query(hostname, (signed int) rdclass, (signed int) rdtype, | ||
146 | answer, sizeof(answer)); | ||
147 | if (length < 0) { | ||
148 | switch(h_errno) { | ||
149 | case HOST_NOT_FOUND: | ||
150 | result = ERRSET_NONAME; | ||
151 | goto fail; | ||
152 | case NO_DATA: | ||
153 | result = ERRSET_NODATA; | ||
154 | goto fail; | ||
155 | default: | ||
156 | result = ERRSET_FAIL; | ||
157 | goto fail; | ||
158 | } | ||
159 | } | ||
160 | |||
161 | /* parse result */ | ||
162 | response = parse_dns_response(answer, length); | ||
163 | if (response == NULL) { | ||
164 | result = ERRSET_FAIL; | ||
165 | goto fail; | ||
166 | } | ||
167 | |||
168 | if (response->header.qdcount != 1) { | ||
169 | result = ERRSET_FAIL; | ||
170 | goto fail; | ||
171 | } | ||
172 | |||
173 | /* initialize rrset */ | ||
174 | rrset = calloc(1, sizeof(struct rrsetinfo)); | ||
175 | if (rrset == NULL) { | ||
176 | result = ERRSET_NOMEMORY; | ||
177 | goto fail; | ||
178 | } | ||
179 | rrset->rri_rdclass = response->query->class; | ||
180 | rrset->rri_rdtype = response->query->type; | ||
181 | rrset->rri_ttl = response->answer->ttl; | ||
182 | rrset->rri_nrdatas = response->header.ancount; | ||
183 | |||
184 | /* check for authenticated data */ | ||
185 | if (response->header.ad == 1) | ||
186 | rrset->rri_flags |= RRSET_VALIDATED; | ||
187 | |||
188 | /* copy name from answer section */ | ||
189 | length = strlen(response->answer->name); | ||
190 | rrset->rri_name = malloc(length + 1); | ||
191 | if (rrset->rri_name == NULL) { | ||
192 | result = ERRSET_NOMEMORY; | ||
193 | goto fail; | ||
194 | } | ||
195 | strlcpy(rrset->rri_name, response->answer->name, length + 1); | ||
196 | |||
197 | /* count answers */ | ||
198 | rrset->rri_nrdatas = count_dns_rr(response->answer, rrset->rri_rdclass, | ||
199 | rrset->rri_rdtype); | ||
200 | rrset->rri_nsigs = count_dns_rr(response->answer, rrset->rri_rdclass, | ||
201 | T_SIG); | ||
202 | |||
203 | /* allocate memory for answers */ | ||
204 | rrset->rri_rdatas = calloc(rrset->rri_nrdatas, | ||
205 | sizeof(struct rdatainfo)); | ||
206 | if (rrset->rri_rdatas == NULL) { | ||
207 | result = ERRSET_NOMEMORY; | ||
208 | goto fail; | ||
209 | } | ||
210 | |||
211 | /* allocate memory for signatures */ | ||
212 | rrset->rri_sigs = calloc(rrset->rri_nsigs, sizeof(struct rdatainfo)); | ||
213 | if (rrset->rri_sigs == NULL) { | ||
214 | result = ERRSET_NOMEMORY; | ||
215 | goto fail; | ||
216 | } | ||
217 | |||
218 | /* copy answers & signatures */ | ||
219 | for (rr = response->answer, index_ans = 0, index_sig = 0; | ||
220 | rr; rr = rr->next) { | ||
221 | |||
222 | rdata = NULL; | ||
223 | |||
224 | if (rr->class == rrset->rri_rdclass && | ||
225 | rr->type == rrset->rri_rdtype) | ||
226 | rdata = &rrset->rri_rdatas[index_ans++]; | ||
227 | |||
228 | if (rr->class == rrset->rri_rdclass && | ||
229 | rr->type == T_SIG) | ||
230 | rdata = &rrset->rri_sigs[index_sig++]; | ||
231 | |||
232 | if (rdata) { | ||
233 | rdata->rdi_length = rr->size; | ||
234 | rdata->rdi_data = malloc(rr->size); | ||
235 | |||
236 | if (rdata->rdi_data == NULL) { | ||
237 | result = ERRSET_NOMEMORY; | ||
238 | goto fail; | ||
239 | } | ||
240 | memcpy(rdata->rdi_data, rr->rdata, rr->size); | ||
241 | } | ||
242 | } | ||
243 | |||
244 | *res = rrset; | ||
245 | return (ERRSET_SUCCESS); | ||
246 | |||
247 | fail: | ||
248 | if (rrset != NULL) | ||
249 | freerrset(rrset); | ||
250 | return (result); | ||
251 | } | ||
252 | |||
253 | void | ||
254 | freerrset(struct rrsetinfo *rrset) | ||
255 | { | ||
256 | u_int16_t i; | ||
257 | |||
258 | if (rrset == NULL) | ||
259 | return; | ||
260 | |||
261 | if (rrset->rri_rdatas) { | ||
262 | for (i = 0; i < rrset->rri_nrdatas; i++) { | ||
263 | if (rrset->rri_rdatas[i].rdi_data == NULL) | ||
264 | break; | ||
265 | free(rrset->rri_rdatas[i].rdi_data); | ||
266 | } | ||
267 | free(rrset->rri_rdatas); | ||
268 | } | ||
269 | |||
270 | if (rrset->rri_sigs) { | ||
271 | for (i = 0; i < rrset->rri_nsigs; i++) { | ||
272 | if (rrset->rri_sigs[i].rdi_data == NULL) | ||
273 | break; | ||
274 | free(rrset->rri_sigs[i].rdi_data); | ||
275 | } | ||
276 | free(rrset->rri_sigs); | ||
277 | } | ||
278 | |||
279 | if (rrset->rri_name) | ||
280 | free(rrset->rri_name); | ||
281 | free(rrset); | ||
282 | } | ||
283 | |||
284 | /* | ||
285 | * DNS response parsing routines | ||
286 | */ | ||
287 | static struct dns_response * | ||
288 | parse_dns_response(const u_char *answer, int size) | ||
289 | { | ||
290 | struct dns_response *resp; | ||
291 | const u_char *cp; | ||
292 | |||
293 | /* allocate memory for the response */ | ||
294 | resp = calloc(1, sizeof(*resp)); | ||
295 | if (resp == NULL) | ||
296 | return (NULL); | ||
297 | |||
298 | /* initialize current pointer */ | ||
299 | cp = answer; | ||
300 | |||
301 | /* copy header */ | ||
302 | memcpy(&resp->header, cp, HFIXEDSZ); | ||
303 | cp += HFIXEDSZ; | ||
304 | |||
305 | /* fix header byte order */ | ||
306 | resp->header.qdcount = ntohs(resp->header.qdcount); | ||
307 | resp->header.ancount = ntohs(resp->header.ancount); | ||
308 | resp->header.nscount = ntohs(resp->header.nscount); | ||
309 | resp->header.arcount = ntohs(resp->header.arcount); | ||
310 | |||
311 | /* there must be at least one query */ | ||
312 | if (resp->header.qdcount < 1) { | ||
313 | free_dns_response(resp); | ||
314 | return (NULL); | ||
315 | } | ||
316 | |||
317 | /* parse query section */ | ||
318 | resp->query = parse_dns_qsection(answer, size, &cp, | ||
319 | resp->header.qdcount); | ||
320 | if (resp->header.qdcount && resp->query == NULL) { | ||
321 | free_dns_response(resp); | ||
322 | return (NULL); | ||
323 | } | ||
324 | |||
325 | /* parse answer section */ | ||
326 | resp->answer = parse_dns_rrsection(answer, size, &cp, | ||
327 | resp->header.ancount); | ||
328 | if (resp->header.ancount && resp->answer == NULL) { | ||
329 | free_dns_response(resp); | ||
330 | return (NULL); | ||
331 | } | ||
332 | |||
333 | /* parse authority section */ | ||
334 | resp->authority = parse_dns_rrsection(answer, size, &cp, | ||
335 | resp->header.nscount); | ||
336 | if (resp->header.nscount && resp->authority == NULL) { | ||
337 | free_dns_response(resp); | ||
338 | return (NULL); | ||
339 | } | ||
340 | |||
341 | /* parse additional section */ | ||
342 | resp->additional = parse_dns_rrsection(answer, size, &cp, | ||
343 | resp->header.arcount); | ||
344 | if (resp->header.arcount && resp->additional == NULL) { | ||
345 | free_dns_response(resp); | ||
346 | return (NULL); | ||
347 | } | ||
348 | |||
349 | return (resp); | ||
350 | } | ||
351 | |||
352 | static struct dns_query * | ||
353 | parse_dns_qsection(const u_char *answer, int size, const u_char **cp, int count) | ||
354 | { | ||
355 | struct dns_query *head, *curr, *prev; | ||
356 | int i, length; | ||
357 | char name[MAXDNAME]; | ||
358 | |||
359 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { | ||
360 | |||
361 | /* allocate and initialize struct */ | ||
362 | curr = calloc(1, sizeof(struct dns_query)); | ||
363 | if (curr == NULL) { | ||
364 | free_dns_query(head); | ||
365 | return (NULL); | ||
366 | } | ||
367 | if (head == NULL) | ||
368 | head = curr; | ||
369 | if (prev != NULL) | ||
370 | prev->next = curr; | ||
371 | |||
372 | /* name */ | ||
373 | length = dn_expand(answer, answer + size, *cp, name, | ||
374 | sizeof(name)); | ||
375 | if (length < 0) { | ||
376 | free_dns_query(head); | ||
377 | return (NULL); | ||
378 | } | ||
379 | curr->name = strdup(name); | ||
380 | if (curr->name == NULL) { | ||
381 | free_dns_query(head); | ||
382 | return (NULL); | ||
383 | } | ||
384 | *cp += length; | ||
385 | |||
386 | /* type */ | ||
387 | curr->type = _getshort(*cp); | ||
388 | *cp += INT16SZ; | ||
389 | |||
390 | /* class */ | ||
391 | curr->class = _getshort(*cp); | ||
392 | *cp += INT16SZ; | ||
393 | } | ||
394 | |||
395 | return (head); | ||
396 | } | ||
397 | |||
398 | static struct dns_rr * | ||
399 | parse_dns_rrsection(const u_char *answer, int size, const u_char **cp, int count) | ||
400 | { | ||
401 | struct dns_rr *head, *curr, *prev; | ||
402 | int i, length; | ||
403 | char name[MAXDNAME]; | ||
404 | |||
405 | for (i = 1, head = NULL, prev = NULL; i <= count; i++, prev = curr) { | ||
406 | |||
407 | /* allocate and initialize struct */ | ||
408 | curr = calloc(1, sizeof(struct dns_rr)); | ||
409 | if (curr == NULL) { | ||
410 | free_dns_rr(head); | ||
411 | return (NULL); | ||
412 | } | ||
413 | if (head == NULL) | ||
414 | head = curr; | ||
415 | if (prev != NULL) | ||
416 | prev->next = curr; | ||
417 | |||
418 | /* name */ | ||
419 | length = dn_expand(answer, answer + size, *cp, name, | ||
420 | sizeof(name)); | ||
421 | if (length < 0) { | ||
422 | free_dns_rr(head); | ||
423 | return (NULL); | ||
424 | } | ||
425 | curr->name = strdup(name); | ||
426 | if (curr->name == NULL) { | ||
427 | free_dns_rr(head); | ||
428 | return (NULL); | ||
429 | } | ||
430 | *cp += length; | ||
431 | |||
432 | /* type */ | ||
433 | curr->type = _getshort(*cp); | ||
434 | *cp += INT16SZ; | ||
435 | |||
436 | /* class */ | ||
437 | curr->class = _getshort(*cp); | ||
438 | *cp += INT16SZ; | ||
439 | |||
440 | /* ttl */ | ||
441 | curr->ttl = _getlong(*cp); | ||
442 | *cp += INT32SZ; | ||
443 | |||
444 | /* rdata size */ | ||
445 | curr->size = _getshort(*cp); | ||
446 | *cp += INT16SZ; | ||
447 | |||
448 | /* rdata itself */ | ||
449 | curr->rdata = malloc(curr->size); | ||
450 | if (curr->rdata == NULL) { | ||
451 | free_dns_rr(head); | ||
452 | return (NULL); | ||
453 | } | ||
454 | memcpy(curr->rdata, *cp, curr->size); | ||
455 | *cp += curr->size; | ||
456 | } | ||
457 | |||
458 | return (head); | ||
459 | } | ||
460 | |||
461 | static void | ||
462 | free_dns_query(struct dns_query *p) | ||
463 | { | ||
464 | if (p == NULL) | ||
465 | return; | ||
466 | |||
467 | if (p->name) | ||
468 | free(p->name); | ||
469 | free_dns_query(p->next); | ||
470 | free(p); | ||
471 | } | ||
472 | |||
473 | static void | ||
474 | free_dns_rr(struct dns_rr *p) | ||
475 | { | ||
476 | if (p == NULL) | ||
477 | return; | ||
478 | |||
479 | if (p->name) | ||
480 | free(p->name); | ||
481 | if (p->rdata) | ||
482 | free(p->rdata); | ||
483 | free_dns_rr(p->next); | ||
484 | free(p); | ||
485 | } | ||
486 | |||
487 | static void | ||
488 | free_dns_response(struct dns_response *p) | ||
489 | { | ||
490 | if (p == NULL) | ||
491 | return; | ||
492 | |||
493 | free_dns_query(p->query); | ||
494 | free_dns_rr(p->answer); | ||
495 | free_dns_rr(p->authority); | ||
496 | free_dns_rr(p->additional); | ||
497 | free(p); | ||
498 | } | ||
499 | |||
500 | static int | ||
501 | count_dns_rr(struct dns_rr *p, u_int16_t class, u_int16_t type) | ||
502 | { | ||
503 | int n = 0; | ||
504 | |||
505 | while(p) { | ||
506 | if (p->class == class && p->type == type) | ||
507 | n++; | ||
508 | p = p->next; | ||
509 | } | ||
510 | |||
511 | return (n); | ||
512 | } | ||
diff --git a/src/lib/libc/net/getservbyname.c b/src/lib/libc/net/getservbyname.c new file mode 100644 index 0000000000..f1c312ea82 --- /dev/null +++ b/src/lib/libc/net/getservbyname.c | |||
@@ -0,0 +1,83 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getservbyname.c,v 1.6 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <netdb.h> | ||
35 | #include <string.h> | ||
36 | #include "thread_private.h" | ||
37 | |||
38 | extern int _serv_stayopen; | ||
39 | |||
40 | _THREAD_PRIVATE_MUTEX(getservbyname_r); | ||
41 | |||
42 | struct servent * | ||
43 | getservbyname_r(name, proto, se, buf, buflen) | ||
44 | const char *name, *proto; | ||
45 | struct servent *se; | ||
46 | char *buf; | ||
47 | int buflen; | ||
48 | { | ||
49 | register struct servent *p; | ||
50 | register char **cp; | ||
51 | |||
52 | _THREAD_PRIVATE_MUTEX_LOCK(getservbyname_r); | ||
53 | setservent(_serv_stayopen); | ||
54 | while ((p = getservent())) { | ||
55 | if (strcmp(name, p->s_name) == 0) | ||
56 | goto gotname; | ||
57 | for (cp = p->s_aliases; *cp; cp++) | ||
58 | if (strcmp(name, *cp) == 0) | ||
59 | goto gotname; | ||
60 | continue; | ||
61 | gotname: | ||
62 | if (proto == 0 || strcmp(p->s_proto, proto) == 0) | ||
63 | break; | ||
64 | } | ||
65 | if (!_serv_stayopen) | ||
66 | endservent(); | ||
67 | _THREAD_PRIVATE_MUTEX_UNLOCK(getservbyname_r); | ||
68 | return (p); | ||
69 | } | ||
70 | |||
71 | struct servent *getservbyname(name, proto) | ||
72 | const char *name, *proto; | ||
73 | { | ||
74 | _THREAD_PRIVATE_KEY(getservbyname); | ||
75 | static char buf[4096]; | ||
76 | char *bufp = (char*)_THREAD_PRIVATE(getservbyname, buf, NULL); | ||
77 | |||
78 | if (bufp == NULL) | ||
79 | return (NULL); | ||
80 | return getservbyname_r(name, proto, (struct servent*) bufp, | ||
81 | bufp + sizeof(struct servent), | ||
82 | sizeof buf - sizeof(struct servent) ); | ||
83 | } | ||
diff --git a/src/lib/libc/net/getservbyport.c b/src/lib/libc/net/getservbyport.c new file mode 100644 index 0000000000..992a77d638 --- /dev/null +++ b/src/lib/libc/net/getservbyport.c | |||
@@ -0,0 +1,56 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getservbyport.c,v 1.4 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <netdb.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | extern int _serv_stayopen; | ||
38 | |||
39 | struct servent * | ||
40 | getservbyport(port, proto) | ||
41 | int port; | ||
42 | const char *proto; | ||
43 | { | ||
44 | register struct servent *p; | ||
45 | |||
46 | setservent(_serv_stayopen); | ||
47 | while ((p = getservent())) { | ||
48 | if (p->s_port != port) | ||
49 | continue; | ||
50 | if (proto == 0 || strcmp(p->s_proto, proto) == 0) | ||
51 | break; | ||
52 | } | ||
53 | if (!_serv_stayopen) | ||
54 | endservent(); | ||
55 | return (p); | ||
56 | } | ||
diff --git a/src/lib/libc/net/getservent.3 b/src/lib/libc/net/getservent.3 new file mode 100644 index 0000000000..72201126f8 --- /dev/null +++ b/src/lib/libc/net/getservent.3 | |||
@@ -0,0 +1,137 @@ | |||
1 | .\" $OpenBSD: getservent.3,v 1.12 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd January 12, 1994 | ||
31 | .Dt GETSERVENT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getservent , | ||
35 | .Nm getservbyport , | ||
36 | .Nm getservbyname , | ||
37 | .Nm setservent , | ||
38 | .Nm endservent | ||
39 | .Nd get service entry | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <netdb.h> | ||
42 | .Ft struct servent * | ||
43 | .Fn getservent "void" | ||
44 | .Ft struct servent * | ||
45 | .Fn getservbyname "char *name" "char *proto" | ||
46 | .Ft struct servent * | ||
47 | .Fn getservbyport "int port" "char *proto" | ||
48 | .Ft void | ||
49 | .Fn setservent "int stayopen" | ||
50 | .Ft void | ||
51 | .Fn endservent "void" | ||
52 | .Sh DESCRIPTION | ||
53 | The | ||
54 | .Fn getservent , | ||
55 | .Fn getservbyname , | ||
56 | and | ||
57 | .Fn getservbyport | ||
58 | functions each return a pointer to an object with the following structure | ||
59 | containing the broken-out fields of a line in the network services database, | ||
60 | .Pa /etc/services . | ||
61 | .Bd -literal -offset indent | ||
62 | struct servent { | ||
63 | char *s_name; /* official name of service */ | ||
64 | char **s_aliases; /* alias list */ | ||
65 | int s_port; /* port service resides at */ | ||
66 | char *s_proto; /* protocol to use */ | ||
67 | }; | ||
68 | .Ed | ||
69 | .Pp | ||
70 | The members of this structure are: | ||
71 | .Bl -tag -width s_aliases | ||
72 | .It Fa s_name | ||
73 | The official name of the service. | ||
74 | .It Fa s_aliases | ||
75 | A zero-terminated list of alternate names for the service. | ||
76 | .It Fa s_port | ||
77 | The port number at which the service resides. | ||
78 | Port numbers are returned in network byte order. | ||
79 | .It Fa s_proto | ||
80 | The name of the protocol to use when contacting the service. | ||
81 | .El | ||
82 | .Pp | ||
83 | The | ||
84 | .Fn getservent | ||
85 | function reads the next line of the file, opening the file if necessary. | ||
86 | .Pp | ||
87 | The | ||
88 | .Fn setservent | ||
89 | function opens and rewinds the file. | ||
90 | If the | ||
91 | .Fa stayopen | ||
92 | flag is non-zero, | ||
93 | the net database will not be closed after each call to | ||
94 | .Fn getservbyname | ||
95 | or | ||
96 | .Fn getservbyport . | ||
97 | .Pp | ||
98 | The | ||
99 | .Fn endservent | ||
100 | function closes the file. | ||
101 | .Pp | ||
102 | The | ||
103 | .Fn getservbyname | ||
104 | and | ||
105 | .Fn getservbyport | ||
106 | functions sequentially search from the beginning of the file until a | ||
107 | matching protocol name or port number (specified in network byte order) | ||
108 | is found, or until | ||
109 | .Dv EOF | ||
110 | is encountered. | ||
111 | If a protocol name is also supplied (non-null), | ||
112 | searches must also match the protocol. | ||
113 | .Sh FILES | ||
114 | .Bl -tag -width /etc/services -compact | ||
115 | .It Pa /etc/services | ||
116 | .El | ||
117 | .Sh DIAGNOSTICS | ||
118 | Null pointer (0) returned on | ||
119 | .Dv EOF | ||
120 | or error. | ||
121 | .Sh SEE ALSO | ||
122 | .Xr getprotoent 3 , | ||
123 | .Xr services 5 | ||
124 | .Sh HISTORY | ||
125 | The | ||
126 | .Fn getservent , | ||
127 | .Fn getservbyport , | ||
128 | .Fn getservbyname , | ||
129 | .Fn setservent , | ||
130 | and | ||
131 | .Fn endservent | ||
132 | functions appeared in | ||
133 | .Bx 4.2 . | ||
134 | .Sh BUGS | ||
135 | These functions use static data storage; if the data is needed for future use, | ||
136 | it should be copied before any subsequent calls overwrite it. | ||
137 | Expecting port numbers to fit in a 32-bit quantity is probably naive. | ||
diff --git a/src/lib/libc/net/getservent.c b/src/lib/libc/net/getservent.c new file mode 100644 index 0000000000..bad3316f6c --- /dev/null +++ b/src/lib/libc/net/getservent.c | |||
@@ -0,0 +1,125 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: getservent.c,v 1.6 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | #include <netdb.h> | ||
37 | #include <stdio.h> | ||
38 | #include <string.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | #define MAXALIASES 35 | ||
42 | |||
43 | static FILE *servf = NULL; | ||
44 | static char line[BUFSIZ+1]; | ||
45 | static struct servent serv; | ||
46 | static char *serv_aliases[MAXALIASES]; | ||
47 | int _serv_stayopen; | ||
48 | |||
49 | void | ||
50 | setservent(f) | ||
51 | int f; | ||
52 | { | ||
53 | if (servf == NULL) | ||
54 | servf = fopen(_PATH_SERVICES, "r" ); | ||
55 | else | ||
56 | rewind(servf); | ||
57 | _serv_stayopen |= f; | ||
58 | } | ||
59 | |||
60 | void | ||
61 | endservent() | ||
62 | { | ||
63 | if (servf) { | ||
64 | fclose(servf); | ||
65 | servf = NULL; | ||
66 | } | ||
67 | _serv_stayopen = 0; | ||
68 | } | ||
69 | |||
70 | struct servent * | ||
71 | getservent() | ||
72 | { | ||
73 | char *p, *cp, **q, *endp; | ||
74 | long l; | ||
75 | size_t len; | ||
76 | |||
77 | if (servf == NULL && (servf = fopen(_PATH_SERVICES, "r" )) == NULL) | ||
78 | return (NULL); | ||
79 | again: | ||
80 | if ((p = fgetln(servf, &len)) == NULL) | ||
81 | return (NULL); | ||
82 | if (p[len-1] == '\n') | ||
83 | len--; | ||
84 | if (len >= sizeof(line) || len == 0) | ||
85 | goto again; | ||
86 | p = memcpy(line, p, len); | ||
87 | line[len] = '\0'; | ||
88 | if (*p == '#') | ||
89 | goto again; | ||
90 | if ((cp = strchr(p, '#')) != NULL) | ||
91 | *cp = '\0'; | ||
92 | serv.s_name = p; | ||
93 | p = strpbrk(p, " \t"); | ||
94 | if (p == NULL) | ||
95 | goto again; | ||
96 | *p++ = '\0'; | ||
97 | while (*p == ' ' || *p == '\t') | ||
98 | p++; | ||
99 | cp = strpbrk(p, ",/"); | ||
100 | if (cp == NULL) | ||
101 | goto again; | ||
102 | *cp++ = '\0'; | ||
103 | l = strtol(p, &endp, 10); | ||
104 | if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX) | ||
105 | goto again; | ||
106 | serv.s_port = htons((in_port_t)l); | ||
107 | serv.s_proto = cp; | ||
108 | q = serv.s_aliases = serv_aliases; | ||
109 | cp = strpbrk(cp, " \t"); | ||
110 | if (cp != NULL) | ||
111 | *cp++ = '\0'; | ||
112 | while (cp && *cp) { | ||
113 | if (*cp == ' ' || *cp == '\t') { | ||
114 | cp++; | ||
115 | continue; | ||
116 | } | ||
117 | if (q < &serv_aliases[MAXALIASES - 1]) | ||
118 | *q++ = cp; | ||
119 | cp = strpbrk(cp, " \t"); | ||
120 | if (cp != NULL) | ||
121 | *cp++ = '\0'; | ||
122 | } | ||
123 | *q = NULL; | ||
124 | return (&serv); | ||
125 | } | ||
diff --git a/src/lib/libc/net/herror.c b/src/lib/libc/net/herror.c new file mode 100644 index 0000000000..e8f6fa28fc --- /dev/null +++ b/src/lib/libc/net/herror.c | |||
@@ -0,0 +1,117 @@ | |||
1 | /* $OpenBSD: herror.c,v 1.5 2003/06/02 20:18:35 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1987, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1987, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #if defined(LIBC_SCCS) && !defined(lint) | ||
55 | #if 0 | ||
56 | static char sccsid[] = "@(#)herror.c 8.1 (Berkeley) 6/4/93"; | ||
57 | static char rcsid[] = "$From: herror.c,v 8.3 1996/08/05 08:31:35 vixie Exp $"; | ||
58 | #else | ||
59 | static char rcsid[] = "$OpenBSD: herror.c,v 1.5 2003/06/02 20:18:35 millert Exp $"; | ||
60 | #endif | ||
61 | #endif /* LIBC_SCCS and not lint */ | ||
62 | |||
63 | #include <sys/types.h> | ||
64 | #include <sys/param.h> | ||
65 | #include <sys/uio.h> | ||
66 | #include <netdb.h> | ||
67 | #include <unistd.h> | ||
68 | #include <string.h> | ||
69 | |||
70 | const char *h_errlist[] = { | ||
71 | "Resolver Error 0 (no error)", | ||
72 | "Unknown host", /* 1 HOST_NOT_FOUND */ | ||
73 | "Host name lookup failure", /* 2 TRY_AGAIN */ | ||
74 | "Unknown server error", /* 3 NO_RECOVERY */ | ||
75 | "No address associated with name", /* 4 NO_ADDRESS */ | ||
76 | }; | ||
77 | int h_nerr = { sizeof h_errlist / sizeof h_errlist[0] }; | ||
78 | |||
79 | extern int h_errno; | ||
80 | |||
81 | /* | ||
82 | * herror -- | ||
83 | * print the error indicated by the h_errno value. | ||
84 | */ | ||
85 | void | ||
86 | herror(s) | ||
87 | const char *s; | ||
88 | { | ||
89 | struct iovec iov[4]; | ||
90 | register struct iovec *v = iov; | ||
91 | |||
92 | if (s && *s) { | ||
93 | v->iov_base = (char *)s; | ||
94 | v->iov_len = strlen(s); | ||
95 | v++; | ||
96 | v->iov_base = ": "; | ||
97 | v->iov_len = 2; | ||
98 | v++; | ||
99 | } | ||
100 | v->iov_base = (char *)hstrerror(h_errno); | ||
101 | v->iov_len = strlen(v->iov_base); | ||
102 | v++; | ||
103 | v->iov_base = "\n"; | ||
104 | v->iov_len = 1; | ||
105 | writev(STDERR_FILENO, iov, (v - iov) + 1); | ||
106 | } | ||
107 | |||
108 | const char * | ||
109 | hstrerror(err) | ||
110 | int err; | ||
111 | { | ||
112 | if (err < 0) | ||
113 | return ("Resolver internal error"); | ||
114 | else if (err < h_nerr) | ||
115 | return (h_errlist[err]); | ||
116 | return ("Unknown resolver error"); | ||
117 | } | ||
diff --git a/src/lib/libc/net/htonl.c b/src/lib/libc/net/htonl.c new file mode 100644 index 0000000000..73b7432731 --- /dev/null +++ b/src/lib/libc/net/htonl.c | |||
@@ -0,0 +1,25 @@ | |||
1 | /* | ||
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
3 | * Public domain. | ||
4 | */ | ||
5 | |||
6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
7 | static char *rcsid = "$OpenBSD: htonl.c,v 1.4 1996/12/12 03:19:55 tholo Exp $"; | ||
8 | #endif /* LIBC_SCCS and not lint */ | ||
9 | |||
10 | #include <sys/types.h> | ||
11 | #include <machine/endian.h> | ||
12 | |||
13 | #undef htonl | ||
14 | |||
15 | u_int32_t | ||
16 | htonl(x) | ||
17 | u_int32_t x; | ||
18 | { | ||
19 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
20 | u_char *s = (u_char *)&x; | ||
21 | return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); | ||
22 | #else | ||
23 | return x; | ||
24 | #endif | ||
25 | } | ||
diff --git a/src/lib/libc/net/htons.c b/src/lib/libc/net/htons.c new file mode 100644 index 0000000000..ded70712ea --- /dev/null +++ b/src/lib/libc/net/htons.c | |||
@@ -0,0 +1,24 @@ | |||
1 | /* | ||
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
3 | * Public domain. | ||
4 | */ | ||
5 | |||
6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
7 | static char *rcsid = "$OpenBSD: htons.c,v 1.7 2002/02/19 19:39:36 millert Exp $"; | ||
8 | #endif /* LIBC_SCCS and not lint */ | ||
9 | |||
10 | #include <sys/types.h> | ||
11 | #include <machine/endian.h> | ||
12 | |||
13 | #undef htons | ||
14 | |||
15 | u_int16_t | ||
16 | htons(u_int16_t x) | ||
17 | { | ||
18 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
19 | u_char *s = (u_char *) &x; | ||
20 | return (u_int16_t)(s[0] << 8 | s[1]); | ||
21 | #else | ||
22 | return x; | ||
23 | #endif | ||
24 | } | ||
diff --git a/src/lib/libc/net/if_indextoname.3 b/src/lib/libc/net/if_indextoname.3 new file mode 100644 index 0000000000..30293dcdf2 --- /dev/null +++ b/src/lib/libc/net/if_indextoname.3 | |||
@@ -0,0 +1,136 @@ | |||
1 | .\" $OpenBSD: if_indextoname.3,v 1.7 2003/08/08 09:26:02 jmc Exp $ | ||
2 | .\" Copyright (c) 1983, 1991, 1993 | ||
3 | .\" The Regents of the University of California. All rights reserved. | ||
4 | .\" | ||
5 | .\" Redistribution and use in source and binary forms, with or without | ||
6 | .\" modification, are permitted provided that the following conditions | ||
7 | .\" are met: | ||
8 | .\" 1. Redistributions of source code must retain the above copyright | ||
9 | .\" notice, this list of conditions and the following disclaimer. | ||
10 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer in the | ||
12 | .\" documentation and/or other materials provided with the distribution. | ||
13 | .\" 3. Neither the name of the University nor the names of its contributors | ||
14 | .\" may be used to endorse or promote products derived from this software | ||
15 | .\" without specific prior written permission. | ||
16 | .\" | ||
17 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | .\" SUCH DAMAGE. | ||
28 | .\" | ||
29 | .\" From: @(#)rcmd.3 8.1 (Berkeley) 6/4/93 | ||
30 | .\" | ||
31 | .Dd May 21, 1998 | ||
32 | .Dt IF_NAMETOINDEX 3 | ||
33 | .Os | ||
34 | .Sh NAME | ||
35 | .Nm if_nametoindex , | ||
36 | .Nm if_indextoname , | ||
37 | .Nm if_nameindex , | ||
38 | .Nm if_freenameindex | ||
39 | .Nd convert interface index to name, and vice versa | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <net/if.h> | ||
42 | .Ft "unsigned int" | ||
43 | .Fn if_nametoindex "const char *ifname" | ||
44 | .Ft "char *" | ||
45 | .Fn if_indextoname "unsigned int ifindex" "char *ifname" | ||
46 | .Ft "struct if_nameindex *" | ||
47 | .Fn if_nameindex "void" | ||
48 | .Ft "void" | ||
49 | .Fn if_freenameindex "struct if_nameindex *ptr" | ||
50 | .Sh DESCRIPTION | ||
51 | These functions map interface indexes to interface names (such as | ||
52 | .Dq lo0 ) , | ||
53 | and vice versa. | ||
54 | .Pp | ||
55 | The | ||
56 | .Fn if_nametoindex | ||
57 | function converts an interface name specified by the | ||
58 | .Fa ifname | ||
59 | argument to an interface index (positive integer value). | ||
60 | If the specified interface does not exist, 0 will be returned. | ||
61 | .Pp | ||
62 | .Fn if_indextoname | ||
63 | converts an interface index specified by the | ||
64 | .Fa ifindex | ||
65 | argument to an interface name. | ||
66 | The | ||
67 | .Fa ifname | ||
68 | argument must point to a buffer of at least | ||
69 | .Dv IF_NAMESIZE | ||
70 | bytes into which the interface name corresponding to the specified index is | ||
71 | returned. | ||
72 | .Pf ( Dv IF_NAMESIZE | ||
73 | is also defined in | ||
74 | .Aq Pa net/if.h | ||
75 | and its value includes a terminating null byte at the end of the | ||
76 | interface name.) | ||
77 | This pointer is also the return value of the function. | ||
78 | If there is no interface corresponding to the specified index, | ||
79 | .Dv NULL | ||
80 | is returned. | ||
81 | .Pp | ||
82 | .Fn if_nameindex | ||
83 | returns an array of | ||
84 | .Fa if_nameindex | ||
85 | structures. | ||
86 | .Fa if_nametoindex | ||
87 | is also defined in | ||
88 | .Aq Pa net/if.h , | ||
89 | and is as follows: | ||
90 | .Bd -literal -offset | ||
91 | struct if_nameindex { | ||
92 | unsigned int if_index; /* 1, 2, ... */ | ||
93 | char *if_name; /* null terminated name: "le0", ... */ | ||
94 | }; | ||
95 | .Ed | ||
96 | .Pp | ||
97 | The end of the array of structures is indicated by a structure with | ||
98 | an | ||
99 | .Fa if_index | ||
100 | of 0 and an | ||
101 | .Fa if_name | ||
102 | of | ||
103 | .Dv NULL . | ||
104 | The function returns a null pointer on error. | ||
105 | The memory used for this array of structures along with the interface | ||
106 | names pointed to by the | ||
107 | .Fa if_name | ||
108 | members is obtained dynamically. | ||
109 | This memory is freed by the | ||
110 | .Fn if_freenameindex | ||
111 | function. | ||
112 | .Pp | ||
113 | .Fn if_freenameindex | ||
114 | takes a pointer that was returned by | ||
115 | .Fn if_nameindex | ||
116 | as argument | ||
117 | .Pq Fa ptr , | ||
118 | and it reclaims the region allocated. | ||
119 | .Sh DIAGNOSTICS | ||
120 | .Fn if_nametoindex | ||
121 | returns 0 on error, positive integer on success. | ||
122 | .Fn if_indextoname | ||
123 | and | ||
124 | .Fn if_nameindex | ||
125 | return | ||
126 | .Dv NULL | ||
127 | on errors. | ||
128 | .Sh SEE ALSO | ||
129 | .Xr getifaddrs 3 , | ||
130 | .Xr networking 4 | ||
131 | .Pp | ||
132 | R. Gilligan, S. Thomson, J. Bound, and W. Stevens, | ||
133 | ``Basic Socket Interface Extensions for IPv6,'' RFC 2553, March 1999. | ||
134 | .Sh STANDARDS | ||
135 | These functions are defined in ``Basic Socket Interface Extensions for IPv6'' | ||
136 | .Pq RFC 2533 . | ||
diff --git a/src/lib/libc/net/if_indextoname.c b/src/lib/libc/net/if_indextoname.c new file mode 100644 index 0000000000..f99e52e387 --- /dev/null +++ b/src/lib/libc/net/if_indextoname.c | |||
@@ -0,0 +1,86 @@ | |||
1 | /* $OpenBSD: if_indextoname.c,v 1.9 2002/03/07 22:40:23 millert Exp $ */ | ||
2 | /* $KAME: if_indextoname.c,v 1.6 2000/11/07 22:33:25 jinmei Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1997, 2000 | ||
6 | * Berkeley Software Design, Inc. 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 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
24 | * SUCH DAMAGE. | ||
25 | * | ||
26 | * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp | ||
27 | */ | ||
28 | |||
29 | #include <sys/types.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if_dl.h> | ||
32 | #include <net/if.h> | ||
33 | #include <ifaddrs.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <errno.h> | ||
37 | |||
38 | /* | ||
39 | * From RFC 2533: | ||
40 | * | ||
41 | * The second function maps an interface index into its corresponding | ||
42 | * name. | ||
43 | * | ||
44 | * #include <net/if.h> | ||
45 | * | ||
46 | * char *if_indextoname(unsigned int ifindex, char *ifname); | ||
47 | * | ||
48 | * The ifname argument must point to a buffer of at least IF_NAMESIZE | ||
49 | * bytes into which the interface name corresponding to the specified | ||
50 | * index is returned. (IF_NAMESIZE is also defined in <net/if.h> and | ||
51 | * its value includes a terminating null byte at the end of the | ||
52 | * interface name.) This pointer is also the return value of the | ||
53 | * function. If there is no interface corresponding to the specified | ||
54 | * index, NULL is returned, and errno is set to ENXIO, if there was a | ||
55 | * system error (such as running out of memory), if_indextoname returns | ||
56 | * NULL and errno would be set to the proper value (e.g., ENOMEM). | ||
57 | */ | ||
58 | |||
59 | char * | ||
60 | if_indextoname(unsigned int ifindex, char *ifname) | ||
61 | { | ||
62 | struct ifaddrs *ifaddrs, *ifa; | ||
63 | int error = 0; | ||
64 | |||
65 | if (getifaddrs(&ifaddrs) < 0) | ||
66 | return(NULL); /* getifaddrs properly set errno */ | ||
67 | |||
68 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
69 | if (ifa->ifa_addr && | ||
70 | ifa->ifa_addr->sa_family == AF_LINK && | ||
71 | ifindex == ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index) | ||
72 | break; | ||
73 | } | ||
74 | |||
75 | if (ifa == NULL) { | ||
76 | error = ENXIO; | ||
77 | ifname = NULL; | ||
78 | } | ||
79 | else | ||
80 | strlcpy(ifname, ifa->ifa_name, IFNAMSIZ); | ||
81 | |||
82 | freeifaddrs(ifaddrs); | ||
83 | |||
84 | errno = error; | ||
85 | return(ifname); | ||
86 | } | ||
diff --git a/src/lib/libc/net/if_nameindex.c b/src/lib/libc/net/if_nameindex.c new file mode 100644 index 0000000000..3675a2a450 --- /dev/null +++ b/src/lib/libc/net/if_nameindex.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* $OpenBSD: if_nameindex.c,v 1.9 2002/03/07 22:40:23 millert Exp $ */ | ||
2 | /* $KAME: if_nameindex.c,v 1.7 2000/11/24 08:17:20 itojun Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1997, 2000 | ||
6 | * Berkeley Software Design, Inc. 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 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
24 | * SUCH DAMAGE. | ||
25 | * | ||
26 | * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp | ||
27 | */ | ||
28 | |||
29 | #include <sys/types.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if_dl.h> | ||
32 | #include <net/if.h> | ||
33 | #include <ifaddrs.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | /* | ||
38 | * From RFC 2553: | ||
39 | * | ||
40 | * 4.3 Return All Interface Names and Indexes | ||
41 | * | ||
42 | * The if_nameindex structure holds the information about a single | ||
43 | * interface and is defined as a result of including the <net/if.h> | ||
44 | * header. | ||
45 | * | ||
46 | * struct if_nameindex { | ||
47 | * unsigned int if_index; | ||
48 | * char *if_name; | ||
49 | * }; | ||
50 | * | ||
51 | * The final function returns an array of if_nameindex structures, one | ||
52 | * structure per interface. | ||
53 | * | ||
54 | * struct if_nameindex *if_nameindex(void); | ||
55 | * | ||
56 | * The end of the array of structures is indicated by a structure with | ||
57 | * an if_index of 0 and an if_name of NULL. The function returns a NULL | ||
58 | * pointer upon an error, and would set errno to the appropriate value. | ||
59 | * | ||
60 | * The memory used for this array of structures along with the interface | ||
61 | * names pointed to by the if_name members is obtained dynamically. | ||
62 | * This memory is freed by the next function. | ||
63 | * | ||
64 | * 4.4. Free Memory | ||
65 | * | ||
66 | * The following function frees the dynamic memory that was allocated by | ||
67 | * if_nameindex(). | ||
68 | * | ||
69 | * #include <net/if.h> | ||
70 | * | ||
71 | * void if_freenameindex(struct if_nameindex *ptr); | ||
72 | * | ||
73 | * The argument to this function must be a pointer that was returned by | ||
74 | * if_nameindex(). | ||
75 | */ | ||
76 | |||
77 | struct if_nameindex * | ||
78 | if_nameindex(void) | ||
79 | { | ||
80 | struct ifaddrs *ifaddrs, *ifa; | ||
81 | unsigned int ni; | ||
82 | size_t nbytes; | ||
83 | struct if_nameindex *ifni, *ifni2; | ||
84 | char *cp; | ||
85 | |||
86 | if (getifaddrs(&ifaddrs) < 0) | ||
87 | return(NULL); | ||
88 | |||
89 | /* | ||
90 | * First, find out how many interfaces there are, and how | ||
91 | * much space we need for the string names. | ||
92 | */ | ||
93 | ni = 0; | ||
94 | nbytes = 0; | ||
95 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
96 | if (ifa->ifa_addr && | ||
97 | ifa->ifa_addr->sa_family == AF_LINK) { | ||
98 | nbytes += strlen(ifa->ifa_name) + 1; | ||
99 | ni++; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | /* | ||
104 | * Next, allocate a chunk of memory, use the first part | ||
105 | * for the array of structures, and the last part for | ||
106 | * the strings. | ||
107 | */ | ||
108 | cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes); | ||
109 | ifni = (struct if_nameindex *)cp; | ||
110 | if (ifni == NULL) | ||
111 | goto out; | ||
112 | cp += (ni + 1) * sizeof(struct if_nameindex); | ||
113 | |||
114 | /* | ||
115 | * Now just loop through the list of interfaces again, | ||
116 | * filling in the if_nameindex array and making copies | ||
117 | * of all the strings. | ||
118 | */ | ||
119 | ifni2 = ifni; | ||
120 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
121 | if (ifa->ifa_addr && | ||
122 | ifa->ifa_addr->sa_family == AF_LINK) { | ||
123 | ifni2->if_index = | ||
124 | ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index; | ||
125 | ifni2->if_name = cp; | ||
126 | nbytes = strlen(ifa->ifa_name) + 1; | ||
127 | memcpy(cp, ifa->ifa_name, nbytes); | ||
128 | ifni2++; | ||
129 | cp += nbytes; | ||
130 | } | ||
131 | } | ||
132 | /* | ||
133 | * Finally, don't forget to terminate the array. | ||
134 | */ | ||
135 | ifni2->if_index = 0; | ||
136 | ifni2->if_name = NULL; | ||
137 | out: | ||
138 | freeifaddrs(ifaddrs); | ||
139 | return(ifni); | ||
140 | } | ||
diff --git a/src/lib/libc/net/if_nametoindex.c b/src/lib/libc/net/if_nametoindex.c new file mode 100644 index 0000000000..8bd792b949 --- /dev/null +++ b/src/lib/libc/net/if_nametoindex.c | |||
@@ -0,0 +1,81 @@ | |||
1 | /* $OpenBSD: if_nametoindex.c,v 1.8 2002/03/07 22:40:23 millert Exp $ */ | ||
2 | /* $KAME: if_nametoindex.c,v 1.5 2000/11/24 08:04:40 itojun Exp $ */ | ||
3 | |||
4 | /*- | ||
5 | * Copyright (c) 1997, 2000 | ||
6 | * Berkeley Software Design, Inc. 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 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND | ||
15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
17 | * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE | ||
18 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
19 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
21 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
22 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
23 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
24 | * SUCH DAMAGE. | ||
25 | * | ||
26 | * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp | ||
27 | */ | ||
28 | |||
29 | #include <sys/types.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <net/if.h> | ||
32 | #include <net/if_dl.h> | ||
33 | #include <ifaddrs.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <errno.h> | ||
37 | |||
38 | /* | ||
39 | * From RFC 2553: | ||
40 | * | ||
41 | * 4.1 Name-to-Index | ||
42 | * | ||
43 | * | ||
44 | * The first function maps an interface name into its corresponding | ||
45 | * index. | ||
46 | * | ||
47 | * #include <net/if.h> | ||
48 | * | ||
49 | * unsigned int if_nametoindex(const char *ifname); | ||
50 | * | ||
51 | * If the specified interface name does not exist, the return value is | ||
52 | * 0, and errno is set to ENXIO. If there was a system error (such as | ||
53 | * running out of memory), the return value is 0 and errno is set to the | ||
54 | * proper value (e.g., ENOMEM). | ||
55 | */ | ||
56 | |||
57 | unsigned int | ||
58 | if_nametoindex(const char *ifname) | ||
59 | { | ||
60 | struct ifaddrs *ifaddrs, *ifa; | ||
61 | unsigned int ni; | ||
62 | |||
63 | if (getifaddrs(&ifaddrs) < 0) | ||
64 | return(0); | ||
65 | |||
66 | ni = 0; | ||
67 | |||
68 | for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { | ||
69 | if (ifa->ifa_addr && | ||
70 | ifa->ifa_addr->sa_family == AF_LINK && | ||
71 | strcmp(ifa->ifa_name, ifname) == 0) { | ||
72 | ni = ((struct sockaddr_dl*)ifa->ifa_addr)->sdl_index; | ||
73 | break; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | freeifaddrs(ifaddrs); | ||
78 | if (!ni) | ||
79 | errno = ENXIO; | ||
80 | return(ni); | ||
81 | } | ||
diff --git a/src/lib/libc/net/inet.3 b/src/lib/libc/net/inet.3 new file mode 100644 index 0000000000..43cb5bafd7 --- /dev/null +++ b/src/lib/libc/net/inet.3 | |||
@@ -0,0 +1,355 @@ | |||
1 | .\" $OpenBSD: inet.3,v 1.18 2003/09/09 12:00:39 jmc Exp $ | ||
2 | .\" $NetBSD: inet.3,v 1.7 1997/06/18 02:25:24 lukem Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1983, 1990, 1991, 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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" @(#)inet.3 8.1 (Berkeley) 6/4/93 | ||
32 | .\" | ||
33 | .Dd June 18, 1997 | ||
34 | .Dt INET 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm inet_addr , | ||
38 | .Nm inet_aton , | ||
39 | .Nm inet_lnaof , | ||
40 | .Nm inet_makeaddr , | ||
41 | .Nm inet_netof , | ||
42 | .Nm inet_network , | ||
43 | .Nm inet_ntoa , | ||
44 | .Nm inet_ntop , | ||
45 | .Nm inet_pton | ||
46 | .Nd Internet address manipulation routines | ||
47 | .Sh SYNOPSIS | ||
48 | .Fd #include <sys/types.h> | ||
49 | .Fd #include <sys/socket.h> | ||
50 | .Fd #include <netinet/in.h> | ||
51 | .Fd #include <arpa/inet.h> | ||
52 | .Ft in_addr_t | ||
53 | .Fn inet_addr "const char *cp" | ||
54 | .Ft int | ||
55 | .Fn inet_aton "const char *cp" "struct in_addr *addr" | ||
56 | .Ft in_addr_t | ||
57 | .Fn inet_lnaof "struct in_addr in" | ||
58 | .Ft struct in_addr | ||
59 | .Fn inet_makeaddr "in_addr_t net" "in_addr_t lna" | ||
60 | .Ft in_addr_t | ||
61 | .Fn inet_netof "struct in_addr in" | ||
62 | .Ft in_addr_t | ||
63 | .Fn inet_network "const char *cp" | ||
64 | .Ft char * | ||
65 | .Fn inet_ntoa "struct in_addr in" | ||
66 | .Ft const char * | ||
67 | .Fn inet_ntop "int af" "const void *src" "char *dst" "size_t size" | ||
68 | .Ft int | ||
69 | .Fn inet_pton "int af" "const char *src" "void *dst" | ||
70 | .Sh DESCRIPTION | ||
71 | The routines | ||
72 | .Fn inet_aton , | ||
73 | .Fn inet_addr | ||
74 | and | ||
75 | .Fn inet_network | ||
76 | interpret character strings representing | ||
77 | numbers expressed in the Internet standard | ||
78 | .Ql \&. | ||
79 | notation. | ||
80 | The | ||
81 | .Fn inet_pton | ||
82 | function converts a presentation format address (that is, printable form | ||
83 | as held in a character string) to network format (usually a | ||
84 | .Li struct in_addr | ||
85 | or some other internal binary representation, in network byte order). | ||
86 | It returns 1 if the address was valid for the specified address family, or | ||
87 | 0 if the address wasn't parseable in the specified address family, or \-1 | ||
88 | if some system error occurred (in which case | ||
89 | .Va errno | ||
90 | will have been set). | ||
91 | This function is presently valid for | ||
92 | .Dv AF_INET | ||
93 | and | ||
94 | .Dv AF_INET6 . | ||
95 | The | ||
96 | .Fn inet_aton | ||
97 | routine interprets the specified character string as an Internet address, | ||
98 | placing the address into the structure provided. | ||
99 | It returns 1 if the string was successfully interpreted, | ||
100 | or 0 if the string was invalid. | ||
101 | The | ||
102 | .Fn inet_addr | ||
103 | and | ||
104 | .Fn inet_network | ||
105 | functions return numbers suitable for use | ||
106 | as Internet addresses and Internet network | ||
107 | numbers, respectively. | ||
108 | .Pp | ||
109 | The function | ||
110 | .Fn inet_ntop | ||
111 | converts an address from network format (usually a | ||
112 | .Li struct in_addr | ||
113 | or some other binary form, in network byte order) to presentation format | ||
114 | (suitable for external display purposes). | ||
115 | It returns | ||
116 | .Dv NULL | ||
117 | if a system | ||
118 | error occurs (in which case, | ||
119 | .Va errno | ||
120 | will have been set), or it returns a pointer to the destination string. | ||
121 | The routine | ||
122 | .Fn inet_ntoa | ||
123 | takes an Internet address and returns an | ||
124 | .Tn ASCII | ||
125 | string representing the address in | ||
126 | .Ql \&. | ||
127 | notation. | ||
128 | The routine | ||
129 | .Fn inet_makeaddr | ||
130 | takes an Internet network number and a local | ||
131 | network address and constructs an Internet address | ||
132 | from it. | ||
133 | The routines | ||
134 | .Fn inet_netof | ||
135 | and | ||
136 | .Fn inet_lnaof | ||
137 | break apart Internet host addresses, returning | ||
138 | the network number and local network address part, | ||
139 | respectively. | ||
140 | .Pp | ||
141 | All Internet addresses are returned in network | ||
142 | order (bytes ordered from left to right). | ||
143 | All network numbers and local address parts are | ||
144 | returned as machine format integer values. | ||
145 | .Sh INTERNET ADDRESSES (IP VERSION 4) | ||
146 | Values specified using the | ||
147 | .Ql \&. | ||
148 | notation take one | ||
149 | of the following forms: | ||
150 | .Bd -literal -offset indent | ||
151 | a.b.c.d | ||
152 | a.b.c | ||
153 | a.b | ||
154 | a | ||
155 | .Ed | ||
156 | .Pp | ||
157 | When four parts are specified, each is interpreted | ||
158 | as a byte of data and assigned, from left to right, | ||
159 | to the four bytes of an Internet address. | ||
160 | Note that when an Internet address is viewed as a 32-bit | ||
161 | integer quantity on a system that uses little-endian | ||
162 | byte order (such as the | ||
163 | .Tn Intel 386, 486 | ||
164 | and | ||
165 | .Tn Pentium | ||
166 | processors) the bytes referred to above appear as | ||
167 | .Dq Li d.c.b.a . | ||
168 | That is, little-endian bytes are ordered from right to left. | ||
169 | .Pp | ||
170 | When a three part address is specified, the last | ||
171 | part is interpreted as a 16-bit quantity and placed | ||
172 | in the rightmost two bytes of the network address. | ||
173 | This makes the three part address format convenient | ||
174 | for specifying Class B network addresses as | ||
175 | .Dq Li 128.net.host . | ||
176 | .Pp | ||
177 | When a two part address is supplied, the last part | ||
178 | is interpreted as a 24-bit quantity and placed in | ||
179 | the rightmost three bytes of the network address. | ||
180 | This makes the two part address format convenient | ||
181 | for specifying Class A network addresses as | ||
182 | .Dq Li net.host . | ||
183 | .Pp | ||
184 | When only one part is given, the value is stored | ||
185 | directly in the network address without any byte | ||
186 | rearrangement. | ||
187 | .Pp | ||
188 | All numbers supplied as | ||
189 | .Dq parts | ||
190 | in a | ||
191 | .Ql \&. | ||
192 | notation | ||
193 | may be decimal, octal, or hexadecimal, as specified | ||
194 | in the C language (i.e., a leading 0x or 0X implies | ||
195 | hexadecimal; otherwise, a leading 0 implies octal; | ||
196 | otherwise, the number is interpreted as decimal). | ||
197 | .Sh INTERNET ADDRESSES (IP VERSION 6) | ||
198 | In order to support scoped IPv6 addresses, | ||
199 | .Xr getaddrinfo 3 | ||
200 | and | ||
201 | .Xr getnameinfo 3 | ||
202 | are recommended rather than the functions presented here. | ||
203 | .Pp | ||
204 | The presentation format of an IPv6 address is given in RFC 2373: | ||
205 | .Pp | ||
206 | There are three conventional forms for representing IPv6 addresses as | ||
207 | text strings: | ||
208 | .Bl -enum | ||
209 | .It | ||
210 | The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the | ||
211 | hexadecimal values of the eight 16-bit pieces of the address. | ||
212 | Examples: | ||
213 | .Bd -literal -offset indent | ||
214 | FEDC:BA98:7654:3210:FEDC:BA98:7654:3210 | ||
215 | 1080:0:0:0:8:800:200C:417A | ||
216 | .Ed | ||
217 | .Pp | ||
218 | Note that it is not necessary to write the leading zeros in an | ||
219 | individual field, but there must be at least one numeral in | ||
220 | every field (except for the case described in 2.). | ||
221 | .It | ||
222 | Due to the method of allocating certain styles of IPv6 | ||
223 | addresses, it will be common for addresses to contain long | ||
224 | strings of zero bits. | ||
225 | In order to make writing addresses | ||
226 | containing zero bits easier, a special syntax is available to | ||
227 | compress the zeros. | ||
228 | The use of | ||
229 | .Dq \&:\&: | ||
230 | indicates multiple groups | ||
231 | of 16 bits of zeros. | ||
232 | The | ||
233 | .Dq \&:\&: | ||
234 | can only appear once in an | ||
235 | address. | ||
236 | The | ||
237 | .Dq \&:\&: | ||
238 | can also be used to compress the leading and/or trailing zeros in an address. | ||
239 | .Pp | ||
240 | For example the following addresses: | ||
241 | .Bd -literal -offset indent | ||
242 | 1080:0:0:0:8:800:200C:417A a unicast address | ||
243 | FF01:0:0:0:0:0:0:43 a multicast address | ||
244 | 0:0:0:0:0:0:0:1 the loopback address | ||
245 | 0:0:0:0:0:0:0:0 the unspecified addresses | ||
246 | .Ed | ||
247 | .Pp | ||
248 | may be represented as: | ||
249 | .Bd -literal -offset indent | ||
250 | 1080::8:800:200C:417A a unicast address | ||
251 | FF01::43 a multicast address | ||
252 | ::1 the loopback address | ||
253 | :: the unspecified addresses | ||
254 | .Ed | ||
255 | .It | ||
256 | An alternative form that is sometimes more convenient when | ||
257 | dealing with a mixed environment of IPv4 and IPv6 nodes is | ||
258 | x:x:x:x:x:x:d.d.d.d, where the 'x's are the hexadecimal values | ||
259 | of the six high-order 16-bit pieces of the address, and the 'd's | ||
260 | are the decimal values of the four low-order 8-bit pieces of the | ||
261 | address (standard IPv4 representation). | ||
262 | Examples: | ||
263 | .Bd -literal -offset indent | ||
264 | 0:0:0:0:0:0:13.1.68.3 | ||
265 | 0:0:0:0:0:FFFF:129.144.52.38 | ||
266 | .Ed | ||
267 | .Pp | ||
268 | or in compressed form: | ||
269 | .Bd -literal -offset indent | ||
270 | ::13.1.68.3 | ||
271 | ::FFFF:129.144.52.38 | ||
272 | .Ed | ||
273 | .El | ||
274 | .Sh DIAGNOSTICS | ||
275 | The constant | ||
276 | .Dv INADDR_NONE | ||
277 | is returned by | ||
278 | .Fn inet_addr | ||
279 | and | ||
280 | .Fn inet_network | ||
281 | for malformed requests. | ||
282 | .Sh SEE ALSO | ||
283 | .Xr byteorder 3 , | ||
284 | .Xr gethostbyname 3 , | ||
285 | .Xr getnetent 3 , | ||
286 | .Xr inet_net 3 , | ||
287 | .Xr hosts 5 , | ||
288 | .Xr networks 5 | ||
289 | .Rs | ||
290 | .%R RFC 2373 | ||
291 | .%D July 1998 | ||
292 | .%T "IP Version 6 Addressing Architecture" | ||
293 | .Re | ||
294 | .Rs | ||
295 | .%R RFC 3493 | ||
296 | .%D February 2003 | ||
297 | .%T "Basic Socket Interface Extensions for IPv6" | ||
298 | .Re | ||
299 | .Sh STANDARDS | ||
300 | The | ||
301 | .Nm inet_ntop | ||
302 | and | ||
303 | .Nm inet_pton | ||
304 | functions conform to the IETF IPv6 BSD API and address formatting | ||
305 | specifications. | ||
306 | Note that | ||
307 | .Nm inet_pton | ||
308 | does not accept 1-, 2-, or 3-part dotted addresses; all four parts | ||
309 | must be specified. | ||
310 | This is a narrower input set than that accepted by | ||
311 | .Nm inet_aton . | ||
312 | .Sh HISTORY | ||
313 | The | ||
314 | .Nm inet_addr , | ||
315 | .Nm inet_network , | ||
316 | .Nm inet_makeaddr , | ||
317 | .Nm inet_lnaof | ||
318 | and | ||
319 | .Nm inet_netof | ||
320 | functions appeared in | ||
321 | .Bx 4.2 . | ||
322 | The | ||
323 | .Nm inet_aton | ||
324 | and | ||
325 | .Nm inet_ntoa | ||
326 | functions appeared in | ||
327 | .Bx 4.3 . | ||
328 | The | ||
329 | .Nm inet_pton | ||
330 | and | ||
331 | .Nm inet_ntop | ||
332 | functions appeared in BIND 4.9.4. | ||
333 | .Sh BUGS | ||
334 | The value | ||
335 | .Dv INADDR_NONE | ||
336 | (0xffffffff) is a valid broadcast address, but | ||
337 | .Fn inet_addr | ||
338 | cannot return that value without indicating failure. | ||
339 | Also, | ||
340 | .Fn inet_addr | ||
341 | should have been designed to return a | ||
342 | .Li struct in_addr . | ||
343 | The newer | ||
344 | .Fn inet_aton | ||
345 | function does not share these problems, and almost all existing code | ||
346 | should be modified to use | ||
347 | .Fn inet_aton | ||
348 | instead. | ||
349 | .Pp | ||
350 | The problem of host byte ordering versus network byte ordering is | ||
351 | confusing. | ||
352 | .Pp | ||
353 | The string returned by | ||
354 | .Fn inet_ntoa | ||
355 | resides in a static memory area. | ||
diff --git a/src/lib/libc/net/inet6_option_space.3 b/src/lib/libc/net/inet6_option_space.3 new file mode 100644 index 0000000000..4e5dc22711 --- /dev/null +++ b/src/lib/libc/net/inet6_option_space.3 | |||
@@ -0,0 +1,444 @@ | |||
1 | .\" $OpenBSD: inet6_option_space.3,v 1.13 2003/08/08 09:26:02 jmc Exp $ | ||
2 | .\" $KAME: inet6_option_space.3,v 1.7 2000/05/17 14:32:13 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1983, 1987, 1991, 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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd December 10, 1999 | ||
32 | .Dt INET6_OPTION_SPACE 3 | ||
33 | .Os | ||
34 | .\" | ||
35 | .Sh NAME | ||
36 | .Nm inet6_option_space , | ||
37 | .Nm inet6_option_init , | ||
38 | .Nm inet6_option_append , | ||
39 | .Nm inet6_option_alloc , | ||
40 | .Nm inet6_option_next , | ||
41 | .Nm inet6_option_find | ||
42 | .Nd IPv6 Hop-by-Hop and Destination Options manipulation | ||
43 | .\" | ||
44 | .Sh SYNOPSIS | ||
45 | .Fd #include <netinet/in.h> | ||
46 | .Ft "int" | ||
47 | .Fn inet6_option_space "int nbytes" | ||
48 | .Ft "int" | ||
49 | .Fn inet6_option_init "void *bp" "struct cmsghdr **cmsgp" "int type" | ||
50 | .Ft "int" | ||
51 | .Fn inet6_option_append "struct cmsghdr *cmsg" "const u_int8_t *typep" "int multx" "int plusy" | ||
52 | .Ft "u_int8_t *" | ||
53 | .Fn inet6_option_alloc "struct cmsghdr *cmsg" "int datalen" "int multx" "int plusy" | ||
54 | .Ft "int" | ||
55 | .Fn inet6_option_next "const struct cmsghdr *cmsg" "u_int8_t **tptrp" | ||
56 | .Ft "int" | ||
57 | .Fn inet6_option_find "const struct cmsghdr *cmsg" "u_int8_t **tptrp" "int type" | ||
58 | .\" | ||
59 | .Sh DESCRIPTION | ||
60 | .\" | ||
61 | Building and parsing the Hop-by-Hop and Destination options is | ||
62 | complicated due to alignment constraints, padding and | ||
63 | ancillary data manipulation. | ||
64 | RFC 2292 defines a set of functions to help the application. | ||
65 | The function prototypes for | ||
66 | these functions are all in the | ||
67 | .Aq Pa netinet/in.h | ||
68 | header. | ||
69 | .\" | ||
70 | .Ss inet6_option_space | ||
71 | .Fn inet6_option_space | ||
72 | returns the number of bytes required to hold an option when it is stored as | ||
73 | ancillary data, including the | ||
74 | .Li cmsghdr | ||
75 | structure at the beginning, | ||
76 | and any padding at the end | ||
77 | .Po | ||
78 | to make its size a multiple of 8 bytes | ||
79 | .Pc . | ||
80 | The argument is the size of the structure defining the option, | ||
81 | which must include any pad bytes at the beginning | ||
82 | .Po | ||
83 | the value | ||
84 | .Li y | ||
85 | in the alignment term | ||
86 | .Dq Li xn + y | ||
87 | .Pc , | ||
88 | the type byte, the length byte, and the option data. | ||
89 | .Pp | ||
90 | Note: If multiple options are stored in a single ancillary data | ||
91 | object, which is the recommended technique, this function | ||
92 | overestimates the amount of space required by the size of | ||
93 | .Li N-1 | ||
94 | .Li cmsghdr | ||
95 | structures, | ||
96 | where | ||
97 | .Li N | ||
98 | is the number of options to be stored in the object. | ||
99 | This is of little consequence, since it is assumed that most | ||
100 | Hop-by-Hop option headers and Destination option headers carry only | ||
101 | one option | ||
102 | .Pq appendix B of [RFC 2460] . | ||
103 | .\" | ||
104 | .Ss inet6_option_init | ||
105 | .Fn inet6_option_init | ||
106 | is called once per ancillary data object that will | ||
107 | contain either Hop-by-Hop or Destination options. | ||
108 | It returns | ||
109 | .Li 0 | ||
110 | on success or | ||
111 | .Li -1 | ||
112 | on an error. | ||
113 | .Pp | ||
114 | .Fa bp | ||
115 | is a pointer to previously allocated space that will contain the | ||
116 | ancillary data object. | ||
117 | It must be large enough to contain all the | ||
118 | individual options to be added by later calls to | ||
119 | .Fn inet6_option_append | ||
120 | and | ||
121 | .Fn inet6_option_alloc . | ||
122 | .Pp | ||
123 | .Fa cmsgp | ||
124 | is a pointer to a pointer to a | ||
125 | .Li cmsghdr | ||
126 | structure. | ||
127 | .Fa *cmsgp | ||
128 | is initialized by this function to point to the | ||
129 | .Li cmsghdr | ||
130 | structure constructed by this function in the buffer pointed to by | ||
131 | .Fa bp . | ||
132 | .Pp | ||
133 | .Fa type | ||
134 | is either | ||
135 | .Dv IPV6_HOPOPTS | ||
136 | or | ||
137 | .Dv IPV6_DSTOPTS . | ||
138 | This | ||
139 | .Fa type | ||
140 | is stored in the | ||
141 | .Li cmsg_type | ||
142 | member of the | ||
143 | .Li cmsghdr | ||
144 | structure pointed to by | ||
145 | .Fa *cmsgp . | ||
146 | .\" | ||
147 | .Ss inet6_option_append | ||
148 | This function appends a Hop-by-Hop option or a Destination option | ||
149 | into an ancillary data object that has been initialized by | ||
150 | .Fn inet6_option_init . | ||
151 | This function returns | ||
152 | .Li 0 | ||
153 | if it succeeds or | ||
154 | .Li -1 | ||
155 | on an error. | ||
156 | .Pp | ||
157 | .Fa cmsg | ||
158 | is a pointer to the | ||
159 | .Li cmsghdr | ||
160 | structure that must have been | ||
161 | initialized by | ||
162 | .Fn inet6_option_init . | ||
163 | .Pp | ||
164 | .Fa typep | ||
165 | is a pointer to the 8-bit option type. | ||
166 | It is assumed that this | ||
167 | field is immediately followed by the 8-bit option data length field, | ||
168 | which is then followed immediately by the option data. | ||
169 | The caller | ||
170 | initializes these three fields | ||
171 | .Pq the type-length-value, or TLV | ||
172 | before calling this function. | ||
173 | .Pp | ||
174 | The option type must have a value from | ||
175 | .Li 2 | ||
176 | to | ||
177 | .Li 255 , | ||
178 | inclusive. | ||
179 | .Po | ||
180 | .Li 0 | ||
181 | and | ||
182 | .Li 1 | ||
183 | are reserved for the | ||
184 | .Li Pad1 | ||
185 | and | ||
186 | .Li PadN | ||
187 | options, respectively. | ||
188 | .Pc | ||
189 | .Pp | ||
190 | The option data length must have a value between | ||
191 | .Li 0 | ||
192 | and | ||
193 | .Li 255 , | ||
194 | inclusive, and is the length of the option data that follows. | ||
195 | .Pp | ||
196 | .Fa multx | ||
197 | is the value | ||
198 | .Li x | ||
199 | in the alignment term | ||
200 | .Dq Li xn + y . | ||
201 | It must have a value of | ||
202 | .Li 1 , | ||
203 | .Li 2 , | ||
204 | .Li 4 , | ||
205 | or | ||
206 | .Li 8 . | ||
207 | .Pp | ||
208 | .Fa plusy | ||
209 | is the value | ||
210 | .Li y | ||
211 | in the alignment term | ||
212 | .Dq Li xn + y . | ||
213 | It must have a value between | ||
214 | .Li 0 | ||
215 | and | ||
216 | .Li 7 , | ||
217 | inclusive. | ||
218 | .\" | ||
219 | .Ss inet6_option_alloc | ||
220 | This function appends a Hop-by-Hop option or a Destination option | ||
221 | into an ancillary data object that has been initialized by | ||
222 | .Fn inet6_option_init . | ||
223 | This function returns a pointer to the 8-bit | ||
224 | option type field that starts the option on success, or | ||
225 | .Dv NULL | ||
226 | on an error. | ||
227 | .Pp | ||
228 | The difference between this function and | ||
229 | .Fn inet6_option_append | ||
230 | is that the latter copies the contents of a previously built option into | ||
231 | the ancillary data object while the current function returns a | ||
232 | pointer to the space in the data object where the option's TLV must | ||
233 | then be built by the caller. | ||
234 | .Pp | ||
235 | .Fa cmsg | ||
236 | is a pointer to the | ||
237 | .Li cmsghdr | ||
238 | structure that must have been | ||
239 | initialized by | ||
240 | .Fn inet6_option_init . | ||
241 | .Pp | ||
242 | .Fa datalen | ||
243 | is the value of the option data length byte for this option. | ||
244 | This value is required as an argument to allow the function to | ||
245 | determine if padding must be appended at the end of the option. | ||
246 | (The | ||
247 | .Fn inet6_option_append | ||
248 | function does not need a data length argument | ||
249 | since the option data length must already be stored by the caller.) | ||
250 | .Pp | ||
251 | .Fa multx | ||
252 | is the value | ||
253 | .Li x | ||
254 | in the alignment term | ||
255 | .Dq Li xn + y . | ||
256 | It must have a value of | ||
257 | .Li 1 , | ||
258 | .Li 2 , | ||
259 | .Li 4 , | ||
260 | or | ||
261 | .Li 8 . | ||
262 | .Pp | ||
263 | .Fa plusy | ||
264 | is the value | ||
265 | .Li y | ||
266 | in the alignment term | ||
267 | .Dq Li xn + y . | ||
268 | It must have a value between | ||
269 | .Li 0 | ||
270 | and | ||
271 | .Li 7 , | ||
272 | inclusive. | ||
273 | .\" | ||
274 | .Ss inet6_option_next | ||
275 | This function processes the next Hop-by-Hop option or Destination | ||
276 | option in an ancillary data object. | ||
277 | If another option remains to be | ||
278 | processed, the return value of the function is | ||
279 | .Li 0 | ||
280 | and | ||
281 | .Fa *tptrp | ||
282 | points to | ||
283 | the 8-bit option type field | ||
284 | (which is followed by the 8-bit option | ||
285 | data length, followed by the option data). | ||
286 | If no more options remain | ||
287 | to be processed, the return value is | ||
288 | .Li -1 | ||
289 | and | ||
290 | .Fa *tptrp | ||
291 | is | ||
292 | .Dv NULL . | ||
293 | If an error occurs, the return value is | ||
294 | .Li -1 | ||
295 | and | ||
296 | .Fa *tptrp | ||
297 | is not | ||
298 | .Dv NULL . | ||
299 | .Pp | ||
300 | .Fa cmsg | ||
301 | is a pointer to | ||
302 | .Li cmsghdr | ||
303 | structure of which | ||
304 | .Li cmsg_level | ||
305 | equals | ||
306 | .Dv IPPROTO_IPV6 | ||
307 | and | ||
308 | .Li cmsg_type | ||
309 | equals either | ||
310 | .Dv IPV6_HOPOPTS | ||
311 | or | ||
312 | .Dv IPV6_DSTOPTS . | ||
313 | .Pp | ||
314 | .Fa tptrp | ||
315 | is a pointer to a pointer to an 8-bit byte and | ||
316 | .Fa *tptrp | ||
317 | is used | ||
318 | by the function to remember its place in the ancillary data object | ||
319 | each time the function is called. | ||
320 | The first time this function is | ||
321 | called for a given ancillary data object, | ||
322 | .Fa *tptrp | ||
323 | must be set to | ||
324 | .Dv NULL . | ||
325 | .Pp | ||
326 | Each time this function returns success, | ||
327 | .Fa *tptrp | ||
328 | points to the 8-bit | ||
329 | option type field for the next option to be processed. | ||
330 | .\" | ||
331 | .Ss inet6_option_find | ||
332 | This function is similar to the previously described | ||
333 | .Fn inet6_option_next | ||
334 | function, except this function lets the caller | ||
335 | specify the option type to be searched for, instead of always | ||
336 | returning the next option in the ancillary data object. | ||
337 | .Fa cmsg | ||
338 | is a | ||
339 | pointer to | ||
340 | .Li cmsghdr | ||
341 | structure of which | ||
342 | .Li cmsg_level | ||
343 | equals | ||
344 | .Dv IPPROTO_IPV6 | ||
345 | and | ||
346 | .Li cmsg_type | ||
347 | equals either | ||
348 | .Dv IPV6_HOPOPTS | ||
349 | or | ||
350 | .Dv IPV6_DSTOPTS . | ||
351 | .Pp | ||
352 | .Fa tptrp | ||
353 | is a pointer to a pointer to an 8-bit byte and | ||
354 | .Fa *tptrp | ||
355 | is used | ||
356 | by the function to remember its place in the ancillary data object | ||
357 | each time the function is called. | ||
358 | The first time this function is | ||
359 | called for a given ancillary data object, | ||
360 | .Fa *tptrp | ||
361 | must be set to | ||
362 | .Dv NULL . | ||
363 | .Pp | ||
364 | This function starts searching for an option of the specified type | ||
365 | beginning after the value of | ||
366 | .Fa *tptrp . | ||
367 | If an option of the specified | ||
368 | type is located, this function returns | ||
369 | .Li 0 | ||
370 | and | ||
371 | .Fa *tptrp | ||
372 | points to the 8- | ||
373 | bit option type field for the option of the specified type. | ||
374 | If an | ||
375 | option of the specified type is not located, the return value is | ||
376 | .Li -1 | ||
377 | and | ||
378 | .Fa *tptrp | ||
379 | is | ||
380 | .Dv NULL . | ||
381 | If an error occurs, the return value is | ||
382 | .Li -1 | ||
383 | and | ||
384 | .Fa *tptrp | ||
385 | is not | ||
386 | .Dv NULL . | ||
387 | .\" | ||
388 | .Sh EXAMPLES | ||
389 | RFC 2292 gives comprehensive examples in chapter 6. | ||
390 | .\" | ||
391 | .Sh DIAGNOSTICS | ||
392 | .Fn inet6_option_init | ||
393 | and | ||
394 | .Fn inet6_option_append | ||
395 | return | ||
396 | .Li 0 | ||
397 | on success or | ||
398 | .Li -1 | ||
399 | on an error. | ||
400 | .Pp | ||
401 | .Fn inet6_option_alloc | ||
402 | returns | ||
403 | .Dv NULL | ||
404 | on an error. | ||
405 | .Pp | ||
406 | On errors, | ||
407 | .Fn inet6_option_next | ||
408 | and | ||
409 | .Fn inet6_option_find | ||
410 | return | ||
411 | .Li -1 | ||
412 | setting | ||
413 | .Fa *tptrp | ||
414 | to non | ||
415 | .Dv NULL | ||
416 | value. | ||
417 | .\" | ||
418 | .Sh SEE ALSO | ||
419 | .Rs | ||
420 | .%A W. Stevens | ||
421 | .%A M. Thomas | ||
422 | .%T "Advanced Sockets API for IPv6" | ||
423 | .%N RFC 2292 | ||
424 | .%D February 1998 | ||
425 | .Re | ||
426 | .Rs | ||
427 | .%A S. Deering | ||
428 | .%A R. Hinden | ||
429 | .%T "Internet Protocol, Version 6 (IPv6) Specification" | ||
430 | .%N RFC 2460 | ||
431 | .%D December 1998 | ||
432 | .Re | ||
433 | .\" | ||
434 | .Sh STANDARDS | ||
435 | The functions | ||
436 | are documented in | ||
437 | .Dq Advanced Sockets API for IPv6 | ||
438 | .Pq RFC 2292 . | ||
439 | .\" | ||
440 | .Sh HISTORY | ||
441 | The implementation first appeared in KAME advanced networking kit. | ||
442 | .\" | ||
443 | .Sh BUGS | ||
444 | The text was shamelessly copied from RFC 2292. | ||
diff --git a/src/lib/libc/net/inet6_rthdr_space.3 b/src/lib/libc/net/inet6_rthdr_space.3 new file mode 100644 index 0000000000..33b209af4f --- /dev/null +++ b/src/lib/libc/net/inet6_rthdr_space.3 | |||
@@ -0,0 +1,317 @@ | |||
1 | .\" $OpenBSD: inet6_rthdr_space.3,v 1.13 2003/08/08 09:26:02 jmc Exp $ | ||
2 | .\" $KAME: inet6_rthdr_space.3,v 1.8 2000/05/17 14:30:15 itojun Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1983, 1987, 1991, 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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd December 10, 1999 | ||
32 | .Dt INET6_RTHDR_SPACE 3 | ||
33 | .Os | ||
34 | .\" | ||
35 | .Sh NAME | ||
36 | .Nm inet6_rthdr_space , | ||
37 | .Nm inet6_rthdr_init , | ||
38 | .Nm inet6_rthdr_add , | ||
39 | .Nm inet6_rthdr_lasthop , | ||
40 | .Nm inet6_rthdr_reverse , | ||
41 | .Nm inet6_rthdr_segments , | ||
42 | .Nm inet6_rthdr_getaddr , | ||
43 | .Nm inet6_rthdr_getflags | ||
44 | .Nd IPv6 Routing Header Options manipulation | ||
45 | .\" | ||
46 | .Sh SYNOPSIS | ||
47 | .Fd #include <netinet/in.h> | ||
48 | .Ft size_t | ||
49 | .Fn inet6_rthdr_space "int type" "int segments" | ||
50 | .Ft "struct cmsghdr *" | ||
51 | .Fn inet6_rthdr_init "void *bp" "int type" | ||
52 | .Ft int | ||
53 | .Fn inet6_rthdr_add "struct cmsghdr *cmsg" "const struct in6_addr *addr" "unsigned int flags" | ||
54 | .Ft int | ||
55 | .Fn inet6_rthdr_lasthop "struct cmsghdr *cmsg" "unsigned int flags" | ||
56 | .Ft int | ||
57 | .Fn inet6_rthdr_reverse "const struct cmsghdr *in" "struct cmsghdr *out" | ||
58 | .Ft int | ||
59 | .Fn inet6_rthdr_segments "const struct cmsghdr *cmsg" | ||
60 | .Ft "struct in6_addr *" | ||
61 | .Fn inet6_rthdr_getaddr "struct cmsghdr *cmsg" "int index" | ||
62 | .Ft int | ||
63 | .Fn inet6_rthdr_getflags "const struct cmsghdr *cmsg" "int index" | ||
64 | .\" | ||
65 | .Sh DESCRIPTION | ||
66 | RFC 2292 IPv6 advanced API defines eight | ||
67 | functions that the application calls to build and examine a Routing | ||
68 | header. | ||
69 | Four functions build a Routing header: | ||
70 | .Bl -hang | ||
71 | .It Fn inet6_rthdr_space | ||
72 | return #bytes required for ancillary data | ||
73 | .It Fn inet6_rthdr_init | ||
74 | initialize ancillary data for Routing header | ||
75 | .It Fn inet6_rthdr_add | ||
76 | add IPv6 address & flags to Routing header | ||
77 | .It Fn inet6_rthdr_lasthop | ||
78 | specify the flags for the final hop | ||
79 | .El | ||
80 | .Pp | ||
81 | Four functions deal with a returned Routing header: | ||
82 | .Bl -hang | ||
83 | .It Fn inet6_rthdr_reverse | ||
84 | reverse a Routing header | ||
85 | .It Fn inet6_rthdr_segments | ||
86 | return #segments in a Routing header | ||
87 | .It Fn inet6_rthdr_getaddr | ||
88 | fetch one address from a Routing header | ||
89 | .It Fn inet6_rthdr_getflags | ||
90 | fetch one flag from a Routing header | ||
91 | .El | ||
92 | .Pp | ||
93 | The function prototypes for these functions are all in the | ||
94 | .Aq Pa netinet/in.h | ||
95 | header. | ||
96 | .\" | ||
97 | .Ss inet6_rthdr_space | ||
98 | This function returns the number of bytes required to hold a Routing | ||
99 | header of the specified | ||
100 | .Fa type | ||
101 | containing the specified number of | ||
102 | .Fa segments | ||
103 | .Pq addresses . | ||
104 | For an IPv6 Type 0 Routing header, the number | ||
105 | of segments must be between 1 and 23, inclusive. | ||
106 | The return value | ||
107 | includes the size of the cmsghdr structure that precedes the Routing | ||
108 | header, and any required padding. | ||
109 | .Pp | ||
110 | If the return value is 0, then either the type of the Routing header | ||
111 | is not supported by this implementation or the number of segments is | ||
112 | invalid for this type of Routing header. | ||
113 | .Pp | ||
114 | Note: This function returns the size but does not allocate the space | ||
115 | required for the ancillary data. | ||
116 | This allows an application to | ||
117 | allocate a larger buffer, if other ancillary data objects are | ||
118 | desired, since all the ancillary data objects must be specified to | ||
119 | .Xr sendmsg 2 | ||
120 | as a single | ||
121 | .Li msg_control | ||
122 | buffer. | ||
123 | .\" | ||
124 | .Ss inet6_rthdr_init | ||
125 | This function initializes the buffer pointed to by | ||
126 | .Fa bp | ||
127 | to contain a | ||
128 | .Li cmsghdr | ||
129 | structure followed by a Routing header of the specified | ||
130 | .Fa type . | ||
131 | The | ||
132 | .Li cmsg_len | ||
133 | member of the | ||
134 | .Li cmsghdr | ||
135 | structure is initialized to the | ||
136 | size of the structure plus the amount of space required by the | ||
137 | Routing header. | ||
138 | The | ||
139 | .Li cmsg_level | ||
140 | and | ||
141 | .Li cmsg_type | ||
142 | members are also initialized as required. | ||
143 | .Pp | ||
144 | The caller must allocate the buffer and its size can be determined by | ||
145 | calling | ||
146 | .Fn inet6_rthdr_space . | ||
147 | .Pp | ||
148 | Upon success the return value is the pointer to the | ||
149 | .Li cmsghdr | ||
150 | structure, and this is then used as the first argument to the next | ||
151 | two functions. | ||
152 | Upon an error the return value is | ||
153 | .Dv NULL . | ||
154 | .\" | ||
155 | .Ss inet6_rthdr_add | ||
156 | This function adds the address pointed to by | ||
157 | .Fa addr | ||
158 | to the end of the | ||
159 | Routing header being constructed and sets the type of this hop to the | ||
160 | value of | ||
161 | .Fa flags . | ||
162 | For an IPv6 Type 0 Routing header, | ||
163 | .Fa flags | ||
164 | must be | ||
165 | either | ||
166 | .Dv IPV6_RTHDR_LOOSE | ||
167 | or | ||
168 | .Dv IPV6_RTHDR_STRICT . | ||
169 | .Pp | ||
170 | If successful, the | ||
171 | .Li cmsg_len | ||
172 | member of the | ||
173 | .Li cmsghdr | ||
174 | structure is | ||
175 | updated to account for the new address in the Routing header and the | ||
176 | return value of the function is 0. | ||
177 | Upon an error the return value of | ||
178 | the function is -1. | ||
179 | .\" | ||
180 | .Ss inet6_rthdr_lasthop | ||
181 | This function specifies the Strict/Loose flag for the final hop of a | ||
182 | Routing header. | ||
183 | For an IPv6 Type 0 Routing header, | ||
184 | .Fa flags | ||
185 | must be either | ||
186 | .Dv IPV6_RTHDR_LOOSE | ||
187 | or | ||
188 | .Dv IPV6_RTHDR_STRICT . | ||
189 | .Pp | ||
190 | The return value of the function is 0 upon success, or -1 upon an error. | ||
191 | .Pp | ||
192 | Notice that a Routing header specifying | ||
193 | .Li N | ||
194 | intermediate nodes requires | ||
195 | .Li N+1 | ||
196 | Strict/Loose flags. | ||
197 | This requires | ||
198 | .Li N | ||
199 | calls to | ||
200 | .Fn inet6_rthdr_add | ||
201 | followed by one call to | ||
202 | .Fn inet6_rthdr_lasthop . | ||
203 | .\" | ||
204 | .Ss inet6_rthdr_reverse | ||
205 | This function takes a Routing header that was received as ancillary | ||
206 | data | ||
207 | .Po | ||
208 | pointed to by the first argument, | ||
209 | .Fa in | ||
210 | .Pc | ||
211 | and writes a new Routing | ||
212 | header that sends datagrams along the reverse of that route. | ||
213 | Both | ||
214 | arguments are allowed to point to the same buffer | ||
215 | .Pq that is, the reversal can occur in place . | ||
216 | .Pp | ||
217 | The return value of the function is 0 on success, or -1 upon an | ||
218 | error. | ||
219 | .\" | ||
220 | .Ss inet6_rthdr_segments | ||
221 | This function returns the number of segments | ||
222 | .Pq addresses | ||
223 | contained in | ||
224 | the Routing header described by | ||
225 | .Fa cmsg . | ||
226 | On success the return value is | ||
227 | between 1 and 23, inclusive. | ||
228 | The return value of the function is -1 upon an error. | ||
229 | .\" | ||
230 | .Ss inet6_rthdr_getaddr | ||
231 | This function returns a pointer to the IPv6 address specified by | ||
232 | .Fa index | ||
233 | (which must have a value between 1 and the value returned by | ||
234 | .Fn inet6_rthdr_segments ) | ||
235 | in the Routing header described by | ||
236 | .Fa cmsg . | ||
237 | An | ||
238 | application should first call | ||
239 | .Fn inet6_rthdr_segments | ||
240 | to obtain the number of segments in the Routing header. | ||
241 | .Pp | ||
242 | Upon an error the return value of the function is | ||
243 | .Dv NULL . | ||
244 | .\" | ||
245 | .Ss inet6_rthdr_getflags | ||
246 | This function returns the flags value specified by | ||
247 | .Fa index | ||
248 | (which must | ||
249 | have a value between 0 and the value returned by | ||
250 | .Fn inet6_rthdr_segments ) | ||
251 | in the Routing header described by | ||
252 | .Fa cmsg . | ||
253 | For an IPv6 Type 0 Routing header the return value will be either | ||
254 | .Dv IPV6_RTHDR_LOOSE | ||
255 | or | ||
256 | .Dv IPV6_RTHDR_STRICT . | ||
257 | .Pp | ||
258 | Upon an error the return value of the function is -1. | ||
259 | .Pp | ||
260 | Note: Addresses are indexed starting at 1, and flags starting at 0, | ||
261 | to maintain consistency with the terminology and figures in RFC 2460. | ||
262 | .\" | ||
263 | .Sh EXAMPLES | ||
264 | RFC 2292 gives comprehensive examples in chapter 8. | ||
265 | .\" | ||
266 | .Sh DIAGNOSTICS | ||
267 | .Fn inet6_rthdr_space | ||
268 | returns 0 on errors. | ||
269 | .Pp | ||
270 | .Fn inet6_rthdr_add , | ||
271 | .Fn inet6_rthdr_lasthop | ||
272 | and | ||
273 | .Fn inet6_rthdr_reverse | ||
274 | return 0 on success, and returns -1 on error. | ||
275 | .Pp | ||
276 | .Fn inet6_rthdr_init | ||
277 | and | ||
278 | .Fn inet6_rthdr_getaddr | ||
279 | return | ||
280 | .Dv NULL | ||
281 | on error. | ||
282 | .Pp | ||
283 | .Fn inet6_rthdr_segments | ||
284 | and | ||
285 | .Fn inet6_rthdr_getflags | ||
286 | return -1 on error. | ||
287 | .\" | ||
288 | .Sh SEE ALSO | ||
289 | .Rs | ||
290 | .%A W. Stevens | ||
291 | .%A M. Thomas | ||
292 | .%T "Advanced Sockets API for IPv6" | ||
293 | .%N RFC 2292 | ||
294 | .%D February 1998 | ||
295 | .Re | ||
296 | .Rs | ||
297 | .%A S. Deering | ||
298 | .%A R. Hinden | ||
299 | .%T "Internet Protocol, Version 6 (IPv6) Specification" | ||
300 | .%N RFC 2460 | ||
301 | .%D December 1998 | ||
302 | .Re | ||
303 | .\" | ||
304 | .Sh STANDARDS | ||
305 | The functions | ||
306 | are documented in | ||
307 | .Dq Advanced Sockets API for IPv6 | ||
308 | .Pq RFC 2292 . | ||
309 | .\" | ||
310 | .Sh HISTORY | ||
311 | The implementation first appeared in KAME advanced networking kit. | ||
312 | .\" | ||
313 | .Sh BUGS | ||
314 | The text was shamelessly copied from RFC 2292. | ||
315 | .Pp | ||
316 | .Fn inet6_rthdr_reverse | ||
317 | is not implemented yet. | ||
diff --git a/src/lib/libc/net/inet_addr.c b/src/lib/libc/net/inet_addr.c new file mode 100644 index 0000000000..716dcb6f80 --- /dev/null +++ b/src/lib/libc/net/inet_addr.c | |||
@@ -0,0 +1,183 @@ | |||
1 | /* $OpenBSD: inet_addr.c,v 1.7 2003/06/02 20:18:35 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1983, 1990, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1983, 1990, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #if defined(LIBC_SCCS) && !defined(lint) | ||
55 | #if 0 | ||
56 | static char sccsid[] = "@(#)inet_addr.c 8.1 (Berkeley) 6/17/93"; | ||
57 | static char rcsid[] = "$From: inet_addr.c,v 8.5 1996/08/05 08:31:35 vixie Exp $"; | ||
58 | #else | ||
59 | static char rcsid[] = "$OpenBSD: inet_addr.c,v 1.7 2003/06/02 20:18:35 millert Exp $"; | ||
60 | #endif | ||
61 | #endif /* LIBC_SCCS and not lint */ | ||
62 | |||
63 | #include <sys/types.h> | ||
64 | #include <sys/param.h> | ||
65 | #include <netinet/in.h> | ||
66 | #include <arpa/inet.h> | ||
67 | #include <ctype.h> | ||
68 | |||
69 | /* | ||
70 | * Ascii internet address interpretation routine. | ||
71 | * The value returned is in network order. | ||
72 | */ | ||
73 | in_addr_t | ||
74 | inet_addr(cp) | ||
75 | register const char *cp; | ||
76 | { | ||
77 | struct in_addr val; | ||
78 | |||
79 | if (inet_aton(cp, &val)) | ||
80 | return (val.s_addr); | ||
81 | return (INADDR_NONE); | ||
82 | } | ||
83 | |||
84 | /* | ||
85 | * Check whether "cp" is a valid ascii representation | ||
86 | * of an Internet address and convert to a binary address. | ||
87 | * Returns 1 if the address is valid, 0 if not. | ||
88 | * This replaces inet_addr, the return value from which | ||
89 | * cannot distinguish between failure and a local broadcast address. | ||
90 | */ | ||
91 | int | ||
92 | inet_aton(cp, addr) | ||
93 | register const char *cp; | ||
94 | struct in_addr *addr; | ||
95 | { | ||
96 | register in_addr_t val; | ||
97 | register int base, n; | ||
98 | register char c; | ||
99 | u_int parts[4]; | ||
100 | register u_int *pp = parts; | ||
101 | |||
102 | c = *cp; | ||
103 | for (;;) { | ||
104 | /* | ||
105 | * Collect number up to ``.''. | ||
106 | * Values are specified as for C: | ||
107 | * 0x=hex, 0=octal, isdigit=decimal. | ||
108 | */ | ||
109 | if (!isdigit(c)) | ||
110 | return (0); | ||
111 | val = 0; base = 10; | ||
112 | if (c == '0') { | ||
113 | c = *++cp; | ||
114 | if (c == 'x' || c == 'X') | ||
115 | base = 16, c = *++cp; | ||
116 | else | ||
117 | base = 8; | ||
118 | } | ||
119 | for (;;) { | ||
120 | if (isascii(c) && isdigit(c)) { | ||
121 | val = (val * base) + (c - '0'); | ||
122 | c = *++cp; | ||
123 | } else if (base == 16 && isascii(c) && isxdigit(c)) { | ||
124 | val = (val << 4) | | ||
125 | (c + 10 - (islower(c) ? 'a' : 'A')); | ||
126 | c = *++cp; | ||
127 | } else | ||
128 | break; | ||
129 | } | ||
130 | if (c == '.') { | ||
131 | /* | ||
132 | * Internet format: | ||
133 | * a.b.c.d | ||
134 | * a.b.c (with c treated as 16 bits) | ||
135 | * a.b (with b treated as 24 bits) | ||
136 | */ | ||
137 | if (pp >= parts + 3) | ||
138 | return (0); | ||
139 | *pp++ = val; | ||
140 | c = *++cp; | ||
141 | } else | ||
142 | break; | ||
143 | } | ||
144 | /* | ||
145 | * Check for trailing characters. | ||
146 | */ | ||
147 | if (c != '\0' && (!isascii(c) || !isspace(c))) | ||
148 | return (0); | ||
149 | /* | ||
150 | * Concoct the address according to | ||
151 | * the number of parts specified. | ||
152 | */ | ||
153 | n = pp - parts + 1; | ||
154 | switch (n) { | ||
155 | |||
156 | case 0: | ||
157 | return (0); /* initial nondigit */ | ||
158 | |||
159 | case 1: /* a -- 32 bits */ | ||
160 | break; | ||
161 | |||
162 | case 2: /* a.b -- 8.24 bits */ | ||
163 | if ((val > 0xffffff) || (parts[0] > 0xff)) | ||
164 | return (0); | ||
165 | val |= parts[0] << 24; | ||
166 | break; | ||
167 | |||
168 | case 3: /* a.b.c -- 8.8.16 bits */ | ||
169 | if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff)) | ||
170 | return (0); | ||
171 | val |= (parts[0] << 24) | (parts[1] << 16); | ||
172 | break; | ||
173 | |||
174 | case 4: /* a.b.c.d -- 8.8.8.8 bits */ | ||
175 | if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff)) | ||
176 | return (0); | ||
177 | val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8); | ||
178 | break; | ||
179 | } | ||
180 | if (addr) | ||
181 | addr->s_addr = htonl(val); | ||
182 | return (1); | ||
183 | } | ||
diff --git a/src/lib/libc/net/inet_lnaof.c b/src/lib/libc/net/inet_lnaof.c new file mode 100644 index 0000000000..c1e9780cda --- /dev/null +++ b/src/lib/libc/net/inet_lnaof.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: inet_lnaof.c,v 1.4 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/param.h> | ||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/inet.h> | ||
37 | |||
38 | /* | ||
39 | * Return the local network address portion of an | ||
40 | * internet address; handles class a/b/c network | ||
41 | * number formats. | ||
42 | */ | ||
43 | in_addr_t | ||
44 | inet_lnaof(in) | ||
45 | struct in_addr in; | ||
46 | { | ||
47 | register in_addr_t i = ntohl(in.s_addr); | ||
48 | |||
49 | if (IN_CLASSA(i)) | ||
50 | return ((i)&IN_CLASSA_HOST); | ||
51 | else if (IN_CLASSB(i)) | ||
52 | return ((i)&IN_CLASSB_HOST); | ||
53 | else | ||
54 | return ((i)&IN_CLASSC_HOST); | ||
55 | } | ||
diff --git a/src/lib/libc/net/inet_makeaddr.c b/src/lib/libc/net/inet_makeaddr.c new file mode 100644 index 0000000000..a6b59707fa --- /dev/null +++ b/src/lib/libc/net/inet_makeaddr.c | |||
@@ -0,0 +1,58 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: inet_makeaddr.c,v 1.4 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/param.h> | ||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/inet.h> | ||
37 | |||
38 | /* | ||
39 | * Formulate an Internet address from network + host. Used in | ||
40 | * building addresses stored in the ifnet structure. | ||
41 | */ | ||
42 | struct in_addr | ||
43 | inet_makeaddr(net, host) | ||
44 | in_addr_t net, host; | ||
45 | { | ||
46 | in_addr_t addr; | ||
47 | |||
48 | if (net < 128) | ||
49 | addr = (net << IN_CLASSA_NSHIFT) | (host & IN_CLASSA_HOST); | ||
50 | else if (net < 65536) | ||
51 | addr = (net << IN_CLASSB_NSHIFT) | (host & IN_CLASSB_HOST); | ||
52 | else if (net < 16777216L) | ||
53 | addr = (net << IN_CLASSC_NSHIFT) | (host & IN_CLASSC_HOST); | ||
54 | else | ||
55 | addr = net | host; | ||
56 | addr = htonl(addr); | ||
57 | return (*(struct in_addr *)&addr); | ||
58 | } | ||
diff --git a/src/lib/libc/net/inet_net.3 b/src/lib/libc/net/inet_net.3 new file mode 100644 index 0000000000..c14fa52e38 --- /dev/null +++ b/src/lib/libc/net/inet_net.3 | |||
@@ -0,0 +1,181 @@ | |||
1 | .\" $OpenBSD: inet_net.3,v 1.10 2003/05/01 19:17:37 jmc Exp $ | ||
2 | .\" $NetBSD: inet_net.3,v 1.1 1997/06/18 02:25:27 lukem Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1997 The NetBSD Foundation, Inc. | ||
5 | .\" All rights reserved. | ||
6 | .\" | ||
7 | .\" This code is derived from software contributed to The NetBSD Foundation | ||
8 | .\" by Luke Mewburn. | ||
9 | .\" | ||
10 | .\" Redistribution and use in source and binary forms, with or without | ||
11 | .\" modification, are permitted provided that the following conditions | ||
12 | .\" are met: | ||
13 | .\" 1. Redistributions of source code must retain the above copyright | ||
14 | .\" notice, this list of conditions and the following disclaimer. | ||
15 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
16 | .\" notice, this list of conditions and the following disclaimer in the | ||
17 | .\" documentation and/or other materials provided with the distribution. | ||
18 | .\" 3. All advertising materials mentioning features or use of this software | ||
19 | .\" must display the following acknowledgement: | ||
20 | .\" This product includes software developed by the NetBSD | ||
21 | .\" Foundation, Inc. and its contributors. | ||
22 | .\" 4. Neither the name of The NetBSD Foundation nor the names of its | ||
23 | .\" contributors may be used to endorse or promote products derived | ||
24 | .\" from this software without specific prior written permission. | ||
25 | .\" | ||
26 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
27 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
28 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
29 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE | ||
30 | .\" LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
31 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
32 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
33 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
34 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
35 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
36 | .\" POSSIBILITY OF SUCH DAMAGE. | ||
37 | .\" | ||
38 | .Dd June 18, 1997 | ||
39 | .Dt INET_NET 3 | ||
40 | .Os | ||
41 | .Sh NAME | ||
42 | .Nm inet_net_ntop , | ||
43 | .Nm inet_net_pton | ||
44 | .Nd Internet network number manipulation routines | ||
45 | .Sh SYNOPSIS | ||
46 | .Fd #include <sys/socket.h> | ||
47 | .Fd #include <netinet/in.h> | ||
48 | .Fd #include <arpa/inet.h> | ||
49 | .Ft char * | ||
50 | .Fn inet_net_ntop "int af" "const void *src" "int bits" "char *dst" "size_t size" | ||
51 | .Ft int | ||
52 | .Fn inet_net_pton "int af" "const char *src" "void *dst" "size_t size" | ||
53 | .Sh DESCRIPTION | ||
54 | The | ||
55 | .Fn inet_net_ntop | ||
56 | function converts an Internet network number from network format (usually a | ||
57 | .Li struct in_addr | ||
58 | or some other binary form, in network byte order) to CIDR presentation format | ||
59 | (suitable for external display purposes). | ||
60 | .Fa bits | ||
61 | is the number of bits in | ||
62 | .Fa src | ||
63 | that are the network number. | ||
64 | It returns | ||
65 | .Dv NULL | ||
66 | if a system error occurs (in which case, | ||
67 | .Va errno | ||
68 | will have been set), or it returns a pointer to the destination string. | ||
69 | .Pp | ||
70 | The | ||
71 | .Fn inet_net_pton | ||
72 | function converts a presentation format Internet network number (that is, | ||
73 | printable form as held in a character string) to network format (usually a | ||
74 | .Li struct in_addr | ||
75 | or some other internal binary representation, in network byte order). | ||
76 | It returns the number of bits (either computed based on the class, or | ||
77 | specified with /CIDR), or \-1 if a failure occurred | ||
78 | (in which case | ||
79 | .Va errno | ||
80 | will have been set. | ||
81 | It will be set to | ||
82 | .Er ENOENT | ||
83 | if the Internet network number was not valid). | ||
84 | .Pp | ||
85 | Caution: | ||
86 | The | ||
87 | .Fa dst | ||
88 | field should be zeroed before calling | ||
89 | .Fn inet_net_pton | ||
90 | as the function will only fill the number of bytes necessary to | ||
91 | encode the network number in network byte order. | ||
92 | .Pp | ||
93 | The only value for | ||
94 | .Fa af | ||
95 | currently supported is | ||
96 | .Dv AF_INET . | ||
97 | .Fa size | ||
98 | is the size of the result buffer | ||
99 | .Fa dst . | ||
100 | .Sh NETWORK NUMBERS (IP VERSION 4) | ||
101 | The external representation of Internet network numbers may be specified in | ||
102 | one of the following forms: | ||
103 | .Bd -literal -offset indent | ||
104 | a | ||
105 | a.b | ||
106 | a.b.c | ||
107 | a.b.c.d | ||
108 | .Ed | ||
109 | .Pp | ||
110 | Any of the above four forms may have | ||
111 | .Dq Li /bits | ||
112 | appended where | ||
113 | .Dq Li bits | ||
114 | is in the range | ||
115 | .Li 0-32 | ||
116 | and is used to explicitly specify the number of bits in the network address. | ||
117 | When | ||
118 | .Dq Li /bits | ||
119 | is not specified the number of bits in the network address is calculated | ||
120 | as the larger of the number of bits in the class to which the address | ||
121 | belongs and the number of bits provided rounded up modulo 8. | ||
122 | Examples: | ||
123 | .Bl -tag -width 10.1.2.3/24 | ||
124 | .It Li 10 | ||
125 | an 8 bit network number (class A), value | ||
126 | .Li 10.0.0.0 . | ||
127 | .It Li 192 | ||
128 | a 24 bit network number (class C), value | ||
129 | .Li 192.0.0.0 . | ||
130 | .It Li 10.10 | ||
131 | a 16 bit network number, value | ||
132 | .Li 10.10.0.0 . | ||
133 | .It Li 10.1.2 | ||
134 | a 24 bit network number, value | ||
135 | .Li 10.1.2.0 . | ||
136 | .It Li 10.1.2.3 | ||
137 | a 32 bit network number, value | ||
138 | .Li 10.1.2.3 . | ||
139 | .It Li 10.1.2.3/24 | ||
140 | a 24 bit network number (explicit), value | ||
141 | .Li 10.1.2.3 . | ||
142 | .El | ||
143 | .Pp | ||
144 | Note that when the number of bits is specified using | ||
145 | .Dq Li /bits | ||
146 | notation, the value of the address still includes all bits suplied | ||
147 | in the external representation, even those bits which are the host | ||
148 | part of an internet address. | ||
149 | Also, unlike | ||
150 | .Xr inet_pton 3 | ||
151 | where the external representation is assumed to be an internet address, the | ||
152 | external representation for | ||
153 | .Fn inet_net_pton | ||
154 | is assumed to be a network address. | ||
155 | Thus | ||
156 | .Dq Li 10.1 | ||
157 | is assumed to be | ||
158 | .Dq Li 10.1.0.0 | ||
159 | not | ||
160 | .Dq Li 10.0.0.1 | ||
161 | .Pp | ||
162 | All numbers supplied as | ||
163 | .Dq parts | ||
164 | in a | ||
165 | .Ql \&. | ||
166 | notation | ||
167 | may be decimal, octal, or hexadecimal, as specified | ||
168 | in the C language (i.e., a leading 0x or 0X implies | ||
169 | hexadecimal; otherwise, a leading 0 implies octal; | ||
170 | otherwise, the number is interpreted as decimal). | ||
171 | .Sh SEE ALSO | ||
172 | .Xr byteorder 3 , | ||
173 | .Xr inet 3 , | ||
174 | .Xr inet_pton 3 , | ||
175 | .Xr networks 5 | ||
176 | .Sh HISTORY | ||
177 | The | ||
178 | .Nm inet_net_ntop | ||
179 | and | ||
180 | .Nm inet_net_pton | ||
181 | functions first appeared in BIND 4.9.4. | ||
diff --git a/src/lib/libc/net/inet_net_ntop.c b/src/lib/libc/net/inet_net_ntop.c new file mode 100644 index 0000000000..18eea6bb6d --- /dev/null +++ b/src/lib/libc/net/inet_net_ntop.c | |||
@@ -0,0 +1,153 @@ | |||
1 | /* $OpenBSD: inet_net_ntop.c,v 1.3 2002/08/19 03:01:54 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #if defined(LIBC_SCCS) && !defined(lint) | ||
21 | #if 0 | ||
22 | static const char rcsid[] = "$From: inet_net_ntop.c,v 8.2 1996/08/08 06:54:44 vixie Exp $"; | ||
23 | #else | ||
24 | static const char rcsid[] = "$OpenBSD: inet_net_ntop.c,v 1.3 2002/08/19 03:01:54 itojun Exp $"; | ||
25 | #endif | ||
26 | #endif | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/socket.h> | ||
30 | #include <netinet/in.h> | ||
31 | #include <arpa/inet.h> | ||
32 | |||
33 | #include <errno.h> | ||
34 | #include <stdio.h> | ||
35 | #include <string.h> | ||
36 | #include <stdlib.h> | ||
37 | |||
38 | static char *inet_net_ntop_ipv4(const u_char *, int, char *, size_t); | ||
39 | |||
40 | /* | ||
41 | * char * | ||
42 | * inet_net_ntop(af, src, bits, dst, size) | ||
43 | * convert network number from network to presentation format. | ||
44 | * generates CIDR style result always. | ||
45 | * return: | ||
46 | * pointer to dst, or NULL if an error occurred (check errno). | ||
47 | * author: | ||
48 | * Paul Vixie (ISC), July 1996 | ||
49 | */ | ||
50 | char * | ||
51 | inet_net_ntop(af, src, bits, dst, size) | ||
52 | int af; | ||
53 | const void *src; | ||
54 | int bits; | ||
55 | char *dst; | ||
56 | size_t size; | ||
57 | { | ||
58 | switch (af) { | ||
59 | case AF_INET: | ||
60 | return (inet_net_ntop_ipv4(src, bits, dst, size)); | ||
61 | default: | ||
62 | errno = EAFNOSUPPORT; | ||
63 | return (NULL); | ||
64 | } | ||
65 | } | ||
66 | |||
67 | /* | ||
68 | * static char * | ||
69 | * inet_net_ntop_ipv4(src, bits, dst, size) | ||
70 | * convert IPv4 network number from network to presentation format. | ||
71 | * generates CIDR style result always. | ||
72 | * return: | ||
73 | * pointer to dst, or NULL if an error occurred (check errno). | ||
74 | * note: | ||
75 | * network byte order assumed. this means 192.5.5.240/28 has | ||
76 | * 0x11110000 in its fourth octet. | ||
77 | * author: | ||
78 | * Paul Vixie (ISC), July 1996 | ||
79 | */ | ||
80 | static char * | ||
81 | inet_net_ntop_ipv4(src, bits, dst, size) | ||
82 | const u_char *src; | ||
83 | int bits; | ||
84 | char *dst; | ||
85 | size_t size; | ||
86 | { | ||
87 | char *odst = dst; | ||
88 | char *t; | ||
89 | u_int m; | ||
90 | int b; | ||
91 | char *ep; | ||
92 | int advance; | ||
93 | |||
94 | ep = dst + size; | ||
95 | if (ep <= dst) | ||
96 | goto emsgsize; | ||
97 | |||
98 | if (bits < 0 || bits > 32) { | ||
99 | errno = EINVAL; | ||
100 | return (NULL); | ||
101 | } | ||
102 | if (bits == 0) { | ||
103 | if (ep - dst < sizeof "0") | ||
104 | goto emsgsize; | ||
105 | *dst++ = '0'; | ||
106 | *dst = '\0'; | ||
107 | } | ||
108 | |||
109 | /* Format whole octets. */ | ||
110 | for (b = bits / 8; b > 0; b--) { | ||
111 | if (ep - dst < sizeof "255.") | ||
112 | goto emsgsize; | ||
113 | advance = snprintf(dst, ep - dst, "%u", *src++); | ||
114 | if (advance <= 0 || advance >= ep - dst) | ||
115 | goto emsgsize; | ||
116 | dst += advance; | ||
117 | if (b > 1) { | ||
118 | if (dst + 1 >= ep) | ||
119 | goto emsgsize; | ||
120 | *dst++ = '.'; | ||
121 | *dst = '\0'; | ||
122 | } | ||
123 | } | ||
124 | |||
125 | /* Format partial octet. */ | ||
126 | b = bits % 8; | ||
127 | if (b > 0) { | ||
128 | if (ep - dst < sizeof ".255") | ||
129 | goto emsgsize; | ||
130 | if (dst != odst) | ||
131 | if (dst + 1 >= ep) | ||
132 | goto emsgsize; | ||
133 | *dst++ = '.'; | ||
134 | m = ((1 << b) - 1) << (8 - b); | ||
135 | advance = snprintf(dst, ep - dst, "%u", *src & m); | ||
136 | if (advance <= 0 || advance >= ep - dst) | ||
137 | goto emsgsize; | ||
138 | dst += advance; | ||
139 | } | ||
140 | |||
141 | /* Format CIDR /width. */ | ||
142 | if (ep - dst < sizeof "/32") | ||
143 | goto emsgsize; | ||
144 | advance = snprintf(dst, ep - dst, "/%u", bits); | ||
145 | if (advance <= 0 || advance >= ep - dst) | ||
146 | goto emsgsize; | ||
147 | dst += advance; | ||
148 | return (odst); | ||
149 | |||
150 | emsgsize: | ||
151 | errno = EMSGSIZE; | ||
152 | return (NULL); | ||
153 | } | ||
diff --git a/src/lib/libc/net/inet_net_pton.c b/src/lib/libc/net/inet_net_pton.c new file mode 100644 index 0000000000..3c72efac6b --- /dev/null +++ b/src/lib/libc/net/inet_net_pton.c | |||
@@ -0,0 +1,206 @@ | |||
1 | /* $OpenBSD: inet_net_pton.c,v 1.3 2003/04/25 19:40:25 henning Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #if defined(LIBC_SCCS) && !defined(lint) | ||
21 | #if 0 | ||
22 | static const char rcsid[] = "$From: inet_net_pton.c,v 8.3 1996/11/11 06:36:52 vixie Exp $"; | ||
23 | #else | ||
24 | static const char rcsid[] = "$OpenBSD: inet_net_pton.c,v 1.3 2003/04/25 19:40:25 henning Exp $"; | ||
25 | #endif | ||
26 | #endif | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/socket.h> | ||
30 | #include <netinet/in.h> | ||
31 | #include <arpa/inet.h> | ||
32 | |||
33 | #include <assert.h> | ||
34 | #include <ctype.h> | ||
35 | #include <errno.h> | ||
36 | #include <stdio.h> | ||
37 | #include <string.h> | ||
38 | #include <stdlib.h> | ||
39 | |||
40 | static int inet_net_pton_ipv4(const char *, u_char *, size_t); | ||
41 | |||
42 | /* | ||
43 | * static int | ||
44 | * inet_net_pton(af, src, dst, size) | ||
45 | * convert network number from presentation to network format. | ||
46 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | ||
47 | * "size" is in bytes and describes "dst". | ||
48 | * return: | ||
49 | * number of bits, either imputed classfully or specified with /CIDR, | ||
50 | * or -1 if some failure occurred (check errno). ENOENT means it was | ||
51 | * not a valid network specification. | ||
52 | * author: | ||
53 | * Paul Vixie (ISC), June 1996 | ||
54 | */ | ||
55 | int | ||
56 | inet_net_pton(af, src, dst, size) | ||
57 | int af; | ||
58 | const char *src; | ||
59 | void *dst; | ||
60 | size_t size; | ||
61 | { | ||
62 | switch (af) { | ||
63 | case AF_INET: | ||
64 | return (inet_net_pton_ipv4(src, dst, size)); | ||
65 | default: | ||
66 | errno = EAFNOSUPPORT; | ||
67 | return (-1); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /* | ||
72 | * static int | ||
73 | * inet_net_pton_ipv4(src, dst, size) | ||
74 | * convert IPv4 network number from presentation to network format. | ||
75 | * accepts hex octets, hex strings, decimal octets, and /CIDR. | ||
76 | * "size" is in bytes and describes "dst". | ||
77 | * return: | ||
78 | * number of bits, either imputed classfully or specified with /CIDR, | ||
79 | * or -1 if some failure occurred (check errno). ENOENT means it was | ||
80 | * not an IPv4 network specification. | ||
81 | * note: | ||
82 | * network byte order assumed. this means 192.5.5.240/28 has | ||
83 | * 0x11110000 in its fourth octet. | ||
84 | * author: | ||
85 | * Paul Vixie (ISC), June 1996 | ||
86 | */ | ||
87 | static int | ||
88 | inet_net_pton_ipv4(src, dst, size) | ||
89 | const char *src; | ||
90 | u_char *dst; | ||
91 | size_t size; | ||
92 | { | ||
93 | static const char | ||
94 | xdigits[] = "0123456789abcdef", | ||
95 | digits[] = "0123456789"; | ||
96 | int n, ch, tmp, dirty, bits; | ||
97 | const u_char *odst = dst; | ||
98 | |||
99 | ch = *src++; | ||
100 | if (ch == '0' && (src[0] == 'x' || src[0] == 'X') | ||
101 | && isascii(src[1]) && isxdigit(src[1])) { | ||
102 | /* Hexadecimal: Eat nybble string. */ | ||
103 | if (size <= 0) | ||
104 | goto emsgsize; | ||
105 | *dst = 0, dirty = 0; | ||
106 | src++; /* skip x or X. */ | ||
107 | while ((ch = *src++) != '\0' && | ||
108 | isascii(ch) && isxdigit(ch)) { | ||
109 | if (isupper(ch)) | ||
110 | ch = tolower(ch); | ||
111 | n = strchr(xdigits, ch) - xdigits; | ||
112 | assert(n >= 0 && n <= 15); | ||
113 | *dst |= n; | ||
114 | if (!dirty++) | ||
115 | *dst <<= 4; | ||
116 | else if (size-- > 0) | ||
117 | *++dst = 0, dirty = 0; | ||
118 | else | ||
119 | goto emsgsize; | ||
120 | } | ||
121 | if (dirty) | ||
122 | size--; | ||
123 | } else if (isascii(ch) && isdigit(ch)) { | ||
124 | /* Decimal: eat dotted digit string. */ | ||
125 | for (;;) { | ||
126 | tmp = 0; | ||
127 | do { | ||
128 | n = strchr(digits, ch) - digits; | ||
129 | assert(n >= 0 && n <= 9); | ||
130 | tmp *= 10; | ||
131 | tmp += n; | ||
132 | if (tmp > 255) | ||
133 | goto enoent; | ||
134 | } while ((ch = *src++) != '\0' && | ||
135 | isascii(ch) && isdigit(ch)); | ||
136 | if (size-- <= 0) | ||
137 | goto emsgsize; | ||
138 | *dst++ = (u_char) tmp; | ||
139 | if (ch == '\0' || ch == '/') | ||
140 | break; | ||
141 | if (ch != '.') | ||
142 | goto enoent; | ||
143 | ch = *src++; | ||
144 | if (!isascii(ch) || !isdigit(ch)) | ||
145 | goto enoent; | ||
146 | } | ||
147 | } else | ||
148 | goto enoent; | ||
149 | |||
150 | bits = -1; | ||
151 | if (ch == '/' && isascii(src[0]) && isdigit(src[0]) && dst > odst) { | ||
152 | /* CIDR width specifier. Nothing can follow it. */ | ||
153 | ch = *src++; /* Skip over the /. */ | ||
154 | bits = 0; | ||
155 | do { | ||
156 | n = strchr(digits, ch) - digits; | ||
157 | assert(n >= 0 && n <= 9); | ||
158 | bits *= 10; | ||
159 | bits += n; | ||
160 | } while ((ch = *src++) != '\0' && | ||
161 | isascii(ch) && isdigit(ch)); | ||
162 | if (ch != '\0') | ||
163 | goto enoent; | ||
164 | if (bits > 32) | ||
165 | goto emsgsize; | ||
166 | } | ||
167 | |||
168 | /* Firey death and destruction unless we prefetched EOS. */ | ||
169 | if (ch != '\0') | ||
170 | goto enoent; | ||
171 | |||
172 | /* If nothing was written to the destination, we found no address. */ | ||
173 | if (dst == odst) | ||
174 | goto enoent; | ||
175 | /* If no CIDR spec was given, infer width from net class. */ | ||
176 | if (bits == -1) { | ||
177 | if (*odst >= 240) /* Class E */ | ||
178 | bits = 32; | ||
179 | else if (*odst >= 224) /* Class D */ | ||
180 | bits = 4; | ||
181 | else if (*odst >= 192) /* Class C */ | ||
182 | bits = 24; | ||
183 | else if (*odst >= 128) /* Class B */ | ||
184 | bits = 16; | ||
185 | else /* Class A */ | ||
186 | bits = 8; | ||
187 | /* If imputed mask is narrower than specified octets, widen. */ | ||
188 | if (bits < ((dst - odst) * 8)) | ||
189 | bits = (dst - odst) * 8; | ||
190 | } | ||
191 | /* Extend network to cover the actual mask. */ | ||
192 | while (bits > ((dst - odst) * 8)) { | ||
193 | if (size-- <= 0) | ||
194 | goto emsgsize; | ||
195 | *dst++ = '\0'; | ||
196 | } | ||
197 | return (bits); | ||
198 | |||
199 | enoent: | ||
200 | errno = ENOENT; | ||
201 | return (-1); | ||
202 | |||
203 | emsgsize: | ||
204 | errno = EMSGSIZE; | ||
205 | return (-1); | ||
206 | } | ||
diff --git a/src/lib/libc/net/inet_neta.c b/src/lib/libc/net/inet_neta.c new file mode 100644 index 0000000000..6960bcd0b5 --- /dev/null +++ b/src/lib/libc/net/inet_neta.c | |||
@@ -0,0 +1,91 @@ | |||
1 | /* $OpenBSD: inet_neta.c,v 1.4 2002/08/19 03:01:54 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #if defined(LIBC_SCCS) && !defined(lint) | ||
21 | #if 0 | ||
22 | static const char rcsid[] = "$Id: inet_neta.c,v 1.4 2002/08/19 03:01:54 itojun Exp $"; | ||
23 | #else | ||
24 | static const char rcsid[] = "$OpenBSD: inet_neta.c,v 1.4 2002/08/19 03:01:54 itojun Exp $"; | ||
25 | #endif | ||
26 | #endif | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/socket.h> | ||
30 | #include <netinet/in.h> | ||
31 | #include <arpa/inet.h> | ||
32 | |||
33 | #include <errno.h> | ||
34 | #include <stdio.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | /* | ||
38 | * char * | ||
39 | * inet_neta(src, dst, size) | ||
40 | * format an in_addr_t network number into presentation format. | ||
41 | * return: | ||
42 | * pointer to dst, or NULL if an error occurred (check errno). | ||
43 | * note: | ||
44 | * format of ``src'' is as for inet_network(). | ||
45 | * author: | ||
46 | * Paul Vixie (ISC), July 1996 | ||
47 | */ | ||
48 | char * | ||
49 | inet_neta(src, dst, size) | ||
50 | in_addr_t src; | ||
51 | char *dst; | ||
52 | size_t size; | ||
53 | { | ||
54 | char *odst = dst; | ||
55 | char *ep; | ||
56 | int advance; | ||
57 | |||
58 | if (src == 0x00000000) { | ||
59 | if (size < sizeof "0.0.0.0") | ||
60 | goto emsgsize; | ||
61 | strlcpy(dst, "0.0.0.0", size); | ||
62 | return dst; | ||
63 | } | ||
64 | ep = dst + size; | ||
65 | if (ep <= dst) | ||
66 | goto emsgsize; | ||
67 | while (src & 0xffffffff) { | ||
68 | u_char b = (src & 0xff000000) >> 24; | ||
69 | |||
70 | src <<= 8; | ||
71 | if (b || src) { | ||
72 | if (ep - dst < sizeof "255.") | ||
73 | goto emsgsize; | ||
74 | advance = snprintf(dst, ep - dst, "%u", b); | ||
75 | if (advance <= 0 || advance >= ep - dst) | ||
76 | goto emsgsize; | ||
77 | dst += advance; | ||
78 | if (src != 0L) { | ||
79 | if (dst + 1 >= ep) | ||
80 | goto emsgsize; | ||
81 | *dst++ = '.'; | ||
82 | *dst = '\0'; | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | return (odst); | ||
87 | |||
88 | emsgsize: | ||
89 | errno = EMSGSIZE; | ||
90 | return (NULL); | ||
91 | } | ||
diff --git a/src/lib/libc/net/inet_netof.c b/src/lib/libc/net/inet_netof.c new file mode 100644 index 0000000000..58be2d05cd --- /dev/null +++ b/src/lib/libc/net/inet_netof.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: inet_netof.c,v 1.4 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/param.h> | ||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/inet.h> | ||
37 | |||
38 | /* | ||
39 | * Return the network number from an internet | ||
40 | * address; handles class a/b/c network #'s. | ||
41 | */ | ||
42 | in_addr_t | ||
43 | inet_netof(in) | ||
44 | struct in_addr in; | ||
45 | { | ||
46 | register in_addr_t i = ntohl(in.s_addr); | ||
47 | |||
48 | if (IN_CLASSA(i)) | ||
49 | return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); | ||
50 | else if (IN_CLASSB(i)) | ||
51 | return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); | ||
52 | else | ||
53 | return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); | ||
54 | } | ||
diff --git a/src/lib/libc/net/inet_network.c b/src/lib/libc/net/inet_network.c new file mode 100644 index 0000000000..5e36f78069 --- /dev/null +++ b/src/lib/libc/net/inet_network.c | |||
@@ -0,0 +1,88 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: inet_network.c,v 1.8 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/inet.h> | ||
37 | #include <ctype.h> | ||
38 | |||
39 | /* | ||
40 | * Internet network address interpretation routine. | ||
41 | * The library routines call this routine to interpret | ||
42 | * network numbers. | ||
43 | */ | ||
44 | in_addr_t | ||
45 | inet_network(cp) | ||
46 | register const char *cp; | ||
47 | { | ||
48 | register in_addr_t val, base, n; | ||
49 | register char c; | ||
50 | in_addr_t parts[4], *pp = parts; | ||
51 | register int i; | ||
52 | |||
53 | again: | ||
54 | val = 0; base = 10; | ||
55 | if (*cp == '0') | ||
56 | base = 8, cp++; | ||
57 | if (*cp == 'x' || *cp == 'X') | ||
58 | base = 16, cp++; | ||
59 | while ((c = *cp)) { | ||
60 | if (isdigit(c)) { | ||
61 | val = (val * base) + (c - '0'); | ||
62 | cp++; | ||
63 | continue; | ||
64 | } | ||
65 | if (base == 16 && isxdigit(c)) { | ||
66 | val = (val << 4) + (c + 10 - (islower(c) ? 'a' : 'A')); | ||
67 | cp++; | ||
68 | continue; | ||
69 | } | ||
70 | break; | ||
71 | } | ||
72 | if (*cp == '.') { | ||
73 | if (pp >= parts + 3) | ||
74 | return (INADDR_NONE); | ||
75 | *pp++ = val, cp++; | ||
76 | goto again; | ||
77 | } | ||
78 | if (*cp && !isspace(*cp)) | ||
79 | return (INADDR_NONE); | ||
80 | *pp++ = val; | ||
81 | n = pp - parts; | ||
82 | for (val = 0, i = 0; i < 4; i++) { | ||
83 | val <<= 8; | ||
84 | if (i < n) | ||
85 | val |= parts[i] & 0xff; | ||
86 | } | ||
87 | return (val); | ||
88 | } | ||
diff --git a/src/lib/libc/net/inet_ntoa.c b/src/lib/libc/net/inet_ntoa.c new file mode 100644 index 0000000000..bd3fd98b09 --- /dev/null +++ b/src/lib/libc/net/inet_ntoa.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: inet_ntoa.c,v 1.4 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | /* | ||
35 | * Convert network-format internet address | ||
36 | * to base 256 d.d.d.d representation. | ||
37 | */ | ||
38 | #include <sys/types.h> | ||
39 | #include <netinet/in.h> | ||
40 | #include <arpa/inet.h> | ||
41 | #include <stdio.h> | ||
42 | |||
43 | char * | ||
44 | inet_ntoa(in) | ||
45 | struct in_addr in; | ||
46 | { | ||
47 | static char b[18]; | ||
48 | register char *p; | ||
49 | |||
50 | p = (char *)∈ | ||
51 | #define UC(b) (((int)b)&0xff) | ||
52 | (void)snprintf(b, sizeof(b), | ||
53 | "%u.%u.%u.%u", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); | ||
54 | return (b); | ||
55 | } | ||
diff --git a/src/lib/libc/net/inet_ntop.c b/src/lib/libc/net/inet_ntop.c new file mode 100644 index 0000000000..adce61c1d4 --- /dev/null +++ b/src/lib/libc/net/inet_ntop.c | |||
@@ -0,0 +1,213 @@ | |||
1 | /* $OpenBSD: inet_ntop.c,v 1.5 2002/08/23 16:27:31 itojun Exp $ */ | ||
2 | |||
3 | /* Copyright (c) 1996 by Internet Software Consortium. | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
10 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
11 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
12 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
13 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
14 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
15 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
16 | * SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #if defined(LIBC_SCCS) && !defined(lint) | ||
20 | #if 0 | ||
21 | static char rcsid[] = "$From: inet_ntop.c,v 8.7 1996/08/05 08:41:18 vixie Exp $"; | ||
22 | #else | ||
23 | static char rcsid[] = "$OpenBSD: inet_ntop.c,v 1.5 2002/08/23 16:27:31 itojun Exp $"; | ||
24 | #endif | ||
25 | #endif /* LIBC_SCCS and not lint */ | ||
26 | |||
27 | #include <sys/param.h> | ||
28 | #include <sys/types.h> | ||
29 | #include <sys/socket.h> | ||
30 | #include <netinet/in.h> | ||
31 | #include <arpa/inet.h> | ||
32 | #include <arpa/nameser.h> | ||
33 | #include <string.h> | ||
34 | #include <errno.h> | ||
35 | #include <stdio.h> | ||
36 | |||
37 | /* | ||
38 | * WARNING: Don't even consider trying to compile this on a system where | ||
39 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | ||
40 | */ | ||
41 | |||
42 | static const char *inet_ntop4(const u_char *src, char *dst, size_t size); | ||
43 | static const char *inet_ntop6(const u_char *src, char *dst, size_t size); | ||
44 | |||
45 | /* char * | ||
46 | * inet_ntop(af, src, dst, size) | ||
47 | * convert a network format address to presentation format. | ||
48 | * return: | ||
49 | * pointer to presentation format address (`dst'), or NULL (see errno). | ||
50 | * author: | ||
51 | * Paul Vixie, 1996. | ||
52 | */ | ||
53 | const char * | ||
54 | inet_ntop(af, src, dst, size) | ||
55 | int af; | ||
56 | const void *src; | ||
57 | char *dst; | ||
58 | size_t size; | ||
59 | { | ||
60 | switch (af) { | ||
61 | case AF_INET: | ||
62 | return (inet_ntop4(src, dst, size)); | ||
63 | case AF_INET6: | ||
64 | return (inet_ntop6(src, dst, size)); | ||
65 | default: | ||
66 | errno = EAFNOSUPPORT; | ||
67 | return (NULL); | ||
68 | } | ||
69 | /* NOTREACHED */ | ||
70 | } | ||
71 | |||
72 | /* const char * | ||
73 | * inet_ntop4(src, dst, size) | ||
74 | * format an IPv4 address, more or less like inet_ntoa() | ||
75 | * return: | ||
76 | * `dst' (as a const) | ||
77 | * notes: | ||
78 | * (1) uses no statics | ||
79 | * (2) takes a u_char* not an in_addr as input | ||
80 | * author: | ||
81 | * Paul Vixie, 1996. | ||
82 | */ | ||
83 | static const char * | ||
84 | inet_ntop4(src, dst, size) | ||
85 | const u_char *src; | ||
86 | char *dst; | ||
87 | size_t size; | ||
88 | { | ||
89 | static const char fmt[] = "%u.%u.%u.%u"; | ||
90 | char tmp[sizeof "255.255.255.255"]; | ||
91 | int l; | ||
92 | |||
93 | l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]); | ||
94 | if (l <= 0 || l >= size) { | ||
95 | errno = ENOSPC; | ||
96 | return (NULL); | ||
97 | } | ||
98 | strlcpy(dst, tmp, size); | ||
99 | return (dst); | ||
100 | } | ||
101 | |||
102 | /* const char * | ||
103 | * inet_ntop6(src, dst, size) | ||
104 | * convert IPv6 binary address into presentation (printable) format | ||
105 | * author: | ||
106 | * Paul Vixie, 1996. | ||
107 | */ | ||
108 | static const char * | ||
109 | inet_ntop6(src, dst, size) | ||
110 | const u_char *src; | ||
111 | char *dst; | ||
112 | size_t size; | ||
113 | { | ||
114 | /* | ||
115 | * Note that int32_t and int16_t need only be "at least" large enough | ||
116 | * to contain a value of the specified size. On some systems, like | ||
117 | * Crays, there is no such thing as an integer variable with 16 bits. | ||
118 | * Keep this in mind if you think this function should have been coded | ||
119 | * to use pointer overlays. All the world's not a VAX. | ||
120 | */ | ||
121 | char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; | ||
122 | char *tp, *ep; | ||
123 | struct { int base, len; } best, cur; | ||
124 | u_int words[IN6ADDRSZ / INT16SZ]; | ||
125 | int i; | ||
126 | int advance; | ||
127 | |||
128 | /* | ||
129 | * Preprocess: | ||
130 | * Copy the input (bytewise) array into a wordwise array. | ||
131 | * Find the longest run of 0x00's in src[] for :: shorthanding. | ||
132 | */ | ||
133 | memset(words, '\0', sizeof words); | ||
134 | for (i = 0; i < IN6ADDRSZ; i++) | ||
135 | words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3)); | ||
136 | best.base = -1; | ||
137 | cur.base = -1; | ||
138 | for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) { | ||
139 | if (words[i] == 0) { | ||
140 | if (cur.base == -1) | ||
141 | cur.base = i, cur.len = 1; | ||
142 | else | ||
143 | cur.len++; | ||
144 | } else { | ||
145 | if (cur.base != -1) { | ||
146 | if (best.base == -1 || cur.len > best.len) | ||
147 | best = cur; | ||
148 | cur.base = -1; | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | if (cur.base != -1) { | ||
153 | if (best.base == -1 || cur.len > best.len) | ||
154 | best = cur; | ||
155 | } | ||
156 | if (best.base != -1 && best.len < 2) | ||
157 | best.base = -1; | ||
158 | |||
159 | /* | ||
160 | * Format the result. | ||
161 | */ | ||
162 | tp = tmp; | ||
163 | ep = tmp + sizeof(tmp); | ||
164 | for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) { | ||
165 | /* Are we inside the best run of 0x00's? */ | ||
166 | if (best.base != -1 && i >= best.base && | ||
167 | i < (best.base + best.len)) { | ||
168 | if (i == best.base) { | ||
169 | if (tp + 1 >= ep) | ||
170 | return (NULL); | ||
171 | *tp++ = ':'; | ||
172 | } | ||
173 | continue; | ||
174 | } | ||
175 | /* Are we following an initial run of 0x00s or any real hex? */ | ||
176 | if (i != 0) { | ||
177 | if (tp + 1 >= ep) | ||
178 | return (NULL); | ||
179 | *tp++ = ':'; | ||
180 | } | ||
181 | /* Is this address an encapsulated IPv4? */ | ||
182 | if (i == 6 && best.base == 0 && | ||
183 | (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) { | ||
184 | if (!inet_ntop4(src+12, tp, (size_t)(ep - tp))) | ||
185 | return (NULL); | ||
186 | tp += strlen(tp); | ||
187 | break; | ||
188 | } | ||
189 | advance = snprintf(tp, ep - tp, "%x", words[i]); | ||
190 | if (advance <= 0 || advance >= ep - tp) | ||
191 | return (NULL); | ||
192 | tp += advance; | ||
193 | } | ||
194 | /* Was it a trailing run of 0x00's? */ | ||
195 | if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) { | ||
196 | if (tp + 1 >= ep) | ||
197 | return (NULL); | ||
198 | *tp++ = ':'; | ||
199 | } | ||
200 | if (tp + 1 >= ep) | ||
201 | return (NULL); | ||
202 | *tp++ = '\0'; | ||
203 | |||
204 | /* | ||
205 | * Check for overflow, copy, and we're done. | ||
206 | */ | ||
207 | if ((size_t)(tp - tmp) > size) { | ||
208 | errno = ENOSPC; | ||
209 | return (NULL); | ||
210 | } | ||
211 | strlcpy(dst, tmp, size); | ||
212 | return (dst); | ||
213 | } | ||
diff --git a/src/lib/libc/net/inet_pton.c b/src/lib/libc/net/inet_pton.c new file mode 100644 index 0000000000..b04ef05c31 --- /dev/null +++ b/src/lib/libc/net/inet_pton.c | |||
@@ -0,0 +1,222 @@ | |||
1 | /* $OpenBSD: inet_pton.c,v 1.4 2002/02/16 21:27:23 millert Exp $ */ | ||
2 | |||
3 | /* Copyright (c) 1996 by Internet Software Consortium. | ||
4 | * | ||
5 | * Permission to use, copy, modify, and distribute this software for any | ||
6 | * purpose with or without fee is hereby granted, provided that the above | ||
7 | * copyright notice and this permission notice appear in all copies. | ||
8 | * | ||
9 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
10 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
11 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
12 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
13 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
14 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
15 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
16 | * SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #if defined(LIBC_SCCS) && !defined(lint) | ||
20 | #if 0 | ||
21 | static char rcsid[] = "$From: inet_pton.c,v 8.7 1996/08/05 08:31:35 vixie Exp $"; | ||
22 | #else | ||
23 | static char rcsid[] = "$OpenBSD: inet_pton.c,v 1.4 2002/02/16 21:27:23 millert Exp $"; | ||
24 | #endif | ||
25 | #endif /* LIBC_SCCS and not lint */ | ||
26 | |||
27 | #include <sys/param.h> | ||
28 | #include <sys/types.h> | ||
29 | #include <sys/socket.h> | ||
30 | #include <netinet/in.h> | ||
31 | #include <arpa/inet.h> | ||
32 | #include <arpa/nameser.h> | ||
33 | #include <string.h> | ||
34 | #include <errno.h> | ||
35 | |||
36 | /* | ||
37 | * WARNING: Don't even consider trying to compile this on a system where | ||
38 | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. | ||
39 | */ | ||
40 | |||
41 | static int inet_pton4(const char *src, u_char *dst); | ||
42 | static int inet_pton6(const char *src, u_char *dst); | ||
43 | |||
44 | /* int | ||
45 | * inet_pton(af, src, dst) | ||
46 | * convert from presentation format (which usually means ASCII printable) | ||
47 | * to network format (which is usually some kind of binary format). | ||
48 | * return: | ||
49 | * 1 if the address was valid for the specified address family | ||
50 | * 0 if the address wasn't valid (`dst' is untouched in this case) | ||
51 | * -1 if some other error occurred (`dst' is untouched in this case, too) | ||
52 | * author: | ||
53 | * Paul Vixie, 1996. | ||
54 | */ | ||
55 | int | ||
56 | inet_pton(af, src, dst) | ||
57 | int af; | ||
58 | const char *src; | ||
59 | void *dst; | ||
60 | { | ||
61 | switch (af) { | ||
62 | case AF_INET: | ||
63 | return (inet_pton4(src, dst)); | ||
64 | case AF_INET6: | ||
65 | return (inet_pton6(src, dst)); | ||
66 | default: | ||
67 | errno = EAFNOSUPPORT; | ||
68 | return (-1); | ||
69 | } | ||
70 | /* NOTREACHED */ | ||
71 | } | ||
72 | |||
73 | /* int | ||
74 | * inet_pton4(src, dst) | ||
75 | * like inet_aton() but without all the hexadecimal and shorthand. | ||
76 | * return: | ||
77 | * 1 if `src' is a valid dotted quad, else 0. | ||
78 | * notice: | ||
79 | * does not touch `dst' unless it's returning 1. | ||
80 | * author: | ||
81 | * Paul Vixie, 1996. | ||
82 | */ | ||
83 | static int | ||
84 | inet_pton4(src, dst) | ||
85 | const char *src; | ||
86 | u_char *dst; | ||
87 | { | ||
88 | static const char digits[] = "0123456789"; | ||
89 | int saw_digit, octets, ch; | ||
90 | u_char tmp[INADDRSZ], *tp; | ||
91 | |||
92 | saw_digit = 0; | ||
93 | octets = 0; | ||
94 | *(tp = tmp) = 0; | ||
95 | while ((ch = *src++) != '\0') { | ||
96 | const char *pch; | ||
97 | |||
98 | if ((pch = strchr(digits, ch)) != NULL) { | ||
99 | u_int new = *tp * 10 + (pch - digits); | ||
100 | |||
101 | if (new > 255) | ||
102 | return (0); | ||
103 | if (! saw_digit) { | ||
104 | if (++octets > 4) | ||
105 | return (0); | ||
106 | saw_digit = 1; | ||
107 | } | ||
108 | *tp = new; | ||
109 | } else if (ch == '.' && saw_digit) { | ||
110 | if (octets == 4) | ||
111 | return (0); | ||
112 | *++tp = 0; | ||
113 | saw_digit = 0; | ||
114 | } else | ||
115 | return (0); | ||
116 | } | ||
117 | if (octets < 4) | ||
118 | return (0); | ||
119 | |||
120 | memcpy(dst, tmp, INADDRSZ); | ||
121 | return (1); | ||
122 | } | ||
123 | |||
124 | /* int | ||
125 | * inet_pton6(src, dst) | ||
126 | * convert presentation level address to network order binary form. | ||
127 | * return: | ||
128 | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. | ||
129 | * notice: | ||
130 | * (1) does not touch `dst' unless it's returning 1. | ||
131 | * (2) :: in a full address is silently ignored. | ||
132 | * credit: | ||
133 | * inspired by Mark Andrews. | ||
134 | * author: | ||
135 | * Paul Vixie, 1996. | ||
136 | */ | ||
137 | static int | ||
138 | inet_pton6(src, dst) | ||
139 | const char *src; | ||
140 | u_char *dst; | ||
141 | { | ||
142 | static const char xdigits_l[] = "0123456789abcdef", | ||
143 | xdigits_u[] = "0123456789ABCDEF"; | ||
144 | u_char tmp[IN6ADDRSZ], *tp, *endp, *colonp; | ||
145 | const char *xdigits, *curtok; | ||
146 | int ch, saw_xdigit; | ||
147 | u_int val; | ||
148 | |||
149 | memset((tp = tmp), '\0', IN6ADDRSZ); | ||
150 | endp = tp + IN6ADDRSZ; | ||
151 | colonp = NULL; | ||
152 | /* Leading :: requires some special handling. */ | ||
153 | if (*src == ':') | ||
154 | if (*++src != ':') | ||
155 | return (0); | ||
156 | curtok = src; | ||
157 | saw_xdigit = 0; | ||
158 | val = 0; | ||
159 | while ((ch = *src++) != '\0') { | ||
160 | const char *pch; | ||
161 | |||
162 | if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) | ||
163 | pch = strchr((xdigits = xdigits_u), ch); | ||
164 | if (pch != NULL) { | ||
165 | val <<= 4; | ||
166 | val |= (pch - xdigits); | ||
167 | if (val > 0xffff) | ||
168 | return (0); | ||
169 | saw_xdigit = 1; | ||
170 | continue; | ||
171 | } | ||
172 | if (ch == ':') { | ||
173 | curtok = src; | ||
174 | if (!saw_xdigit) { | ||
175 | if (colonp) | ||
176 | return (0); | ||
177 | colonp = tp; | ||
178 | continue; | ||
179 | } else if (*src == '\0') { | ||
180 | return (0); | ||
181 | } | ||
182 | if (tp + INT16SZ > endp) | ||
183 | return (0); | ||
184 | *tp++ = (u_char) (val >> 8) & 0xff; | ||
185 | *tp++ = (u_char) val & 0xff; | ||
186 | saw_xdigit = 0; | ||
187 | val = 0; | ||
188 | continue; | ||
189 | } | ||
190 | if (ch == '.' && ((tp + INADDRSZ) <= endp) && | ||
191 | inet_pton4(curtok, tp) > 0) { | ||
192 | tp += INADDRSZ; | ||
193 | saw_xdigit = 0; | ||
194 | break; /* '\0' was seen by inet_pton4(). */ | ||
195 | } | ||
196 | return (0); | ||
197 | } | ||
198 | if (saw_xdigit) { | ||
199 | if (tp + INT16SZ > endp) | ||
200 | return (0); | ||
201 | *tp++ = (u_char) (val >> 8) & 0xff; | ||
202 | *tp++ = (u_char) val & 0xff; | ||
203 | } | ||
204 | if (colonp != NULL) { | ||
205 | /* | ||
206 | * Since some memmove()'s erroneously fail to handle | ||
207 | * overlapping regions, we'll do the shift by hand. | ||
208 | */ | ||
209 | const int n = tp - colonp; | ||
210 | int i; | ||
211 | |||
212 | for (i = 1; i <= n; i++) { | ||
213 | endp[- i] = colonp[n - i]; | ||
214 | colonp[n - i] = 0; | ||
215 | } | ||
216 | tp = endp; | ||
217 | } | ||
218 | if (tp != endp) | ||
219 | return (0); | ||
220 | memcpy(dst, tmp, IN6ADDRSZ); | ||
221 | return (1); | ||
222 | } | ||
diff --git a/src/lib/libc/net/ip6opt.c b/src/lib/libc/net/ip6opt.c new file mode 100644 index 0000000000..cbd49f0c22 --- /dev/null +++ b/src/lib/libc/net/ip6opt.c | |||
@@ -0,0 +1,388 @@ | |||
1 | /* $OpenBSD: ip6opt.c,v 1.1 1999/12/11 08:09:11 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
5 | * 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. Neither the name of the project nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <sys/param.h> | ||
33 | #include <sys/types.h> | ||
34 | #include <sys/socket.h> | ||
35 | |||
36 | #include <netinet/in.h> | ||
37 | #include <netinet/ip6.h> | ||
38 | |||
39 | #include <string.h> | ||
40 | #include <stdio.h> | ||
41 | |||
42 | static int ip6optlen(u_int8_t *opt, u_int8_t *lim); | ||
43 | static void inet6_insert_padopt(u_char *p, int len); | ||
44 | |||
45 | /* | ||
46 | * This function returns the number of bytes required to hold an option | ||
47 | * when it is stored as ancillary data, including the cmsghdr structure | ||
48 | * at the beginning, and any padding at the end (to make its size a | ||
49 | * multiple of 8 bytes). The argument is the size of the structure | ||
50 | * defining the option, which must include any pad bytes at the | ||
51 | * beginning (the value y in the alignment term "xn + y"), the type | ||
52 | * byte, the length byte, and the option data. | ||
53 | */ | ||
54 | int | ||
55 | inet6_option_space(nbytes) | ||
56 | int nbytes; | ||
57 | { | ||
58 | nbytes += 2; /* we need space for nxt-hdr and length fields */ | ||
59 | return(CMSG_SPACE((nbytes + 7) & ~7)); | ||
60 | } | ||
61 | |||
62 | /* | ||
63 | * This function is called once per ancillary data object that will | ||
64 | * contain either Hop-by-Hop or Destination options. It returns 0 on | ||
65 | * success or -1 on an error. | ||
66 | */ | ||
67 | int | ||
68 | inet6_option_init(bp, cmsgp, type) | ||
69 | void *bp; | ||
70 | struct cmsghdr **cmsgp; | ||
71 | int type; | ||
72 | { | ||
73 | register struct cmsghdr *ch = (struct cmsghdr *)bp; | ||
74 | |||
75 | /* argument validation */ | ||
76 | if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS) | ||
77 | return(-1); | ||
78 | |||
79 | ch->cmsg_level = IPPROTO_IPV6; | ||
80 | ch->cmsg_type = type; | ||
81 | ch->cmsg_len = CMSG_LEN(0); | ||
82 | |||
83 | *cmsgp = ch; | ||
84 | return(0); | ||
85 | } | ||
86 | |||
87 | /* | ||
88 | * This function appends a Hop-by-Hop option or a Destination option | ||
89 | * into an ancillary data object that has been initialized by | ||
90 | * inet6_option_init(). This function returns 0 if it succeeds or -1 on | ||
91 | * an error. | ||
92 | * multx is the value x in the alignment term "xn + y" described | ||
93 | * earlier. It must have a value of 1, 2, 4, or 8. | ||
94 | * plusy is the value y in the alignment term "xn + y" described | ||
95 | * earlier. It must have a value between 0 and 7, inclusive. | ||
96 | */ | ||
97 | int | ||
98 | inet6_option_append(cmsg, typep, multx, plusy) | ||
99 | struct cmsghdr *cmsg; | ||
100 | const u_int8_t *typep; | ||
101 | int multx; | ||
102 | int plusy; | ||
103 | { | ||
104 | int padlen, optlen, off; | ||
105 | register u_char *bp = (u_char *)cmsg + cmsg->cmsg_len; | ||
106 | struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
107 | |||
108 | /* argument validation */ | ||
109 | if (multx != 1 && multx != 2 && multx != 4 && multx != 8) | ||
110 | return(-1); | ||
111 | if (plusy < 0 || plusy > 7) | ||
112 | return(-1); | ||
113 | if (typep[0] > 255) | ||
114 | return(-1); | ||
115 | |||
116 | /* | ||
117 | * If this is the first option, allocate space for the | ||
118 | * first 2 bytes(for next header and length fields) of | ||
119 | * the option header. | ||
120 | */ | ||
121 | if (bp == (u_char *)eh) { | ||
122 | bp += 2; | ||
123 | cmsg->cmsg_len += 2; | ||
124 | } | ||
125 | |||
126 | /* calculate pad length before the option. */ | ||
127 | off = bp - (u_char *)eh; | ||
128 | padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - | ||
129 | (off % multx); | ||
130 | padlen += plusy; | ||
131 | padlen %= multx; /* keep the pad as short as possible */ | ||
132 | /* insert padding */ | ||
133 | inet6_insert_padopt(bp, padlen); | ||
134 | cmsg->cmsg_len += padlen; | ||
135 | bp += padlen; | ||
136 | |||
137 | /* copy the option */ | ||
138 | if (typep[0] == IP6OPT_PAD1) | ||
139 | optlen = 1; | ||
140 | else | ||
141 | optlen = typep[1] + 2; | ||
142 | memcpy(bp, typep, optlen); | ||
143 | bp += optlen; | ||
144 | cmsg->cmsg_len += optlen; | ||
145 | |||
146 | /* calculate pad length after the option and insert the padding */ | ||
147 | off = bp - (u_char *)eh; | ||
148 | padlen = ((off + 7) & ~7) - off; | ||
149 | inet6_insert_padopt(bp, padlen); | ||
150 | bp += padlen; | ||
151 | cmsg->cmsg_len += padlen; | ||
152 | |||
153 | /* update the length field of the ip6 option header */ | ||
154 | eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; | ||
155 | |||
156 | return(0); | ||
157 | } | ||
158 | |||
159 | /* | ||
160 | * This function appends a Hop-by-Hop option or a Destination option | ||
161 | * into an ancillary data object that has been initialized by | ||
162 | * inet6_option_init(). This function returns a pointer to the 8-bit | ||
163 | * option type field that starts the option on success, or NULL on an | ||
164 | * error. | ||
165 | * The difference between this function and inet6_option_append() is | ||
166 | * that the latter copies the contents of a previously built option into | ||
167 | * the ancillary data object while the current function returns a | ||
168 | * pointer to the space in the data object where the option's TLV must | ||
169 | * then be built by the caller. | ||
170 | * | ||
171 | */ | ||
172 | u_int8_t * | ||
173 | inet6_option_alloc(cmsg, datalen, multx, plusy) | ||
174 | struct cmsghdr *cmsg; | ||
175 | int datalen; | ||
176 | int multx; | ||
177 | int plusy; | ||
178 | { | ||
179 | int padlen, off; | ||
180 | register u_int8_t *bp = (u_char *)cmsg + cmsg->cmsg_len; | ||
181 | u_int8_t *retval; | ||
182 | struct ip6_ext *eh = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
183 | |||
184 | /* argument validation */ | ||
185 | if (multx != 1 && multx != 2 && multx != 4 && multx != 8) | ||
186 | return(NULL); | ||
187 | if (plusy < 0 || plusy > 7) | ||
188 | return(NULL); | ||
189 | |||
190 | /* | ||
191 | * If this is the first option, allocate space for the | ||
192 | * first 2 bytes(for next header and length fields) of | ||
193 | * the option header. | ||
194 | */ | ||
195 | if (bp == (u_char *)eh) { | ||
196 | bp += 2; | ||
197 | cmsg->cmsg_len += 2; | ||
198 | } | ||
199 | |||
200 | /* calculate pad length before the option. */ | ||
201 | off = bp - (u_char *)eh; | ||
202 | padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - | ||
203 | (off % multx); | ||
204 | padlen += plusy; | ||
205 | padlen %= multx; /* keep the pad as short as possible */ | ||
206 | /* insert padding */ | ||
207 | inet6_insert_padopt(bp, padlen); | ||
208 | cmsg->cmsg_len += padlen; | ||
209 | bp += padlen; | ||
210 | |||
211 | /* keep space to store specified length of data */ | ||
212 | retval = bp; | ||
213 | bp += datalen; | ||
214 | cmsg->cmsg_len += datalen; | ||
215 | |||
216 | /* calculate pad length after the option and insert the padding */ | ||
217 | off = bp - (u_char *)eh; | ||
218 | padlen = ((off + 7) & ~7) - off; | ||
219 | inet6_insert_padopt(bp, padlen); | ||
220 | bp += padlen; | ||
221 | cmsg->cmsg_len += padlen; | ||
222 | |||
223 | /* update the length field of the ip6 option header */ | ||
224 | eh->ip6e_len = ((bp - (u_char *)eh) >> 3) - 1; | ||
225 | |||
226 | return(retval); | ||
227 | } | ||
228 | |||
229 | /* | ||
230 | * This function processes the next Hop-by-Hop option or Destination | ||
231 | * option in an ancillary data object. If another option remains to be | ||
232 | * processed, the return value of the function is 0 and *tptrp points to | ||
233 | * the 8-bit option type field (which is followed by the 8-bit option | ||
234 | * data length, followed by the option data). If no more options remain | ||
235 | * to be processed, the return value is -1 and *tptrp is NULL. If an | ||
236 | * error occurs, the return value is -1 and *tptrp is not NULL. | ||
237 | * (RFC 2292, 6.3.5) | ||
238 | */ | ||
239 | int | ||
240 | inet6_option_next(cmsg, tptrp) | ||
241 | const struct cmsghdr *cmsg; | ||
242 | u_int8_t **tptrp; | ||
243 | { | ||
244 | struct ip6_ext *ip6e; | ||
245 | int hdrlen, optlen; | ||
246 | u_int8_t *lim; | ||
247 | |||
248 | if (cmsg->cmsg_level != IPPROTO_IPV6 || | ||
249 | (cmsg->cmsg_type != IPV6_HOPOPTS && | ||
250 | cmsg->cmsg_type != IPV6_DSTOPTS)) | ||
251 | return(-1); | ||
252 | |||
253 | /* message length validation */ | ||
254 | if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) | ||
255 | return(-1); | ||
256 | ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
257 | hdrlen = (ip6e->ip6e_len + 1) << 3; | ||
258 | if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) | ||
259 | return(-1); | ||
260 | |||
261 | /* | ||
262 | * If the caller does not specify the starting point, | ||
263 | * simply return the 1st option. | ||
264 | * Otherwise, search the option list for the next option. | ||
265 | */ | ||
266 | lim = (u_int8_t *)ip6e + hdrlen; | ||
267 | if (*tptrp == NULL) | ||
268 | *tptrp = (u_int8_t *)(ip6e + 1); | ||
269 | else { | ||
270 | if ((optlen = ip6optlen(*tptrp, lim)) == 0) | ||
271 | return(-1); | ||
272 | |||
273 | *tptrp = *tptrp + optlen; | ||
274 | } | ||
275 | if (*tptrp >= lim) { /* there is no option */ | ||
276 | *tptrp = NULL; | ||
277 | return(-1); | ||
278 | } | ||
279 | /* | ||
280 | * Finally, checks if the next option is safely stored in the | ||
281 | * cmsg data. | ||
282 | */ | ||
283 | if (ip6optlen(*tptrp, lim) == 0) | ||
284 | return(-1); | ||
285 | else | ||
286 | return(0); | ||
287 | } | ||
288 | |||
289 | /* | ||
290 | * This function is similar to the inet6_option_next() function, | ||
291 | * except this function lets the caller specify the option type to be | ||
292 | * searched for, instead of always returning the next option in the | ||
293 | * ancillary data object. | ||
294 | * Note: RFC 2292 says the type of tptrp is u_int8_t *, but we think | ||
295 | * it's a typo. The variable should be type of u_int8_t **. | ||
296 | */ | ||
297 | int | ||
298 | inet6_option_find(cmsg, tptrp, type) | ||
299 | const struct cmsghdr *cmsg; | ||
300 | u_int8_t **tptrp; | ||
301 | int type; | ||
302 | { | ||
303 | struct ip6_ext *ip6e; | ||
304 | int hdrlen, optlen; | ||
305 | u_int8_t *optp, *lim; | ||
306 | |||
307 | if (cmsg->cmsg_level != IPPROTO_IPV6 || | ||
308 | (cmsg->cmsg_type != IPV6_HOPOPTS && | ||
309 | cmsg->cmsg_type != IPV6_DSTOPTS)) | ||
310 | return(-1); | ||
311 | |||
312 | /* message length validation */ | ||
313 | if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) | ||
314 | return(-1); | ||
315 | ip6e = (struct ip6_ext *)CMSG_DATA(cmsg); | ||
316 | hdrlen = (ip6e->ip6e_len + 1) << 3; | ||
317 | if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) | ||
318 | return(-1); | ||
319 | |||
320 | /* | ||
321 | * If the caller does not specify the starting point, | ||
322 | * search from the beginning of the option list. | ||
323 | * Otherwise, search from *the next option* of the specified point. | ||
324 | */ | ||
325 | lim = (u_int8_t *)ip6e + hdrlen; | ||
326 | if (*tptrp == NULL) | ||
327 | *tptrp = (u_int8_t *)(ip6e + 1); | ||
328 | else { | ||
329 | if ((optlen = ip6optlen(*tptrp, lim)) == 0) | ||
330 | return(-1); | ||
331 | |||
332 | *tptrp = *tptrp + optlen; | ||
333 | } | ||
334 | for (optp = *tptrp; optp < lim; optp += optlen) { | ||
335 | if (*optp == type) { | ||
336 | *tptrp = optp; | ||
337 | return(0); | ||
338 | } | ||
339 | if ((optlen = ip6optlen(optp, lim)) == 0) | ||
340 | return(-1); | ||
341 | } | ||
342 | |||
343 | /* search failed */ | ||
344 | *tptrp = NULL; | ||
345 | return(-1); | ||
346 | } | ||
347 | |||
348 | /* | ||
349 | * Calculate the length of a given IPv6 option. Also checks | ||
350 | * if the option is safely stored in user's buffer according to the | ||
351 | * calculated length and the limitation of the buffer. | ||
352 | */ | ||
353 | static int | ||
354 | ip6optlen(opt, lim) | ||
355 | u_int8_t *opt, *lim; | ||
356 | { | ||
357 | int optlen; | ||
358 | |||
359 | if (*opt == IP6OPT_PAD1) | ||
360 | optlen = 1; | ||
361 | else { | ||
362 | /* is there enough space to store type and len? */ | ||
363 | if (opt + 2 > lim) | ||
364 | return(0); | ||
365 | optlen = *(opt + 1) + 2; | ||
366 | } | ||
367 | if (opt + optlen <= lim) | ||
368 | return(optlen); | ||
369 | |||
370 | return(0); | ||
371 | } | ||
372 | |||
373 | static void | ||
374 | inet6_insert_padopt(u_char *p, int len) | ||
375 | { | ||
376 | switch(len) { | ||
377 | case 0: | ||
378 | return; | ||
379 | case 1: | ||
380 | p[0] = IP6OPT_PAD1; | ||
381 | return; | ||
382 | default: | ||
383 | p[0] = IP6OPT_PADN; | ||
384 | p[1] = len - 2; | ||
385 | memset(&p[2], 0, len - 2); | ||
386 | return; | ||
387 | } | ||
388 | } | ||
diff --git a/src/lib/libc/net/ipx.3 b/src/lib/libc/net/ipx.3 new file mode 100644 index 0000000000..64f6028db0 --- /dev/null +++ b/src/lib/libc/net/ipx.3 | |||
@@ -0,0 +1,123 @@ | |||
1 | .\" $OpenBSD: ipx.3,v 1.10 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1986, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt IPX 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm ipx_addr , | ||
35 | .Nm ipx_ntoa | ||
36 | .Nd IPX address conversion routines | ||
37 | .Sh SYNOPSIS | ||
38 | .Fd #include <sys/types.h> | ||
39 | .Fd #include <netipx/ipx.h> | ||
40 | .Ft struct ipx_addr | ||
41 | .Fn ipx_addr "const char *cp" | ||
42 | .Ft char * | ||
43 | .Fn ipx_ntoa "struct ipx_addr ipx" | ||
44 | .Sh DESCRIPTION | ||
45 | The routine | ||
46 | .Fn ipx_addr | ||
47 | interprets character strings representing | ||
48 | .Tn IPX | ||
49 | addresses, returning binary information suitable | ||
50 | for use in system calls. | ||
51 | The routine | ||
52 | .Fn ipx_ntoa | ||
53 | takes | ||
54 | .Tn IPX | ||
55 | addresses and returns | ||
56 | .Tn ASCII | ||
57 | strings representing the address in a | ||
58 | notation in common use: | ||
59 | .Bd -filled -offset indent | ||
60 | <network number>.<host number>.<port number> | ||
61 | .Ed | ||
62 | .Pp | ||
63 | Trailing zero fields are suppressed, and each number is printed in hexadecimal, | ||
64 | in a format suitable for input to | ||
65 | .Fn ipx_addr . | ||
66 | Any fields lacking super-decimal digits will have a | ||
67 | trailing | ||
68 | .Sq H | ||
69 | appended. | ||
70 | .Pp | ||
71 | An effort has been made to ensure that | ||
72 | .Fn ipx_addr | ||
73 | be compatible with most formats in common use. | ||
74 | It will first separate an address into 1 to 3 fields using a single delimiter | ||
75 | chosen from | ||
76 | period | ||
77 | .Pq Ql \&. , | ||
78 | colon | ||
79 | .Pq Ql \&: , | ||
80 | or pound-sign | ||
81 | .Pq Ql # . | ||
82 | Each field is then examined for byte separators (colon or period). | ||
83 | If there are byte separators, each subfield separated is taken to be | ||
84 | a small hexadecimal number, and the entirety is taken as a network-byte-ordered | ||
85 | quantity to be zero extended in the high-network-order bytes. | ||
86 | Next, the field is inspected for hyphens, in which case | ||
87 | the field is assumed to be a number in decimal notation | ||
88 | with hyphens separating the millenia. | ||
89 | Next, the field is assumed to be a number: | ||
90 | It is interpreted | ||
91 | as hexadecimal if there is a leading | ||
92 | .Ql 0x | ||
93 | (as in C), | ||
94 | a trailing | ||
95 | .Sq H | ||
96 | (as in Mesa), or there are any super-decimal digits present. | ||
97 | It is interpreted as octal if there is a leading | ||
98 | .Ql 0 | ||
99 | and there are no super-octal digits. | ||
100 | Otherwise, it is converted as a decimal number. | ||
101 | .Sh RETURN VALUES | ||
102 | None. | ||
103 | (See | ||
104 | .Sx BUGS . ) | ||
105 | .Sh SEE ALSO | ||
106 | .Xr ns 4 , | ||
107 | .Xr hosts 5 , | ||
108 | .Xr networks 5 | ||
109 | .Sh HISTORY | ||
110 | The precursor | ||
111 | .Fn ns_addr | ||
112 | and | ||
113 | .Fn ns_ntoa | ||
114 | functions appeared in | ||
115 | .Bx 4.3 . | ||
116 | .Sh BUGS | ||
117 | The string returned by | ||
118 | .Fn ipx_ntoa | ||
119 | resides in a static memory area. | ||
120 | The function | ||
121 | .Fn ipx_addr | ||
122 | should diagnose improperly formed input, and there should be an unambiguous | ||
123 | way to recognize this. | ||
diff --git a/src/lib/libc/net/ipx_addr.c b/src/lib/libc/net/ipx_addr.c new file mode 100644 index 0000000000..371ac7c917 --- /dev/null +++ b/src/lib/libc/net/ipx_addr.c | |||
@@ -0,0 +1,224 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1986, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * J.Q. Johnson. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | * | ||
32 | * from @(#)ipx_addr.c | ||
33 | */ | ||
34 | |||
35 | #if defined(LIBC_SCCS) && !defined(lint) | ||
36 | static char rcsid[] = "$OpenBSD: ipx_addr.c,v 1.6 2003/06/02 20:18:35 millert Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <sys/param.h> | ||
40 | #include <netipx/ipx.h> | ||
41 | #include <stdio.h> | ||
42 | #include <string.h> | ||
43 | |||
44 | static struct ipx_addr addr, zero_addr; | ||
45 | |||
46 | static void Field(), cvtbase(); | ||
47 | |||
48 | struct ipx_addr | ||
49 | ipx_addr(name) | ||
50 | const char *name; | ||
51 | { | ||
52 | char separator; | ||
53 | char *hostname, *socketname, *cp; | ||
54 | char buf[50]; | ||
55 | |||
56 | strlcpy(buf, name, sizeof(buf)); | ||
57 | |||
58 | /* | ||
59 | * First, figure out what he intends as a field separtor. | ||
60 | * Despite the way this routine is written, the prefered | ||
61 | * form 2-272.AA001234H.01777, i.e. XDE standard. | ||
62 | * Great efforts are made to insure backward compatibility. | ||
63 | */ | ||
64 | if ((hostname = strchr(buf, '#'))) | ||
65 | separator = '#'; | ||
66 | else { | ||
67 | hostname = strchr(buf, '.'); | ||
68 | if ((cp = strchr(buf, ':')) && | ||
69 | ((hostname && cp < hostname) || (hostname == 0))) { | ||
70 | hostname = cp; | ||
71 | separator = ':'; | ||
72 | } else | ||
73 | separator = '.'; | ||
74 | } | ||
75 | if (hostname) | ||
76 | *hostname++ = 0; | ||
77 | |||
78 | addr = zero_addr; | ||
79 | Field(buf, addr.ipx_net.c_net, 4); | ||
80 | if (hostname == 0) | ||
81 | return (addr); /* No separator means net only */ | ||
82 | |||
83 | socketname = strchr(hostname, separator); | ||
84 | if (socketname) { | ||
85 | *socketname++ = 0; | ||
86 | Field(socketname, (u_char *)&addr.ipx_port, 2); | ||
87 | } | ||
88 | |||
89 | Field(hostname, addr.ipx_host.c_host, 6); | ||
90 | |||
91 | return (addr); | ||
92 | } | ||
93 | |||
94 | static void | ||
95 | Field(buf, out, len) | ||
96 | char *buf; | ||
97 | u_char *out; | ||
98 | int len; | ||
99 | { | ||
100 | register char *bp = buf; | ||
101 | int i, ibase, base16 = 0, base10 = 0, clen = 0; | ||
102 | int hb[6], *hp; | ||
103 | char *fmt; | ||
104 | |||
105 | /* | ||
106 | * first try 2-273#2-852-151-014#socket | ||
107 | */ | ||
108 | if ((*buf != '-') && | ||
109 | (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", | ||
110 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { | ||
111 | cvtbase(1000L, 256, hb, i, out, len); | ||
112 | return; | ||
113 | } | ||
114 | /* | ||
115 | * try form 8E1#0.0.AA.0.5E.E6#socket | ||
116 | */ | ||
117 | if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", | ||
118 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
119 | cvtbase(256L, 256, hb, i, out, len); | ||
120 | return; | ||
121 | } | ||
122 | /* | ||
123 | * try form 8E1#0:0:AA:0:5E:E6#socket | ||
124 | */ | ||
125 | if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", | ||
126 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
127 | cvtbase(256L, 256, hb, i, out, len); | ||
128 | return; | ||
129 | } | ||
130 | /* | ||
131 | * This is REALLY stretching it but there was a | ||
132 | * comma notation separting shorts -- definitely non standard | ||
133 | */ | ||
134 | if (1 < (i = sscanf(buf,"%x,%x,%x", | ||
135 | &hb[0], &hb[1], &hb[2]))) { | ||
136 | hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); | ||
137 | hb[2] = htons(hb[2]); | ||
138 | cvtbase(65536L, 256, hb, i, out, len); | ||
139 | return; | ||
140 | } | ||
141 | |||
142 | /* Need to decide if base 10, 16 or 8 */ | ||
143 | while (*bp) switch (*bp++) { | ||
144 | |||
145 | case '0': case '1': case '2': case '3': case '4': case '5': | ||
146 | case '6': case '7': case '-': | ||
147 | break; | ||
148 | |||
149 | case '8': case '9': | ||
150 | base10 = 1; | ||
151 | break; | ||
152 | |||
153 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | ||
154 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | ||
155 | base16 = 1; | ||
156 | break; | ||
157 | |||
158 | case 'x': case 'X': | ||
159 | *--bp = '0'; | ||
160 | base16 = 1; | ||
161 | break; | ||
162 | |||
163 | case 'h': case 'H': | ||
164 | base16 = 1; | ||
165 | /* fall into */ | ||
166 | |||
167 | default: | ||
168 | *--bp = 0; /* Ends Loop */ | ||
169 | } | ||
170 | if (base16) { | ||
171 | fmt = "%3x"; | ||
172 | ibase = 4096; | ||
173 | } else if (base10 == 0 && *buf == '0') { | ||
174 | fmt = "%3o"; | ||
175 | ibase = 512; | ||
176 | } else { | ||
177 | fmt = "%3d"; | ||
178 | ibase = 1000; | ||
179 | } | ||
180 | |||
181 | for (bp = buf; *bp++; ) clen++; | ||
182 | if (clen == 0) clen++; | ||
183 | if (clen > 18) clen = 18; | ||
184 | i = ((clen - 1) / 3) + 1; | ||
185 | bp = clen + buf - 3; | ||
186 | hp = hb + i - 1; | ||
187 | |||
188 | while (hp > hb) { | ||
189 | (void)sscanf(bp, fmt, hp); | ||
190 | bp[0] = 0; | ||
191 | hp--; | ||
192 | bp -= 3; | ||
193 | } | ||
194 | (void)sscanf(buf, fmt, hp); | ||
195 | cvtbase((long)ibase, 256, hb, i, out, len); | ||
196 | } | ||
197 | |||
198 | static void | ||
199 | cvtbase(oldbase,newbase,input,inlen,result,reslen) | ||
200 | long oldbase; | ||
201 | int newbase; | ||
202 | int input[]; | ||
203 | int inlen; | ||
204 | unsigned char result[]; | ||
205 | int reslen; | ||
206 | { | ||
207 | int d, e; | ||
208 | long sum; | ||
209 | |||
210 | e = 1; | ||
211 | while (e > 0 && reslen > 0) { | ||
212 | d = 0; e = 0; sum = 0; | ||
213 | /* long division: input=input/newbase */ | ||
214 | while (d < inlen) { | ||
215 | sum = sum*oldbase + (long) input[d]; | ||
216 | e += (sum > 0); | ||
217 | input[d++] = sum / newbase; | ||
218 | sum %= newbase; | ||
219 | } | ||
220 | result[--reslen] = sum; /* accumulate remainder */ | ||
221 | } | ||
222 | for (d=0; d < reslen; d++) | ||
223 | result[d] = 0; | ||
224 | } | ||
diff --git a/src/lib/libc/net/ipx_ntoa.c b/src/lib/libc/net/ipx_ntoa.c new file mode 100644 index 0000000000..64ba146b31 --- /dev/null +++ b/src/lib/libc/net/ipx_ntoa.c | |||
@@ -0,0 +1,49 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1986, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: ipx_ntoa.c,v 1.5 2003/09/26 06:19:25 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/param.h> | ||
35 | #include <netipx/ipx.h> | ||
36 | #include <stdio.h> | ||
37 | |||
38 | char * | ||
39 | ipx_ntoa(struct ipx_addr addr) | ||
40 | { | ||
41 | static char obuf[] = "xxxxxxxxH.xx:xx:xx:xx:xx:xx.uuuuu"; | ||
42 | |||
43 | snprintf(obuf, sizeof obuf, "%8xH.%02x:%02x:%02x:%02x:%02x:%02x.%u", | ||
44 | ntohl(addr.ipx_net.l_net), addr.ipx_host.c_host[0], | ||
45 | addr.ipx_host.c_host[1], addr.ipx_host.c_host[2], | ||
46 | addr.ipx_host.c_host[3], addr.ipx_host.c_host[4], | ||
47 | addr.ipx_host.c_host[5], ntohs(addr.ipx_port)); | ||
48 | return (obuf); | ||
49 | } | ||
diff --git a/src/lib/libc/net/iso_addr.3 b/src/lib/libc/net/iso_addr.3 new file mode 100644 index 0000000000..896f769482 --- /dev/null +++ b/src/lib/libc/net/iso_addr.3 | |||
@@ -0,0 +1,107 @@ | |||
1 | .\" $OpenBSD: iso_addr.3,v 1.5 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt ISO_ADDR 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm iso_addr , | ||
35 | .Nm iso_ntoa | ||
36 | .Nd "network address conversion routines for Open System Interconnection" | ||
37 | .Sh SYNOPSIS | ||
38 | .Fd #include <sys/types.h> | ||
39 | .Fd #include <netiso/iso.h> | ||
40 | .Ft struct iso_addr * | ||
41 | .Fn iso_addr "char *cp" | ||
42 | .Ft char * | ||
43 | .Fn iso_ntoa "struct iso_addr *isoa" | ||
44 | .Sh DESCRIPTION | ||
45 | The routine | ||
46 | .Fn iso_addr | ||
47 | interprets character strings representing | ||
48 | .Tn OSI | ||
49 | addresses, returning binary information suitable | ||
50 | for use in system calls. | ||
51 | The routine | ||
52 | .Fn iso_ntoa | ||
53 | takes | ||
54 | .Tn OSI | ||
55 | addresses and returns | ||
56 | .Tn ASCII | ||
57 | strings representing NSAPs (network service | ||
58 | access points) in a | ||
59 | notation inverse to that accepted by | ||
60 | .Fn iso_addr . | ||
61 | .Pp | ||
62 | Unfortunately, no universal standard exists for representing | ||
63 | .Tn OSI | ||
64 | network addresses. | ||
65 | .Pp | ||
66 | The format employed by | ||
67 | .Fn iso_addr | ||
68 | is a sequence of hexadecimal | ||
69 | .Dq digits | ||
70 | (optionally separated by periods), | ||
71 | of the form: | ||
72 | .Bd -filled -offset indent | ||
73 | <hex digits>.<hex digits>.<hex digits> | ||
74 | .Ed | ||
75 | .Pp | ||
76 | Each pair of hexadecimal digits represents a byte | ||
77 | with the leading digit indicating the higher-ordered bits. | ||
78 | A period following an even number of bytes has no | ||
79 | effect (but may be used to increase legibility). | ||
80 | A period following an odd number of bytes has the | ||
81 | effect of causing the byte of address being translated | ||
82 | to have its higher order bits filled with zeros. | ||
83 | .Sh RETURN VALUES | ||
84 | .Fn iso_ntoa | ||
85 | always returns a null terminated string. | ||
86 | .Fn iso_addr | ||
87 | always returns a pointer to a | ||
88 | .Li struct iso_addr . | ||
89 | (See | ||
90 | .Sx BUGS . ) | ||
91 | .Sh SEE ALSO | ||
92 | .Xr iso 4 | ||
93 | .Sh HISTORY | ||
94 | The | ||
95 | .Fn iso_addr | ||
96 | and | ||
97 | .Fn iso_ntoa | ||
98 | functions appeared in | ||
99 | .Bx 4.3 Reno . | ||
100 | .Sh BUGS | ||
101 | The returned values | ||
102 | reside in a static memory area. | ||
103 | .Pp | ||
104 | The function | ||
105 | .Fn iso_addr | ||
106 | should diagnose improperly formed input, and there should be an unambiguous | ||
107 | way to recognize this. | ||
diff --git a/src/lib/libc/net/iso_addr.c b/src/lib/libc/net/iso_addr.c new file mode 100644 index 0000000000..f0f8444325 --- /dev/null +++ b/src/lib/libc/net/iso_addr.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1989, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: iso_addr.c,v 1.3 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <netiso/iso.h> | ||
36 | #include <string.h> | ||
37 | |||
38 | /* States*/ | ||
39 | #define VIRGIN 0 | ||
40 | #define GOTONE 1 | ||
41 | #define GOTTWO 2 | ||
42 | /* Inputs */ | ||
43 | #define DIGIT (4*0) | ||
44 | #define END (4*1) | ||
45 | #define DELIM (4*2) | ||
46 | |||
47 | struct iso_addr * | ||
48 | iso_addr(addr) | ||
49 | register const char *addr; | ||
50 | { | ||
51 | static struct iso_addr out_addr; | ||
52 | register char *cp = out_addr.isoa_genaddr; | ||
53 | char *cplim = cp + sizeof(out_addr.isoa_genaddr); | ||
54 | register int byte = 0, state = VIRGIN, new; | ||
55 | |||
56 | bzero((char *)&out_addr, sizeof(out_addr)); | ||
57 | do { | ||
58 | if ((*addr >= '0') && (*addr <= '9')) { | ||
59 | new = *addr - '0'; | ||
60 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | ||
61 | new = *addr - 'a' + 10; | ||
62 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | ||
63 | new = *addr - 'A' + 10; | ||
64 | } else if (*addr == 0) | ||
65 | state |= END; | ||
66 | else | ||
67 | state |= DELIM; | ||
68 | addr++; | ||
69 | switch (state /* | INPUT */) { | ||
70 | case GOTTWO | DIGIT: | ||
71 | *cp++ = byte; /*FALLTHROUGH*/ | ||
72 | case VIRGIN | DIGIT: | ||
73 | state = GOTONE; byte = new; continue; | ||
74 | case GOTONE | DIGIT: | ||
75 | state = GOTTWO; byte = new + (byte << 4); continue; | ||
76 | default: /* | DELIM */ | ||
77 | state = VIRGIN; *cp++ = byte; byte = 0; continue; | ||
78 | case GOTONE | END: | ||
79 | case GOTTWO | END: | ||
80 | *cp++ = byte; /* FALLTHROUGH */ | ||
81 | case VIRGIN | END: | ||
82 | break; | ||
83 | } | ||
84 | break; | ||
85 | } while (cp < cplim); | ||
86 | out_addr.isoa_len = cp - out_addr.isoa_genaddr; | ||
87 | return (&out_addr); | ||
88 | } | ||
89 | static char hexlist[] = "0123456789abcdef"; | ||
90 | |||
91 | char * | ||
92 | iso_ntoa(isoa) | ||
93 | const struct iso_addr *isoa; | ||
94 | { | ||
95 | static char obuf[64]; | ||
96 | register char *out = obuf; | ||
97 | register int i; | ||
98 | register u_char *in = (u_char *)isoa->isoa_genaddr; | ||
99 | u_char *inlim = in + isoa->isoa_len; | ||
100 | |||
101 | out[1] = 0; | ||
102 | while (in < inlim) { | ||
103 | i = *in++; | ||
104 | *out++ = '.'; | ||
105 | if (i > 0xf) { | ||
106 | out[1] = hexlist[i & 0xf]; | ||
107 | i >>= 4; | ||
108 | out[0] = hexlist[i]; | ||
109 | out += 2; | ||
110 | } else | ||
111 | *out++ = hexlist[i]; | ||
112 | } | ||
113 | *out = 0; | ||
114 | return(obuf + 1); | ||
115 | } | ||
diff --git a/src/lib/libc/net/link_addr.3 b/src/lib/libc/net/link_addr.3 new file mode 100644 index 0000000000..929db4e1d6 --- /dev/null +++ b/src/lib/libc/net/link_addr.3 | |||
@@ -0,0 +1,127 @@ | |||
1 | .\" $OpenBSD: link_addr.3,v 1.8 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" This code is derived from software contributed to Berkeley by | ||
7 | .\" Donn Seeley at BSDI. | ||
8 | .\" | ||
9 | .\" Redistribution and use in source and binary forms, with or without | ||
10 | .\" modification, are permitted provided that the following conditions | ||
11 | .\" are met: | ||
12 | .\" 1. Redistributions of source code must retain the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer. | ||
14 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
15 | .\" notice, this list of conditions and the following disclaimer in the | ||
16 | .\" documentation and/or other materials provided with the distribution. | ||
17 | .\" 3. Neither the name of the University nor the names of its contributors | ||
18 | .\" may be used to endorse or promote products derived from this software | ||
19 | .\" without specific prior written permission. | ||
20 | .\" | ||
21 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | .\" SUCH DAMAGE. | ||
32 | .\" | ||
33 | .Dd July 28, 1993 | ||
34 | .Dt LINK_ADDR 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm link_addr , | ||
38 | .Nm link_ntoa | ||
39 | .Nd elementary address specification routines for link level access | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <sys/types.h> | ||
42 | .Fd #include <sys/socket.h> | ||
43 | .Fd #include <net/if_dl.h> | ||
44 | .Ft void | ||
45 | .Fn link_addr "const char *addr" "struct sockaddr_dl *sdl" | ||
46 | .Ft char * | ||
47 | .Fn link_ntoa "const struct sockaddr_dl *sdl" | ||
48 | .Sh DESCRIPTION | ||
49 | The | ||
50 | .Fn link_addr | ||
51 | function interprets character strings representing | ||
52 | link-level addresses, returning binary information suitable | ||
53 | for use in system calls. | ||
54 | .Fn link_ntoa | ||
55 | takes | ||
56 | a link-level | ||
57 | address and returns an | ||
58 | .Tn ASCII | ||
59 | string representing some of the information present, | ||
60 | including the link level address itself, and the interface name | ||
61 | or number, if present. | ||
62 | This facility is experimental and is | ||
63 | still subject to change. | ||
64 | .Pp | ||
65 | For | ||
66 | .Fn link_addr , | ||
67 | the string | ||
68 | .Fa addr | ||
69 | may contain | ||
70 | an optional network interface identifier of the form | ||
71 | .Dq name unit-number , | ||
72 | suitable for the first argument to | ||
73 | .Xr ifconfig 8 , | ||
74 | followed in all cases by a colon and | ||
75 | an interface address in the form of | ||
76 | groups of hexadecimal digits | ||
77 | separated by periods. | ||
78 | Each group represents a byte of address; | ||
79 | address bytes are filled left to right from | ||
80 | low order bytes through high order bytes. | ||
81 | .Pp | ||
82 | .\" A regular expression may make this format clearer: | ||
83 | .\" .Bd -literal -offset indent | ||
84 | .\" ([a-z]+[0-9]+:)?[0-9a-f]+(\e.[0-9a-f]+)* | ||
85 | .\" .Ed | ||
86 | .\" .Pp | ||
87 | Thus | ||
88 | .Li le0:8.0.9.13.d.30 | ||
89 | represents an Ethernet address | ||
90 | to be transmitted on the first Lance Ethernet interface. | ||
91 | .Sh RETURN VALUES | ||
92 | .Fn link_ntoa | ||
93 | always returns a null-terminated string. | ||
94 | .Fn link_addr | ||
95 | has no return value. | ||
96 | (See | ||
97 | .Sx BUGS . ) | ||
98 | .Sh SEE ALSO | ||
99 | .Xr iso 4 , | ||
100 | .Xr ifconfig 8 | ||
101 | .Sh HISTORY | ||
102 | The | ||
103 | .Fn link_addr | ||
104 | and | ||
105 | .Fn link_ntoa | ||
106 | functions appeared in | ||
107 | .Bx 4.3 Reno . | ||
108 | .Sh BUGS | ||
109 | The returned values for link_ntoa | ||
110 | reside in a static memory area. | ||
111 | .Pp | ||
112 | The function | ||
113 | .Fn link_addr | ||
114 | should diagnose improperly formed input, and there should be an unambiguous | ||
115 | way to recognize this. | ||
116 | .Pp | ||
117 | If the | ||
118 | .Fa sdl_len | ||
119 | field of the link socket address | ||
120 | .Fa sdl | ||
121 | is 0, | ||
122 | .Fn link_ntoa | ||
123 | will not insert a colon before the interface address bytes. | ||
124 | If this translated address is given to | ||
125 | .Fn link_addr | ||
126 | without inserting an initial colon, | ||
127 | the latter will not interpret it correctly. | ||
diff --git a/src/lib/libc/net/linkaddr.c b/src/lib/libc/net/linkaddr.c new file mode 100644 index 0000000000..ee45ff767d --- /dev/null +++ b/src/lib/libc/net/linkaddr.c | |||
@@ -0,0 +1,154 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: linkaddr.c,v 1.3 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | #include <net/if_dl.h> | ||
37 | #include <string.h> | ||
38 | |||
39 | /* States*/ | ||
40 | #define NAMING 0 | ||
41 | #define GOTONE 1 | ||
42 | #define GOTTWO 2 | ||
43 | #define RESET 3 | ||
44 | /* Inputs */ | ||
45 | #define DIGIT (4*0) | ||
46 | #define END (4*1) | ||
47 | #define DELIM (4*2) | ||
48 | #define LETTER (4*3) | ||
49 | |||
50 | void | ||
51 | link_addr(addr, sdl) | ||
52 | register const char *addr; | ||
53 | register struct sockaddr_dl *sdl; | ||
54 | { | ||
55 | register char *cp = sdl->sdl_data; | ||
56 | char *cplim = sdl->sdl_len + (char *)sdl; | ||
57 | register int byte = 0, state = NAMING, new; | ||
58 | |||
59 | bzero((char *)&sdl->sdl_family, sdl->sdl_len - 1); | ||
60 | sdl->sdl_family = AF_LINK; | ||
61 | do { | ||
62 | state &= ~LETTER; | ||
63 | if ((*addr >= '0') && (*addr <= '9')) { | ||
64 | new = *addr - '0'; | ||
65 | } else if ((*addr >= 'a') && (*addr <= 'f')) { | ||
66 | new = *addr - 'a' + 10; | ||
67 | } else if ((*addr >= 'A') && (*addr <= 'F')) { | ||
68 | new = *addr - 'A' + 10; | ||
69 | } else if (*addr == 0) { | ||
70 | state |= END; | ||
71 | } else if (state == NAMING && | ||
72 | (((*addr >= 'A') && (*addr <= 'Z')) || | ||
73 | ((*addr >= 'a') && (*addr <= 'z')))) | ||
74 | state |= LETTER; | ||
75 | else | ||
76 | state |= DELIM; | ||
77 | addr++; | ||
78 | switch (state /* | INPUT */) { | ||
79 | case NAMING | DIGIT: | ||
80 | case NAMING | LETTER: | ||
81 | *cp++ = addr[-1]; | ||
82 | continue; | ||
83 | case NAMING | DELIM: | ||
84 | state = RESET; | ||
85 | sdl->sdl_nlen = cp - sdl->sdl_data; | ||
86 | continue; | ||
87 | case GOTTWO | DIGIT: | ||
88 | *cp++ = byte; | ||
89 | /* FALLTHROUGH */ | ||
90 | case RESET | DIGIT: | ||
91 | state = GOTONE; | ||
92 | byte = new; | ||
93 | continue; | ||
94 | case GOTONE | DIGIT: | ||
95 | state = GOTTWO; | ||
96 | byte = new + (byte << 4); | ||
97 | continue; | ||
98 | default: /* | DELIM */ | ||
99 | state = RESET; | ||
100 | *cp++ = byte; | ||
101 | byte = 0; | ||
102 | continue; | ||
103 | case GOTONE | END: | ||
104 | case GOTTWO | END: | ||
105 | *cp++ = byte; | ||
106 | /* FALLTHROUGH */ | ||
107 | case RESET | END: | ||
108 | break; | ||
109 | } | ||
110 | break; | ||
111 | } while (cp < cplim); | ||
112 | sdl->sdl_alen = cp - LLADDR(sdl); | ||
113 | new = cp - (char *)sdl; | ||
114 | if (new > sizeof(*sdl)) | ||
115 | sdl->sdl_len = new; | ||
116 | return; | ||
117 | } | ||
118 | |||
119 | static char hexlist[] = "0123456789abcdef"; | ||
120 | |||
121 | char * | ||
122 | link_ntoa(sdl) | ||
123 | register const struct sockaddr_dl *sdl; | ||
124 | { | ||
125 | static char obuf[64]; | ||
126 | register char *out = obuf; | ||
127 | register int i; | ||
128 | register u_char *in = (u_char *)LLADDR(sdl); | ||
129 | u_char *inlim = in + sdl->sdl_alen; | ||
130 | int firsttime = 1; | ||
131 | |||
132 | if (sdl->sdl_nlen) { | ||
133 | bcopy(sdl->sdl_data, obuf, sdl->sdl_nlen); | ||
134 | out += sdl->sdl_nlen; | ||
135 | if (sdl->sdl_alen) | ||
136 | *out++ = ':'; | ||
137 | } | ||
138 | while (in < inlim) { | ||
139 | if (firsttime) | ||
140 | firsttime = 0; | ||
141 | else | ||
142 | *out++ = '.'; | ||
143 | i = *in++; | ||
144 | if (i > 0xf) { | ||
145 | out[1] = hexlist[i & 0xf]; | ||
146 | i >>= 4; | ||
147 | out[0] = hexlist[i]; | ||
148 | out += 2; | ||
149 | } else | ||
150 | *out++ = hexlist[i]; | ||
151 | } | ||
152 | *out = 0; | ||
153 | return (obuf); | ||
154 | } | ||
diff --git a/src/lib/libc/net/net_addrcmp.3 b/src/lib/libc/net/net_addrcmp.3 new file mode 100644 index 0000000000..f1ce4c8cd1 --- /dev/null +++ b/src/lib/libc/net/net_addrcmp.3 | |||
@@ -0,0 +1,91 @@ | |||
1 | .\" $OpenBSD: net_addrcmp.3,v 1.3 2001/08/07 06:53:27 deraadt Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1999 Theo de Raadt | ||
4 | .\" | ||
5 | .\" Redistribution and use in source and binary forms, with or without | ||
6 | .\" modification, are permitted provided that the following conditions | ||
7 | .\" are met: | ||
8 | .\" 1. Redistributions of source code must retain the above copyright | ||
9 | .\" notice, this list of conditions and the following disclaimer. | ||
10 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer in the | ||
12 | .\" documentation and/or other materials provided with the distribution. | ||
13 | .\" | ||
14 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
15 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
16 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
17 | .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
18 | .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
19 | .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
20 | .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
21 | .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
22 | .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
23 | .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
24 | .\" | ||
25 | .Dd July 3, 1999 | ||
26 | .Dt NET_ADDRCMP 3 | ||
27 | .Os | ||
28 | .Sh NAME | ||
29 | .Nm net_addrcmp | ||
30 | .Nd compare socket address structures | ||
31 | .Sh SYNOPSIS | ||
32 | .Fd #include <netdb.h> | ||
33 | .Ft int | ||
34 | .Fn net_addrcmp "struct sockaddr *sa1" "struct sockaddr *sa2" | ||
35 | .Sh DESCRIPTION | ||
36 | The | ||
37 | .Fn net_addrcmp | ||
38 | function compares two socket address structures, | ||
39 | .Fa sa1 | ||
40 | and | ||
41 | .Fa sa2 . | ||
42 | .Sh RETURN VALUES | ||
43 | If | ||
44 | .Fa sa1 | ||
45 | and | ||
46 | .Fa sa2 | ||
47 | are for the same address, | ||
48 | .Fn net_addrcmp | ||
49 | returns 0. | ||
50 | .Pp | ||
51 | The | ||
52 | .Fa sa_len | ||
53 | fields are compared first. | ||
54 | If they do not match, | ||
55 | .Fn net_addrcmp | ||
56 | returns \-1 or 1 if | ||
57 | .Li sa1->sa_len | ||
58 | is less than or greater than | ||
59 | .Li sa2->sa_len , | ||
60 | respectively. | ||
61 | .Pp | ||
62 | Next, the | ||
63 | .Fa sa_family | ||
64 | members are compared. | ||
65 | If they do not match, | ||
66 | .Fn net_addrcmp | ||
67 | returns \-1 or 1 if | ||
68 | .Li sa1->sa_family | ||
69 | is less than or greater than | ||
70 | .Li sa2->sa_family , | ||
71 | respectively. | ||
72 | .Pp | ||
73 | Lastly, if each socket address structure's | ||
74 | .Fa sa_len | ||
75 | and | ||
76 | .Fa sa_family | ||
77 | fields match, | ||
78 | the protocol-specific data (the | ||
79 | .Fa sa_data | ||
80 | field) is compared. | ||
81 | If there's a match, both | ||
82 | .Fa sa1 | ||
83 | and | ||
84 | .Fa sa2 | ||
85 | must refer to the same address, and 0 is returned; otherwise, a value >0 | ||
86 | or <0 is returned. | ||
87 | .Sh HISTORY | ||
88 | A | ||
89 | .Fn net_addrcmp | ||
90 | function was added in | ||
91 | .Ox 2.5 . | ||
diff --git a/src/lib/libc/net/net_addrcmp.c b/src/lib/libc/net/net_addrcmp.c new file mode 100644 index 0000000000..c77a2fda48 --- /dev/null +++ b/src/lib/libc/net/net_addrcmp.c | |||
@@ -0,0 +1,68 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1999 Theo de Raadt | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * | ||
14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
24 | */ | ||
25 | |||
26 | #include <sys/types.h> | ||
27 | #include <sys/socket.h> | ||
28 | #include <sys/un.h> | ||
29 | #include <netinet/in.h> | ||
30 | #include <netns/ns.h> | ||
31 | #include <string.h> | ||
32 | |||
33 | int | ||
34 | net_addrcmp(sa1, sa2) | ||
35 | struct sockaddr *sa1; | ||
36 | struct sockaddr *sa2; | ||
37 | { | ||
38 | |||
39 | if (sa1->sa_len != sa2->sa_len) | ||
40 | return (sa1->sa_len < sa2->sa_len) ? -1 : 1; | ||
41 | if (sa1->sa_family != sa2->sa_family) | ||
42 | return (sa1->sa_family < sa2->sa_family) ? -1 : 1; | ||
43 | |||
44 | switch(sa1->sa_family) { | ||
45 | case AF_INET: | ||
46 | return (memcmp(&((struct sockaddr_in *)sa1)->sin_addr, | ||
47 | &((struct sockaddr_in *)sa2)->sin_addr, | ||
48 | sizeof(struct in_addr))); | ||
49 | case AF_INET6: | ||
50 | if (((struct sockaddr_in6 *)sa1)->sin6_scope_id != | ||
51 | ((struct sockaddr_in6 *)sa2)->sin6_scope_id) | ||
52 | return (((struct sockaddr_in6 *)sa1)->sin6_scope_id < | ||
53 | ((struct sockaddr_in6 *)sa2)->sin6_scope_id) | ||
54 | ? -1 : 1; | ||
55 | return memcmp(&((struct sockaddr_in6 *)sa1)->sin6_addr, | ||
56 | &((struct sockaddr_in6 *)sa2)->sin6_addr, | ||
57 | sizeof(struct in6_addr)); | ||
58 | case AF_NS: | ||
59 | return (memcmp(&((struct sockaddr_ns *)sa1)->sns_addr, | ||
60 | &((struct sockaddr_ns *)sa2)->sns_addr, | ||
61 | sizeof(struct ns_addr))); | ||
62 | case AF_LOCAL: | ||
63 | return (strcmp(((struct sockaddr_un *)sa1)->sun_path, | ||
64 | ((struct sockaddr_un *)sa1)->sun_path)); | ||
65 | default: | ||
66 | return -1; | ||
67 | } | ||
68 | } | ||
diff --git a/src/lib/libc/net/ns.3 b/src/lib/libc/net/ns.3 new file mode 100644 index 0000000000..9c7aeab312 --- /dev/null +++ b/src/lib/libc/net/ns.3 | |||
@@ -0,0 +1,127 @@ | |||
1 | .\" $OpenBSD: ns.3,v 1.9 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1986, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt NS 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm ns_addr , | ||
35 | .Nm ns_ntoa | ||
36 | .Nd Xerox | ||
37 | .Tn NS Ns (tm) | ||
38 | address conversion routines | ||
39 | .Sh SYNOPSIS | ||
40 | .Fd #include <sys/types.h> | ||
41 | .Fd #include <netns/ns.h> | ||
42 | .Ft struct ns_addr | ||
43 | .Fn ns_addr "char *cp" | ||
44 | .Ft char * | ||
45 | .Fn ns_ntoa "struct ns_addr ns" | ||
46 | .Sh DESCRIPTION | ||
47 | The routine | ||
48 | .Fn ns_addr | ||
49 | interprets character strings representing | ||
50 | .Tn XNS | ||
51 | addresses, returning binary information suitable | ||
52 | for use in system calls. | ||
53 | The routine | ||
54 | .Fn ns_ntoa | ||
55 | takes | ||
56 | .Tn XNS | ||
57 | addresses and returns | ||
58 | .Tn ASCII | ||
59 | strings representing the address in a | ||
60 | notation in common use in the Xerox Development Environment: | ||
61 | .Bd -filled -offset indent | ||
62 | <network number>.<host number>.<port number> | ||
63 | .Ed | ||
64 | .Pp | ||
65 | Trailing zero fields are suppressed, and each number is printed in hexadecimal, | ||
66 | in a format suitable for input to | ||
67 | .Fn ns_addr . | ||
68 | Any fields lacking super-decimal digits will have a | ||
69 | trailing | ||
70 | .Sq H | ||
71 | appended. | ||
72 | .Pp | ||
73 | Unfortunately, no universal standard exists for representing | ||
74 | .Tn XNS | ||
75 | addresses. | ||
76 | An effort has been made to ensure that | ||
77 | .Fn ns_addr | ||
78 | be compatible with most formats in common use. | ||
79 | It will first separate an address into 1 to 3 fields using a single delimiter | ||
80 | chosen from | ||
81 | period | ||
82 | .Pq Ql \&. , | ||
83 | colon | ||
84 | .Pq Ql \&: , | ||
85 | or pound-sign | ||
86 | .Ql # . | ||
87 | Each field is then examined for byte separators (colon or period). | ||
88 | If there are byte separators, each subfield separated is taken to be | ||
89 | a small hexadecimal number, and the entirety is taken as a network-byte-ordered | ||
90 | quantity to be zero extended in the high-network-order bytes. | ||
91 | Next, the field is inspected for hyphens, in which case | ||
92 | the field is assumed to be a number in decimal notation | ||
93 | with hyphens separating the millenia. | ||
94 | Next, the field is assumed to be a number: | ||
95 | It is interpreted | ||
96 | as hexadecimal if there is a leading | ||
97 | .Ql 0x | ||
98 | (as in C), | ||
99 | a trailing | ||
100 | .Sq H | ||
101 | (as in Mesa), or there are any super-decimal digits present. | ||
102 | It is interpreted as octal if there is a leading | ||
103 | .Ql 0 | ||
104 | and there are no super-octal digits. | ||
105 | Otherwise, it is converted as a decimal number. | ||
106 | .Sh RETURN VALUES | ||
107 | None. | ||
108 | (See | ||
109 | .Sx BUGS . ) | ||
110 | .Sh SEE ALSO | ||
111 | .Xr hosts 5 , | ||
112 | .Xr networks 5 | ||
113 | .Sh HISTORY | ||
114 | The | ||
115 | .Fn ns_addr | ||
116 | and | ||
117 | .Fn ns_toa | ||
118 | functions appeared in | ||
119 | .Bx 4.3 . | ||
120 | .Sh BUGS | ||
121 | The string returned by | ||
122 | .Fn ns_ntoa | ||
123 | resides in a static memory area. | ||
124 | The function | ||
125 | .Fn ns_addr | ||
126 | should diagnose improperly formed input, and there should be an unambiguous | ||
127 | way to recognize this. | ||
diff --git a/src/lib/libc/net/ns_addr.c b/src/lib/libc/net/ns_addr.c new file mode 100644 index 0000000000..0f8feda55c --- /dev/null +++ b/src/lib/libc/net/ns_addr.c | |||
@@ -0,0 +1,223 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1986, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * J.Q. Johnson. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char rcsid[] = "$OpenBSD: ns_addr.c,v 1.8 2003/06/02 20:18:35 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <sys/param.h> | ||
38 | #include <netns/ns.h> | ||
39 | #include <stdio.h> | ||
40 | #include <string.h> | ||
41 | |||
42 | static struct ns_addr addr, zero_addr; | ||
43 | |||
44 | static void Field(char *, u_int8_t *, int); | ||
45 | static void cvtbase(long, int, int[], int, u_int8_t[], int); | ||
46 | |||
47 | struct ns_addr | ||
48 | ns_addr(name) | ||
49 | const char *name; | ||
50 | { | ||
51 | char separator; | ||
52 | char *hostname, *socketname, *cp; | ||
53 | char buf[50]; | ||
54 | |||
55 | strlcpy(buf, name, sizeof(buf)); | ||
56 | |||
57 | /* | ||
58 | * First, figure out what he intends as a field separtor. | ||
59 | * Despite the way this routine is written, the prefered | ||
60 | * form 2-272.AA001234H.01777, i.e. XDE standard. | ||
61 | * Great efforts are made to insure backward compatibility. | ||
62 | */ | ||
63 | if ((hostname = strchr(buf, '#'))) | ||
64 | separator = '#'; | ||
65 | else { | ||
66 | hostname = strchr(buf, '.'); | ||
67 | if ((cp = strchr(buf, ':')) && | ||
68 | ((hostname && cp < hostname) || (hostname == 0))) { | ||
69 | hostname = cp; | ||
70 | separator = ':'; | ||
71 | } else | ||
72 | separator = '.'; | ||
73 | } | ||
74 | if (hostname) | ||
75 | *hostname++ = 0; | ||
76 | |||
77 | addr = zero_addr; | ||
78 | Field(buf, addr.x_net.c_net, 4); | ||
79 | if (hostname == 0) | ||
80 | return (addr); /* No separator means net only */ | ||
81 | |||
82 | socketname = strchr(hostname, separator); | ||
83 | if (socketname) { | ||
84 | *socketname++ = 0; | ||
85 | Field(socketname, (u_char *)&addr.x_port, 2); | ||
86 | } | ||
87 | |||
88 | Field(hostname, (u_char *)addr.x_host.c_host, 6); | ||
89 | |||
90 | return (addr); | ||
91 | } | ||
92 | |||
93 | static void | ||
94 | Field(buf, out, len) | ||
95 | char *buf; | ||
96 | u_char *out; | ||
97 | int len; | ||
98 | { | ||
99 | register char *bp = buf; | ||
100 | int i, ibase, base16 = 0, base10 = 0, clen = 0; | ||
101 | int hb[6], *hp; | ||
102 | char *fmt; | ||
103 | |||
104 | /* | ||
105 | * first try 2-273#2-852-151-014#socket | ||
106 | */ | ||
107 | if ((*buf != '-') && | ||
108 | (1 < (i = sscanf(buf, "%d-%d-%d-%d-%d", | ||
109 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4])))) { | ||
110 | cvtbase(1000L, 256, hb, i, out, len); | ||
111 | return; | ||
112 | } | ||
113 | /* | ||
114 | * try form 8E1#0.0.AA.0.5E.E6#socket | ||
115 | */ | ||
116 | if (1 < (i = sscanf(buf,"%x.%x.%x.%x.%x.%x", | ||
117 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
118 | cvtbase(256L, 256, hb, i, out, len); | ||
119 | return; | ||
120 | } | ||
121 | /* | ||
122 | * try form 8E1#0:0:AA:0:5E:E6#socket | ||
123 | */ | ||
124 | if (1 < (i = sscanf(buf,"%x:%x:%x:%x:%x:%x", | ||
125 | &hb[0], &hb[1], &hb[2], &hb[3], &hb[4], &hb[5]))) { | ||
126 | cvtbase(256L, 256, hb, i, out, len); | ||
127 | return; | ||
128 | } | ||
129 | /* | ||
130 | * This is REALLY stretching it but there was a | ||
131 | * comma notation separting shorts -- definitely non standard | ||
132 | */ | ||
133 | if (1 < (i = sscanf(buf,"%x,%x,%x", | ||
134 | &hb[0], &hb[1], &hb[2]))) { | ||
135 | hb[0] = htons(hb[0]); hb[1] = htons(hb[1]); | ||
136 | hb[2] = htons(hb[2]); | ||
137 | cvtbase(65536L, 256, hb, i, out, len); | ||
138 | return; | ||
139 | } | ||
140 | |||
141 | /* Need to decide if base 10, 16 or 8 */ | ||
142 | while (*bp) switch (*bp++) { | ||
143 | |||
144 | case '0': case '1': case '2': case '3': case '4': case '5': | ||
145 | case '6': case '7': case '-': | ||
146 | break; | ||
147 | |||
148 | case '8': case '9': | ||
149 | base10 = 1; | ||
150 | break; | ||
151 | |||
152 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | ||
153 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | ||
154 | base16 = 1; | ||
155 | break; | ||
156 | |||
157 | case 'x': case 'X': | ||
158 | *--bp = '0'; | ||
159 | base16 = 1; | ||
160 | break; | ||
161 | |||
162 | case 'h': case 'H': | ||
163 | base16 = 1; | ||
164 | /* fall into */ | ||
165 | |||
166 | default: | ||
167 | *--bp = 0; /* Ends Loop */ | ||
168 | } | ||
169 | if (base16) { | ||
170 | fmt = "%3x"; | ||
171 | ibase = 4096; | ||
172 | } else if (base10 == 0 && *buf == '0') { | ||
173 | fmt = "%3o"; | ||
174 | ibase = 512; | ||
175 | } else { | ||
176 | fmt = "%3d"; | ||
177 | ibase = 1000; | ||
178 | } | ||
179 | |||
180 | for (bp = buf; *bp++; ) clen++; | ||
181 | if (clen == 0) clen++; | ||
182 | if (clen > 18) clen = 18; | ||
183 | i = ((clen - 1) / 3) + 1; | ||
184 | bp = clen + buf - 3; | ||
185 | hp = hb + i - 1; | ||
186 | |||
187 | while (hp > hb) { | ||
188 | (void)sscanf(bp, fmt, hp); | ||
189 | bp[0] = 0; | ||
190 | hp--; | ||
191 | bp -= 3; | ||
192 | } | ||
193 | (void)sscanf(buf, fmt, hp); | ||
194 | cvtbase((long)ibase, 256, hb, i, out, len); | ||
195 | } | ||
196 | |||
197 | static void | ||
198 | cvtbase(oldbase,newbase,input,inlen,result,reslen) | ||
199 | long oldbase; | ||
200 | int newbase; | ||
201 | int input[]; | ||
202 | int inlen; | ||
203 | unsigned char result[]; | ||
204 | int reslen; | ||
205 | { | ||
206 | int d, e; | ||
207 | long sum; | ||
208 | |||
209 | e = 1; | ||
210 | while (e > 0 && reslen > 0) { | ||
211 | d = 0; e = 0; sum = 0; | ||
212 | /* long division: input=input/newbase */ | ||
213 | while (d < inlen) { | ||
214 | sum = sum*oldbase + (long) input[d]; | ||
215 | e += (sum > 0); | ||
216 | input[d++] = sum / newbase; | ||
217 | sum %= newbase; | ||
218 | } | ||
219 | result[--reslen] = sum; /* accumulate remainder */ | ||
220 | } | ||
221 | for (d=0; d < reslen; d++) | ||
222 | result[d] = 0; | ||
223 | } | ||
diff --git a/src/lib/libc/net/ns_ntoa.c b/src/lib/libc/net/ns_ntoa.c new file mode 100644 index 0000000000..fd67e459da --- /dev/null +++ b/src/lib/libc/net/ns_ntoa.c | |||
@@ -0,0 +1,106 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1986, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: ns_ntoa.c,v 1.13 2003/09/25 21:14:46 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/param.h> | ||
35 | #include <netns/ns.h> | ||
36 | #include <stdio.h> | ||
37 | |||
38 | static char *spectHex(char *); | ||
39 | |||
40 | char * | ||
41 | ns_ntoa(struct ns_addr addr) | ||
42 | { | ||
43 | static char obuf[40]; | ||
44 | union { union ns_net net_e; u_int32_t long_e; } net; | ||
45 | in_port_t port = htons(addr.x_port); | ||
46 | char *cp, *cp2; | ||
47 | u_char *up = addr.x_host.c_host; | ||
48 | u_char *uplim = up + 6; | ||
49 | size_t rem; | ||
50 | |||
51 | net.net_e = addr.x_net; | ||
52 | snprintf(obuf, sizeof obuf, "%x", ntohl(net.long_e)); | ||
53 | cp = spectHex(obuf); | ||
54 | rem = sizeof(obuf) - (cp - obuf); | ||
55 | cp2 = cp + 1; | ||
56 | while (up < uplim && *up==0) | ||
57 | up++; | ||
58 | if (up == uplim) { | ||
59 | if (port) { | ||
60 | snprintf(cp, rem, ".0"); | ||
61 | cp += 2; | ||
62 | rem -= 2; | ||
63 | } | ||
64 | } else { | ||
65 | snprintf(cp, rem, ".%x", *up++); | ||
66 | while (up < uplim) { | ||
67 | while (*cp) { | ||
68 | cp++; | ||
69 | rem--; | ||
70 | } | ||
71 | snprintf(cp, rem, "%02x", *up++); | ||
72 | } | ||
73 | cp = spectHex(cp2); | ||
74 | rem = sizeof(obuf) - (cp - obuf); | ||
75 | } | ||
76 | if (port) { | ||
77 | snprintf(cp, rem, ".%x", port); | ||
78 | spectHex(cp + 1); | ||
79 | } | ||
80 | return (obuf); | ||
81 | } | ||
82 | |||
83 | static char * | ||
84 | spectHex(char *p0) | ||
85 | { | ||
86 | int ok = 0, nonzero = 0; | ||
87 | char *p = p0; | ||
88 | |||
89 | for (; *p; p++) { | ||
90 | switch (*p) { | ||
91 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | ||
92 | *p += ('A' - 'a'); | ||
93 | /* fall into . . . */ | ||
94 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | ||
95 | ok = 1; | ||
96 | case '1': case '2': case '3': case '4': case '5': | ||
97 | case '6': case '7': case '8': case '9': | ||
98 | nonzero = 1; | ||
99 | } | ||
100 | } | ||
101 | if (nonzero && !ok) { | ||
102 | *p++ = 'H'; | ||
103 | *p = 0; | ||
104 | } | ||
105 | return (p); | ||
106 | } | ||
diff --git a/src/lib/libc/net/nsap_addr.c b/src/lib/libc/net/nsap_addr.c new file mode 100644 index 0000000000..22a5f8d66e --- /dev/null +++ b/src/lib/libc/net/nsap_addr.c | |||
@@ -0,0 +1,109 @@ | |||
1 | /* $OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 by Internet Software Consortium. | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | ||
11 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | ||
12 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | ||
13 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
14 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
15 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
16 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
17 | * SOFTWARE. | ||
18 | */ | ||
19 | |||
20 | #if defined(LIBC_SCCS) && !defined(lint) | ||
21 | #if 0 | ||
22 | static char rcsid[] = "$From: nsap_addr.c,v 8.3 1996/08/05 08:31:35 vixie Exp $"; | ||
23 | #else | ||
24 | static char rcsid[] = "$OpenBSD: nsap_addr.c,v 1.4 1997/07/09 01:08:45 millert Exp $"; | ||
25 | #endif | ||
26 | #endif /* LIBC_SCCS and not lint */ | ||
27 | |||
28 | #include <sys/types.h> | ||
29 | #include <sys/param.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <netinet/in.h> | ||
32 | #include <arpa/nameser.h> | ||
33 | #include <ctype.h> | ||
34 | #include <resolv.h> | ||
35 | |||
36 | static char | ||
37 | xtob(c) | ||
38 | register int c; | ||
39 | { | ||
40 | return (c - (((c >= '0') && (c <= '9')) ? '0' : '7')); | ||
41 | } | ||
42 | |||
43 | u_int | ||
44 | inet_nsap_addr(ascii, binary, maxlen) | ||
45 | const char *ascii; | ||
46 | u_char *binary; | ||
47 | int maxlen; | ||
48 | { | ||
49 | register u_char c, nib; | ||
50 | u_int len = 0; | ||
51 | |||
52 | while ((c = *ascii++) != '\0' && len < maxlen) { | ||
53 | if (c == '.' || c == '+' || c == '/') | ||
54 | continue; | ||
55 | if (!isascii(c)) | ||
56 | return (0); | ||
57 | if (islower(c)) | ||
58 | c = toupper(c); | ||
59 | if (isxdigit(c)) { | ||
60 | nib = xtob(c); | ||
61 | if ((c = *ascii++)) { | ||
62 | c = toupper(c); | ||
63 | if (isxdigit(c)) { | ||
64 | *binary++ = (nib << 4) | xtob(c); | ||
65 | len++; | ||
66 | } else | ||
67 | return (0); | ||
68 | } | ||
69 | else | ||
70 | return (0); | ||
71 | } | ||
72 | else | ||
73 | return (0); | ||
74 | } | ||
75 | return (len); | ||
76 | } | ||
77 | |||
78 | char * | ||
79 | inet_nsap_ntoa(binlen, binary, ascii) | ||
80 | int binlen; | ||
81 | register const u_char *binary; | ||
82 | register char *ascii; | ||
83 | { | ||
84 | register int nib; | ||
85 | int i; | ||
86 | static char tmpbuf[255*3]; | ||
87 | char *start; | ||
88 | |||
89 | if (ascii) | ||
90 | start = ascii; | ||
91 | else { | ||
92 | ascii = tmpbuf; | ||
93 | start = tmpbuf; | ||
94 | } | ||
95 | |||
96 | if (binlen > 255) | ||
97 | binlen = 255; | ||
98 | |||
99 | for (i = 0; i < binlen; i++) { | ||
100 | nib = *binary >> 4; | ||
101 | *ascii++ = nib + (nib < 10 ? '0' : '7'); | ||
102 | nib = *binary++ & 0x0f; | ||
103 | *ascii++ = nib + (nib < 10 ? '0' : '7'); | ||
104 | if (((i % 2) == 0 && (i + 1) < binlen)) | ||
105 | *ascii++ = '.'; | ||
106 | } | ||
107 | *ascii = '\0'; | ||
108 | return (start); | ||
109 | } | ||
diff --git a/src/lib/libc/net/ntohl.c b/src/lib/libc/net/ntohl.c new file mode 100644 index 0000000000..7d3e227e60 --- /dev/null +++ b/src/lib/libc/net/ntohl.c | |||
@@ -0,0 +1,25 @@ | |||
1 | /* | ||
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
3 | * Public domain. | ||
4 | */ | ||
5 | |||
6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
7 | static char *rcsid = "$OpenBSD: ntohl.c,v 1.4 1996/12/12 03:19:56 tholo Exp $"; | ||
8 | #endif /* LIBC_SCCS and not lint */ | ||
9 | |||
10 | #include <sys/types.h> | ||
11 | #include <machine/endian.h> | ||
12 | |||
13 | #undef ntohl | ||
14 | |||
15 | u_int32_t | ||
16 | ntohl(x) | ||
17 | u_int32_t x; | ||
18 | { | ||
19 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
20 | u_char *s = (u_char *)&x; | ||
21 | return (u_int32_t)(s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3]); | ||
22 | #else | ||
23 | return x; | ||
24 | #endif | ||
25 | } | ||
diff --git a/src/lib/libc/net/ntohs.c b/src/lib/libc/net/ntohs.c new file mode 100644 index 0000000000..4c3ab33f31 --- /dev/null +++ b/src/lib/libc/net/ntohs.c | |||
@@ -0,0 +1,24 @@ | |||
1 | /* | ||
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
3 | * Public domain. | ||
4 | */ | ||
5 | |||
6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
7 | static char *rcsid = "$OpenBSD: ntohs.c,v 1.7 2002/02/19 19:39:36 millert Exp $"; | ||
8 | #endif /* LIBC_SCCS and not lint */ | ||
9 | |||
10 | #include <sys/types.h> | ||
11 | #include <machine/endian.h> | ||
12 | |||
13 | #undef ntohs | ||
14 | |||
15 | u_int16_t | ||
16 | ntohs(u_int16_t x) | ||
17 | { | ||
18 | #if BYTE_ORDER == LITTLE_ENDIAN | ||
19 | u_char *s = (u_char *) &x; | ||
20 | return (u_int16_t)(s[0] << 8 | s[1]); | ||
21 | #else | ||
22 | return x; | ||
23 | #endif | ||
24 | } | ||
diff --git a/src/lib/libc/net/rcmd.3 b/src/lib/libc/net/rcmd.3 new file mode 100644 index 0000000000..df31a60b63 --- /dev/null +++ b/src/lib/libc/net/rcmd.3 | |||
@@ -0,0 +1,266 @@ | |||
1 | .\" $OpenBSD: rcmd.3,v 1.26 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt RCMD 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm rcmd , | ||
35 | .Nm rcmd_af , | ||
36 | .Nm rresvport , | ||
37 | .Nm rresvport_af , | ||
38 | .Nm iruserok , | ||
39 | .Nm ruserok , | ||
40 | .Nm iruserok_sa | ||
41 | .Nd routines for returning a stream to a remote command | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <unistd.h> | ||
44 | .Ft int | ||
45 | .Fn rcmd "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" | ||
46 | .Ft int | ||
47 | .Fn rcmd_af "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "int *fd2p" "int af" | ||
48 | .Ft int | ||
49 | .Fn rresvport "int *port" | ||
50 | .Ft int | ||
51 | .Fn rresvport_af "int *port" "int af" | ||
52 | .Ft int | ||
53 | .Fn iruserok "u_int32_t raddr" "int superuser" "const char *ruser" "const char *luser" | ||
54 | .Ft int | ||
55 | .Fn ruserok "const char *rhost" "int superuser" "const char *ruser" "const char *luser" | ||
56 | .Ft int | ||
57 | .Fn iruserok_sa "const void *sa" "int salen" "int superuser" "const char *ruser" "const char *luser" | ||
58 | .Sh DESCRIPTION | ||
59 | The | ||
60 | .Fn rcmd | ||
61 | function is used by the superuser to execute a command on a remote | ||
62 | machine using an authentication scheme based on reserved | ||
63 | port numbers. | ||
64 | If the calling process is not setuid, the | ||
65 | .Ev RSH | ||
66 | environment variable is set, and | ||
67 | .Fa inport | ||
68 | is | ||
69 | .Dq shell/tcp , | ||
70 | .Xr rcmdsh 3 | ||
71 | is called instead with the value of | ||
72 | .Ev RSH . | ||
73 | Alternately, if the user is not the superuser, | ||
74 | .Fn rcmd | ||
75 | will invoke | ||
76 | .Xr rcmdsh 3 | ||
77 | to run the command via | ||
78 | .Xr rsh 1 . | ||
79 | While | ||
80 | .Fn rcmd | ||
81 | can handle IPv4 cases only, | ||
82 | the | ||
83 | .Fn rcmd_af | ||
84 | function can handle other cases as well. | ||
85 | .Pp | ||
86 | The | ||
87 | .Fn rresvport | ||
88 | and | ||
89 | .Fn rresvport_af | ||
90 | functions return a descriptor to a socket | ||
91 | with an address in the privileged port space. | ||
92 | The | ||
93 | .Fn iruserok | ||
94 | and | ||
95 | .Fn ruserok | ||
96 | functions are used by servers | ||
97 | to authenticate clients requesting service with | ||
98 | .Fn rcmd . | ||
99 | All four functions are present in the same file and are used | ||
100 | by the | ||
101 | .Xr rshd 8 | ||
102 | server (among others). | ||
103 | .Fn iruserok_sa | ||
104 | is an address family independent variant of | ||
105 | .Fn iruserok . | ||
106 | .Pp | ||
107 | The | ||
108 | .Fn rcmd | ||
109 | function looks up the host | ||
110 | .Fa *ahost | ||
111 | using | ||
112 | .Xr gethostbyname 3 , | ||
113 | returning \-1 if the host does not exist. | ||
114 | Otherwise | ||
115 | .Fa *ahost | ||
116 | is set to the standard name of the host | ||
117 | and a connection is established to a server | ||
118 | residing at the well-known Internet port | ||
119 | .Fa inport . | ||
120 | If the user is not the superuser, the only valid port is | ||
121 | .Dq shell/tcp | ||
122 | (usually port 514). | ||
123 | .Pp | ||
124 | If the connection succeeds, | ||
125 | a socket in the Internet domain of type | ||
126 | .Dv SOCK_STREAM | ||
127 | is returned to the caller, and given to the remote | ||
128 | command as stdin and stdout. | ||
129 | If | ||
130 | .Fa fd2p | ||
131 | is non-zero, then an auxiliary channel to a control | ||
132 | process will be set up, and a descriptor for it will be placed | ||
133 | in | ||
134 | .Fa *fd2p . | ||
135 | The control process will return diagnostic | ||
136 | output from the command (unit 2) on this channel, and will also | ||
137 | accept bytes on this channel as being | ||
138 | .Tn UNIX | ||
139 | signal numbers, to be | ||
140 | forwarded to the process group of the command. | ||
141 | If | ||
142 | .Fa fd2p | ||
143 | is | ||
144 | .Va NULL , | ||
145 | then the standard error (unit 2 of the remote command) will be made | ||
146 | the same as the standard output and no provision is made for sending | ||
147 | arbitrary signals to the remote process, although you may be able to | ||
148 | get its attention by using out-of-band data. | ||
149 | Note that if the user is not the superuser, | ||
150 | .Fa fd2p | ||
151 | must be | ||
152 | .Va NULL . | ||
153 | .Pp | ||
154 | .Fn rcmd_af | ||
155 | takes address family in the last argument. | ||
156 | If the last argument is | ||
157 | .Dv PF_UNSPEC , | ||
158 | interpretation of | ||
159 | .Fa *ahost | ||
160 | will obey the underlying address resolution like DNS. | ||
161 | .Pp | ||
162 | The protocol is described in detail in | ||
163 | .Xr rshd 8 . | ||
164 | .Pp | ||
165 | The | ||
166 | .Fn rresvport | ||
167 | and | ||
168 | .Fn rresvport_af | ||
169 | functions are used to obtain a socket with a privileged | ||
170 | address bound to it. | ||
171 | This socket is suitable for use by | ||
172 | .Fn rcmd | ||
173 | and several other functions. | ||
174 | Privileged Internet ports are those in the range 0 to | ||
175 | .Va IPPORT_RESERVED - 1 , | ||
176 | which happens to be 1023. | ||
177 | Only the superuser is allowed to bind an address of this sort to a socket. | ||
178 | .Fn rresvport | ||
179 | and | ||
180 | .Fn rresvport_af | ||
181 | need to be seeded with a port number; if that port | ||
182 | is not available these functions will find another. | ||
183 | .Pp | ||
184 | The | ||
185 | .Fn iruserok | ||
186 | and | ||
187 | .Fn ruserok | ||
188 | functions take a remote host's IP address or name, respectively, | ||
189 | two user names and a flag indicating whether the local user's | ||
190 | name is that of the superuser. | ||
191 | Then, if the user is | ||
192 | .Em not | ||
193 | the superuser, it checks the | ||
194 | .Pa /etc/hosts.equiv | ||
195 | file. | ||
196 | If that lookup is not done, or is unsuccessful, the | ||
197 | .Pa .rhosts | ||
198 | in the local user's home directory is checked to see if the request for | ||
199 | service is allowed. | ||
200 | .Pp | ||
201 | If this file does not exist, is not a regular file, is owned by anyone | ||
202 | other than the user or the superuser, or is writeable by anyone other | ||
203 | than the owner, the check automatically fails. | ||
204 | Zero is returned if the machine name is listed in the | ||
205 | .Pa hosts.equiv | ||
206 | file, or the host and remote user name are found in the | ||
207 | .Pa .rhosts | ||
208 | file; otherwise | ||
209 | .Fn iruserok | ||
210 | and | ||
211 | .Fn ruserok | ||
212 | return \-1. | ||
213 | If the local domain (as obtained from | ||
214 | .Xr gethostname 3 ) | ||
215 | is the same as the remote domain, only the machine name need be specified. | ||
216 | .Pp | ||
217 | If the IP address of the remote host is known, | ||
218 | .Fn iruserok | ||
219 | should be used in preference to | ||
220 | .Fn ruserok , | ||
221 | as it does not require trusting the DNS server for the remote host's domain. | ||
222 | .Pp | ||
223 | While | ||
224 | .Fn iruserok | ||
225 | can handle IPv4 addresses only, | ||
226 | .Fn iruserok_sa | ||
227 | and | ||
228 | .Fn ruserok | ||
229 | can handle other address families as well, like IPv6. | ||
230 | The first argument of | ||
231 | .Fn iruserok_sa | ||
232 | is typed as | ||
233 | .Li "void *" | ||
234 | to avoid dependency between | ||
235 | .Aq Pa unistd.h | ||
236 | and | ||
237 | .Aq Pa sys/socket.h . | ||
238 | .Sh DIAGNOSTICS | ||
239 | The | ||
240 | .Fn rcmd | ||
241 | function returns a valid socket descriptor on success. | ||
242 | It returns \-1 on error and prints a diagnostic message on the standard error. | ||
243 | .Pp | ||
244 | The | ||
245 | .Fn rresvport | ||
246 | and | ||
247 | .Fn rresvport_af | ||
248 | functions return a valid, bound socket descriptor on success. | ||
249 | It returns \-1 on error with the global value | ||
250 | .Va errno | ||
251 | set according to the reason for failure. | ||
252 | The error code | ||
253 | .Er EAGAIN | ||
254 | is overloaded to mean | ||
255 | .Dq all network ports in use . | ||
256 | .Sh SEE ALSO | ||
257 | .Xr rsh 1 , | ||
258 | .Xr intro 2 , | ||
259 | .Xr bindresvport 3 , | ||
260 | .Xr bindresvport_sa 3 , | ||
261 | .Xr rcmdsh 3 , | ||
262 | .Xr rshd 8 | ||
263 | .Sh HISTORY | ||
264 | These | ||
265 | functions appeared in | ||
266 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/net/rcmd.c b/src/lib/libc/net/rcmd.c new file mode 100644 index 0000000000..769e85e0a4 --- /dev/null +++ b/src/lib/libc/net/rcmd.c | |||
@@ -0,0 +1,728 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. | ||
3 | * Copyright (c) 1983, 1993, 1994 | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #if defined(LIBC_SCCS) && !defined(lint) | ||
32 | static char *rcsid = "$OpenBSD: rcmd.c,v 1.48 2003/09/25 21:14:46 millert Exp $"; | ||
33 | #endif /* LIBC_SCCS and not lint */ | ||
34 | |||
35 | #include <sys/param.h> | ||
36 | #include <sys/socket.h> | ||
37 | #include <sys/stat.h> | ||
38 | |||
39 | #include <netinet/in.h> | ||
40 | #include <arpa/inet.h> | ||
41 | |||
42 | #include <signal.h> | ||
43 | #include <fcntl.h> | ||
44 | #include <netdb.h> | ||
45 | #include <unistd.h> | ||
46 | #include <pwd.h> | ||
47 | #include <errno.h> | ||
48 | #include <stdio.h> | ||
49 | #include <ctype.h> | ||
50 | #include <string.h> | ||
51 | #include <syslog.h> | ||
52 | #include <stdlib.h> | ||
53 | #include <netgroup.h> | ||
54 | |||
55 | int __ivaliduser(FILE *, in_addr_t, const char *, const char *); | ||
56 | int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t, | ||
57 | const char *, const char *); | ||
58 | static int __icheckhost(struct sockaddr *, socklen_t, const char *); | ||
59 | static char *__gethostloop(struct sockaddr *, socklen_t); | ||
60 | |||
61 | int | ||
62 | rcmd(ahost, rport, locuser, remuser, cmd, fd2p) | ||
63 | char **ahost; | ||
64 | in_port_t rport; | ||
65 | const char *locuser, *remuser, *cmd; | ||
66 | int *fd2p; | ||
67 | { | ||
68 | return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); | ||
69 | } | ||
70 | |||
71 | int | ||
72 | rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) | ||
73 | char **ahost; | ||
74 | in_port_t rport; | ||
75 | const char *locuser, *remuser, *cmd; | ||
76 | int *fd2p; | ||
77 | int af; | ||
78 | { | ||
79 | static char hbuf[MAXHOSTNAMELEN]; | ||
80 | char pbuf[NI_MAXSERV]; | ||
81 | struct addrinfo hints, *res, *r; | ||
82 | int error; | ||
83 | struct sockaddr_storage from; | ||
84 | fd_set *readsp = NULL; | ||
85 | sigset_t oldmask, mask; | ||
86 | pid_t pid; | ||
87 | int s, lport, timo; | ||
88 | char c, *p; | ||
89 | int refused; | ||
90 | |||
91 | /* call rcmdsh() with specified remote shell if appropriate. */ | ||
92 | if (!issetugid() && (p = getenv("RSH")) && *p) { | ||
93 | struct servent *sp = getservbyname("shell", "tcp"); | ||
94 | |||
95 | if (sp && sp->s_port == rport) | ||
96 | return (rcmdsh(ahost, rport, locuser, remuser, | ||
97 | cmd, p)); | ||
98 | } | ||
99 | |||
100 | /* use rsh(1) if non-root and remote port is shell. */ | ||
101 | if (geteuid()) { | ||
102 | struct servent *sp = getservbyname("shell", "tcp"); | ||
103 | |||
104 | if (sp && sp->s_port == rport) | ||
105 | return (rcmdsh(ahost, rport, locuser, remuser, | ||
106 | cmd, NULL)); | ||
107 | } | ||
108 | |||
109 | pid = getpid(); | ||
110 | snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); | ||
111 | memset(&hints, 0, sizeof(hints)); | ||
112 | hints.ai_family = af; | ||
113 | hints.ai_socktype = SOCK_STREAM; | ||
114 | hints.ai_flags = AI_CANONNAME; | ||
115 | error = getaddrinfo(*ahost, pbuf, &hints, &res); | ||
116 | if (error) { | ||
117 | #if 0 | ||
118 | warnx("%s: %s", *ahost, gai_strerror(error)); | ||
119 | #endif | ||
120 | return (-1); | ||
121 | } | ||
122 | if (res->ai_canonname) { | ||
123 | strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); | ||
124 | *ahost = hbuf; | ||
125 | } else | ||
126 | ; /*XXX*/ | ||
127 | |||
128 | r = res; | ||
129 | refused = 0; | ||
130 | sigemptyset(&mask); | ||
131 | sigaddset(&mask, SIGURG); | ||
132 | oldmask = sigprocmask(SIG_BLOCK, &mask, &oldmask); | ||
133 | for (timo = 1, lport = IPPORT_RESERVED - 1;;) { | ||
134 | s = rresvport_af(&lport, r->ai_family); | ||
135 | if (s < 0) { | ||
136 | if (errno == EAGAIN) | ||
137 | (void)fprintf(stderr, | ||
138 | "rcmd: socket: All ports in use\n"); | ||
139 | else | ||
140 | (void)fprintf(stderr, "rcmd: socket: %s\n", | ||
141 | strerror(errno)); | ||
142 | if (r->ai_next) { | ||
143 | r = r->ai_next; | ||
144 | continue; | ||
145 | } else { | ||
146 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
147 | freeaddrinfo(res); | ||
148 | return (-1); | ||
149 | } | ||
150 | } | ||
151 | fcntl(s, F_SETOWN, pid); | ||
152 | if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) | ||
153 | break; | ||
154 | (void)close(s); | ||
155 | if (errno == EADDRINUSE) { | ||
156 | lport--; | ||
157 | continue; | ||
158 | } | ||
159 | if (errno == ECONNREFUSED) | ||
160 | refused++; | ||
161 | if (r->ai_next) { | ||
162 | int oerrno = errno; | ||
163 | char hbuf[NI_MAXHOST]; | ||
164 | #ifdef NI_WITHSCOPEID | ||
165 | const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; | ||
166 | #else | ||
167 | const int niflags = NI_NUMERICHOST; | ||
168 | #endif | ||
169 | |||
170 | hbuf[0] = '\0'; | ||
171 | if (getnameinfo(r->ai_addr, r->ai_addrlen, | ||
172 | hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) | ||
173 | strlcpy(hbuf, "(invalid)", sizeof hbuf); | ||
174 | (void)fprintf(stderr, "connect to address %s: ", hbuf); | ||
175 | errno = oerrno; | ||
176 | perror(0); | ||
177 | r = r->ai_next; | ||
178 | hbuf[0] = '\0'; | ||
179 | if (getnameinfo(r->ai_addr, r->ai_addrlen, | ||
180 | hbuf, sizeof(hbuf), NULL, 0, niflags) != 0) | ||
181 | strlcpy(hbuf, "(invalid)", sizeof hbuf); | ||
182 | (void)fprintf(stderr, "Trying %s...\n", hbuf); | ||
183 | continue; | ||
184 | } | ||
185 | if (refused && timo <= 16) { | ||
186 | (void)sleep(timo); | ||
187 | timo *= 2; | ||
188 | r = res; | ||
189 | refused = 0; | ||
190 | continue; | ||
191 | } | ||
192 | (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, | ||
193 | strerror(errno)); | ||
194 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
195 | freeaddrinfo(res); | ||
196 | return (-1); | ||
197 | } | ||
198 | /* given "af" can be PF_UNSPEC, we need the real af for "s" */ | ||
199 | af = r->ai_family; | ||
200 | freeaddrinfo(res); | ||
201 | #if 0 | ||
202 | /* | ||
203 | * try to rresvport() to the same port. This will make rresvport() | ||
204 | * fail it's first bind, resulting in it choosing a random port. | ||
205 | */ | ||
206 | lport--; | ||
207 | #endif | ||
208 | if (fd2p == 0) { | ||
209 | write(s, "", 1); | ||
210 | lport = 0; | ||
211 | } else { | ||
212 | char num[8]; | ||
213 | int s2 = rresvport_af(&lport, af), s3; | ||
214 | socklen_t len = sizeof(from); | ||
215 | int fdssize = howmany(MAX(s, s2)+1, NFDBITS) * sizeof(fd_mask); | ||
216 | |||
217 | if (s2 < 0) | ||
218 | goto bad; | ||
219 | readsp = (fd_set *)malloc(fdssize); | ||
220 | if (readsp == NULL) | ||
221 | goto bad; | ||
222 | listen(s2, 1); | ||
223 | (void)snprintf(num, sizeof(num), "%d", lport); | ||
224 | if (write(s, num, strlen(num)+1) != strlen(num)+1) { | ||
225 | (void)fprintf(stderr, | ||
226 | "rcmd: write (setting up stderr): %s\n", | ||
227 | strerror(errno)); | ||
228 | (void)close(s2); | ||
229 | goto bad; | ||
230 | } | ||
231 | again: | ||
232 | bzero(readsp, fdssize); | ||
233 | FD_SET(s, readsp); | ||
234 | FD_SET(s2, readsp); | ||
235 | errno = 0; | ||
236 | if (select(MAX(s, s2) + 1, readsp, 0, 0, 0) < 1 || | ||
237 | !FD_ISSET(s2, readsp)) { | ||
238 | if (errno != 0) | ||
239 | (void)fprintf(stderr, | ||
240 | "rcmd: select (setting up stderr): %s\n", | ||
241 | strerror(errno)); | ||
242 | else | ||
243 | (void)fprintf(stderr, | ||
244 | "select: protocol failure in circuit setup\n"); | ||
245 | (void)close(s2); | ||
246 | goto bad; | ||
247 | } | ||
248 | s3 = accept(s2, (struct sockaddr *)&from, &len); | ||
249 | /* | ||
250 | * XXX careful for ftp bounce attacks. If discovered, shut them | ||
251 | * down and check for the real auxiliary channel to connect. | ||
252 | */ | ||
253 | switch (from.ss_family) { | ||
254 | case AF_INET: | ||
255 | case AF_INET6: | ||
256 | if (getnameinfo((struct sockaddr *)&from, len, | ||
257 | NULL, 0, num, sizeof(num), NI_NUMERICSERV) == 0 && | ||
258 | atoi(num) != 20) { | ||
259 | break; | ||
260 | } | ||
261 | close(s3); | ||
262 | goto again; | ||
263 | default: | ||
264 | break; | ||
265 | } | ||
266 | (void)close(s2); | ||
267 | if (s3 < 0) { | ||
268 | (void)fprintf(stderr, | ||
269 | "rcmd: accept: %s\n", strerror(errno)); | ||
270 | lport = 0; | ||
271 | goto bad; | ||
272 | } | ||
273 | *fd2p = s3; | ||
274 | switch (from.ss_family) { | ||
275 | case AF_INET: | ||
276 | case AF_INET6: | ||
277 | if (getnameinfo((struct sockaddr *)&from, len, | ||
278 | NULL, 0, num, sizeof(num), NI_NUMERICSERV) != 0 || | ||
279 | (atoi(num) >= IPPORT_RESERVED || | ||
280 | atoi(num) < IPPORT_RESERVED / 2)) { | ||
281 | (void)fprintf(stderr, | ||
282 | "socket: protocol failure in circuit setup.\n"); | ||
283 | goto bad2; | ||
284 | } | ||
285 | break; | ||
286 | default: | ||
287 | break; | ||
288 | } | ||
289 | } | ||
290 | (void)write(s, locuser, strlen(locuser)+1); | ||
291 | (void)write(s, remuser, strlen(remuser)+1); | ||
292 | (void)write(s, cmd, strlen(cmd)+1); | ||
293 | if (read(s, &c, 1) != 1) { | ||
294 | (void)fprintf(stderr, | ||
295 | "rcmd: %s: %s\n", *ahost, strerror(errno)); | ||
296 | goto bad2; | ||
297 | } | ||
298 | if (c != 0) { | ||
299 | while (read(s, &c, 1) == 1) { | ||
300 | (void)write(STDERR_FILENO, &c, 1); | ||
301 | if (c == '\n') | ||
302 | break; | ||
303 | } | ||
304 | goto bad2; | ||
305 | } | ||
306 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
307 | free(readsp); | ||
308 | return (s); | ||
309 | bad2: | ||
310 | if (lport) | ||
311 | (void)close(*fd2p); | ||
312 | bad: | ||
313 | if (readsp) | ||
314 | free(readsp); | ||
315 | (void)close(s); | ||
316 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | ||
317 | return (-1); | ||
318 | } | ||
319 | |||
320 | int __check_rhosts_file = 1; | ||
321 | char *__rcmd_errstr; | ||
322 | |||
323 | int | ||
324 | ruserok(rhost, superuser, ruser, luser) | ||
325 | const char *rhost, *ruser, *luser; | ||
326 | int superuser; | ||
327 | { | ||
328 | struct addrinfo hints, *res, *r; | ||
329 | int error; | ||
330 | |||
331 | memset(&hints, 0, sizeof(hints)); | ||
332 | hints.ai_family = PF_UNSPEC; | ||
333 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
334 | error = getaddrinfo(rhost, "0", &hints, &res); | ||
335 | if (error) | ||
336 | return (-1); | ||
337 | |||
338 | for (r = res; r; r = r->ai_next) { | ||
339 | if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, | ||
340 | luser) == 0) { | ||
341 | freeaddrinfo(res); | ||
342 | return (0); | ||
343 | } | ||
344 | } | ||
345 | freeaddrinfo(res); | ||
346 | return (-1); | ||
347 | } | ||
348 | |||
349 | /* | ||
350 | * New .rhosts strategy: We are passed an ip address. We spin through | ||
351 | * hosts.equiv and .rhosts looking for a match. When the .rhosts only | ||
352 | * has ip addresses, we don't have to trust a nameserver. When it | ||
353 | * contains hostnames, we spin through the list of addresses the nameserver | ||
354 | * gives us and look for a match. | ||
355 | * | ||
356 | * Returns 0 if ok, -1 if not ok. | ||
357 | */ | ||
358 | int | ||
359 | iruserok(raddr, superuser, ruser, luser) | ||
360 | u_int32_t raddr; | ||
361 | int superuser; | ||
362 | const char *ruser, *luser; | ||
363 | { | ||
364 | struct sockaddr_in sin; | ||
365 | |||
366 | memset(&sin, 0, sizeof(sin)); | ||
367 | sin.sin_family = AF_INET; | ||
368 | sin.sin_len = sizeof(struct sockaddr_in); | ||
369 | memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); | ||
370 | return iruserok_sa(&sin, sizeof(struct sockaddr_in), superuser, ruser, | ||
371 | luser); | ||
372 | } | ||
373 | |||
374 | int | ||
375 | iruserok_sa(raddr, rlen, superuser, ruser, luser) | ||
376 | const void *raddr; | ||
377 | int rlen; | ||
378 | int superuser; | ||
379 | const char *ruser, *luser; | ||
380 | { | ||
381 | struct sockaddr *sa; | ||
382 | register char *cp; | ||
383 | struct stat sbuf; | ||
384 | struct passwd *pwd; | ||
385 | FILE *hostf; | ||
386 | uid_t uid; | ||
387 | int first; | ||
388 | char pbuf[MAXPATHLEN]; | ||
389 | |||
390 | sa = (struct sockaddr *)raddr; | ||
391 | first = 1; | ||
392 | hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); | ||
393 | again: | ||
394 | if (hostf) { | ||
395 | if (__ivaliduser_sa(hostf, sa, rlen, luser, ruser) == 0) { | ||
396 | (void)fclose(hostf); | ||
397 | return (0); | ||
398 | } | ||
399 | (void)fclose(hostf); | ||
400 | } | ||
401 | if (first == 1 && (__check_rhosts_file || superuser)) { | ||
402 | first = 0; | ||
403 | if ((pwd = getpwnam(luser)) == NULL) | ||
404 | return (-1); | ||
405 | snprintf(pbuf, sizeof pbuf, "%s/.rhosts", pwd->pw_dir); | ||
406 | |||
407 | /* | ||
408 | * Change effective uid while opening .rhosts. If root and | ||
409 | * reading an NFS mounted file system, can't read files that | ||
410 | * are protected read/write owner only. | ||
411 | */ | ||
412 | uid = geteuid(); | ||
413 | (void)seteuid(pwd->pw_uid); | ||
414 | hostf = fopen(pbuf, "r"); | ||
415 | (void)seteuid(uid); | ||
416 | |||
417 | if (hostf == NULL) | ||
418 | return (-1); | ||
419 | /* | ||
420 | * If not a regular file, or is owned by someone other than | ||
421 | * user or root or if writeable by anyone but the owner, quit. | ||
422 | */ | ||
423 | cp = NULL; | ||
424 | if (lstat(pbuf, &sbuf) < 0) | ||
425 | cp = ".rhosts lstat failed"; | ||
426 | else if (!S_ISREG(sbuf.st_mode)) | ||
427 | cp = ".rhosts not regular file"; | ||
428 | else if (fstat(fileno(hostf), &sbuf) < 0) | ||
429 | cp = ".rhosts fstat failed"; | ||
430 | else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) | ||
431 | cp = "bad .rhosts owner"; | ||
432 | else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) | ||
433 | cp = ".rhosts writable by other than owner"; | ||
434 | /* If there were any problems, quit. */ | ||
435 | if (cp) { | ||
436 | __rcmd_errstr = cp; | ||
437 | (void)fclose(hostf); | ||
438 | return (-1); | ||
439 | } | ||
440 | goto again; | ||
441 | } | ||
442 | return (-1); | ||
443 | } | ||
444 | |||
445 | /* | ||
446 | * XXX | ||
447 | * Don't make static, used by lpd(8). | ||
448 | * | ||
449 | * Returns 0 if ok, -1 if not ok. | ||
450 | */ | ||
451 | int | ||
452 | __ivaliduser(hostf, raddrl, luser, ruser) | ||
453 | FILE *hostf; | ||
454 | in_addr_t raddrl; | ||
455 | const char *luser, *ruser; | ||
456 | { | ||
457 | struct sockaddr_in sin; | ||
458 | |||
459 | memset(&sin, 0, sizeof(sin)); | ||
460 | sin.sin_family = AF_INET; | ||
461 | sin.sin_len = sizeof(struct sockaddr_in); | ||
462 | memcpy(&sin.sin_addr, &raddrl, sizeof(sin.sin_addr)); | ||
463 | return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, | ||
464 | luser, ruser); | ||
465 | } | ||
466 | |||
467 | int | ||
468 | __ivaliduser_sa(hostf, raddr, salen, luser, ruser) | ||
469 | FILE *hostf; | ||
470 | struct sockaddr *raddr; | ||
471 | socklen_t salen; | ||
472 | const char *luser, *ruser; | ||
473 | { | ||
474 | register char *user, *p; | ||
475 | char *buf; | ||
476 | const char *auser, *ahost; | ||
477 | int hostok, userok; | ||
478 | char *rhost = (char *)-1; | ||
479 | char domain[MAXHOSTNAMELEN]; | ||
480 | size_t buflen; | ||
481 | |||
482 | getdomainname(domain, sizeof(domain)); | ||
483 | |||
484 | while ((buf = fgetln(hostf, &buflen))) { | ||
485 | p = buf; | ||
486 | if (*p == '#') | ||
487 | continue; | ||
488 | while (p < buf + buflen && *p != '\n' && *p != ' ' && *p != '\t') { | ||
489 | if (!isprint(*p)) | ||
490 | goto bail; | ||
491 | *p = isupper(*p) ? tolower(*p) : *p; | ||
492 | p++; | ||
493 | } | ||
494 | if (p >= buf + buflen) | ||
495 | continue; | ||
496 | if (*p == ' ' || *p == '\t') { | ||
497 | *p++ = '\0'; | ||
498 | while (p < buf + buflen && (*p == ' ' || *p == '\t')) | ||
499 | p++; | ||
500 | if (p >= buf + buflen) | ||
501 | continue; | ||
502 | user = p; | ||
503 | while (p < buf + buflen && *p != '\n' && *p != ' ' && | ||
504 | *p != '\t') { | ||
505 | if (!isprint(*p)) | ||
506 | goto bail; | ||
507 | p++; | ||
508 | } | ||
509 | } else | ||
510 | user = p; | ||
511 | *p = '\0'; | ||
512 | |||
513 | if (p == buf) | ||
514 | continue; | ||
515 | |||
516 | auser = *user ? user : luser; | ||
517 | ahost = buf; | ||
518 | |||
519 | if (strlen(ahost) >= MAXHOSTNAMELEN) | ||
520 | continue; | ||
521 | |||
522 | /* | ||
523 | * innetgr() must lookup a hostname (we do not attempt | ||
524 | * to change the semantics so that netgroups may have | ||
525 | * #.#.#.# addresses in the list.) | ||
526 | */ | ||
527 | if (ahost[0] == '+') | ||
528 | switch (ahost[1]) { | ||
529 | case '\0': | ||
530 | hostok = 1; | ||
531 | break; | ||
532 | case '@': | ||
533 | if (rhost == (char *)-1) | ||
534 | rhost = __gethostloop(raddr, salen); | ||
535 | hostok = 0; | ||
536 | if (rhost) | ||
537 | hostok = innetgr(&ahost[2], rhost, | ||
538 | NULL, domain); | ||
539 | break; | ||
540 | default: | ||
541 | hostok = __icheckhost(raddr, salen, &ahost[1]); | ||
542 | break; | ||
543 | } | ||
544 | else if (ahost[0] == '-') | ||
545 | switch (ahost[1]) { | ||
546 | case '\0': | ||
547 | hostok = -1; | ||
548 | break; | ||
549 | case '@': | ||
550 | if (rhost == (char *)-1) | ||
551 | rhost = __gethostloop(raddr, salen); | ||
552 | hostok = 0; | ||
553 | if (rhost) | ||
554 | hostok = -innetgr(&ahost[2], rhost, | ||
555 | NULL, domain); | ||
556 | break; | ||
557 | default: | ||
558 | hostok = -__icheckhost(raddr, salen, &ahost[1]); | ||
559 | break; | ||
560 | } | ||
561 | else | ||
562 | hostok = __icheckhost(raddr, salen, ahost); | ||
563 | |||
564 | |||
565 | if (auser[0] == '+') | ||
566 | switch (auser[1]) { | ||
567 | case '\0': | ||
568 | userok = 1; | ||
569 | break; | ||
570 | case '@': | ||
571 | userok = innetgr(&auser[2], NULL, ruser, | ||
572 | domain); | ||
573 | break; | ||
574 | default: | ||
575 | userok = strcmp(ruser, &auser[1]) ? 0 : 1; | ||
576 | break; | ||
577 | } | ||
578 | else if (auser[0] == '-') | ||
579 | switch (auser[1]) { | ||
580 | case '\0': | ||
581 | userok = -1; | ||
582 | break; | ||
583 | case '@': | ||
584 | userok = -innetgr(&auser[2], NULL, ruser, | ||
585 | domain); | ||
586 | break; | ||
587 | default: | ||
588 | userok = strcmp(ruser, &auser[1]) ? 0 : -1; | ||
589 | break; | ||
590 | } | ||
591 | else | ||
592 | userok = strcmp(ruser, auser) ? 0 : 1; | ||
593 | |||
594 | /* Check if one component did not match */ | ||
595 | if (hostok == 0 || userok == 0) | ||
596 | continue; | ||
597 | |||
598 | /* Check if we got a forbidden pair */ | ||
599 | if (userok <= -1 || hostok <= -1) | ||
600 | return (-1); | ||
601 | |||
602 | /* Check if we got a valid pair */ | ||
603 | if (hostok >= 1 && userok >= 1) | ||
604 | return (0); | ||
605 | } | ||
606 | bail: | ||
607 | return (-1); | ||
608 | } | ||
609 | |||
610 | /* | ||
611 | * Returns "true" if match, 0 if no match. If we do not find any | ||
612 | * semblance of an A->PTR->A loop, allow a simple #.#.#.# match to work. | ||
613 | * | ||
614 | * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion | ||
615 | * if af == AF_INET6. | ||
616 | */ | ||
617 | static int | ||
618 | __icheckhost(raddr, salen, lhost) | ||
619 | struct sockaddr *raddr; | ||
620 | socklen_t salen; | ||
621 | const char *lhost; | ||
622 | { | ||
623 | struct addrinfo hints, *res, *r; | ||
624 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
625 | int error; | ||
626 | #ifdef NI_WITHSCOPEID | ||
627 | const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; | ||
628 | #else | ||
629 | const int niflags = NI_NUMERICHOST; | ||
630 | #endif | ||
631 | |||
632 | h1[0] = '\0'; | ||
633 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
634 | niflags) != 0) | ||
635 | return (0); | ||
636 | |||
637 | /* Resolve laddr into sockaddr */ | ||
638 | memset(&hints, 0, sizeof(hints)); | ||
639 | hints.ai_family = raddr->sa_family; | ||
640 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
641 | res = NULL; | ||
642 | error = getaddrinfo(lhost, "0", &hints, &res); | ||
643 | if (error) | ||
644 | return (0); | ||
645 | |||
646 | /* | ||
647 | * Try string comparisons between raddr and laddr. | ||
648 | */ | ||
649 | for (r = res; r; r = r->ai_next) { | ||
650 | h2[0] = '\0'; | ||
651 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
652 | NULL, 0, niflags) != 0) | ||
653 | continue; | ||
654 | if (strcmp(h1, h2) == 0) { | ||
655 | freeaddrinfo(res); | ||
656 | return (1); | ||
657 | } | ||
658 | } | ||
659 | |||
660 | /* No match. */ | ||
661 | freeaddrinfo(res); | ||
662 | return (0); | ||
663 | } | ||
664 | |||
665 | /* | ||
666 | * Return the hostname associated with the supplied address. | ||
667 | * Do a reverse lookup as well for security. If a loop cannot | ||
668 | * be found, pack the result of inet_ntoa() into the string. | ||
669 | * | ||
670 | * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion | ||
671 | * if af == AF_INET6. | ||
672 | */ | ||
673 | static char * | ||
674 | __gethostloop(raddr, salen) | ||
675 | struct sockaddr *raddr; | ||
676 | socklen_t salen; | ||
677 | { | ||
678 | static char remotehost[NI_MAXHOST]; | ||
679 | char h1[NI_MAXHOST], h2[NI_MAXHOST]; | ||
680 | struct addrinfo hints, *res, *r; | ||
681 | int error; | ||
682 | #ifdef NI_WITHSCOPEID | ||
683 | const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; | ||
684 | #else | ||
685 | const int niflags = NI_NUMERICHOST; | ||
686 | #endif | ||
687 | |||
688 | h1[0] = remotehost[0] = '\0'; | ||
689 | if (getnameinfo(raddr, salen, remotehost, sizeof(remotehost), | ||
690 | NULL, 0, NI_NAMEREQD) != 0) | ||
691 | return (NULL); | ||
692 | if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, | ||
693 | niflags) != 0) | ||
694 | return (NULL); | ||
695 | |||
696 | /* | ||
697 | * Look up the name and check that the supplied | ||
698 | * address is in the list | ||
699 | */ | ||
700 | memset(&hints, 0, sizeof(hints)); | ||
701 | hints.ai_family = raddr->sa_family; | ||
702 | hints.ai_socktype = SOCK_DGRAM; /*dummy*/ | ||
703 | hints.ai_flags = AI_CANONNAME; | ||
704 | res = NULL; | ||
705 | error = getaddrinfo(remotehost, "0", &hints, &res); | ||
706 | if (error) | ||
707 | return (NULL); | ||
708 | |||
709 | for (r = res; r; r = r->ai_next) { | ||
710 | h2[0] = '\0'; | ||
711 | if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), | ||
712 | NULL, 0, niflags) != 0) | ||
713 | continue; | ||
714 | if (strcmp(h1, h2) == 0) { | ||
715 | freeaddrinfo(res); | ||
716 | return (remotehost); | ||
717 | } | ||
718 | } | ||
719 | |||
720 | /* | ||
721 | * either the DNS adminstrator has made a configuration | ||
722 | * mistake, or someone has attempted to spoof us | ||
723 | */ | ||
724 | syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", | ||
725 | h1, res->ai_canonname ? res->ai_canonname : remotehost); | ||
726 | freeaddrinfo(res); | ||
727 | return (NULL); | ||
728 | } | ||
diff --git a/src/lib/libc/net/rcmdsh.3 b/src/lib/libc/net/rcmdsh.3 new file mode 100644 index 0000000000..be566f09ca --- /dev/null +++ b/src/lib/libc/net/rcmdsh.3 | |||
@@ -0,0 +1,96 @@ | |||
1 | .\" $OpenBSD: rcmdsh.3,v 1.11 2003/06/02 20:18:35 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1983, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd May 5, 2003 | ||
31 | .Dt RCMDSH 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm rcmdsh | ||
35 | .Nd return a stream to a remote command without superuser | ||
36 | .Sh SYNOPSIS | ||
37 | .Fd #include <unistd.h> | ||
38 | .Ft int | ||
39 | .Fn rcmdsh "char **ahost" "int inport" "const char *locuser" "const char *remuser" "const char *cmd" "char *rshprog" | ||
40 | .Sh DESCRIPTION | ||
41 | The | ||
42 | .Fn rcmdsh | ||
43 | function is used by normal users to execute a command on a remote machine | ||
44 | using an authentication scheme based on reserved port numbers using | ||
45 | .Xr rsh 1 | ||
46 | or the value of | ||
47 | .Fa rshprog | ||
48 | (if non-null). | ||
49 | .Fa rshprog | ||
50 | may be a fully-qualified path, a non-qualified command, or a command containing | ||
51 | space-separated command line arguments. | ||
52 | .Pp | ||
53 | The | ||
54 | .Fn rcmdsh | ||
55 | function looks up the host | ||
56 | .Fa *ahost | ||
57 | using | ||
58 | .Xr gethostbyname 3 , | ||
59 | returning \-1 if the host does not exist. | ||
60 | Otherwise | ||
61 | .Fa *ahost | ||
62 | is set to the standard name of the host and a connection is established to | ||
63 | a server residing at the well-known Internet port | ||
64 | .Li shell/tcp | ||
65 | (or whatever port is used by | ||
66 | .Fa rshprog ) . | ||
67 | The parameter | ||
68 | .Fa inport | ||
69 | is ignored; it is only included to provide an interface similar to | ||
70 | .Xr rcmd 3 . | ||
71 | .Pp | ||
72 | If the connection succeeds, a socket in the | ||
73 | .Tn UNIX | ||
74 | domain of type | ||
75 | .Dv SOCK_STREAM | ||
76 | is returned to the caller, and given to the remote | ||
77 | command as stdin and stdout, and stderr. | ||
78 | .Sh DIAGNOSTICS | ||
79 | The | ||
80 | .Fn rcmdsh | ||
81 | function returns a valid socket descriptor on success. | ||
82 | It returns \-1 on error and prints a diagnostic message on the standard error. | ||
83 | .Sh SEE ALSO | ||
84 | .Xr rsh 1 , | ||
85 | .Xr socketpair 2 , | ||
86 | .Xr rcmd 3 , | ||
87 | .Xr rshd 8 | ||
88 | .Sh HISTORY | ||
89 | The | ||
90 | .Fn rcmdsh | ||
91 | function first appeared in | ||
92 | .Ox 2.0 . | ||
93 | .Sh BUGS | ||
94 | If | ||
95 | .Xr rsh 1 | ||
96 | encounters an error, a file descriptor is still returned instead of \-1. | ||
diff --git a/src/lib/libc/net/rcmdsh.c b/src/lib/libc/net/rcmdsh.c new file mode 100644 index 0000000000..2a2a5d77fe --- /dev/null +++ b/src/lib/libc/net/rcmdsh.c | |||
@@ -0,0 +1,192 @@ | |||
1 | /* $OpenBSD: rcmdsh.c,v 1.8 2003/05/05 22:13:03 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2001, MagniComp | ||
5 | * 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 | ||
14 | * the documentation and/or other materials provided with the distribution. | ||
15 | * 3. Neither the name of the MagniComp nor the names of its contributors may | ||
16 | * be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR | ||
23 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE | ||
28 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | /* | ||
32 | * This is an rcmd() replacement originally by | ||
33 | * Chris Siebenmann <cks@utcc.utoronto.ca>. | ||
34 | */ | ||
35 | |||
36 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | static char *rcsid = "$OpenBSD: rcmdsh.c,v 1.8 2003/05/05 22:13:03 millert Exp $"; | ||
38 | #endif /* LIBC_SCCS and not lint */ | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include <sys/socket.h> | ||
42 | #include <sys/wait.h> | ||
43 | #include <signal.h> | ||
44 | #include <errno.h> | ||
45 | #include <netdb.h> | ||
46 | #include <stdio.h> | ||
47 | #include <stdlib.h> | ||
48 | #include <string.h> | ||
49 | #include <pwd.h> | ||
50 | #include <paths.h> | ||
51 | #include <unistd.h> | ||
52 | |||
53 | /* | ||
54 | * This is a replacement rcmd() function that uses the rsh(1) | ||
55 | * program in place of a direct rcmd(3) function call so as to | ||
56 | * avoid having to be root. Note that rport is ignored. | ||
57 | */ | ||
58 | /* ARGSUSED */ | ||
59 | int | ||
60 | rcmdsh(ahost, rport, locuser, remuser, cmd, rshprog) | ||
61 | char **ahost; | ||
62 | int rport; | ||
63 | const char *locuser, *remuser, *cmd; | ||
64 | char *rshprog; | ||
65 | { | ||
66 | struct hostent *hp; | ||
67 | int sp[2]; | ||
68 | pid_t cpid; | ||
69 | char *p; | ||
70 | struct passwd *pw; | ||
71 | |||
72 | /* What rsh/shell to use. */ | ||
73 | if (rshprog == NULL) | ||
74 | rshprog = _PATH_RSH; | ||
75 | |||
76 | /* locuser must exist on this host. */ | ||
77 | if ((pw = getpwnam(locuser)) == NULL) { | ||
78 | (void) fprintf(stderr, "rcmdsh: unknown user: %s\n", locuser); | ||
79 | return(-1); | ||
80 | } | ||
81 | |||
82 | /* Validate remote hostname. */ | ||
83 | if (strcmp(*ahost, "localhost") != 0) { | ||
84 | if ((hp = gethostbyname(*ahost)) == NULL) { | ||
85 | herror(*ahost); | ||
86 | return(-1); | ||
87 | } | ||
88 | *ahost = hp->h_name; | ||
89 | } | ||
90 | |||
91 | /* Get a socketpair we'll use for stdin and stdout. */ | ||
92 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, sp) < 0) { | ||
93 | perror("rcmdsh: socketpair"); | ||
94 | return(-1); | ||
95 | } | ||
96 | |||
97 | cpid = fork(); | ||
98 | if (cpid < 0) { | ||
99 | perror("rcmdsh: fork failed"); | ||
100 | return(-1); | ||
101 | } else if (cpid == 0) { | ||
102 | /* | ||
103 | * Child. We use sp[1] to be stdin/stdout, and close sp[0]. | ||
104 | */ | ||
105 | (void) close(sp[0]); | ||
106 | if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { | ||
107 | perror("rcmdsh: dup2 failed"); | ||
108 | _exit(255); | ||
109 | } | ||
110 | /* Fork again to lose parent. */ | ||
111 | cpid = fork(); | ||
112 | if (cpid < 0) { | ||
113 | perror("rcmdsh: fork to lose parent failed"); | ||
114 | _exit(255); | ||
115 | } | ||
116 | if (cpid > 0) | ||
117 | _exit(0); | ||
118 | |||
119 | /* In grandchild here. Become local user for rshprog. */ | ||
120 | if (setuid(pw->pw_uid)) { | ||
121 | (void) fprintf(stderr, "rcmdsh: setuid(%u): %s\n", | ||
122 | pw->pw_uid, strerror(errno)); | ||
123 | _exit(255); | ||
124 | } | ||
125 | |||
126 | /* | ||
127 | * If remote host is "localhost" and local and remote user | ||
128 | * are the same, avoid running remote shell for efficiency. | ||
129 | */ | ||
130 | if (!strcmp(*ahost, "localhost") && !strcmp(locuser, remuser)) { | ||
131 | char *argv[4]; | ||
132 | if (pw->pw_shell[0] == '\0') | ||
133 | rshprog = _PATH_BSHELL; | ||
134 | else | ||
135 | rshprog = pw->pw_shell; | ||
136 | p = strrchr(rshprog, '/'); | ||
137 | argv[0] = p ? p + 1 : rshprog; | ||
138 | argv[1] = "-c"; | ||
139 | argv[2] = (char *)cmd; | ||
140 | argv[3] = NULL; | ||
141 | execvp(rshprog, argv); | ||
142 | } else if ((p = strchr(rshprog, ' ')) == NULL) { | ||
143 | /* simple case */ | ||
144 | char *argv[6]; | ||
145 | p = strrchr(rshprog, '/'); | ||
146 | argv[0] = p ? p + 1 : rshprog; | ||
147 | argv[1] = "-l"; | ||
148 | argv[2] = (char *)remuser; | ||
149 | argv[3] = *ahost; | ||
150 | argv[4] = (char *)cmd; | ||
151 | argv[5] = NULL; | ||
152 | execvp(rshprog, argv); | ||
153 | } else { | ||
154 | /* must pull args out of rshprog and dyn alloc argv */ | ||
155 | char **argv, **ap; | ||
156 | int n; | ||
157 | for (n = 7; (p = strchr(++p, ' ')) != NULL; n++) | ||
158 | continue; | ||
159 | rshprog = strdup(rshprog); | ||
160 | ap = argv = malloc(sizeof(char *) * n); | ||
161 | if (rshprog == NULL || argv == NULL) { | ||
162 | perror("rcmdsh"); | ||
163 | _exit(255); | ||
164 | } | ||
165 | while ((p = strsep(&rshprog, " ")) != NULL) { | ||
166 | if (*p == '\0') | ||
167 | continue; | ||
168 | *ap++ = p; | ||
169 | } | ||
170 | if (ap != argv) /* all spaces?!? */ | ||
171 | rshprog = argv[0]; | ||
172 | if ((p = strrchr(argv[0], '/')) != NULL) | ||
173 | argv[0] = p + 1; | ||
174 | *ap++ = "-l"; | ||
175 | *ap++ = (char *)remuser; | ||
176 | *ap++ = *ahost; | ||
177 | *ap++ = (char *)cmd; | ||
178 | *ap++ = NULL; | ||
179 | execvp(rshprog, argv); | ||
180 | } | ||
181 | (void) fprintf(stderr, "rcmdsh: execvp %s failed: %s\n", | ||
182 | rshprog, strerror(errno)); | ||
183 | _exit(255); | ||
184 | } else { | ||
185 | /* Parent. close sp[1], return sp[0]. */ | ||
186 | (void) close(sp[1]); | ||
187 | /* Reap child. */ | ||
188 | (void) wait(NULL); | ||
189 | return(sp[0]); | ||
190 | } | ||
191 | /* NOTREACHED */ | ||
192 | } | ||
diff --git a/src/lib/libc/net/recv.c b/src/lib/libc/net/recv.c new file mode 100644 index 0000000000..0947465377 --- /dev/null +++ b/src/lib/libc/net/recv.c | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: recv.c,v 1.3 2003/06/02 20:18:35 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | |||
37 | #include <stddef.h> | ||
38 | |||
39 | ssize_t | ||
40 | recv(s, buf, len, flags) | ||
41 | int s, flags; | ||
42 | size_t len; | ||
43 | void *buf; | ||
44 | { | ||
45 | return (recvfrom(s, buf, len, flags, NULL, 0)); | ||
46 | } | ||
diff --git a/src/lib/libc/net/res_comp.c b/src/lib/libc/net/res_comp.c new file mode 100644 index 0000000000..c10eb52931 --- /dev/null +++ b/src/lib/libc/net/res_comp.c | |||
@@ -0,0 +1,499 @@ | |||
1 | /* $OpenBSD: res_comp.c,v 1.11 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #if defined(LIBC_SCCS) && !defined(lint) | ||
55 | #if 0 | ||
56 | static char sccsid[] = "@(#)res_comp.c 8.1 (Berkeley) 6/4/93"; | ||
57 | static char rcsid[] = "$From: res_comp.c,v 8.11 1996/12/02 09:17:22 vixie Exp $"; | ||
58 | #else | ||
59 | static char rcsid[] = "$OpenBSD: res_comp.c,v 1.11 2003/06/02 20:18:36 millert Exp $"; | ||
60 | #endif | ||
61 | #endif /* LIBC_SCCS and not lint */ | ||
62 | |||
63 | #include <sys/types.h> | ||
64 | #include <sys/param.h> | ||
65 | #include <netinet/in.h> | ||
66 | #include <arpa/nameser.h> | ||
67 | |||
68 | #include <stdio.h> | ||
69 | #include <resolv.h> | ||
70 | #include <ctype.h> | ||
71 | |||
72 | #include <unistd.h> | ||
73 | #include <string.h> | ||
74 | |||
75 | static int dn_find(u_char *, u_char *, u_char **, u_char **); | ||
76 | |||
77 | /* | ||
78 | * Expand compressed domain name 'comp_dn' to full domain name. | ||
79 | * 'msg' is a pointer to the begining of the message, | ||
80 | * 'eomorig' points to the first location after the message, | ||
81 | * 'exp_dn' is a pointer to a buffer of size 'length' for the result. | ||
82 | * Return size of compressed name or -1 if there was an error. | ||
83 | */ | ||
84 | int | ||
85 | dn_expand(msg, eomorig, comp_dn, exp_dn, length) | ||
86 | const u_char *msg, *eomorig, *comp_dn; | ||
87 | char *exp_dn; | ||
88 | int length; | ||
89 | { | ||
90 | register const u_char *cp; | ||
91 | register char *dn; | ||
92 | register int n, c; | ||
93 | char *eom; | ||
94 | int len = -1, checked = 0; | ||
95 | |||
96 | dn = exp_dn; | ||
97 | cp = comp_dn; | ||
98 | if (length > MAXHOSTNAMELEN-1) | ||
99 | length = MAXHOSTNAMELEN-1; | ||
100 | eom = exp_dn + length; | ||
101 | /* | ||
102 | * fetch next label in domain name | ||
103 | */ | ||
104 | while ((n = *cp++)) { | ||
105 | /* | ||
106 | * Check for indirection | ||
107 | */ | ||
108 | switch (n & INDIR_MASK) { | ||
109 | case 0: | ||
110 | if (dn != exp_dn) { | ||
111 | if (dn >= eom) | ||
112 | return (-1); | ||
113 | *dn++ = '.'; | ||
114 | } | ||
115 | if (dn+n >= eom) | ||
116 | return (-1); | ||
117 | checked += n + 1; | ||
118 | while (--n >= 0) { | ||
119 | if (((c = *cp++) == '.') || (c == '\\')) { | ||
120 | if (dn + n + 2 >= eom) | ||
121 | return (-1); | ||
122 | *dn++ = '\\'; | ||
123 | } | ||
124 | *dn++ = c; | ||
125 | if (cp >= eomorig) /* out of range */ | ||
126 | return (-1); | ||
127 | } | ||
128 | break; | ||
129 | |||
130 | case INDIR_MASK: | ||
131 | if (len < 0) | ||
132 | len = cp - comp_dn + 1; | ||
133 | cp = msg + (((n & 0x3f) << 8) | (*cp & 0xff)); | ||
134 | if (cp < msg || cp >= eomorig) /* out of range */ | ||
135 | return (-1); | ||
136 | checked += 2; | ||
137 | /* | ||
138 | * Check for loops in the compressed name; | ||
139 | * if we've looked at the whole message, | ||
140 | * there must be a loop. | ||
141 | */ | ||
142 | if (checked >= eomorig - msg) | ||
143 | return (-1); | ||
144 | break; | ||
145 | |||
146 | default: | ||
147 | return (-1); /* flag error */ | ||
148 | } | ||
149 | } | ||
150 | *dn = '\0'; | ||
151 | if (len < 0) | ||
152 | len = cp - comp_dn; | ||
153 | return (len); | ||
154 | } | ||
155 | |||
156 | /* | ||
157 | * Compress domain name 'exp_dn' into 'comp_dn'. | ||
158 | * Return the size of the compressed name or -1. | ||
159 | * 'length' is the size of the array pointed to by 'comp_dn'. | ||
160 | * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] | ||
161 | * is a pointer to the beginning of the message. The list ends with NULL. | ||
162 | * 'lastdnptr' is a pointer to the end of the arrary pointed to | ||
163 | * by 'dnptrs'. Side effect is to update the list of pointers for | ||
164 | * labels inserted into the message as we compress the name. | ||
165 | * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' | ||
166 | * is NULL, we don't update the list. | ||
167 | */ | ||
168 | int | ||
169 | dn_comp(exp_dn, comp_dn, length, dnptrs, lastdnptr) | ||
170 | const char *exp_dn; | ||
171 | u_char *comp_dn, **dnptrs, **lastdnptr; | ||
172 | int length; | ||
173 | { | ||
174 | register u_char *cp, *dn; | ||
175 | register int c, l; | ||
176 | u_char **cpp, **lpp, *sp, *eob; | ||
177 | u_char *msg; | ||
178 | |||
179 | dn = (u_char *)exp_dn; | ||
180 | cp = comp_dn; | ||
181 | eob = cp + length; | ||
182 | lpp = cpp = NULL; | ||
183 | if (dnptrs != NULL) { | ||
184 | if ((msg = *dnptrs++) != NULL) { | ||
185 | for (cpp = dnptrs; *cpp != NULL; cpp++) | ||
186 | ; | ||
187 | lpp = cpp; /* end of list to search */ | ||
188 | } | ||
189 | } else | ||
190 | msg = NULL; | ||
191 | for (c = *dn++; c != '\0'; ) { | ||
192 | /* look to see if we can use pointers */ | ||
193 | if (msg != NULL) { | ||
194 | if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { | ||
195 | if (cp+1 >= eob) | ||
196 | return (-1); | ||
197 | *cp++ = (l >> 8) | INDIR_MASK; | ||
198 | *cp++ = l % 256; | ||
199 | return (cp - comp_dn); | ||
200 | } | ||
201 | /* not found, save it */ | ||
202 | if (lastdnptr != NULL && cpp < lastdnptr-1) { | ||
203 | *cpp++ = cp; | ||
204 | *cpp = NULL; | ||
205 | } | ||
206 | } | ||
207 | sp = cp++; /* save ptr to length byte */ | ||
208 | do { | ||
209 | if (c == '.') { | ||
210 | c = *dn++; | ||
211 | break; | ||
212 | } | ||
213 | if (c == '\\') { | ||
214 | if ((c = *dn++) == '\0') | ||
215 | break; | ||
216 | } | ||
217 | if (cp >= eob) { | ||
218 | if (msg != NULL) | ||
219 | *lpp = NULL; | ||
220 | return (-1); | ||
221 | } | ||
222 | *cp++ = c; | ||
223 | } while ((c = *dn++) != '\0'); | ||
224 | /* catch trailing '.'s but not '..' */ | ||
225 | if ((l = cp - sp - 1) == 0 && c == '\0') { | ||
226 | cp--; | ||
227 | break; | ||
228 | } | ||
229 | if (l <= 0 || l > MAXLABEL) { | ||
230 | if (msg != NULL) | ||
231 | *lpp = NULL; | ||
232 | return (-1); | ||
233 | } | ||
234 | *sp = l; | ||
235 | } | ||
236 | if (cp >= eob) { | ||
237 | if (msg != NULL) | ||
238 | *lpp = NULL; | ||
239 | return (-1); | ||
240 | } | ||
241 | *cp++ = '\0'; | ||
242 | return (cp - comp_dn); | ||
243 | } | ||
244 | |||
245 | /* | ||
246 | * Skip over a compressed domain name. Return the size or -1. | ||
247 | */ | ||
248 | int | ||
249 | __dn_skipname(comp_dn, eom) | ||
250 | const u_char *comp_dn, *eom; | ||
251 | { | ||
252 | register const u_char *cp; | ||
253 | register int n; | ||
254 | |||
255 | cp = comp_dn; | ||
256 | while (cp < eom && (n = *cp++)) { | ||
257 | /* | ||
258 | * check for indirection | ||
259 | */ | ||
260 | switch (n & INDIR_MASK) { | ||
261 | case 0: /* normal case, n == len */ | ||
262 | cp += n; | ||
263 | continue; | ||
264 | case INDIR_MASK: /* indirection */ | ||
265 | cp++; | ||
266 | break; | ||
267 | default: /* illegal type */ | ||
268 | return (-1); | ||
269 | } | ||
270 | break; | ||
271 | } | ||
272 | if (cp > eom) | ||
273 | return (-1); | ||
274 | return (cp - comp_dn); | ||
275 | } | ||
276 | |||
277 | static int | ||
278 | mklower(ch) | ||
279 | register int ch; | ||
280 | { | ||
281 | if (isascii(ch) && isupper(ch)) | ||
282 | return (tolower(ch)); | ||
283 | return (ch); | ||
284 | } | ||
285 | |||
286 | /* | ||
287 | * Search for expanded name from a list of previously compressed names. | ||
288 | * Return the offset from msg if found or -1. | ||
289 | * dnptrs is the pointer to the first name on the list, | ||
290 | * not the pointer to the start of the message. | ||
291 | */ | ||
292 | static int | ||
293 | dn_find(exp_dn, msg, dnptrs, lastdnptr) | ||
294 | u_char *exp_dn, *msg; | ||
295 | u_char **dnptrs, **lastdnptr; | ||
296 | { | ||
297 | register u_char *dn, *cp, **cpp; | ||
298 | register int n; | ||
299 | u_char *sp; | ||
300 | |||
301 | for (cpp = dnptrs; cpp < lastdnptr; cpp++) { | ||
302 | dn = exp_dn; | ||
303 | sp = cp = *cpp; | ||
304 | while ((n = *cp++)) { | ||
305 | /* | ||
306 | * check for indirection | ||
307 | */ | ||
308 | switch (n & INDIR_MASK) { | ||
309 | case 0: /* normal case, n == len */ | ||
310 | while (--n >= 0) { | ||
311 | if (*dn == '.') | ||
312 | goto next; | ||
313 | if (*dn == '\\') | ||
314 | dn++; | ||
315 | if (mklower(*dn++) != mklower(*cp++)) | ||
316 | goto next; | ||
317 | } | ||
318 | if ((n = *dn++) == '\0' && *cp == '\0') | ||
319 | return (sp - msg); | ||
320 | if (n == '.') | ||
321 | continue; | ||
322 | goto next; | ||
323 | |||
324 | case INDIR_MASK: /* indirection */ | ||
325 | cp = msg + (((n & 0x3f) << 8) | *cp); | ||
326 | break; | ||
327 | |||
328 | default: /* illegal type */ | ||
329 | return (-1); | ||
330 | } | ||
331 | } | ||
332 | if (*dn == '\0') | ||
333 | return (sp - msg); | ||
334 | next: ; | ||
335 | } | ||
336 | return (-1); | ||
337 | } | ||
338 | |||
339 | /* | ||
340 | * Verify that a domain name uses an acceptable character set. | ||
341 | */ | ||
342 | |||
343 | /* | ||
344 | * Note the conspicuous absence of ctype macros in these definitions. On | ||
345 | * non-ASCII hosts, we can't depend on string literals or ctype macros to | ||
346 | * tell us anything about network-format data. The rest of the BIND system | ||
347 | * is not careful about this, but for some reason, we're doing it right here. | ||
348 | */ | ||
349 | #define PERIOD 0x2e | ||
350 | #define hyphenchar(c) ((c) == 0x2d) | ||
351 | #define bslashchar(c) ((c) == 0x5c) | ||
352 | #define periodchar(c) ((c) == PERIOD) | ||
353 | #define asterchar(c) ((c) == 0x2a) | ||
354 | #define alphachar(c) (((c) >= 0x41 && (c) <= 0x5a) \ | ||
355 | || ((c) >= 0x61 && (c) <= 0x7a)) | ||
356 | #define digitchar(c) ((c) >= 0x30 && (c) <= 0x39) | ||
357 | |||
358 | #define borderchar(c) (alphachar(c) || digitchar(c)) | ||
359 | #define middlechar(c) (borderchar(c) || hyphenchar(c)) | ||
360 | #define domainchar(c) ((c) > 0x20 && (c) < 0x7f) | ||
361 | |||
362 | int | ||
363 | res_hnok(dn) | ||
364 | const char *dn; | ||
365 | { | ||
366 | int pch = PERIOD, ch = *dn++; | ||
367 | |||
368 | while (ch != '\0') { | ||
369 | int nch = *dn++; | ||
370 | |||
371 | if (periodchar(ch)) { | ||
372 | ; | ||
373 | } else if (periodchar(pch)) { | ||
374 | if (!borderchar(ch)) | ||
375 | return (0); | ||
376 | } else if (periodchar(nch) || nch == '\0') { | ||
377 | if (!borderchar(ch)) | ||
378 | return (0); | ||
379 | } else { | ||
380 | if (!middlechar(ch)) | ||
381 | return (0); | ||
382 | } | ||
383 | pch = ch, ch = nch; | ||
384 | } | ||
385 | return (1); | ||
386 | } | ||
387 | |||
388 | /* | ||
389 | * hostname-like (A, MX, WKS) owners can have "*" as their first label | ||
390 | * but must otherwise be as a host name. | ||
391 | */ | ||
392 | int | ||
393 | res_ownok(dn) | ||
394 | const char *dn; | ||
395 | { | ||
396 | if (asterchar(dn[0])) { | ||
397 | if (periodchar(dn[1])) | ||
398 | return (res_hnok(dn+2)); | ||
399 | if (dn[1] == '\0') | ||
400 | return (1); | ||
401 | } | ||
402 | return (res_hnok(dn)); | ||
403 | } | ||
404 | |||
405 | /* | ||
406 | * SOA RNAMEs and RP RNAMEs can have any printable character in their first | ||
407 | * label, but the rest of the name has to look like a host name. | ||
408 | */ | ||
409 | int | ||
410 | res_mailok(dn) | ||
411 | const char *dn; | ||
412 | { | ||
413 | int ch, escaped = 0; | ||
414 | |||
415 | /* "." is a valid missing representation */ | ||
416 | if (*dn == '\0') | ||
417 | return(1); | ||
418 | |||
419 | /* otherwise <label>.<hostname> */ | ||
420 | while ((ch = *dn++) != '\0') { | ||
421 | if (!domainchar(ch)) | ||
422 | return (0); | ||
423 | if (!escaped && periodchar(ch)) | ||
424 | break; | ||
425 | if (escaped) | ||
426 | escaped = 0; | ||
427 | else if (bslashchar(ch)) | ||
428 | escaped = 1; | ||
429 | } | ||
430 | if (periodchar(ch)) | ||
431 | return (res_hnok(dn)); | ||
432 | return(0); | ||
433 | } | ||
434 | |||
435 | /* | ||
436 | * This function is quite liberal, since RFC 1034's character sets are only | ||
437 | * recommendations. | ||
438 | */ | ||
439 | int | ||
440 | res_dnok(dn) | ||
441 | const char *dn; | ||
442 | { | ||
443 | int ch; | ||
444 | |||
445 | while ((ch = *dn++) != '\0') | ||
446 | if (!domainchar(ch)) | ||
447 | return (0); | ||
448 | return (1); | ||
449 | } | ||
450 | |||
451 | /* | ||
452 | * Routines to insert/extract short/long's. | ||
453 | */ | ||
454 | |||
455 | u_int16_t | ||
456 | _getshort(msgp) | ||
457 | register const u_char *msgp; | ||
458 | { | ||
459 | register u_int16_t u; | ||
460 | |||
461 | GETSHORT(u, msgp); | ||
462 | return (u); | ||
463 | } | ||
464 | |||
465 | #ifdef NeXT | ||
466 | /* | ||
467 | * nExt machines have some funky library conventions, which we must maintain. | ||
468 | */ | ||
469 | u_int16_t | ||
470 | res_getshort(msgp) | ||
471 | register const u_char *msgp; | ||
472 | { | ||
473 | return (_getshort(msgp)); | ||
474 | } | ||
475 | #endif | ||
476 | |||
477 | u_int32_t | ||
478 | _getlong(msgp) | ||
479 | register const u_char *msgp; | ||
480 | { | ||
481 | register u_int32_t u; | ||
482 | |||
483 | GETLONG(u, msgp); | ||
484 | return (u); | ||
485 | } | ||
486 | |||
487 | void | ||
488 | __putshort(register u_int16_t s, register u_char *msgp) | ||
489 | { | ||
490 | PUTSHORT(s, msgp); | ||
491 | } | ||
492 | |||
493 | void | ||
494 | __putlong(l, msgp) | ||
495 | register u_int32_t l; | ||
496 | register u_char *msgp; | ||
497 | { | ||
498 | PUTLONG(l, msgp); | ||
499 | } | ||
diff --git a/src/lib/libc/net/res_data.c b/src/lib/libc/net/res_data.c new file mode 100644 index 0000000000..967264bcb7 --- /dev/null +++ b/src/lib/libc/net/res_data.c | |||
@@ -0,0 +1,113 @@ | |||
1 | /* $OpenBSD: res_data.c,v 1.2 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1995 | ||
5 | * - | ||
6 | * Copyright (c) 1995 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #if defined(LIBC_SCCS) && !defined(lint) | ||
55 | #if 0 | ||
56 | static char rcsid[] = "$From: res_data.c,v 8.2 1996/08/05 08:31:35 vixie Exp $"; | ||
57 | #else | ||
58 | static char rcsid[] = "$OpenBSD: res_data.c,v 1.2 2003/06/02 20:18:36 millert Exp $"; | ||
59 | #endif | ||
60 | #endif /* LIBC_SCCS and not lint */ | ||
61 | |||
62 | #include <sys/types.h> | ||
63 | #include <sys/param.h> | ||
64 | #include <sys/socket.h> | ||
65 | #include <sys/time.h> | ||
66 | #include <netinet/in.h> | ||
67 | #include <arpa/inet.h> | ||
68 | #include <arpa/nameser.h> | ||
69 | |||
70 | #include <stdio.h> | ||
71 | #include <ctype.h> | ||
72 | #include <resolv.h> | ||
73 | #include <unistd.h> | ||
74 | #include <stdlib.h> | ||
75 | #include <string.h> | ||
76 | |||
77 | const char *_res_opcodes[] = { | ||
78 | "QUERY", | ||
79 | "IQUERY", | ||
80 | "CQUERYM", | ||
81 | "CQUERYU", /* experimental */ | ||
82 | "NOTIFY", /* experimental */ | ||
83 | "5", | ||
84 | "6", | ||
85 | "7", | ||
86 | "8", | ||
87 | "UPDATEA", | ||
88 | "UPDATED", | ||
89 | "UPDATEDA", | ||
90 | "UPDATEM", | ||
91 | "UPDATEMA", | ||
92 | "ZONEINIT", | ||
93 | "ZONEREF", | ||
94 | }; | ||
95 | |||
96 | const char *_res_resultcodes[] = { | ||
97 | "NOERROR", | ||
98 | "FORMERR", | ||
99 | "SERVFAIL", | ||
100 | "NXDOMAIN", | ||
101 | "NOTIMP", | ||
102 | "REFUSED", | ||
103 | "6", | ||
104 | "7", | ||
105 | "8", | ||
106 | "9", | ||
107 | "10", | ||
108 | "11", | ||
109 | "12", | ||
110 | "13", | ||
111 | "14", | ||
112 | "NOCHANGE", | ||
113 | }; | ||
diff --git a/src/lib/libc/net/res_debug.c b/src/lib/libc/net/res_debug.c new file mode 100644 index 0000000000..8f66c7d009 --- /dev/null +++ b/src/lib/libc/net/res_debug.c | |||
@@ -0,0 +1,1561 @@ | |||
1 | /* $OpenBSD: res_debug.c,v 1.17 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1990, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1990, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * Portions Copyright (c) 1995 by International Business Machines, Inc. | ||
52 | * | ||
53 | * International Business Machines, Inc. (hereinafter called IBM) grants | ||
54 | * permission under its copyrights to use, copy, modify, and distribute this | ||
55 | * Software with or without fee, provided that the above copyright notice and | ||
56 | * all paragraphs of this notice appear in all copies, and that the name of IBM | ||
57 | * not be used in connection with the marketing of any product incorporating | ||
58 | * the Software or modifications thereof, without specific, written prior | ||
59 | * permission. | ||
60 | * | ||
61 | * To the extent it has a right to do so, IBM grants an immunity from suit | ||
62 | * under its patents, if any, for the use, sale or manufacture of products to | ||
63 | * the extent that such products are used for performing Domain Name System | ||
64 | * dynamic updates in TCP/IP networks by means of the Software. No immunity is | ||
65 | * granted for any product per se or for any other function of any product. | ||
66 | * | ||
67 | * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, | ||
68 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A | ||
69 | * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, | ||
70 | * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING | ||
71 | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN | ||
72 | * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. | ||
73 | * --Copyright-- | ||
74 | */ | ||
75 | |||
76 | #if defined(LIBC_SCCS) && !defined(lint) | ||
77 | #if 0 | ||
78 | static char sccsid[] = "@(#)res_debug.c 8.1 (Berkeley) 6/4/93"; | ||
79 | static char rcsid[] = "$From: res_debug.c,v 8.19 1996/11/26 10:11:23 vixie Exp $"; | ||
80 | #else | ||
81 | static char rcsid[] = "$OpenBSD: res_debug.c,v 1.17 2003/06/02 20:18:36 millert Exp $"; | ||
82 | #endif | ||
83 | #endif /* LIBC_SCCS and not lint */ | ||
84 | |||
85 | #include <sys/param.h> | ||
86 | #include <sys/types.h> | ||
87 | #include <sys/socket.h> | ||
88 | #include <netinet/in.h> | ||
89 | #include <arpa/inet.h> | ||
90 | #include <arpa/nameser.h> | ||
91 | |||
92 | #include <ctype.h> | ||
93 | #include <netdb.h> | ||
94 | #include <resolv.h> | ||
95 | #include <stdio.h> | ||
96 | #include <time.h> | ||
97 | |||
98 | #include <stdlib.h> | ||
99 | #include <string.h> | ||
100 | |||
101 | #include "thread_private.h" | ||
102 | |||
103 | extern const char *_res_opcodes[]; | ||
104 | extern const char *_res_resultcodes[]; | ||
105 | |||
106 | static const char *loc_ntoal(const u_char *binary, char *ascii, int ascii_len); | ||
107 | |||
108 | /* XXX: we should use getservbyport() instead. */ | ||
109 | static const char * | ||
110 | dewks(wks) | ||
111 | int wks; | ||
112 | { | ||
113 | static char nbuf[20]; | ||
114 | |||
115 | switch (wks) { | ||
116 | case 5: return "rje"; | ||
117 | case 7: return "echo"; | ||
118 | case 9: return "discard"; | ||
119 | case 11: return "systat"; | ||
120 | case 13: return "daytime"; | ||
121 | case 15: return "netstat"; | ||
122 | case 17: return "qotd"; | ||
123 | case 19: return "chargen"; | ||
124 | case 20: return "ftp-data"; | ||
125 | case 21: return "ftp"; | ||
126 | case 23: return "telnet"; | ||
127 | case 25: return "smtp"; | ||
128 | case 37: return "time"; | ||
129 | case 39: return "rlp"; | ||
130 | case 42: return "name"; | ||
131 | case 43: return "whois"; | ||
132 | case 53: return "domain"; | ||
133 | case 57: return "apts"; | ||
134 | case 59: return "apfs"; | ||
135 | case 67: return "bootps"; | ||
136 | case 68: return "bootpc"; | ||
137 | case 69: return "tftp"; | ||
138 | case 77: return "rje"; | ||
139 | case 79: return "finger"; | ||
140 | case 87: return "link"; | ||
141 | case 95: return "supdup"; | ||
142 | case 100: return "newacct"; | ||
143 | case 101: return "hostnames"; | ||
144 | case 102: return "iso-tsap"; | ||
145 | case 103: return "x400"; | ||
146 | case 104: return "x400-snd"; | ||
147 | case 105: return "csnet-ns"; | ||
148 | case 109: return "pop-2"; | ||
149 | case 111: return "sunrpc"; | ||
150 | case 113: return "auth"; | ||
151 | case 115: return "sftp"; | ||
152 | case 117: return "uucp-path"; | ||
153 | case 119: return "nntp"; | ||
154 | case 121: return "erpc"; | ||
155 | case 123: return "ntp"; | ||
156 | case 133: return "statsrv"; | ||
157 | case 136: return "profile"; | ||
158 | case 144: return "NeWS"; | ||
159 | case 161: return "snmp"; | ||
160 | case 162: return "snmp-trap"; | ||
161 | case 170: return "print-srv"; | ||
162 | default: | ||
163 | (void) snprintf(nbuf, sizeof nbuf, "%d", wks); | ||
164 | return (nbuf); | ||
165 | } | ||
166 | } | ||
167 | |||
168 | /* XXX: we should use getprotobynumber() instead. */ | ||
169 | static const char * | ||
170 | deproto(protonum) | ||
171 | int protonum; | ||
172 | { | ||
173 | static char nbuf[20]; | ||
174 | |||
175 | switch (protonum) { | ||
176 | case 1: return "icmp"; | ||
177 | case 2: return "igmp"; | ||
178 | case 3: return "ggp"; | ||
179 | case 5: return "st"; | ||
180 | case 6: return "tcp"; | ||
181 | case 7: return "ucl"; | ||
182 | case 8: return "egp"; | ||
183 | case 9: return "igp"; | ||
184 | case 11: return "nvp-II"; | ||
185 | case 12: return "pup"; | ||
186 | case 16: return "chaos"; | ||
187 | case 17: return "udp"; | ||
188 | default: | ||
189 | (void) snprintf(nbuf, sizeof nbuf, "%d", protonum); | ||
190 | return (nbuf); | ||
191 | } | ||
192 | } | ||
193 | |||
194 | static const u_char * | ||
195 | do_rrset(msg, len, cp, cnt, pflag, file, hs) | ||
196 | int cnt, pflag, len; | ||
197 | const u_char *cp, *msg; | ||
198 | const char *hs; | ||
199 | FILE *file; | ||
200 | { | ||
201 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
202 | int n; | ||
203 | int sflag; | ||
204 | |||
205 | /* | ||
206 | * Print answer records. | ||
207 | */ | ||
208 | sflag = (_resp->pfcode & pflag); | ||
209 | if ((n = ntohs(cnt))) { | ||
210 | if ((!_resp->pfcode) || | ||
211 | ((sflag) && (_resp->pfcode & RES_PRF_HEAD1))) | ||
212 | fprintf(file, "%s", hs); | ||
213 | while (--n >= 0) { | ||
214 | if ((!_resp->pfcode) || sflag) { | ||
215 | cp = p_rr(cp, msg, file); | ||
216 | } else { | ||
217 | unsigned int dlen; | ||
218 | cp += __dn_skipname(cp, cp + MAXCDNAME); | ||
219 | cp += INT16SZ; | ||
220 | cp += INT16SZ; | ||
221 | cp += INT32SZ; | ||
222 | dlen = _getshort((u_char*)cp); | ||
223 | cp += INT16SZ; | ||
224 | cp += dlen; | ||
225 | } | ||
226 | if ((cp - msg) > len) | ||
227 | return (NULL); | ||
228 | } | ||
229 | if ((!_resp->pfcode) || | ||
230 | ((sflag) && (_resp->pfcode & RES_PRF_HEAD1))) | ||
231 | putc('\n', file); | ||
232 | } | ||
233 | return (cp); | ||
234 | } | ||
235 | |||
236 | void | ||
237 | __p_query(msg) | ||
238 | const u_char *msg; | ||
239 | { | ||
240 | __fp_query(msg, stdout); | ||
241 | } | ||
242 | |||
243 | /* | ||
244 | * Print the current options. | ||
245 | * This is intended to be primarily a debugging routine. | ||
246 | */ | ||
247 | void | ||
248 | __fp_resstat(statp, file) | ||
249 | struct __res_state *statp; | ||
250 | FILE *file; | ||
251 | { | ||
252 | register u_long mask; | ||
253 | |||
254 | fprintf(file, ";; res options:"); | ||
255 | if (!statp) | ||
256 | statp = &_res; | ||
257 | for (mask = 1; mask != 0; mask <<= 1) | ||
258 | if (statp->options & mask) | ||
259 | fprintf(file, " %s", p_option(mask)); | ||
260 | putc('\n', file); | ||
261 | } | ||
262 | |||
263 | /* | ||
264 | * Print the contents of a query. | ||
265 | * This is intended to be primarily a debugging routine. | ||
266 | */ | ||
267 | void | ||
268 | __fp_nquery(msg, len, file) | ||
269 | const u_char *msg; | ||
270 | int len; | ||
271 | FILE *file; | ||
272 | { | ||
273 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
274 | register const u_char *cp, *endMark; | ||
275 | register const HEADER *hp; | ||
276 | register int n; | ||
277 | |||
278 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
279 | return; | ||
280 | |||
281 | #define TruncTest(x) if (x > endMark) goto trunc | ||
282 | #define ErrorTest(x) if (x == NULL) goto error | ||
283 | |||
284 | /* | ||
285 | * Print header fields. | ||
286 | */ | ||
287 | hp = (HEADER *)msg; | ||
288 | cp = msg + HFIXEDSZ; | ||
289 | endMark = msg + len; | ||
290 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX) || hp->rcode) { | ||
291 | fprintf(file, ";; ->>HEADER<<- opcode: %s, status: %s, id: %u", | ||
292 | _res_opcodes[hp->opcode], | ||
293 | _res_resultcodes[hp->rcode], | ||
294 | ntohs(hp->id)); | ||
295 | putc('\n', file); | ||
296 | } | ||
297 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEADX)) | ||
298 | putc(';', file); | ||
299 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD2)) { | ||
300 | fprintf(file, "; flags:"); | ||
301 | if (hp->qr) | ||
302 | fprintf(file, " qr"); | ||
303 | if (hp->aa) | ||
304 | fprintf(file, " aa"); | ||
305 | if (hp->tc) | ||
306 | fprintf(file, " tc"); | ||
307 | if (hp->rd) | ||
308 | fprintf(file, " rd"); | ||
309 | if (hp->ra) | ||
310 | fprintf(file, " ra"); | ||
311 | if (hp->unused) | ||
312 | fprintf(file, " UNUSED-BIT-ON"); | ||
313 | if (hp->ad) | ||
314 | fprintf(file, " ad"); | ||
315 | if (hp->cd) | ||
316 | fprintf(file, " cd"); | ||
317 | } | ||
318 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_HEAD1)) { | ||
319 | fprintf(file, "; Ques: %u", ntohs(hp->qdcount)); | ||
320 | fprintf(file, ", Ans: %u", ntohs(hp->ancount)); | ||
321 | fprintf(file, ", Auth: %u", ntohs(hp->nscount)); | ||
322 | fprintf(file, ", Addit: %u", ntohs(hp->arcount)); | ||
323 | } | ||
324 | if ((!_resp->pfcode) || (_resp->pfcode & | ||
325 | (RES_PRF_HEADX | RES_PRF_HEAD2 | RES_PRF_HEAD1))) { | ||
326 | putc('\n',file); | ||
327 | } | ||
328 | /* | ||
329 | * Print question records. | ||
330 | */ | ||
331 | if ((n = ntohs(hp->qdcount))) { | ||
332 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
333 | fprintf(file, ";; QUESTIONS:\n"); | ||
334 | while (--n >= 0) { | ||
335 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
336 | fprintf(file, ";;\t"); | ||
337 | TruncTest(cp); | ||
338 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
339 | cp = p_cdnname(cp, msg, len, file); | ||
340 | else { | ||
341 | int n; | ||
342 | char name[MAXDNAME]; | ||
343 | |||
344 | if ((n = dn_expand(msg, msg+len, cp, name, | ||
345 | sizeof name)) < 0) | ||
346 | cp = NULL; | ||
347 | else | ||
348 | cp += n; | ||
349 | } | ||
350 | ErrorTest(cp); | ||
351 | TruncTest(cp); | ||
352 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
353 | fprintf(file, ", type = %s", | ||
354 | __p_type(_getshort((u_char*)cp))); | ||
355 | cp += INT16SZ; | ||
356 | TruncTest(cp); | ||
357 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
358 | fprintf(file, ", class = %s\n", | ||
359 | __p_class(_getshort((u_char*)cp))); | ||
360 | cp += INT16SZ; | ||
361 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_QUES)) | ||
362 | putc('\n', file); | ||
363 | } | ||
364 | } | ||
365 | /* | ||
366 | * Print authoritative answer records | ||
367 | */ | ||
368 | TruncTest(cp); | ||
369 | cp = do_rrset(msg, len, cp, hp->ancount, RES_PRF_ANS, file, | ||
370 | ";; ANSWERS:\n"); | ||
371 | ErrorTest(cp); | ||
372 | |||
373 | /* | ||
374 | * print name server records | ||
375 | */ | ||
376 | TruncTest(cp); | ||
377 | cp = do_rrset(msg, len, cp, hp->nscount, RES_PRF_AUTH, file, | ||
378 | ";; AUTHORITY RECORDS:\n"); | ||
379 | ErrorTest(cp); | ||
380 | |||
381 | TruncTest(cp); | ||
382 | /* | ||
383 | * print additional records | ||
384 | */ | ||
385 | cp = do_rrset(msg, len, cp, hp->arcount, RES_PRF_ADD, file, | ||
386 | ";; ADDITIONAL RECORDS:\n"); | ||
387 | ErrorTest(cp); | ||
388 | return; | ||
389 | trunc: | ||
390 | fprintf(file, "\n;; ...truncated\n"); | ||
391 | return; | ||
392 | error: | ||
393 | fprintf(file, "\n;; ...malformed\n"); | ||
394 | } | ||
395 | |||
396 | void | ||
397 | __fp_query(msg, file) | ||
398 | const u_char *msg; | ||
399 | FILE *file; | ||
400 | { | ||
401 | fp_nquery(msg, PACKETSZ, file); | ||
402 | } | ||
403 | |||
404 | const u_char * | ||
405 | __p_cdnname(cp, msg, len, file) | ||
406 | const u_char *cp, *msg; | ||
407 | int len; | ||
408 | FILE *file; | ||
409 | { | ||
410 | char name[MAXDNAME]; | ||
411 | int n; | ||
412 | |||
413 | if ((n = dn_expand(msg, msg + len, cp, name, sizeof name)) < 0) | ||
414 | return (NULL); | ||
415 | if (name[0] == '\0') | ||
416 | putc('.', file); | ||
417 | else | ||
418 | fputs(name, file); | ||
419 | return (cp + n); | ||
420 | } | ||
421 | |||
422 | const u_char * | ||
423 | __p_cdname(cp, msg, file) | ||
424 | const u_char *cp, *msg; | ||
425 | FILE *file; | ||
426 | { | ||
427 | return (p_cdnname(cp, msg, PACKETSZ, file)); | ||
428 | } | ||
429 | |||
430 | |||
431 | /* Return a fully-qualified domain name from a compressed name (with | ||
432 | length supplied). */ | ||
433 | |||
434 | const u_char * | ||
435 | __p_fqnname(cp, msg, msglen, name, namelen) | ||
436 | const u_char *cp, *msg; | ||
437 | int msglen; | ||
438 | char *name; | ||
439 | int namelen; | ||
440 | { | ||
441 | int n, newlen; | ||
442 | |||
443 | if ((n = dn_expand(msg, cp + msglen, cp, name, namelen)) < 0) | ||
444 | return (NULL); | ||
445 | newlen = strlen(name); | ||
446 | if (newlen == 0 || name[newlen - 1] != '.') { | ||
447 | if (newlen + 1 >= namelen) /* Lack space for final dot */ | ||
448 | return (NULL); | ||
449 | else | ||
450 | strlcpy(name + newlen, ".", namelen - newlen); | ||
451 | } | ||
452 | return (cp + n); | ||
453 | } | ||
454 | |||
455 | /* XXX: the rest of these functions need to become length-limited, too. (vix) | ||
456 | */ | ||
457 | |||
458 | const u_char * | ||
459 | __p_fqname(cp, msg, file) | ||
460 | const u_char *cp, *msg; | ||
461 | FILE *file; | ||
462 | { | ||
463 | char name[MAXDNAME]; | ||
464 | const u_char *n; | ||
465 | |||
466 | n = __p_fqnname(cp, msg, MAXCDNAME, name, sizeof name); | ||
467 | if (n == NULL) | ||
468 | return (NULL); | ||
469 | fputs(name, file); | ||
470 | return (n); | ||
471 | } | ||
472 | |||
473 | /* | ||
474 | * Print resource record fields in human readable form. | ||
475 | */ | ||
476 | const u_char * | ||
477 | __p_rr(cp, msg, file) | ||
478 | const u_char *cp, *msg; | ||
479 | FILE *file; | ||
480 | { | ||
481 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
482 | int type, class, dlen, n, c; | ||
483 | struct in_addr inaddr; | ||
484 | const u_char *cp1, *cp2; | ||
485 | u_int32_t tmpttl, t; | ||
486 | int lcnt; | ||
487 | u_int16_t keyflags; | ||
488 | char rrname[MAXDNAME]; /* The fqdn of this RR */ | ||
489 | char base64_key[MAX_KEY_BASE64]; | ||
490 | |||
491 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
492 | h_errno = NETDB_INTERNAL; | ||
493 | return (NULL); | ||
494 | } | ||
495 | cp = __p_fqnname(cp, msg, MAXCDNAME, rrname, sizeof rrname); | ||
496 | if (!cp) | ||
497 | return (NULL); /* compression error */ | ||
498 | fputs(rrname, file); | ||
499 | |||
500 | type = _getshort((u_char*)cp); | ||
501 | cp += INT16SZ; | ||
502 | class = _getshort((u_char*)cp); | ||
503 | cp += INT16SZ; | ||
504 | tmpttl = _getlong((u_char*)cp); | ||
505 | cp += INT32SZ; | ||
506 | dlen = _getshort((u_char*)cp); | ||
507 | cp += INT16SZ; | ||
508 | cp1 = cp; | ||
509 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_TTLID)) | ||
510 | fprintf(file, "\t%lu", (u_long)tmpttl); | ||
511 | if ((!_resp->pfcode) || (_resp->pfcode & RES_PRF_CLASS)) | ||
512 | fprintf(file, "\t%s", __p_class(class)); | ||
513 | fprintf(file, "\t%s", __p_type(type)); | ||
514 | /* | ||
515 | * Print type specific data, if appropriate | ||
516 | */ | ||
517 | switch (type) { | ||
518 | case T_A: | ||
519 | switch (class) { | ||
520 | case C_IN: | ||
521 | case C_HS: | ||
522 | bcopy(cp, (char *)&inaddr, INADDRSZ); | ||
523 | if (dlen == 4) { | ||
524 | fprintf(file, "\t%s", inet_ntoa(inaddr)); | ||
525 | cp += dlen; | ||
526 | } else if (dlen == 7) { | ||
527 | char *address; | ||
528 | u_char protocol; | ||
529 | in_port_t port; | ||
530 | |||
531 | address = inet_ntoa(inaddr); | ||
532 | cp += INADDRSZ; | ||
533 | protocol = *(u_char*)cp; | ||
534 | cp += sizeof (u_char); | ||
535 | port = _getshort((u_char*)cp); | ||
536 | cp += INT16SZ; | ||
537 | fprintf(file, "\t%s\t; proto %u, port %u", | ||
538 | address, protocol, port); | ||
539 | } | ||
540 | break; | ||
541 | default: | ||
542 | cp += dlen; | ||
543 | } | ||
544 | break; | ||
545 | case T_CNAME: | ||
546 | case T_MB: | ||
547 | case T_MG: | ||
548 | case T_MR: | ||
549 | case T_NS: | ||
550 | case T_PTR: | ||
551 | putc('\t', file); | ||
552 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
553 | return (NULL); | ||
554 | break; | ||
555 | |||
556 | case T_HINFO: | ||
557 | case T_ISDN: | ||
558 | cp2 = cp + dlen; | ||
559 | (void) fputs("\t\"", file); | ||
560 | if ((n = (unsigned char) *cp++) != 0) { | ||
561 | for (c = n; c > 0 && cp < cp2; c--) { | ||
562 | if (strchr("\n\"\\", *cp)) | ||
563 | (void) putc('\\', file); | ||
564 | (void) putc(*cp++, file); | ||
565 | } | ||
566 | } | ||
567 | putc('"', file); | ||
568 | if (cp < cp2 && (n = (unsigned char) *cp++) != 0) { | ||
569 | (void) fputs ("\t\"", file); | ||
570 | for (c = n; c > 0 && cp < cp2; c--) { | ||
571 | if (strchr("\n\"\\", *cp)) | ||
572 | (void) putc('\\', file); | ||
573 | (void) putc(*cp++, file); | ||
574 | } | ||
575 | putc('"', file); | ||
576 | } else if (type == T_HINFO) { | ||
577 | (void) fputs("\"?\"", file); | ||
578 | fprintf(file, "\n;; *** Warning *** OS-type missing"); | ||
579 | } | ||
580 | break; | ||
581 | |||
582 | case T_SOA: | ||
583 | putc('\t', file); | ||
584 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
585 | return (NULL); | ||
586 | putc(' ', file); | ||
587 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
588 | return (NULL); | ||
589 | fputs(" (\n", file); | ||
590 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
591 | fprintf(file, "\t\t\t%lu\t; serial\n", (u_long)t); | ||
592 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
593 | fprintf(file, "\t\t\t%lu\t; refresh (%s)\n", | ||
594 | (u_long)t, __p_time(t)); | ||
595 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
596 | fprintf(file, "\t\t\t%lu\t; retry (%s)\n", | ||
597 | (u_long)t, __p_time(t)); | ||
598 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
599 | fprintf(file, "\t\t\t%lu\t; expire (%s)\n", | ||
600 | (u_long)t, __p_time(t)); | ||
601 | t = _getlong((u_char*)cp); cp += INT32SZ; | ||
602 | fprintf(file, "\t\t\t%lu )\t; minimum (%s)", | ||
603 | (u_long)t, __p_time(t)); | ||
604 | break; | ||
605 | |||
606 | case T_MX: | ||
607 | case T_AFSDB: | ||
608 | case T_RT: | ||
609 | fprintf(file, "\t%u ", _getshort((u_char*)cp)); | ||
610 | cp += INT16SZ; | ||
611 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
612 | return (NULL); | ||
613 | break; | ||
614 | |||
615 | case T_PX: | ||
616 | fprintf(file, "\t%u ", _getshort((u_char*)cp)); | ||
617 | cp += INT16SZ; | ||
618 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
619 | return (NULL); | ||
620 | putc(' ', file); | ||
621 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
622 | return (NULL); | ||
623 | break; | ||
624 | |||
625 | case T_X25: | ||
626 | cp2 = cp + dlen; | ||
627 | (void) fputs("\t\"", file); | ||
628 | if ((n = (unsigned char) *cp++) != 0) { | ||
629 | for (c = n; c > 0 && cp < cp2; c--) { | ||
630 | if (strchr("\n\"\\", *cp)) | ||
631 | (void) putc('\\', file); | ||
632 | (void) putc(*cp++, file); | ||
633 | } | ||
634 | } | ||
635 | putc('"', file); | ||
636 | break; | ||
637 | |||
638 | case T_TXT: | ||
639 | (void) putc('\t', file); | ||
640 | cp2 = cp1 + dlen; | ||
641 | while (cp < cp2) { | ||
642 | putc('"', file); | ||
643 | if ((n = (unsigned char) *cp++)) { | ||
644 | for (c = n; c > 0 && cp < cp2; c--) { | ||
645 | if (strchr("\n\"\\", *cp)) | ||
646 | (void) putc('\\', file); | ||
647 | (void) putc(*cp++, file); | ||
648 | } | ||
649 | } | ||
650 | putc('"', file); | ||
651 | if (cp < cp2) | ||
652 | putc(' ', file); | ||
653 | } | ||
654 | break; | ||
655 | |||
656 | case T_NSAP: | ||
657 | (void) fprintf(file, "\t%s", inet_nsap_ntoa(dlen, cp, NULL)); | ||
658 | cp += dlen; | ||
659 | break; | ||
660 | |||
661 | case T_AAAA: { | ||
662 | char t[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; | ||
663 | |||
664 | fprintf(file, "\t%s", inet_ntop(AF_INET6, cp, t, sizeof t)); | ||
665 | cp += dlen; | ||
666 | break; | ||
667 | } | ||
668 | |||
669 | case T_LOC: { | ||
670 | char t[255]; | ||
671 | |||
672 | fprintf(file, "\t%s", loc_ntoal(cp, t, sizeof t)); | ||
673 | cp += dlen; | ||
674 | break; | ||
675 | } | ||
676 | |||
677 | case T_NAPTR: { | ||
678 | u_int order, preference; | ||
679 | |||
680 | order = _getshort(cp); cp += INT16SZ; | ||
681 | preference = _getshort(cp); cp += INT16SZ; | ||
682 | fprintf(file, "\t%u %u ",order, preference); | ||
683 | /* Flags */ | ||
684 | n = *cp++; | ||
685 | fprintf(file,"\"%.*s\" ", (int)n, cp); | ||
686 | cp += n; | ||
687 | /* Service */ | ||
688 | n = *cp++; | ||
689 | fprintf(file,"\"%.*s\" ", (int)n, cp); | ||
690 | cp += n; | ||
691 | /* Regexp */ | ||
692 | n = *cp++; | ||
693 | fprintf(file,"\"%.*s\" ", (int)n, cp); | ||
694 | cp += n; | ||
695 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
696 | return (NULL); | ||
697 | break; | ||
698 | } | ||
699 | |||
700 | case T_SRV: { | ||
701 | u_int priority, weight, port; | ||
702 | |||
703 | priority = _getshort(cp); cp += INT16SZ; | ||
704 | weight = _getshort(cp); cp += INT16SZ; | ||
705 | port = _getshort(cp); cp += INT16SZ; | ||
706 | fprintf(file, "\t%u %u %u ", priority, weight, port); | ||
707 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
708 | return (NULL); | ||
709 | break; | ||
710 | } | ||
711 | |||
712 | case T_MINFO: | ||
713 | case T_RP: | ||
714 | putc('\t', file); | ||
715 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
716 | return (NULL); | ||
717 | putc(' ', file); | ||
718 | if ((cp = p_fqname(cp, msg, file)) == NULL) | ||
719 | return (NULL); | ||
720 | break; | ||
721 | |||
722 | case T_UINFO: | ||
723 | putc('\t', file); | ||
724 | fputs((char *)cp, file); | ||
725 | cp += dlen; | ||
726 | break; | ||
727 | |||
728 | case T_UID: | ||
729 | case T_GID: | ||
730 | if (dlen == 4) { | ||
731 | fprintf(file, "\t%u", _getlong((u_char*)cp)); | ||
732 | cp += INT32SZ; | ||
733 | } | ||
734 | break; | ||
735 | |||
736 | case T_WKS: | ||
737 | if (dlen < INT32SZ + 1) | ||
738 | break; | ||
739 | bcopy(cp, (char *)&inaddr, INADDRSZ); | ||
740 | cp += INT32SZ; | ||
741 | fprintf(file, "\t%s %s ( ", | ||
742 | inet_ntoa(inaddr), | ||
743 | deproto((int) *cp)); | ||
744 | cp += sizeof (u_char); | ||
745 | n = 0; | ||
746 | lcnt = 0; | ||
747 | while (cp < cp1 + dlen) { | ||
748 | c = *cp++; | ||
749 | do { | ||
750 | if (c & 0200) { | ||
751 | if (lcnt == 0) { | ||
752 | fputs("\n\t\t\t", file); | ||
753 | lcnt = 5; | ||
754 | } | ||
755 | fputs(dewks(n), file); | ||
756 | putc(' ', file); | ||
757 | lcnt--; | ||
758 | } | ||
759 | c <<= 1; | ||
760 | } while (++n & 07); | ||
761 | } | ||
762 | putc(')', file); | ||
763 | break; | ||
764 | |||
765 | case T_KEY: | ||
766 | putc('\t', file); | ||
767 | keyflags = _getshort(cp); | ||
768 | cp += 2; | ||
769 | fprintf(file,"0x%04x", keyflags ); /* flags */ | ||
770 | fprintf(file," %u", *cp++); /* protocol */ | ||
771 | fprintf(file," %u (", *cp++); /* algorithm */ | ||
772 | |||
773 | n = b64_ntop(cp, (cp1 + dlen) - cp, | ||
774 | base64_key, sizeof base64_key); | ||
775 | for (c = 0; c < n; ++c) { | ||
776 | if (0 == (c & 0x3F)) | ||
777 | fprintf(file, "\n\t"); | ||
778 | putc(base64_key[c], file); /* public key data */ | ||
779 | } | ||
780 | |||
781 | fprintf(file, " )"); | ||
782 | if (n < 0) | ||
783 | fprintf(file, "\t; BAD BASE64"); | ||
784 | fflush(file); | ||
785 | cp = cp1 + dlen; | ||
786 | break; | ||
787 | |||
788 | case T_SIG: | ||
789 | type = _getshort((u_char*)cp); | ||
790 | cp += INT16SZ; | ||
791 | fprintf(file, " %s", p_type(type)); | ||
792 | fprintf(file, "\t%u", *cp++); /* algorithm */ | ||
793 | /* Check label value and print error if wrong. */ | ||
794 | n = *cp++; | ||
795 | c = dn_count_labels (rrname); | ||
796 | if (n != c) | ||
797 | fprintf(file, "\t; LABELS WRONG (%d should be %d)\n\t", | ||
798 | n, c); | ||
799 | /* orig ttl */ | ||
800 | n = _getlong((u_char*)cp); | ||
801 | if (n != tmpttl) | ||
802 | fprintf(file, " %u", n); | ||
803 | cp += INT32SZ; | ||
804 | /* sig expire */ | ||
805 | fprintf(file, " (\n\t%s", | ||
806 | __p_secstodate(_getlong((u_char*)cp))); | ||
807 | cp += INT32SZ; | ||
808 | /* time signed */ | ||
809 | fprintf(file, " %s", __p_secstodate(_getlong((u_char*)cp))); | ||
810 | cp += INT32SZ; | ||
811 | /* sig footprint */ | ||
812 | fprintf(file," %u ", _getshort((u_char*)cp)); | ||
813 | cp += INT16SZ; | ||
814 | /* signer's name */ | ||
815 | cp = p_fqname(cp, msg, file); | ||
816 | n = b64_ntop(cp, (cp1 + dlen) - cp, | ||
817 | base64_key, sizeof base64_key); | ||
818 | for (c = 0; c < n; c++) { | ||
819 | if (0 == (c & 0x3F)) | ||
820 | fprintf (file, "\n\t"); | ||
821 | putc(base64_key[c], file); /* signature */ | ||
822 | } | ||
823 | /* Clean up... */ | ||
824 | fprintf(file, " )"); | ||
825 | if (n < 0) | ||
826 | fprintf(file, "\t; BAD BASE64"); | ||
827 | fflush(file); | ||
828 | cp = cp1+dlen; | ||
829 | break; | ||
830 | |||
831 | #ifdef ALLOW_T_UNSPEC | ||
832 | case T_UNSPEC: | ||
833 | { | ||
834 | int NumBytes = 8; | ||
835 | u_char *DataPtr; | ||
836 | int i; | ||
837 | |||
838 | if (dlen < NumBytes) NumBytes = dlen; | ||
839 | fprintf(file, "\tFirst %d bytes of hex data:", | ||
840 | NumBytes); | ||
841 | for (i = 0, DataPtr = cp; i < NumBytes; i++, DataPtr++) | ||
842 | fprintf(file, " %x", *DataPtr); | ||
843 | cp += dlen; | ||
844 | } | ||
845 | break; | ||
846 | #endif /* ALLOW_T_UNSPEC */ | ||
847 | |||
848 | default: | ||
849 | fprintf(file, "\t?%d?", type); | ||
850 | cp += dlen; | ||
851 | } | ||
852 | #if 0 | ||
853 | fprintf(file, "\t; dlen=%d, ttl %s\n", dlen, __p_time(tmpttl)); | ||
854 | #else | ||
855 | putc('\n', file); | ||
856 | #endif | ||
857 | if (cp - cp1 != dlen) { | ||
858 | fprintf(file, ";; packet size error (found %ld, dlen was %d)\n", | ||
859 | (long)(cp - cp1), dlen); | ||
860 | cp = NULL; | ||
861 | } | ||
862 | return (cp); | ||
863 | } | ||
864 | |||
865 | /* | ||
866 | * Names of RR classes and qclasses. Classes and qclasses are the same, except | ||
867 | * that C_ANY is a qclass but not a class. (You can ask for records of class | ||
868 | * C_ANY, but you can't have any records of that class in the database.) | ||
869 | */ | ||
870 | const struct res_sym __p_class_syms[] = { | ||
871 | {C_IN, "IN"}, | ||
872 | {C_CHAOS, "CHAOS"}, | ||
873 | {C_HS, "HS"}, | ||
874 | {C_HS, "HESIOD"}, | ||
875 | {C_ANY, "ANY"}, | ||
876 | {C_IN, (char *)0} | ||
877 | }; | ||
878 | |||
879 | /* | ||
880 | * Names of RR types and qtypes. Types and qtypes are the same, except | ||
881 | * that T_ANY is a qtype but not a type. (You can ask for records of type | ||
882 | * T_ANY, but you can't have any records of that type in the database.) | ||
883 | */ | ||
884 | const struct res_sym __p_type_syms[] = { | ||
885 | {T_A, "A", "address"}, | ||
886 | {T_NS, "NS", "name server"}, | ||
887 | {T_MD, "MD", "mail destination (deprecated)"}, | ||
888 | {T_MF, "MF", "mail forwarder (deprecated)"}, | ||
889 | {T_CNAME, "CNAME", "canonical name"}, | ||
890 | {T_SOA, "SOA", "start of authority"}, | ||
891 | {T_MB, "MB", "mailbox"}, | ||
892 | {T_MG, "MG", "mail group member"}, | ||
893 | {T_MR, "MR", "mail rename"}, | ||
894 | {T_NULL, "NULL", "null"}, | ||
895 | {T_WKS, "WKS", "well-known service (deprecated)"}, | ||
896 | {T_PTR, "PTR", "domain name pointer"}, | ||
897 | {T_HINFO, "HINFO", "host information"}, | ||
898 | {T_MINFO, "MINFO", "mailbox information"}, | ||
899 | {T_MX, "MX", "mail exchanger"}, | ||
900 | {T_TXT, "TXT", "text"}, | ||
901 | {T_RP, "RP", "responsible person"}, | ||
902 | {T_AFSDB, "AFSDB", "DCE or AFS server"}, | ||
903 | {T_X25, "X25", "X25 address"}, | ||
904 | {T_ISDN, "ISDN", "ISDN address"}, | ||
905 | {T_RT, "RT", "router"}, | ||
906 | {T_NSAP, "NSAP", "nsap address"}, | ||
907 | {T_NSAP_PTR, "NSAP_PTR", "domain name pointer"}, | ||
908 | {T_SIG, "SIG", "signature"}, | ||
909 | {T_KEY, "KEY", "key"}, | ||
910 | {T_PX, "PX", "mapping information"}, | ||
911 | {T_GPOS, "GPOS", "geographical position (withdrawn)"}, | ||
912 | {T_AAAA, "AAAA", "IPv6 address"}, | ||
913 | {T_LOC, "LOC", "location"}, | ||
914 | {T_NXT, "NXT", "next valid name (unimplemented)"}, | ||
915 | {T_EID, "EID", "endpoint identifier (unimplemented)"}, | ||
916 | {T_NIMLOC, "NIMLOC", "NIMROD locator (unimplemented)"}, | ||
917 | {T_SRV, "SRV", "server selection"}, | ||
918 | {T_ATMA, "ATMA", "ATM address (unimplemented)"}, | ||
919 | {T_IXFR, "IXFR", "incremental zone transfer"}, | ||
920 | {T_AXFR, "AXFR", "zone transfer"}, | ||
921 | {T_MAILB, "MAILB", "mailbox-related data (deprecated)"}, | ||
922 | {T_MAILA, "MAILA", "mail agent (deprecated)"}, | ||
923 | {T_UINFO, "UINFO", "user information (nonstandard)"}, | ||
924 | {T_UID, "UID", "user ID (nonstandard)"}, | ||
925 | {T_GID, "GID", "group ID (nonstandard)"}, | ||
926 | {T_NAPTR, "NAPTR", "URN Naming Authority"}, | ||
927 | #ifdef ALLOW_T_UNSPEC | ||
928 | {T_UNSPEC, "UNSPEC", "unspecified data (nonstandard)"}, | ||
929 | #endif /* ALLOW_T_UNSPEC */ | ||
930 | {T_ANY, "ANY", "\"any\""}, | ||
931 | {0, NULL, NULL} | ||
932 | }; | ||
933 | |||
934 | int | ||
935 | __sym_ston(syms, name, success) | ||
936 | const struct res_sym *syms; | ||
937 | char *name; | ||
938 | int *success; | ||
939 | { | ||
940 | for (; syms->name != 0; syms++) { | ||
941 | if (strcasecmp (name, syms->name) == 0) { | ||
942 | if (success) | ||
943 | *success = 1; | ||
944 | return (syms->number); | ||
945 | } | ||
946 | } | ||
947 | if (success) | ||
948 | *success = 0; | ||
949 | return (syms->number); /* The default value. */ | ||
950 | } | ||
951 | |||
952 | const char * | ||
953 | __sym_ntos(syms, number, success) | ||
954 | const struct res_sym *syms; | ||
955 | int number; | ||
956 | int *success; | ||
957 | { | ||
958 | static char unname[20]; | ||
959 | |||
960 | for (; syms->name != 0; syms++) { | ||
961 | if (number == syms->number) { | ||
962 | if (success) | ||
963 | *success = 1; | ||
964 | return (syms->name); | ||
965 | } | ||
966 | } | ||
967 | |||
968 | snprintf(unname, sizeof unname, "%d", number); | ||
969 | if (success) | ||
970 | *success = 0; | ||
971 | return (unname); | ||
972 | } | ||
973 | |||
974 | |||
975 | const char * | ||
976 | __sym_ntop(syms, number, success) | ||
977 | const struct res_sym *syms; | ||
978 | int number; | ||
979 | int *success; | ||
980 | { | ||
981 | static char unname[20]; | ||
982 | |||
983 | for (; syms->name != 0; syms++) { | ||
984 | if (number == syms->number) { | ||
985 | if (success) | ||
986 | *success = 1; | ||
987 | return (syms->humanname); | ||
988 | } | ||
989 | } | ||
990 | snprintf(unname, sizeof unname, "%d", number); | ||
991 | if (success) | ||
992 | *success = 0; | ||
993 | return (unname); | ||
994 | } | ||
995 | |||
996 | /* | ||
997 | * Return a string for the type | ||
998 | */ | ||
999 | const char * | ||
1000 | __p_type(type) | ||
1001 | int type; | ||
1002 | { | ||
1003 | return (__sym_ntos (__p_type_syms, type, (int *)0)); | ||
1004 | } | ||
1005 | |||
1006 | /* | ||
1007 | * Return a mnemonic for class | ||
1008 | */ | ||
1009 | const char * | ||
1010 | __p_class(class) | ||
1011 | int class; | ||
1012 | { | ||
1013 | return (__sym_ntos (__p_class_syms, class, (int *)0)); | ||
1014 | } | ||
1015 | |||
1016 | /* | ||
1017 | * Return a mnemonic for an option | ||
1018 | */ | ||
1019 | const char * | ||
1020 | __p_option(option) | ||
1021 | u_long option; | ||
1022 | { | ||
1023 | static char nbuf[40]; | ||
1024 | |||
1025 | switch (option) { | ||
1026 | case RES_INIT: return "init"; | ||
1027 | case RES_DEBUG: return "debug"; | ||
1028 | case RES_AAONLY: return "aaonly(unimpl)"; | ||
1029 | case RES_USEVC: return "usevc"; | ||
1030 | case RES_PRIMARY: return "primry(unimpl)"; | ||
1031 | case RES_IGNTC: return "igntc"; | ||
1032 | case RES_RECURSE: return "recurs"; | ||
1033 | case RES_DEFNAMES: return "defnam"; | ||
1034 | case RES_STAYOPEN: return "styopn"; | ||
1035 | case RES_DNSRCH: return "dnsrch"; | ||
1036 | case RES_INSECURE1: return "insecure1"; | ||
1037 | case RES_INSECURE2: return "insecure2"; | ||
1038 | case RES_USE_INET6: return "inet6"; | ||
1039 | case RES_USE_EDNS0: return "edns0"; | ||
1040 | default: | ||
1041 | snprintf(nbuf, sizeof nbuf, "?0x%lx?", (u_long)option); | ||
1042 | return (nbuf); | ||
1043 | } | ||
1044 | } | ||
1045 | |||
1046 | /* | ||
1047 | * Return a mnemonic for a time to live | ||
1048 | */ | ||
1049 | const char * | ||
1050 | p_time(value) | ||
1051 | u_int32_t value; | ||
1052 | { | ||
1053 | static char nbuf[40]; | ||
1054 | char *ebuf; | ||
1055 | int secs, mins, hours, days; | ||
1056 | register char *p; | ||
1057 | int tmp; | ||
1058 | |||
1059 | if (value == 0) { | ||
1060 | strlcpy(nbuf, "0 secs", sizeof nbuf); | ||
1061 | return (nbuf); | ||
1062 | } | ||
1063 | |||
1064 | secs = value % 60; | ||
1065 | value /= 60; | ||
1066 | mins = value % 60; | ||
1067 | value /= 60; | ||
1068 | hours = value % 24; | ||
1069 | value /= 24; | ||
1070 | days = value; | ||
1071 | value = 0; | ||
1072 | |||
1073 | #define PLURALIZE(x) x, (x == 1) ? "" : "s" | ||
1074 | p = nbuf; | ||
1075 | ebuf = nbuf + sizeof(nbuf); | ||
1076 | if (days) { | ||
1077 | if ((tmp = snprintf(p, ebuf - p, "%d day%s", | ||
1078 | PLURALIZE(days))) >= ebuf - nbuf || tmp < 0) | ||
1079 | goto full; | ||
1080 | p += tmp; | ||
1081 | } | ||
1082 | if (hours) { | ||
1083 | if (days) | ||
1084 | *p++ = ' '; | ||
1085 | if (p >= ebuf) | ||
1086 | goto full; | ||
1087 | if ((tmp = snprintf(p, ebuf - p, "%d hour%s", | ||
1088 | PLURALIZE(hours))) >= ebuf - nbuf || tmp < 0) | ||
1089 | goto full; | ||
1090 | p += tmp; | ||
1091 | } | ||
1092 | if (mins) { | ||
1093 | if (days || hours) | ||
1094 | *p++ = ' '; | ||
1095 | if (p >= ebuf) | ||
1096 | goto full; | ||
1097 | if ((tmp = snprintf(p, ebuf - p, "%d min%s", | ||
1098 | PLURALIZE(mins))) >= ebuf - nbuf || tmp < 0) | ||
1099 | goto full; | ||
1100 | p += tmp; | ||
1101 | } | ||
1102 | if (secs || ! (days || hours || mins)) { | ||
1103 | if (days || hours || mins) | ||
1104 | *p++ = ' '; | ||
1105 | if (p >= ebuf) | ||
1106 | goto full; | ||
1107 | if ((tmp = snprintf(p, ebuf - p, "%d sec%s", | ||
1108 | PLURALIZE(secs))) >= ebuf - nbuf || tmp < 0) | ||
1109 | goto full; | ||
1110 | } | ||
1111 | return (nbuf); | ||
1112 | full: | ||
1113 | p = nbuf + sizeof(nbuf) - 4; | ||
1114 | *p++ = '.'; | ||
1115 | *p++ = '.'; | ||
1116 | *p++ = '.'; | ||
1117 | *p++ = '\0'; | ||
1118 | return (nbuf); | ||
1119 | } | ||
1120 | |||
1121 | /* | ||
1122 | * routines to convert between on-the-wire RR format and zone file format. | ||
1123 | * Does not contain conversion to/from decimal degrees; divide or multiply | ||
1124 | * by 60*60*1000 for that. | ||
1125 | */ | ||
1126 | |||
1127 | static unsigned int poweroften[10] = {1, 10, 100, 1000, 10000, 100000, | ||
1128 | 1000000,10000000,100000000,1000000000}; | ||
1129 | |||
1130 | /* takes an XeY precision/size value, returns a string representation. */ | ||
1131 | static const char * | ||
1132 | precsize_ntoa(prec) | ||
1133 | u_int8_t prec; | ||
1134 | { | ||
1135 | static char retbuf[sizeof "90000000.00"]; | ||
1136 | unsigned long val; | ||
1137 | int mantissa, exponent; | ||
1138 | |||
1139 | mantissa = (int)((prec >> 4) & 0x0f) % 10; | ||
1140 | exponent = (int)((prec >> 0) & 0x0f) % 10; | ||
1141 | |||
1142 | val = mantissa * poweroften[exponent]; | ||
1143 | |||
1144 | (void) snprintf(retbuf, sizeof retbuf, "%ld.%.2ld", val/100, val%100); | ||
1145 | return (retbuf); | ||
1146 | } | ||
1147 | |||
1148 | /* converts ascii size/precision X * 10**Y(cm) to 0xXY. moves pointer. */ | ||
1149 | static u_int8_t | ||
1150 | precsize_aton(strptr) | ||
1151 | char **strptr; | ||
1152 | { | ||
1153 | unsigned int mval = 0, cmval = 0; | ||
1154 | u_int8_t retval = 0; | ||
1155 | register char *cp; | ||
1156 | register int exponent; | ||
1157 | register int mantissa; | ||
1158 | |||
1159 | cp = *strptr; | ||
1160 | |||
1161 | while (isdigit(*cp)) | ||
1162 | mval = mval * 10 + (*cp++ - '0'); | ||
1163 | |||
1164 | if (*cp == '.') { /* centimeters */ | ||
1165 | cp++; | ||
1166 | if (isdigit(*cp)) { | ||
1167 | cmval = (*cp++ - '0') * 10; | ||
1168 | if (isdigit(*cp)) { | ||
1169 | cmval += (*cp++ - '0'); | ||
1170 | } | ||
1171 | } | ||
1172 | } | ||
1173 | cmval = (mval * 100) + cmval; | ||
1174 | |||
1175 | for (exponent = 0; exponent < 9; exponent++) | ||
1176 | if (cmval < poweroften[exponent+1]) | ||
1177 | break; | ||
1178 | |||
1179 | mantissa = cmval / poweroften[exponent]; | ||
1180 | if (mantissa > 9) | ||
1181 | mantissa = 9; | ||
1182 | |||
1183 | retval = (mantissa << 4) | exponent; | ||
1184 | |||
1185 | *strptr = cp; | ||
1186 | |||
1187 | return (retval); | ||
1188 | } | ||
1189 | |||
1190 | /* converts ascii lat/lon to unsigned encoded 32-bit number. moves pointer. */ | ||
1191 | static u_int32_t | ||
1192 | latlon2ul(latlonstrptr,which) | ||
1193 | char **latlonstrptr; | ||
1194 | int *which; | ||
1195 | { | ||
1196 | register char *cp; | ||
1197 | u_int32_t retval; | ||
1198 | int deg = 0, min = 0, secs = 0, secsfrac = 0; | ||
1199 | |||
1200 | cp = *latlonstrptr; | ||
1201 | |||
1202 | while (isdigit(*cp)) | ||
1203 | deg = deg * 10 + (*cp++ - '0'); | ||
1204 | |||
1205 | while (isspace(*cp)) | ||
1206 | cp++; | ||
1207 | |||
1208 | if (!(isdigit(*cp))) | ||
1209 | goto fndhemi; | ||
1210 | |||
1211 | while (isdigit(*cp)) | ||
1212 | min = min * 10 + (*cp++ - '0'); | ||
1213 | |||
1214 | while (isspace(*cp)) | ||
1215 | cp++; | ||
1216 | |||
1217 | if (!(isdigit(*cp))) | ||
1218 | goto fndhemi; | ||
1219 | |||
1220 | while (isdigit(*cp)) | ||
1221 | secs = secs * 10 + (*cp++ - '0'); | ||
1222 | |||
1223 | if (*cp == '.') { /* decimal seconds */ | ||
1224 | cp++; | ||
1225 | if (isdigit(*cp)) { | ||
1226 | secsfrac = (*cp++ - '0') * 100; | ||
1227 | if (isdigit(*cp)) { | ||
1228 | secsfrac += (*cp++ - '0') * 10; | ||
1229 | if (isdigit(*cp)) { | ||
1230 | secsfrac += (*cp++ - '0'); | ||
1231 | } | ||
1232 | } | ||
1233 | } | ||
1234 | } | ||
1235 | |||
1236 | while (!isspace(*cp)) /* if any trailing garbage */ | ||
1237 | cp++; | ||
1238 | |||
1239 | while (isspace(*cp)) | ||
1240 | cp++; | ||
1241 | |||
1242 | fndhemi: | ||
1243 | switch (*cp) { | ||
1244 | case 'N': case 'n': | ||
1245 | case 'E': case 'e': | ||
1246 | retval = ((unsigned)1<<31) | ||
1247 | + (((((deg * 60) + min) * 60) + secs) * 1000) | ||
1248 | + secsfrac; | ||
1249 | break; | ||
1250 | case 'S': case 's': | ||
1251 | case 'W': case 'w': | ||
1252 | retval = ((unsigned)1<<31) | ||
1253 | - (((((deg * 60) + min) * 60) + secs) * 1000) | ||
1254 | - secsfrac; | ||
1255 | break; | ||
1256 | default: | ||
1257 | retval = 0; /* invalid value -- indicates error */ | ||
1258 | break; | ||
1259 | } | ||
1260 | |||
1261 | switch (*cp) { | ||
1262 | case 'N': case 'n': | ||
1263 | case 'S': case 's': | ||
1264 | *which = 1; /* latitude */ | ||
1265 | break; | ||
1266 | case 'E': case 'e': | ||
1267 | case 'W': case 'w': | ||
1268 | *which = 2; /* longitude */ | ||
1269 | break; | ||
1270 | default: | ||
1271 | *which = 0; /* error */ | ||
1272 | break; | ||
1273 | } | ||
1274 | |||
1275 | cp++; /* skip the hemisphere */ | ||
1276 | |||
1277 | while (!isspace(*cp)) /* if any trailing garbage */ | ||
1278 | cp++; | ||
1279 | |||
1280 | while (isspace(*cp)) /* move to next field */ | ||
1281 | cp++; | ||
1282 | |||
1283 | *latlonstrptr = cp; | ||
1284 | |||
1285 | return (retval); | ||
1286 | } | ||
1287 | |||
1288 | /* converts a zone file representation in a string to an RDATA on-the-wire | ||
1289 | * representation. */ | ||
1290 | int | ||
1291 | loc_aton(ascii, binary) | ||
1292 | const char *ascii; | ||
1293 | u_char *binary; | ||
1294 | { | ||
1295 | const char *maxcp; | ||
1296 | u_char *bcp; | ||
1297 | char *cp; | ||
1298 | |||
1299 | u_int32_t latit = 0, longit = 0, alt = 0; | ||
1300 | u_int32_t lltemp1 = 0, lltemp2 = 0; | ||
1301 | int altmeters = 0, altfrac = 0, altsign = 1; | ||
1302 | u_int8_t hp = 0x16; /* default = 1e6 cm = 10000.00m = 10km */ | ||
1303 | u_int8_t vp = 0x13; /* default = 1e3 cm = 10.00m */ | ||
1304 | u_int8_t siz = 0x12; /* default = 1e2 cm = 1.00m */ | ||
1305 | int which1 = 0, which2 = 0; | ||
1306 | |||
1307 | cp = (char *)ascii; | ||
1308 | maxcp = cp + strlen(ascii); | ||
1309 | |||
1310 | lltemp1 = latlon2ul(&cp, &which1); | ||
1311 | |||
1312 | lltemp2 = latlon2ul(&cp, &which2); | ||
1313 | |||
1314 | switch (which1 + which2) { | ||
1315 | case 3: /* 1 + 2, the only valid combination */ | ||
1316 | if ((which1 == 1) && (which2 == 2)) { /* normal case */ | ||
1317 | latit = lltemp1; | ||
1318 | longit = lltemp2; | ||
1319 | } else if ((which1 == 2) && (which2 == 1)) { /* reversed */ | ||
1320 | longit = lltemp1; | ||
1321 | latit = lltemp2; | ||
1322 | } else { /* some kind of brokenness */ | ||
1323 | return (0); | ||
1324 | } | ||
1325 | break; | ||
1326 | default: /* we didn't get one of each */ | ||
1327 | return (0); | ||
1328 | } | ||
1329 | |||
1330 | /* altitude */ | ||
1331 | if (*cp == '-') { | ||
1332 | altsign = -1; | ||
1333 | cp++; | ||
1334 | } | ||
1335 | |||
1336 | if (*cp == '+') | ||
1337 | cp++; | ||
1338 | |||
1339 | while (isdigit(*cp)) | ||
1340 | altmeters = altmeters * 10 + (*cp++ - '0'); | ||
1341 | |||
1342 | if (*cp == '.') { /* decimal meters */ | ||
1343 | cp++; | ||
1344 | if (isdigit(*cp)) { | ||
1345 | altfrac = (*cp++ - '0') * 10; | ||
1346 | if (isdigit(*cp)) { | ||
1347 | altfrac += (*cp++ - '0'); | ||
1348 | } | ||
1349 | } | ||
1350 | } | ||
1351 | |||
1352 | alt = (10000000 + (altsign * (altmeters * 100 + altfrac))); | ||
1353 | |||
1354 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | ||
1355 | cp++; | ||
1356 | |||
1357 | while (isspace(*cp) && (cp < maxcp)) | ||
1358 | cp++; | ||
1359 | |||
1360 | if (cp >= maxcp) | ||
1361 | goto defaults; | ||
1362 | |||
1363 | siz = precsize_aton(&cp); | ||
1364 | |||
1365 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | ||
1366 | cp++; | ||
1367 | |||
1368 | while (isspace(*cp) && (cp < maxcp)) | ||
1369 | cp++; | ||
1370 | |||
1371 | if (cp >= maxcp) | ||
1372 | goto defaults; | ||
1373 | |||
1374 | hp = precsize_aton(&cp); | ||
1375 | |||
1376 | while (!isspace(*cp) && (cp < maxcp)) /* if trailing garbage or m */ | ||
1377 | cp++; | ||
1378 | |||
1379 | while (isspace(*cp) && (cp < maxcp)) | ||
1380 | cp++; | ||
1381 | |||
1382 | if (cp >= maxcp) | ||
1383 | goto defaults; | ||
1384 | |||
1385 | vp = precsize_aton(&cp); | ||
1386 | |||
1387 | defaults: | ||
1388 | |||
1389 | bcp = binary; | ||
1390 | *bcp++ = (u_int8_t) 0; /* version byte */ | ||
1391 | *bcp++ = siz; | ||
1392 | *bcp++ = hp; | ||
1393 | *bcp++ = vp; | ||
1394 | PUTLONG(latit,bcp); | ||
1395 | PUTLONG(longit,bcp); | ||
1396 | PUTLONG(alt,bcp); | ||
1397 | |||
1398 | return (16); /* size of RR in octets */ | ||
1399 | } | ||
1400 | |||
1401 | const char * | ||
1402 | loc_ntoa(binary, ascii) | ||
1403 | const u_char *binary; | ||
1404 | char *ascii; | ||
1405 | { | ||
1406 | return loc_ntoal(binary, ascii, 255); | ||
1407 | } | ||
1408 | |||
1409 | /* takes an on-the-wire LOC RR and formats it in a human readable format. */ | ||
1410 | static const char * | ||
1411 | loc_ntoal(binary, ascii, ascii_len) | ||
1412 | const u_char *binary; | ||
1413 | char *ascii; | ||
1414 | int ascii_len; | ||
1415 | { | ||
1416 | static char *error = "?"; | ||
1417 | register const u_char *cp = binary; | ||
1418 | |||
1419 | int latdeg, latmin, latsec, latsecfrac; | ||
1420 | int longdeg, longmin, longsec, longsecfrac; | ||
1421 | char northsouth, eastwest; | ||
1422 | int altmeters, altfrac, altsign; | ||
1423 | |||
1424 | const int referencealt = 100000 * 100; | ||
1425 | |||
1426 | int32_t latval, longval, altval; | ||
1427 | u_int32_t templ; | ||
1428 | u_int8_t sizeval, hpval, vpval, versionval; | ||
1429 | |||
1430 | char *sizestr, *hpstr, *vpstr; | ||
1431 | |||
1432 | versionval = *cp++; | ||
1433 | |||
1434 | if (versionval) { | ||
1435 | snprintf(ascii, ascii_len, "; error: unknown LOC RR version"); | ||
1436 | return (ascii); | ||
1437 | } | ||
1438 | |||
1439 | sizeval = *cp++; | ||
1440 | |||
1441 | hpval = *cp++; | ||
1442 | vpval = *cp++; | ||
1443 | |||
1444 | GETLONG(templ, cp); | ||
1445 | latval = (templ - ((unsigned)1<<31)); | ||
1446 | |||
1447 | GETLONG(templ, cp); | ||
1448 | longval = (templ - ((unsigned)1<<31)); | ||
1449 | |||
1450 | GETLONG(templ, cp); | ||
1451 | if (templ < referencealt) { /* below WGS 84 spheroid */ | ||
1452 | altval = referencealt - templ; | ||
1453 | altsign = -1; | ||
1454 | } else { | ||
1455 | altval = templ - referencealt; | ||
1456 | altsign = 1; | ||
1457 | } | ||
1458 | |||
1459 | if (latval < 0) { | ||
1460 | northsouth = 'S'; | ||
1461 | latval = -latval; | ||
1462 | } else | ||
1463 | northsouth = 'N'; | ||
1464 | |||
1465 | latsecfrac = latval % 1000; | ||
1466 | latval = latval / 1000; | ||
1467 | latsec = latval % 60; | ||
1468 | latval = latval / 60; | ||
1469 | latmin = latval % 60; | ||
1470 | latval = latval / 60; | ||
1471 | latdeg = latval; | ||
1472 | |||
1473 | if (longval < 0) { | ||
1474 | eastwest = 'W'; | ||
1475 | longval = -longval; | ||
1476 | } else | ||
1477 | eastwest = 'E'; | ||
1478 | |||
1479 | longsecfrac = longval % 1000; | ||
1480 | longval = longval / 1000; | ||
1481 | longsec = longval % 60; | ||
1482 | longval = longval / 60; | ||
1483 | longmin = longval % 60; | ||
1484 | longval = longval / 60; | ||
1485 | longdeg = longval; | ||
1486 | |||
1487 | altfrac = altval % 100; | ||
1488 | altmeters = (altval / 100) * altsign; | ||
1489 | |||
1490 | if ((sizestr = strdup(precsize_ntoa(sizeval))) == NULL) | ||
1491 | sizestr = error; | ||
1492 | if ((hpstr = strdup(precsize_ntoa(hpval))) == NULL) | ||
1493 | hpstr = error; | ||
1494 | if ((vpstr = strdup(precsize_ntoa(vpval))) == NULL) | ||
1495 | vpstr = error; | ||
1496 | |||
1497 | snprintf(ascii, ascii_len, | ||
1498 | "%d %.2d %.2d.%.3d %c %d %.2d %.2d.%.3d %c %d.%.2dm %sm %sm %sm", | ||
1499 | latdeg, latmin, latsec, latsecfrac, northsouth, | ||
1500 | longdeg, longmin, longsec, longsecfrac, eastwest, | ||
1501 | altmeters, altfrac, sizestr, hpstr, vpstr); | ||
1502 | |||
1503 | if (sizestr != error) | ||
1504 | free(sizestr); | ||
1505 | if (hpstr != error) | ||
1506 | free(hpstr); | ||
1507 | if (vpstr != error) | ||
1508 | free(vpstr); | ||
1509 | |||
1510 | return (ascii); | ||
1511 | } | ||
1512 | |||
1513 | |||
1514 | /* Return the number of DNS hierarchy levels in the name. */ | ||
1515 | int | ||
1516 | __dn_count_labels(name) | ||
1517 | char *name; | ||
1518 | { | ||
1519 | int i, len, count; | ||
1520 | |||
1521 | len = strlen(name); | ||
1522 | |||
1523 | for(i = 0, count = 0; i < len; i++) { | ||
1524 | if (name[i] == '.') | ||
1525 | count++; | ||
1526 | } | ||
1527 | |||
1528 | /* don't count initial wildcard */ | ||
1529 | if (name[0] == '*') | ||
1530 | if (count) | ||
1531 | count--; | ||
1532 | |||
1533 | /* don't count the null label for root. */ | ||
1534 | /* if terminating '.' not found, must adjust */ | ||
1535 | /* count to include last label */ | ||
1536 | if (len > 0 && name[len-1] != '.') | ||
1537 | count++; | ||
1538 | return (count); | ||
1539 | } | ||
1540 | |||
1541 | |||
1542 | /* | ||
1543 | * Make dates expressed in seconds-since-Jan-1-1970 easy to read. | ||
1544 | * SIG records are required to be printed like this, by the Secure DNS RFC. | ||
1545 | */ | ||
1546 | char * | ||
1547 | __p_secstodate (secs) | ||
1548 | unsigned long secs; | ||
1549 | { | ||
1550 | static char output[15]; /* YYYYMMDDHHMMSS and null */ | ||
1551 | time_t clock = secs; | ||
1552 | struct tm *time; | ||
1553 | |||
1554 | time = gmtime(&clock); | ||
1555 | time->tm_year += 1900; | ||
1556 | time->tm_mon += 1; | ||
1557 | snprintf(output, sizeof output, "%04d%02d%02d%02d%02d%02d", | ||
1558 | time->tm_year, time->tm_mon, time->tm_mday, | ||
1559 | time->tm_hour, time->tm_min, time->tm_sec); | ||
1560 | return (output); | ||
1561 | } | ||
diff --git a/src/lib/libc/net/res_init.c b/src/lib/libc/net/res_init.c new file mode 100644 index 0000000000..9c73958883 --- /dev/null +++ b/src/lib/libc/net/res_init.c | |||
@@ -0,0 +1,650 @@ | |||
1 | /* $OpenBSD: res_init.c,v 1.29 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1989, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1989, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #ifndef INET6 | ||
55 | #define INET6 | ||
56 | #endif | ||
57 | |||
58 | #if defined(LIBC_SCCS) && !defined(lint) | ||
59 | #if 0 | ||
60 | static char sccsid[] = "@(#)res_init.c 8.1 (Berkeley) 6/7/93"; | ||
61 | static char rcsid[] = "$From: res_init.c,v 8.7 1996/09/28 06:51:07 vixie Exp $"; | ||
62 | #else | ||
63 | static char rcsid[] = "$OpenBSD: res_init.c,v 1.29 2003/06/02 20:18:36 millert Exp $"; | ||
64 | #endif | ||
65 | #endif /* LIBC_SCCS and not lint */ | ||
66 | |||
67 | #include <sys/types.h> | ||
68 | #include <sys/param.h> | ||
69 | #include <sys/socket.h> | ||
70 | #include <sys/time.h> | ||
71 | #include <netinet/in.h> | ||
72 | #include <arpa/inet.h> | ||
73 | #include <arpa/nameser.h> | ||
74 | |||
75 | #include <stdio.h> | ||
76 | #include <ctype.h> | ||
77 | #include <resolv.h> | ||
78 | #include <unistd.h> | ||
79 | #include <stdlib.h> | ||
80 | #include <string.h> | ||
81 | #ifdef INET6 | ||
82 | #include <netdb.h> | ||
83 | #endif /* INET6 */ | ||
84 | |||
85 | #include "thread_private.h" | ||
86 | |||
87 | /*-------------------------------------- info about "sortlist" -------------- | ||
88 | * Marc Majka 1994/04/16 | ||
89 | * Allan Nathanson 1994/10/29 (BIND 4.9.3.x) | ||
90 | * | ||
91 | * NetInfo resolver configuration directory support. | ||
92 | * | ||
93 | * Allow a NetInfo directory to be created in the hierarchy which | ||
94 | * contains the same information as the resolver configuration file. | ||
95 | * | ||
96 | * - The local domain name is stored as the value of the "domain" property. | ||
97 | * - The Internet address(es) of the name server(s) are stored as values | ||
98 | * of the "nameserver" property. | ||
99 | * - The name server addresses are stored as values of the "nameserver" | ||
100 | * property. | ||
101 | * - The search list for host-name lookup is stored as values of the | ||
102 | * "search" property. | ||
103 | * - The sortlist comprised of IP address netmask pairs are stored as | ||
104 | * values of the "sortlist" property. The IP address and optional netmask | ||
105 | * should be separated by a slash (/) or ampersand (&) character. | ||
106 | * - Internal resolver variables can be set from the value of the "options" | ||
107 | * property. | ||
108 | */ | ||
109 | |||
110 | static void res_setoptions(char *, char *); | ||
111 | |||
112 | #ifdef RESOLVSORT | ||
113 | static const char sort_mask[] = "/&"; | ||
114 | #define ISSORTMASK(ch) (strchr(sort_mask, ch) != NULL) | ||
115 | static u_int32_t net_mask(struct in_addr); | ||
116 | #endif | ||
117 | |||
118 | /* | ||
119 | * Resolver state default settings. | ||
120 | */ | ||
121 | volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res) = { | ||
122 | PTHREAD_ONCE_INIT, 0 | ||
123 | }; | ||
124 | |||
125 | struct __res_state _res | ||
126 | # if defined(__BIND_RES_TEXT) | ||
127 | = { RES_TIMEOUT, } /* Motorola, et al. */ | ||
128 | # endif | ||
129 | ; | ||
130 | #ifdef INET6 | ||
131 | volatile struct _thread_private_key_struct __THREAD_KEY_NAME(_res_ext) = { | ||
132 | PTHREAD_ONCE_INIT, 0 | ||
133 | }; | ||
134 | |||
135 | struct __res_state_ext _res_ext; | ||
136 | #endif /* INET6 */ | ||
137 | |||
138 | /* | ||
139 | * Set up default settings. If the configuration file exist, the values | ||
140 | * there will have precedence. Otherwise, the server address is set to | ||
141 | * INADDR_ANY and the default domain name comes from the gethostname(). | ||
142 | * | ||
143 | * An interrim version of this code (BIND 4.9, pre-4.4BSD) used 127.0.0.1 | ||
144 | * rather than INADDR_ANY ("0.0.0.0") as the default name server address | ||
145 | * since it was noted that INADDR_ANY actually meant ``the first interface | ||
146 | * you "ifconfig"'d at boot time'' and if this was a SLIP or PPP interface, | ||
147 | * it had to be "up" in order for you to reach your own name server. It | ||
148 | * was later decided that since the recommended practice is to always | ||
149 | * install local static routes through 127.0.0.1 for all your network | ||
150 | * interfaces, that we could solve this problem without a code change. | ||
151 | * | ||
152 | * The configuration file should always be used, since it is the only way | ||
153 | * to specify a default domain. If you are running a server on your local | ||
154 | * machine, you should say "nameserver 0.0.0.0" or "nameserver 127.0.0.1" | ||
155 | * in the configuration file. | ||
156 | * | ||
157 | * Return 0 if completes successfully, -1 on error | ||
158 | */ | ||
159 | int | ||
160 | res_init() | ||
161 | { | ||
162 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
163 | #ifdef INET6 | ||
164 | struct __res_state_ext *_res_extp = _THREAD_PRIVATE(_res_ext, _res_ext, | ||
165 | &_res_ext); | ||
166 | #endif | ||
167 | register FILE *fp; | ||
168 | register char *cp, **pp; | ||
169 | register int n; | ||
170 | char buf[BUFSIZ]; | ||
171 | int nserv = 0; /* number of nameserver records read from file */ | ||
172 | int haveenv = 0; | ||
173 | int havesearch = 0; | ||
174 | size_t len; | ||
175 | #ifdef RESOLVSORT | ||
176 | int nsort = 0; | ||
177 | char *net; | ||
178 | #endif | ||
179 | #ifndef RFC1535 | ||
180 | int dots; | ||
181 | #endif | ||
182 | |||
183 | /* | ||
184 | * These three fields used to be statically initialized. This made | ||
185 | * it hard to use this code in a shared library. It is necessary, | ||
186 | * now that we're doing dynamic initialization here, that we preserve | ||
187 | * the old semantics: if an application modifies one of these three | ||
188 | * fields of _res before res_init() is called, res_init() will not | ||
189 | * alter them. Of course, if an application is setting them to | ||
190 | * _zero_ before calling res_init(), hoping to override what used | ||
191 | * to be the static default, we can't detect it and unexpected results | ||
192 | * will follow. Zero for any of these fields would make no sense, | ||
193 | * so one can safely assume that the applications were already getting | ||
194 | * unexpected results. | ||
195 | * | ||
196 | * _res.options is tricky since some apps were known to diddle the bits | ||
197 | * before res_init() was first called. We can't replicate that semantic | ||
198 | * with dynamic initialization (they may have turned bits off that are | ||
199 | * set in RES_DEFAULT). Our solution is to declare such applications | ||
200 | * "broken". They could fool us by setting RES_INIT but none do (yet). | ||
201 | */ | ||
202 | if (!_resp->retrans) | ||
203 | _resp->retrans = RES_TIMEOUT; | ||
204 | if (!_resp->retry) | ||
205 | _resp->retry = 4; | ||
206 | if (!(_resp->options & RES_INIT)) | ||
207 | _resp->options = RES_DEFAULT; | ||
208 | |||
209 | #ifdef USELOOPBACK | ||
210 | _resp->nsaddr.sin_addr = inet_makeaddr(IN_LOOPBACKNET, 1); | ||
211 | #else | ||
212 | _resp->nsaddr.sin_addr.s_addr = INADDR_ANY; | ||
213 | #endif | ||
214 | _resp->nsaddr.sin_family = AF_INET; | ||
215 | _resp->nsaddr.sin_port = htons(NAMESERVER_PORT); | ||
216 | _resp->nsaddr.sin_len = sizeof(struct sockaddr_in); | ||
217 | #ifdef INET6 | ||
218 | if (sizeof(_res_extp->nsaddr) >= _resp->nsaddr.sin_len) | ||
219 | memcpy(&_res_extp->nsaddr, &_resp->nsaddr, _resp->nsaddr.sin_len); | ||
220 | #endif | ||
221 | _resp->nscount = 1; | ||
222 | _resp->ndots = 1; | ||
223 | _resp->pfcode = 0; | ||
224 | strlcpy(_resp->lookups, "f", sizeof _resp->lookups); | ||
225 | |||
226 | /* Allow user to override the local domain definition */ | ||
227 | if (issetugid() == 0 && (cp = getenv("LOCALDOMAIN")) != NULL) { | ||
228 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
229 | haveenv++; | ||
230 | |||
231 | /* | ||
232 | * Set search list to be blank-separated strings | ||
233 | * from rest of env value. Permits users of LOCALDOMAIN | ||
234 | * to still have a search list, and anyone to set the | ||
235 | * one that they want to use as an individual (even more | ||
236 | * important now that the rfc1535 stuff restricts searches) | ||
237 | */ | ||
238 | cp = _resp->defdname; | ||
239 | pp = _resp->dnsrch; | ||
240 | *pp++ = cp; | ||
241 | for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) { | ||
242 | if (*cp == '\n') /* silly backwards compat */ | ||
243 | break; | ||
244 | else if (*cp == ' ' || *cp == '\t') { | ||
245 | *cp = 0; | ||
246 | n = 1; | ||
247 | } else if (n) { | ||
248 | *pp++ = cp; | ||
249 | n = 0; | ||
250 | havesearch = 1; | ||
251 | } | ||
252 | } | ||
253 | /* null terminate last domain if there are excess */ | ||
254 | while (*cp != '\0' && *cp != ' ' && *cp != '\t' && *cp != '\n') | ||
255 | cp++; | ||
256 | *cp = '\0'; | ||
257 | *pp++ = 0; | ||
258 | } | ||
259 | |||
260 | #define MATCH(line, name) \ | ||
261 | (!strncmp(line, name, sizeof(name) - 1) && \ | ||
262 | (line[sizeof(name) - 1] == ' ' || \ | ||
263 | line[sizeof(name) - 1] == '\t')) | ||
264 | |||
265 | if ((fp = fopen(_PATH_RESCONF, "r")) != NULL) { | ||
266 | strlcpy(_resp->lookups, "bf", sizeof _resp->lookups); | ||
267 | |||
268 | /* read the config file */ | ||
269 | buf[0] = '\0'; | ||
270 | while ((cp = fgetln(fp, &len)) != NULL) { | ||
271 | /* skip lines that are too long or zero length */ | ||
272 | if (len >= sizeof(buf) || len == 0) | ||
273 | continue; | ||
274 | (void)memcpy(buf, cp, len); | ||
275 | buf[len] = '\0'; | ||
276 | /* skip comments */ | ||
277 | if ((cp = strpbrk(buf, ";#")) != NULL) | ||
278 | *cp = '\0'; | ||
279 | if (buf[0] == '\0') | ||
280 | continue; | ||
281 | /* read default domain name */ | ||
282 | if (MATCH(buf, "domain")) { | ||
283 | if (haveenv) /* skip if have from environ */ | ||
284 | continue; | ||
285 | cp = buf + sizeof("domain") - 1; | ||
286 | while (*cp == ' ' || *cp == '\t') | ||
287 | cp++; | ||
288 | if ((*cp == '\0') || (*cp == '\n')) | ||
289 | continue; | ||
290 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
291 | if ((cp = strpbrk(_resp->defdname, " \t\n")) != NULL) | ||
292 | *cp = '\0'; | ||
293 | havesearch = 0; | ||
294 | continue; | ||
295 | } | ||
296 | /* lookup types */ | ||
297 | if (MATCH(buf, "lookup")) { | ||
298 | char *sp = NULL; | ||
299 | |||
300 | bzero(_resp->lookups, sizeof _resp->lookups); | ||
301 | cp = buf + sizeof("lookup") - 1; | ||
302 | for (n = 0;; cp++) { | ||
303 | if (n == MAXDNSLUS) | ||
304 | break; | ||
305 | if ((*cp == '\0') || (*cp == '\n')) { | ||
306 | if (sp) { | ||
307 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
308 | _resp->lookups[n++] = *sp; | ||
309 | sp = NULL; | ||
310 | } | ||
311 | break; | ||
312 | } else if ((*cp == ' ') || (*cp == '\t') || (*cp == ',')) { | ||
313 | if (sp) { | ||
314 | if (*sp=='y' || *sp=='b' || *sp=='f') | ||
315 | _resp->lookups[n++] = *sp; | ||
316 | sp = NULL; | ||
317 | } | ||
318 | } else if (sp == NULL) | ||
319 | sp = cp; | ||
320 | } | ||
321 | continue; | ||
322 | } | ||
323 | /* set search list */ | ||
324 | if (MATCH(buf, "search")) { | ||
325 | if (haveenv) /* skip if have from environ */ | ||
326 | continue; | ||
327 | cp = buf + sizeof("search") - 1; | ||
328 | while (*cp == ' ' || *cp == '\t') | ||
329 | cp++; | ||
330 | if ((*cp == '\0') || (*cp == '\n')) | ||
331 | continue; | ||
332 | strlcpy(_resp->defdname, cp, sizeof(_resp->defdname)); | ||
333 | if ((cp = strchr(_resp->defdname, '\n')) != NULL) | ||
334 | *cp = '\0'; | ||
335 | /* | ||
336 | * Set search list to be blank-separated strings | ||
337 | * on rest of line. | ||
338 | */ | ||
339 | cp = _resp->defdname; | ||
340 | pp = _resp->dnsrch; | ||
341 | *pp++ = cp; | ||
342 | for (n = 0; *cp && pp < _resp->dnsrch + MAXDNSRCH; cp++) { | ||
343 | if (*cp == ' ' || *cp == '\t') { | ||
344 | *cp = 0; | ||
345 | n = 1; | ||
346 | } else if (n) { | ||
347 | *pp++ = cp; | ||
348 | n = 0; | ||
349 | } | ||
350 | } | ||
351 | /* null terminate last domain if there are excess */ | ||
352 | while (*cp != '\0' && *cp != ' ' && *cp != '\t') | ||
353 | cp++; | ||
354 | *cp = '\0'; | ||
355 | *pp++ = 0; | ||
356 | havesearch = 1; | ||
357 | continue; | ||
358 | } | ||
359 | /* read nameservers to query */ | ||
360 | if (MATCH(buf, "nameserver") && nserv < MAXNS) { | ||
361 | #ifdef INET6 | ||
362 | char *q; | ||
363 | struct addrinfo hints, *res; | ||
364 | char pbuf[NI_MAXSERV]; | ||
365 | #else | ||
366 | struct in_addr a; | ||
367 | #endif /* INET6 */ | ||
368 | |||
369 | cp = buf + sizeof("nameserver") - 1; | ||
370 | while (*cp == ' ' || *cp == '\t') | ||
371 | cp++; | ||
372 | #ifdef INET6 | ||
373 | if ((*cp == '\0') || (*cp == '\n')) | ||
374 | continue; | ||
375 | for (q = cp; *q; q++) { | ||
376 | if (isspace(*q)) { | ||
377 | *q = '\0'; | ||
378 | break; | ||
379 | } | ||
380 | } | ||
381 | memset(&hints, 0, sizeof(hints)); | ||
382 | hints.ai_flags = AI_NUMERICHOST; | ||
383 | hints.ai_socktype = SOCK_DGRAM; | ||
384 | snprintf(pbuf, sizeof(pbuf), "%u", NAMESERVER_PORT); | ||
385 | res = NULL; | ||
386 | if (getaddrinfo(cp, pbuf, &hints, &res) == 0 && | ||
387 | res->ai_next == NULL) { | ||
388 | if (res->ai_addrlen <= sizeof(_res_extp->nsaddr_list[nserv])) { | ||
389 | memcpy(&_res_extp->nsaddr_list[nserv], res->ai_addr, | ||
390 | res->ai_addrlen); | ||
391 | } else { | ||
392 | memset(&_res_extp->nsaddr_list[nserv], 0, | ||
393 | sizeof(_res_extp->nsaddr_list[nserv])); | ||
394 | } | ||
395 | if (res->ai_addrlen <= sizeof(_resp->nsaddr_list[nserv])) { | ||
396 | memcpy(&_resp->nsaddr_list[nserv], res->ai_addr, | ||
397 | res->ai_addrlen); | ||
398 | } else { | ||
399 | memset(&_resp->nsaddr_list[nserv], 0, | ||
400 | sizeof(_resp->nsaddr_list[nserv])); | ||
401 | } | ||
402 | nserv++; | ||
403 | } | ||
404 | if (res) | ||
405 | freeaddrinfo(res); | ||
406 | #else /* INET6 */ | ||
407 | if ((*cp != '\0') && (*cp != '\n') && inet_aton(cp, &a)) { | ||
408 | _resp->nsaddr_list[nserv].sin_addr = a; | ||
409 | _resp->nsaddr_list[nserv].sin_family = AF_INET; | ||
410 | _resp->nsaddr_list[nserv].sin_port = | ||
411 | htons(NAMESERVER_PORT); | ||
412 | _resp->nsaddr_list[nserv].sin_len = | ||
413 | sizeof(struct sockaddr_in); | ||
414 | nserv++; | ||
415 | } | ||
416 | #endif /* INET6 */ | ||
417 | continue; | ||
418 | } | ||
419 | #ifdef RESOLVSORT | ||
420 | if (MATCH(buf, "sortlist")) { | ||
421 | struct in_addr a; | ||
422 | #ifdef INET6 | ||
423 | struct in6_addr a6; | ||
424 | int m, i; | ||
425 | u_char *u; | ||
426 | #endif /* INET6 */ | ||
427 | |||
428 | cp = buf + sizeof("sortlist") - 1; | ||
429 | while (nsort < MAXRESOLVSORT) { | ||
430 | while (*cp == ' ' || *cp == '\t') | ||
431 | cp++; | ||
432 | if (*cp == '\0' || *cp == '\n' || *cp == ';') | ||
433 | break; | ||
434 | net = cp; | ||
435 | while (*cp && !ISSORTMASK(*cp) && *cp != ';' && | ||
436 | isascii(*cp) && !isspace(*cp)) | ||
437 | cp++; | ||
438 | n = *cp; | ||
439 | *cp = 0; | ||
440 | if (inet_aton(net, &a)) { | ||
441 | _resp->sort_list[nsort].addr = a; | ||
442 | if (ISSORTMASK(n)) { | ||
443 | *cp++ = n; | ||
444 | net = cp; | ||
445 | while (*cp && *cp != ';' && | ||
446 | isascii(*cp) && !isspace(*cp)) | ||
447 | cp++; | ||
448 | n = *cp; | ||
449 | *cp = 0; | ||
450 | if (inet_aton(net, &a)) { | ||
451 | _resp->sort_list[nsort].mask = a.s_addr; | ||
452 | } else { | ||
453 | _resp->sort_list[nsort].mask = | ||
454 | net_mask(_resp->sort_list[nsort].addr); | ||
455 | } | ||
456 | } else { | ||
457 | _resp->sort_list[nsort].mask = | ||
458 | net_mask(_resp->sort_list[nsort].addr); | ||
459 | } | ||
460 | #ifdef INET6 | ||
461 | _res_extp->sort_list[nsort].af = AF_INET; | ||
462 | _res_extp->sort_list[nsort].addr.ina = | ||
463 | _resp->sort_list[nsort].addr; | ||
464 | _res_extp->sort_list[nsort].mask.ina.s_addr = | ||
465 | _resp->sort_list[nsort].mask; | ||
466 | #endif /* INET6 */ | ||
467 | nsort++; | ||
468 | } | ||
469 | #ifdef INET6 | ||
470 | else if (inet_pton(AF_INET6, net, &a6) == 1) { | ||
471 | _res_extp->sort_list[nsort].af = AF_INET6; | ||
472 | _res_extp->sort_list[nsort].addr.in6a = a6; | ||
473 | u = (u_char *)&_res_extp->sort_list[nsort].mask.in6a; | ||
474 | *cp++ = n; | ||
475 | net = cp; | ||
476 | while (*cp && *cp != ';' && | ||
477 | isascii(*cp) && !isspace(*cp)) | ||
478 | cp++; | ||
479 | m = n; | ||
480 | n = *cp; | ||
481 | *cp = 0; | ||
482 | switch (m) { | ||
483 | case '/': | ||
484 | m = atoi(net); | ||
485 | break; | ||
486 | case '&': | ||
487 | if (inet_pton(AF_INET6, net, u) == 1) { | ||
488 | m = -1; | ||
489 | break; | ||
490 | } | ||
491 | /*FALLTHRU*/ | ||
492 | default: | ||
493 | m = sizeof(struct in6_addr) * NBBY; | ||
494 | break; | ||
495 | } | ||
496 | if (m >= 0) { | ||
497 | for (i = 0; i < sizeof(struct in6_addr); i++) { | ||
498 | if (m <= 0) { | ||
499 | *u = 0; | ||
500 | } else { | ||
501 | m -= NBBY; | ||
502 | *u = (u_char)~0; | ||
503 | if (m < 0) | ||
504 | *u <<= -m; | ||
505 | } | ||
506 | u++; | ||
507 | } | ||
508 | } | ||
509 | nsort++; | ||
510 | } | ||
511 | #endif /* INET6 */ | ||
512 | *cp = n; | ||
513 | } | ||
514 | continue; | ||
515 | } | ||
516 | #endif | ||
517 | if (MATCH(buf, "options")) { | ||
518 | res_setoptions(buf + sizeof("options") - 1, "conf"); | ||
519 | continue; | ||
520 | } | ||
521 | } | ||
522 | if (nserv > 1) | ||
523 | _resp->nscount = nserv; | ||
524 | #ifdef RESOLVSORT | ||
525 | _resp->nsort = nsort; | ||
526 | #endif | ||
527 | (void) fclose(fp); | ||
528 | } | ||
529 | if (_resp->defdname[0] == 0 && | ||
530 | gethostname(buf, sizeof(_resp->defdname) - 1) == 0 && | ||
531 | (cp = strchr(buf, '.')) != NULL) | ||
532 | { | ||
533 | strlcpy(_resp->defdname, cp + 1, | ||
534 | sizeof(_resp->defdname)); | ||
535 | } | ||
536 | |||
537 | /* find components of local domain that might be searched */ | ||
538 | if (havesearch == 0) { | ||
539 | pp = _resp->dnsrch; | ||
540 | *pp++ = _resp->defdname; | ||
541 | *pp = NULL; | ||
542 | |||
543 | #ifndef RFC1535 | ||
544 | dots = 0; | ||
545 | for (cp = _resp->defdname; *cp; cp++) | ||
546 | dots += (*cp == '.'); | ||
547 | |||
548 | cp = _resp->defdname; | ||
549 | while (pp < _resp->dnsrch + MAXDFLSRCH) { | ||
550 | if (dots < LOCALDOMAINPARTS) | ||
551 | break; | ||
552 | cp = strchr(cp, '.') + 1; /* we know there is one */ | ||
553 | *pp++ = cp; | ||
554 | dots--; | ||
555 | } | ||
556 | *pp = NULL; | ||
557 | #ifdef DEBUG | ||
558 | if (_resp->options & RES_DEBUG) { | ||
559 | printf(";; res_init()... default dnsrch list:\n"); | ||
560 | for (pp = _resp->dnsrch; *pp; pp++) | ||
561 | printf(";;\t%s\n", *pp); | ||
562 | printf(";;\t..END..\n"); | ||
563 | } | ||
564 | #endif /* DEBUG */ | ||
565 | #endif /* !RFC1535 */ | ||
566 | } | ||
567 | |||
568 | if (issetugid()) | ||
569 | _resp->options |= RES_NOALIASES; | ||
570 | else if ((cp = getenv("RES_OPTIONS")) != NULL) | ||
571 | res_setoptions(cp, "env"); | ||
572 | _resp->options |= RES_INIT; | ||
573 | return (0); | ||
574 | } | ||
575 | |||
576 | /* ARGSUSED */ | ||
577 | static void | ||
578 | res_setoptions(options, source) | ||
579 | char *options, *source; | ||
580 | { | ||
581 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
582 | char *cp = options; | ||
583 | char *endp; | ||
584 | long l; | ||
585 | |||
586 | #ifdef DEBUG | ||
587 | if (_resp->options & RES_DEBUG) | ||
588 | printf(";; res_setoptions(\"%s\", \"%s\")...\n", | ||
589 | options, source); | ||
590 | #endif | ||
591 | while (*cp) { | ||
592 | /* skip leading and inner runs of spaces */ | ||
593 | while (*cp == ' ' || *cp == '\t') | ||
594 | cp++; | ||
595 | /* search for and process individual options */ | ||
596 | if (!strncmp(cp, "ndots:", sizeof("ndots:") - 1)) { | ||
597 | char *p = cp + sizeof("ndots:") - 1; | ||
598 | l = strtol(p, &endp, 10); | ||
599 | if (l >= 0 && endp != p && | ||
600 | (*endp = '\0' || isspace(*endp))) { | ||
601 | if (l <= RES_MAXNDOTS) | ||
602 | _resp->ndots = l; | ||
603 | else | ||
604 | _resp->ndots = RES_MAXNDOTS; | ||
605 | #ifdef DEBUG | ||
606 | if (_resp->options & RES_DEBUG) | ||
607 | printf(";;\tndots=%u\n", _resp->ndots); | ||
608 | #endif | ||
609 | } | ||
610 | } else if (!strncmp(cp, "debug", sizeof("debug") - 1)) { | ||
611 | #ifdef DEBUG | ||
612 | if (!(_resp->options & RES_DEBUG)) { | ||
613 | printf(";; res_setoptions(\"%s\", \"%s\")..\n", | ||
614 | options, source); | ||
615 | _resp->options |= RES_DEBUG; | ||
616 | } | ||
617 | printf(";;\tdebug\n"); | ||
618 | #endif | ||
619 | } else if (!strncmp(cp, "inet6", sizeof("inet6") - 1)) { | ||
620 | _resp->options |= RES_USE_INET6; | ||
621 | } else if (!strncmp(cp, "insecure1", sizeof("insecure1") - 1)) { | ||
622 | _resp->options |= RES_INSECURE1; | ||
623 | } else if (!strncmp(cp, "insecure2", sizeof("insecure2") - 1)) { | ||
624 | _resp->options |= RES_INSECURE2; | ||
625 | } else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { | ||
626 | _resp->options |= RES_USE_EDNS0; | ||
627 | } else { | ||
628 | /* XXX - print a warning here? */ | ||
629 | } | ||
630 | /* skip to next run of spaces */ | ||
631 | while (*cp && *cp != ' ' && *cp != '\t') | ||
632 | cp++; | ||
633 | } | ||
634 | } | ||
635 | |||
636 | #ifdef RESOLVSORT | ||
637 | /* XXX - should really support CIDR which means explicit masks always. */ | ||
638 | static u_int32_t | ||
639 | net_mask(in) /* XXX - should really use system's version of this */ | ||
640 | struct in_addr in; | ||
641 | { | ||
642 | register u_int32_t i = ntohl(in.s_addr); | ||
643 | |||
644 | if (IN_CLASSA(i)) | ||
645 | return (htonl(IN_CLASSA_NET)); | ||
646 | else if (IN_CLASSB(i)) | ||
647 | return (htonl(IN_CLASSB_NET)); | ||
648 | return (htonl(IN_CLASSC_NET)); | ||
649 | } | ||
650 | #endif | ||
diff --git a/src/lib/libc/net/res_mkquery.c b/src/lib/libc/net/res_mkquery.c new file mode 100644 index 0000000000..82aa2796c2 --- /dev/null +++ b/src/lib/libc/net/res_mkquery.c | |||
@@ -0,0 +1,242 @@ | |||
1 | /* $OpenBSD: res_mkquery.c,v 1.14 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #if defined(LIBC_SCCS) && !defined(lint) | ||
55 | #if 0 | ||
56 | static char sccsid[] = "@(#)res_mkquery.c 8.1 (Berkeley) 6/4/93"; | ||
57 | static char rcsid[] = "$From: res_mkquery.c,v 8.5 1996/08/27 08:33:28 vixie Exp $"; | ||
58 | #else | ||
59 | static char rcsid[] = "$OpenBSD: res_mkquery.c,v 1.14 2003/06/02 20:18:36 millert Exp $"; | ||
60 | #endif | ||
61 | #endif /* LIBC_SCCS and not lint */ | ||
62 | |||
63 | #include <sys/types.h> | ||
64 | #include <sys/param.h> | ||
65 | #include <netinet/in.h> | ||
66 | #include <arpa/nameser.h> | ||
67 | |||
68 | #include <stdio.h> | ||
69 | #include <netdb.h> | ||
70 | #include <resolv.h> | ||
71 | #include <string.h> | ||
72 | |||
73 | #include "thread_private.h" | ||
74 | |||
75 | /* | ||
76 | * Form all types of queries. | ||
77 | * Returns the size of the result or -1. | ||
78 | */ | ||
79 | /* ARGSUSED */ | ||
80 | int | ||
81 | res_mkquery(op, dname, class, type, data, datalen, newrr_in, buf, buflen) | ||
82 | int op; /* opcode of query */ | ||
83 | const char *dname; /* domain name */ | ||
84 | int class, type; /* class and type of query */ | ||
85 | const u_char *data; /* resource record data */ | ||
86 | int datalen; /* length of data */ | ||
87 | const u_char *newrr_in; /* new rr for modify or append */ | ||
88 | u_char *buf; /* buffer to put query */ | ||
89 | int buflen; /* size of buffer */ | ||
90 | { | ||
91 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
92 | register HEADER *hp; | ||
93 | register u_char *cp, *ep; | ||
94 | register int n; | ||
95 | u_char *dnptrs[20], **dpp, **lastdnptr; | ||
96 | |||
97 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
98 | h_errno = NETDB_INTERNAL; | ||
99 | return (-1); | ||
100 | } | ||
101 | #ifdef DEBUG | ||
102 | if (_resp->options & RES_DEBUG) | ||
103 | printf(";; res_mkquery(%d, %s, %d, %d)\n", | ||
104 | op, dname, class, type); | ||
105 | #endif | ||
106 | /* | ||
107 | * Initialize header fields. | ||
108 | * | ||
109 | * A special random number generator is used to create non predictable | ||
110 | * and non repeating ids over a long period. It also avoids reuse | ||
111 | * by switching between two distinct number cycles. | ||
112 | */ | ||
113 | |||
114 | if ((buf == NULL) || (buflen < HFIXEDSZ)) | ||
115 | return (-1); | ||
116 | bzero(buf, HFIXEDSZ); | ||
117 | hp = (HEADER *) buf; | ||
118 | _resp->id = res_randomid(); | ||
119 | hp->id = htons(_resp->id); | ||
120 | hp->opcode = op; | ||
121 | hp->rd = (_resp->options & RES_RECURSE) != 0; | ||
122 | hp->rcode = NOERROR; | ||
123 | cp = buf + HFIXEDSZ; | ||
124 | ep = buf + buflen; | ||
125 | dpp = dnptrs; | ||
126 | *dpp++ = buf; | ||
127 | *dpp++ = NULL; | ||
128 | lastdnptr = dnptrs + sizeof dnptrs / sizeof dnptrs[0]; | ||
129 | /* | ||
130 | * perform opcode specific processing | ||
131 | */ | ||
132 | switch (op) { | ||
133 | case QUERY: /*FALLTHROUGH*/ | ||
134 | case NS_NOTIFY_OP: | ||
135 | if (ep - cp < QFIXEDSZ) | ||
136 | return (-1); | ||
137 | if ((n = dn_comp(dname, cp, ep - cp - QFIXEDSZ, dnptrs, | ||
138 | lastdnptr)) < 0) | ||
139 | return (-1); | ||
140 | cp += n; | ||
141 | __putshort(type, cp); | ||
142 | cp += INT16SZ; | ||
143 | __putshort(class, cp); | ||
144 | cp += INT16SZ; | ||
145 | hp->qdcount = htons(1); | ||
146 | if (op == QUERY || data == NULL) | ||
147 | break; | ||
148 | /* | ||
149 | * Make an additional record for completion domain. | ||
150 | */ | ||
151 | if (ep - cp < RRFIXEDSZ) | ||
152 | return (-1); | ||
153 | n = dn_comp((char *)data, cp, ep - cp - RRFIXEDSZ, dnptrs, | ||
154 | lastdnptr); | ||
155 | if (n < 0) | ||
156 | return (-1); | ||
157 | cp += n; | ||
158 | __putshort(T_NULL, cp); | ||
159 | cp += INT16SZ; | ||
160 | __putshort(class, cp); | ||
161 | cp += INT16SZ; | ||
162 | __putlong(0, cp); | ||
163 | cp += INT32SZ; | ||
164 | __putshort(0, cp); | ||
165 | cp += INT16SZ; | ||
166 | hp->arcount = htons(1); | ||
167 | break; | ||
168 | |||
169 | case IQUERY: | ||
170 | /* | ||
171 | * Initialize answer section | ||
172 | */ | ||
173 | if (ep - cp < 1 + RRFIXEDSZ + datalen) | ||
174 | return (-1); | ||
175 | *cp++ = '\0'; /* no domain name */ | ||
176 | __putshort(type, cp); | ||
177 | cp += INT16SZ; | ||
178 | __putshort(class, cp); | ||
179 | cp += INT16SZ; | ||
180 | __putlong(0, cp); | ||
181 | cp += INT32SZ; | ||
182 | __putshort(datalen, cp); | ||
183 | cp += INT16SZ; | ||
184 | if (datalen) { | ||
185 | bcopy(data, cp, datalen); | ||
186 | cp += datalen; | ||
187 | } | ||
188 | hp->ancount = htons(1); | ||
189 | break; | ||
190 | |||
191 | default: | ||
192 | return (-1); | ||
193 | } | ||
194 | return (cp - buf); | ||
195 | } | ||
196 | |||
197 | /* attach OPT pseudo-RR, as documented in RFC2671 (EDNS0). */ | ||
198 | int | ||
199 | res_opt(n0, buf, buflen, anslen) | ||
200 | int n0; | ||
201 | u_char *buf; /* buffer to put query */ | ||
202 | int buflen; /* size of buffer */ | ||
203 | int anslen; /* answer buffer length */ | ||
204 | { | ||
205 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
206 | register HEADER *hp; | ||
207 | register u_char *cp, *ep; | ||
208 | |||
209 | hp = (HEADER *) buf; | ||
210 | cp = buf + n0; | ||
211 | ep = buf + buflen; | ||
212 | |||
213 | if (ep - cp < 1 + RRFIXEDSZ) | ||
214 | return -1; | ||
215 | |||
216 | *cp++ = 0; /* "." */ | ||
217 | |||
218 | __putshort(T_OPT, cp); /* TYPE */ | ||
219 | cp += INT16SZ; | ||
220 | if (anslen > 0xffff) | ||
221 | anslen = 0xffff; /* limit to 16bit value */ | ||
222 | __putshort(anslen & 0xffff, cp); /* CLASS = UDP payload size */ | ||
223 | cp += INT16SZ; | ||
224 | *cp++ = NOERROR; /* extended RCODE */ | ||
225 | *cp++ = 0; /* EDNS version */ | ||
226 | if (_resp->options & RES_USE_DNSSEC) { | ||
227 | #ifdef DEBUG | ||
228 | if (_resp->options & RES_DEBUG) | ||
229 | printf(";; res_opt()... ENDS0 DNSSEC OK\n"); | ||
230 | #endif /* DEBUG */ | ||
231 | __putshort(DNS_MESSAGEEXTFLAG_DO, cp); /* EDNS Z field */ | ||
232 | cp += INT16SZ; | ||
233 | } else { | ||
234 | __putshort(0, cp); /* EDNS Z field */ | ||
235 | cp += INT16SZ; | ||
236 | } | ||
237 | __putshort(0, cp); /* RDLEN */ | ||
238 | cp += INT16SZ; | ||
239 | hp->arcount = htons(ntohs(hp->arcount) + 1); | ||
240 | |||
241 | return cp - buf; | ||
242 | } | ||
diff --git a/src/lib/libc/net/res_query.c b/src/lib/libc/net/res_query.c new file mode 100644 index 0000000000..3cdb7a7477 --- /dev/null +++ b/src/lib/libc/net/res_query.c | |||
@@ -0,0 +1,407 @@ | |||
1 | /* $OpenBSD: res_query.c,v 1.21 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1988, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1988, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #if defined(LIBC_SCCS) && !defined(lint) | ||
55 | #if 0 | ||
56 | static char sccsid[] = "@(#)res_query.c 8.1 (Berkeley) 6/4/93"; | ||
57 | static char rcsid[] = "$From: res_query.c,v 8.9 1996/09/22 00:13:28 vixie Exp $"; | ||
58 | #else | ||
59 | static char rcsid[] = "$OpenBSD: res_query.c,v 1.21 2003/06/02 20:18:36 millert Exp $"; | ||
60 | #endif | ||
61 | #endif /* LIBC_SCCS and not lint */ | ||
62 | |||
63 | #include <sys/types.h> | ||
64 | #include <sys/param.h> | ||
65 | #include <netinet/in.h> | ||
66 | #include <arpa/inet.h> | ||
67 | #include <arpa/nameser.h> | ||
68 | |||
69 | #include <stdio.h> | ||
70 | #include <netdb.h> | ||
71 | #include <resolv.h> | ||
72 | #include <ctype.h> | ||
73 | #include <errno.h> | ||
74 | #include <stdlib.h> | ||
75 | #include <string.h> | ||
76 | #include <unistd.h> | ||
77 | |||
78 | #include "thread_private.h" | ||
79 | |||
80 | #if PACKETSZ > 1024 | ||
81 | #define MAXPACKET PACKETSZ | ||
82 | #else | ||
83 | #define MAXPACKET 1024 | ||
84 | #endif | ||
85 | |||
86 | const char *hostalias(const char *); | ||
87 | int h_errno; | ||
88 | extern int res_opt(int, u_char *, int, int); | ||
89 | |||
90 | /* | ||
91 | * Formulate a normal query, send, and await answer. | ||
92 | * Returned answer is placed in supplied buffer "answer". | ||
93 | * Perform preliminary check of answer, returning success only | ||
94 | * if no error is indicated and the answer count is nonzero. | ||
95 | * Return the size of the response on success, -1 on error. | ||
96 | * Error number is left in h_errno. | ||
97 | * | ||
98 | * Caller must parse answer and determine whether it answers the question. | ||
99 | */ | ||
100 | int | ||
101 | res_query(name, class, type, answer, anslen) | ||
102 | const char *name; /* domain name */ | ||
103 | int class, type; /* class and type of query */ | ||
104 | u_char *answer; /* buffer to put answer */ | ||
105 | int anslen; /* size of answer buffer */ | ||
106 | { | ||
107 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
108 | u_char buf[MAXPACKET]; | ||
109 | register HEADER *hp = (HEADER *) answer; | ||
110 | int n; | ||
111 | |||
112 | hp->rcode = NOERROR; /* default */ | ||
113 | |||
114 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
115 | h_errno = NETDB_INTERNAL; | ||
116 | return (-1); | ||
117 | } | ||
118 | #ifdef DEBUG | ||
119 | if (_resp->options & RES_DEBUG) | ||
120 | printf(";; res_query(%s, %d, %d)\n", name, class, type); | ||
121 | #endif | ||
122 | |||
123 | n = res_mkquery(QUERY, name, class, type, NULL, 0, NULL, | ||
124 | buf, sizeof(buf)); | ||
125 | if (n > 0 && ((_resp->options & RES_USE_EDNS0) || | ||
126 | (_resp->options & RES_USE_DNSSEC))) { | ||
127 | n = res_opt(n, buf, sizeof(buf), anslen); | ||
128 | } | ||
129 | |||
130 | if (n <= 0) { | ||
131 | #ifdef DEBUG | ||
132 | if (_resp->options & RES_DEBUG) | ||
133 | printf(";; res_query: mkquery failed\n"); | ||
134 | #endif | ||
135 | h_errno = NO_RECOVERY; | ||
136 | return (n); | ||
137 | } | ||
138 | n = res_send(buf, n, answer, anslen); | ||
139 | if (n < 0) { | ||
140 | #ifdef DEBUG | ||
141 | if (_resp->options & RES_DEBUG) | ||
142 | printf(";; res_query: send error\n"); | ||
143 | #endif | ||
144 | h_errno = TRY_AGAIN; | ||
145 | return (n); | ||
146 | } | ||
147 | |||
148 | if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { | ||
149 | #ifdef DEBUG | ||
150 | if (_resp->options & RES_DEBUG) | ||
151 | printf(";; rcode = %u, ancount=%u\n", hp->rcode, | ||
152 | ntohs(hp->ancount)); | ||
153 | #endif | ||
154 | switch (hp->rcode) { | ||
155 | case NXDOMAIN: | ||
156 | h_errno = HOST_NOT_FOUND; | ||
157 | break; | ||
158 | case SERVFAIL: | ||
159 | h_errno = TRY_AGAIN; | ||
160 | break; | ||
161 | case NOERROR: | ||
162 | h_errno = NO_DATA; | ||
163 | break; | ||
164 | case FORMERR: | ||
165 | case NOTIMP: | ||
166 | case REFUSED: | ||
167 | default: | ||
168 | h_errno = NO_RECOVERY; | ||
169 | break; | ||
170 | } | ||
171 | return (-1); | ||
172 | } | ||
173 | return (n); | ||
174 | } | ||
175 | |||
176 | /* | ||
177 | * Formulate a normal query, send, and retrieve answer in supplied buffer. | ||
178 | * Return the size of the response on success, -1 on error. | ||
179 | * If enabled, implement search rules until answer or unrecoverable failure | ||
180 | * is detected. Error code, if any, is left in h_errno. | ||
181 | */ | ||
182 | int | ||
183 | res_search(name, class, type, answer, anslen) | ||
184 | const char *name; /* domain name */ | ||
185 | int class, type; /* class and type of query */ | ||
186 | u_char *answer; /* buffer to put answer */ | ||
187 | int anslen; /* size of answer */ | ||
188 | { | ||
189 | register const char *cp, * const *domain; | ||
190 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
191 | HEADER *hp = (HEADER *) answer; | ||
192 | u_int dots; | ||
193 | int trailing_dot, ret, saved_herrno; | ||
194 | int got_nodata = 0, got_servfail = 0, tried_as_is = 0; | ||
195 | |||
196 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
197 | h_errno = NETDB_INTERNAL; | ||
198 | return (-1); | ||
199 | } | ||
200 | errno = 0; | ||
201 | h_errno = HOST_NOT_FOUND; /* default, if we never query */ | ||
202 | dots = 0; | ||
203 | for (cp = name; *cp; cp++) | ||
204 | dots += (*cp == '.'); | ||
205 | trailing_dot = 0; | ||
206 | if (cp > name && *--cp == '.') | ||
207 | trailing_dot++; | ||
208 | |||
209 | /* | ||
210 | * if there aren't any dots, it could be a user-level alias | ||
211 | */ | ||
212 | if (!dots && (cp = __hostalias(name)) != NULL) | ||
213 | return (res_query(cp, class, type, answer, anslen)); | ||
214 | |||
215 | /* | ||
216 | * If there are dots in the name already, let's just give it a try | ||
217 | * 'as is'. The threshold can be set with the "ndots" option. | ||
218 | */ | ||
219 | saved_herrno = -1; | ||
220 | if (dots >= _resp->ndots) { | ||
221 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | ||
222 | if (ret > 0) | ||
223 | return (ret); | ||
224 | saved_herrno = h_errno; | ||
225 | tried_as_is++; | ||
226 | } | ||
227 | |||
228 | /* | ||
229 | * We do at least one level of search if | ||
230 | * - there is no dot and RES_DEFNAME is set, or | ||
231 | * - there is at least one dot, there is no trailing dot, | ||
232 | * and RES_DNSRCH is set. | ||
233 | */ | ||
234 | if ((!dots && (_resp->options & RES_DEFNAMES)) || | ||
235 | (dots && !trailing_dot && (_resp->options & RES_DNSRCH))) { | ||
236 | int done = 0; | ||
237 | |||
238 | for (domain = (const char * const *)_resp->dnsrch; | ||
239 | *domain && !done; | ||
240 | domain++) { | ||
241 | |||
242 | ret = res_querydomain(name, *domain, class, type, | ||
243 | answer, anslen); | ||
244 | if (ret > 0) | ||
245 | return (ret); | ||
246 | |||
247 | /* | ||
248 | * If no server present, give up. | ||
249 | * If name isn't found in this domain, | ||
250 | * keep trying higher domains in the search list | ||
251 | * (if that's enabled). | ||
252 | * On a NO_DATA error, keep trying, otherwise | ||
253 | * a wildcard entry of another type could keep us | ||
254 | * from finding this entry higher in the domain. | ||
255 | * If we get some other error (negative answer or | ||
256 | * server failure), then stop searching up, | ||
257 | * but try the input name below in case it's | ||
258 | * fully-qualified. | ||
259 | */ | ||
260 | if (errno == ECONNREFUSED) { | ||
261 | h_errno = TRY_AGAIN; | ||
262 | return (-1); | ||
263 | } | ||
264 | |||
265 | switch (h_errno) { | ||
266 | case NO_DATA: | ||
267 | got_nodata++; | ||
268 | /* FALLTHROUGH */ | ||
269 | case HOST_NOT_FOUND: | ||
270 | /* keep trying */ | ||
271 | break; | ||
272 | case TRY_AGAIN: | ||
273 | if (hp->rcode == SERVFAIL) { | ||
274 | /* try next search element, if any */ | ||
275 | got_servfail++; | ||
276 | break; | ||
277 | } | ||
278 | /* FALLTHROUGH */ | ||
279 | default: | ||
280 | /* anything else implies that we're done */ | ||
281 | done++; | ||
282 | } | ||
283 | |||
284 | /* if we got here for some reason other than DNSRCH, | ||
285 | * we only wanted one iteration of the loop, so stop. | ||
286 | */ | ||
287 | if (!(_resp->options & RES_DNSRCH)) | ||
288 | done++; | ||
289 | } | ||
290 | } | ||
291 | |||
292 | /* if we have not already tried the name "as is", do that now. | ||
293 | * note that we do this regardless of how many dots were in the | ||
294 | * name or whether it ends with a dot. | ||
295 | */ | ||
296 | if (!tried_as_is) { | ||
297 | ret = res_querydomain(name, NULL, class, type, answer, anslen); | ||
298 | if (ret > 0) | ||
299 | return (ret); | ||
300 | } | ||
301 | |||
302 | /* if we got here, we didn't satisfy the search. | ||
303 | * if we did an initial full query, return that query's h_errno | ||
304 | * (note that we wouldn't be here if that query had succeeded). | ||
305 | * else if we ever got a nodata, send that back as the reason. | ||
306 | * else send back meaningless h_errno, that being the one from | ||
307 | * the last DNSRCH we did. | ||
308 | */ | ||
309 | if (saved_herrno != -1) | ||
310 | h_errno = saved_herrno; | ||
311 | else if (got_nodata) | ||
312 | h_errno = NO_DATA; | ||
313 | else if (got_servfail) | ||
314 | h_errno = TRY_AGAIN; | ||
315 | return (-1); | ||
316 | } | ||
317 | |||
318 | /* | ||
319 | * Perform a call on res_query on the concatenation of name and domain, | ||
320 | * removing a trailing dot from name if domain is NULL. | ||
321 | */ | ||
322 | int | ||
323 | res_querydomain(name, domain, class, type, answer, anslen) | ||
324 | const char *name, *domain; | ||
325 | int class, type; /* class and type of query */ | ||
326 | u_char *answer; /* buffer to put answer */ | ||
327 | int anslen; /* size of answer */ | ||
328 | { | ||
329 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
330 | char nbuf[MAXDNAME*2+1+1]; | ||
331 | const char *longname = nbuf; | ||
332 | int n; | ||
333 | |||
334 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
335 | h_errno = NETDB_INTERNAL; | ||
336 | return (-1); | ||
337 | } | ||
338 | #ifdef DEBUG | ||
339 | if (_resp->options & RES_DEBUG) | ||
340 | printf(";; res_querydomain(%s, %s, %d, %d)\n", | ||
341 | name, domain?domain:"<Nil>", class, type); | ||
342 | #endif | ||
343 | if (domain == NULL) { | ||
344 | /* | ||
345 | * Check for trailing '.'; | ||
346 | * copy without '.' if present. | ||
347 | */ | ||
348 | n = strlen(name) - 1; | ||
349 | if (n != (0 - 1) && name[n] == '.' && n < sizeof(nbuf) - 1) { | ||
350 | bcopy(name, nbuf, n); | ||
351 | nbuf[n] = '\0'; | ||
352 | } else | ||
353 | longname = name; | ||
354 | } else | ||
355 | snprintf(nbuf, sizeof nbuf, "%.*s.%.*s", | ||
356 | MAXDNAME, name, MAXDNAME, domain); | ||
357 | |||
358 | return (res_query(longname, class, type, answer, anslen)); | ||
359 | } | ||
360 | |||
361 | const char * | ||
362 | hostalias(name) | ||
363 | register const char *name; | ||
364 | { | ||
365 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
366 | register char *cp1, *cp2; | ||
367 | FILE *fp; | ||
368 | char *file; | ||
369 | char buf[BUFSIZ]; | ||
370 | static char abuf[MAXDNAME]; | ||
371 | size_t len; | ||
372 | |||
373 | if (_resp->options & RES_NOALIASES) | ||
374 | return (NULL); | ||
375 | file = getenv("HOSTALIASES"); | ||
376 | if (issetugid() != 0 || file == NULL || (fp = fopen(file, "r")) == NULL) | ||
377 | return (NULL); | ||
378 | setbuf(fp, NULL); | ||
379 | while ((cp1 = fgetln(fp, &len)) != NULL) { | ||
380 | if (cp1[len-1] == '\n') | ||
381 | len--; | ||
382 | if (len >= sizeof(buf) || len == 0) | ||
383 | continue; | ||
384 | (void)memcpy(buf, cp1, len); | ||
385 | buf[len] = '\0'; | ||
386 | |||
387 | for (cp1 = buf; *cp1 && !isspace(*cp1); ++cp1) | ||
388 | ; | ||
389 | if (!*cp1) | ||
390 | break; | ||
391 | *cp1 = '\0'; | ||
392 | if (!strcasecmp(buf, name)) { | ||
393 | while (isspace(*++cp1)) | ||
394 | ; | ||
395 | if (!*cp1) | ||
396 | break; | ||
397 | for (cp2 = cp1 + 1; *cp2 && !isspace(*cp2); ++cp2) | ||
398 | ; | ||
399 | *cp2 = '\0'; | ||
400 | strlcpy(abuf, cp1, sizeof(abuf)); | ||
401 | fclose(fp); | ||
402 | return (abuf); | ||
403 | } | ||
404 | } | ||
405 | fclose(fp); | ||
406 | return (NULL); | ||
407 | } | ||
diff --git a/src/lib/libc/net/res_random.c b/src/lib/libc/net/res_random.c new file mode 100644 index 0000000000..c739e4a952 --- /dev/null +++ b/src/lib/libc/net/res_random.c | |||
@@ -0,0 +1,229 @@ | |||
1 | /* $OpenBSD: res_random.c,v 1.12 2002/06/27 10:14:02 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright 1997 Niels Provos <provos@physnet.uni-hamburg.de> | ||
5 | * All rights reserved. | ||
6 | * | ||
7 | * Theo de Raadt <deraadt@openbsd.org> came up with the idea of using | ||
8 | * such a mathematical system to generate more random (yet non-repeating) | ||
9 | * ids to solve the resolver/named problem. But Niels designed the | ||
10 | * actual system based on the constraints. | ||
11 | * | ||
12 | * Redistribution and use in source and binary forms, with or without | ||
13 | * modification, are permitted provided that the following conditions | ||
14 | * are met: | ||
15 | * 1. Redistributions of source code must retain the above copyright | ||
16 | * notice, this list of conditions and the following disclaimer. | ||
17 | * 2. Redistributions in binary form must reproduce the above copyright | ||
18 | * notice, this list of conditions and the following disclaimer in the | ||
19 | * documentation and/or other materials provided with the distribution. | ||
20 | * 3. All advertising materials mentioning features or use of this software | ||
21 | * must display the following acknowledgement: | ||
22 | * This product includes software developed by Niels Provos. | ||
23 | * 4. The name of the author may not be used to endorse or promote products | ||
24 | * derived from this software without specific prior written permission. | ||
25 | * | ||
26 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
27 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
28 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
29 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
30 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
31 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
32 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
33 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
34 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
35 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
36 | */ | ||
37 | |||
38 | /* | ||
39 | * seed = random 15bit | ||
40 | * n = prime, g0 = generator to n, | ||
41 | * j = random so that gcd(j,n-1) == 1 | ||
42 | * g = g0^j mod n will be a generator again. | ||
43 | * | ||
44 | * X[0] = random seed. | ||
45 | * X[n] = a*X[n-1]+b mod m is a Linear Congruential Generator | ||
46 | * with a = 7^(even random) mod m, | ||
47 | * b = random with gcd(b,m) == 1 | ||
48 | * m = 31104 and a maximal period of m-1. | ||
49 | * | ||
50 | * The transaction id is determined by: | ||
51 | * id[n] = seed xor (g^X[n] mod n) | ||
52 | * | ||
53 | * Effectivly the id is restricted to the lower 15 bits, thus | ||
54 | * yielding two different cycles by toggling the msb on and off. | ||
55 | * This avoids reuse issues caused by reseeding. | ||
56 | * | ||
57 | * The 16 bit space is very small and brute force attempts are | ||
58 | * entirly feasible, we skip a random number of transaction ids | ||
59 | * so that an attacker will not get sequential ids. | ||
60 | */ | ||
61 | |||
62 | #include <sys/types.h> | ||
63 | #include <netinet/in.h> | ||
64 | #include <sys/time.h> | ||
65 | #include <resolv.h> | ||
66 | |||
67 | #include <unistd.h> | ||
68 | #include <stdlib.h> | ||
69 | #include <string.h> | ||
70 | |||
71 | #define RU_OUT 180 /* Time after wich will be reseeded */ | ||
72 | #define RU_MAX 30000 /* Uniq cycle, avoid blackjack prediction */ | ||
73 | #define RU_GEN 2 /* Starting generator */ | ||
74 | #define RU_N 32749 /* RU_N-1 = 2*2*3*2729 */ | ||
75 | #define RU_AGEN 7 /* determine ru_a as RU_AGEN^(2*rand) */ | ||
76 | #define RU_M 31104 /* RU_M = 2^7*3^5 - don't change */ | ||
77 | |||
78 | #define PFAC_N 3 | ||
79 | const static u_int16_t pfacts[PFAC_N] = { | ||
80 | 2, | ||
81 | 3, | ||
82 | 2729 | ||
83 | }; | ||
84 | |||
85 | static u_int16_t ru_x; | ||
86 | static u_int16_t ru_seed, ru_seed2; | ||
87 | static u_int16_t ru_a, ru_b; | ||
88 | static u_int16_t ru_g; | ||
89 | static u_int16_t ru_counter = 0; | ||
90 | static u_int16_t ru_msb = 0; | ||
91 | static long ru_reseed; | ||
92 | static u_int32_t tmp; /* Storage for unused random */ | ||
93 | static struct timeval tv; | ||
94 | |||
95 | static u_int16_t pmod(u_int16_t, u_int16_t, u_int16_t); | ||
96 | static void res_initid(void); | ||
97 | |||
98 | /* | ||
99 | * Do a fast modular exponation, returned value will be in the range | ||
100 | * of 0 - (mod-1) | ||
101 | */ | ||
102 | |||
103 | static u_int16_t | ||
104 | pmod(u_int16_t gen, u_int16_t exp, u_int16_t mod) | ||
105 | { | ||
106 | u_int16_t s, t, u; | ||
107 | |||
108 | s = 1; | ||
109 | t = gen; | ||
110 | u = exp; | ||
111 | |||
112 | while (u) { | ||
113 | if (u & 1) | ||
114 | s = (s*t) % mod; | ||
115 | u >>= 1; | ||
116 | t = (t*t) % mod; | ||
117 | } | ||
118 | return (s); | ||
119 | } | ||
120 | |||
121 | /* | ||
122 | * Initializes the seed and chooses a suitable generator. Also toggles | ||
123 | * the msb flag. The msb flag is used to generate two distinct | ||
124 | * cycles of random numbers and thus avoiding reuse of ids. | ||
125 | * | ||
126 | * This function is called from res_randomid() when needed, an | ||
127 | * application does not have to worry about it. | ||
128 | */ | ||
129 | static void | ||
130 | res_initid() | ||
131 | { | ||
132 | u_int16_t j, i; | ||
133 | int noprime = 1; | ||
134 | |||
135 | tmp = arc4random(); | ||
136 | ru_x = (tmp & 0xFFFF) % RU_M; | ||
137 | |||
138 | /* 15 bits of random seed */ | ||
139 | ru_seed = (tmp >> 16) & 0x7FFF; | ||
140 | tmp = arc4random(); | ||
141 | ru_seed2 = tmp & 0x7FFF; | ||
142 | |||
143 | tmp = arc4random(); | ||
144 | |||
145 | /* Determine the LCG we use */ | ||
146 | ru_b = (tmp & 0xfffe) | 1; | ||
147 | ru_a = pmod(RU_AGEN, (tmp >> 16) & 0xfffe, RU_M); | ||
148 | while (ru_b % 3 == 0) | ||
149 | ru_b += 2; | ||
150 | |||
151 | tmp = arc4random(); | ||
152 | j = tmp % RU_N; | ||
153 | tmp = tmp >> 16; | ||
154 | |||
155 | /* | ||
156 | * Do a fast gcd(j,RU_N-1), so we can find a j with | ||
157 | * gcd(j, RU_N-1) == 1, giving a new generator for | ||
158 | * RU_GEN^j mod RU_N | ||
159 | */ | ||
160 | |||
161 | while (noprime) { | ||
162 | for (i=0; i<PFAC_N; i++) | ||
163 | if (j%pfacts[i] == 0) | ||
164 | break; | ||
165 | |||
166 | if (i>=PFAC_N) | ||
167 | noprime = 0; | ||
168 | else | ||
169 | j = (j+1) % RU_N; | ||
170 | } | ||
171 | |||
172 | ru_g = pmod(RU_GEN,j,RU_N); | ||
173 | ru_counter = 0; | ||
174 | |||
175 | gettimeofday(&tv, NULL); | ||
176 | ru_reseed = tv.tv_sec + RU_OUT; | ||
177 | ru_msb = ru_msb == 0x8000 ? 0 : 0x8000; | ||
178 | } | ||
179 | |||
180 | u_int | ||
181 | res_randomid() | ||
182 | { | ||
183 | int i, n; | ||
184 | |||
185 | gettimeofday(&tv, NULL); | ||
186 | if (ru_counter >= RU_MAX || tv.tv_sec > ru_reseed) | ||
187 | res_initid(); | ||
188 | |||
189 | if (!tmp) | ||
190 | tmp = arc4random(); | ||
191 | |||
192 | /* Skip a random number of ids */ | ||
193 | n = tmp & 0x7; tmp = tmp >> 3; | ||
194 | if (ru_counter + n >= RU_MAX) | ||
195 | res_initid(); | ||
196 | |||
197 | for (i=0; i<=n; i++) | ||
198 | /* Linear Congruential Generator */ | ||
199 | ru_x = (ru_a*ru_x + ru_b) % RU_M; | ||
200 | |||
201 | ru_counter += i; | ||
202 | |||
203 | return (ru_seed ^ pmod(ru_g,ru_seed2 ^ ru_x,RU_N)) | ru_msb; | ||
204 | } | ||
205 | |||
206 | #if 0 | ||
207 | void | ||
208 | main(int argc, char **argv) | ||
209 | { | ||
210 | int i, n; | ||
211 | u_int16_t wert; | ||
212 | |||
213 | res_initid(); | ||
214 | |||
215 | printf("Generator: %u\n", ru_g); | ||
216 | printf("Seed: %u\n", ru_seed); | ||
217 | printf("Reseed at %ld\n", ru_reseed); | ||
218 | printf("Ru_X: %u\n", ru_x); | ||
219 | printf("Ru_A: %u\n", ru_a); | ||
220 | printf("Ru_B: %u\n", ru_b); | ||
221 | |||
222 | n = atoi(argv[1]); | ||
223 | for (i=0;i<n;i++) { | ||
224 | wert = res_randomid(); | ||
225 | printf("%06d\n", wert); | ||
226 | } | ||
227 | } | ||
228 | #endif | ||
229 | |||
diff --git a/src/lib/libc/net/res_send.c b/src/lib/libc/net/res_send.c new file mode 100644 index 0000000000..c456a92d32 --- /dev/null +++ b/src/lib/libc/net/res_send.c | |||
@@ -0,0 +1,898 @@ | |||
1 | /* $OpenBSD: res_send.c,v 1.15 2003/06/02 20:18:36 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * ++Copyright++ 1985, 1989, 1993 | ||
5 | * - | ||
6 | * Copyright (c) 1985, 1989, 1993 | ||
7 | * The Regents of the University of California. All rights reserved. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | * - | ||
33 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. | ||
34 | * | ||
35 | * Permission to use, copy, modify, and distribute this software for any | ||
36 | * purpose with or without fee is hereby granted, provided that the above | ||
37 | * copyright notice and this permission notice appear in all copies, and that | ||
38 | * the name of Digital Equipment Corporation not be used in advertising or | ||
39 | * publicity pertaining to distribution of the document or software without | ||
40 | * specific, written prior permission. | ||
41 | * | ||
42 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL | ||
43 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | ||
44 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | ||
45 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | ||
46 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | ||
47 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | ||
48 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | ||
49 | * SOFTWARE. | ||
50 | * - | ||
51 | * --Copyright-- | ||
52 | */ | ||
53 | |||
54 | #ifndef INET6 | ||
55 | #define INET6 | ||
56 | #endif | ||
57 | |||
58 | #if defined(LIBC_SCCS) && !defined(lint) | ||
59 | #if 0 | ||
60 | static char sccsid[] = "@(#)res_send.c 8.1 (Berkeley) 6/4/93"; | ||
61 | static char rcsid[] = "$From: res_send.c,v 8.12 1996/10/08 04:51:06 vixie Exp $"; | ||
62 | #else | ||
63 | static char rcsid[] = "$OpenBSD: res_send.c,v 1.15 2003/06/02 20:18:36 millert Exp $"; | ||
64 | #endif | ||
65 | #endif /* LIBC_SCCS and not lint */ | ||
66 | |||
67 | /* change this to "0" | ||
68 | * if you talk to a lot | ||
69 | * of multi-homed SunOS | ||
70 | * ("broken") name servers. | ||
71 | */ | ||
72 | #define CHECK_SRVR_ADDR 1 /* XXX - should be in options.h */ | ||
73 | |||
74 | /* | ||
75 | * Send query to name server and wait for reply. | ||
76 | */ | ||
77 | |||
78 | #include <sys/types.h> | ||
79 | #include <sys/param.h> | ||
80 | #include <sys/time.h> | ||
81 | #include <sys/socket.h> | ||
82 | #include <sys/uio.h> | ||
83 | #include <netinet/in.h> | ||
84 | #include <arpa/nameser.h> | ||
85 | #include <arpa/inet.h> | ||
86 | |||
87 | #include <stdio.h> | ||
88 | #include <netdb.h> | ||
89 | #include <errno.h> | ||
90 | #include <resolv.h> | ||
91 | #include <stdlib.h> | ||
92 | #include <string.h> | ||
93 | #include <unistd.h> | ||
94 | |||
95 | #include "thread_private.h" | ||
96 | |||
97 | static int s = -1; /* socket used for communications */ | ||
98 | static int connected = 0; /* is the socket connected */ | ||
99 | static int vc = 0; /* is the socket a virtual ciruit? */ | ||
100 | static int af = 0; /* address family of socket */ | ||
101 | |||
102 | #ifndef FD_SET | ||
103 | /* XXX - should be in portability.h */ | ||
104 | #define NFDBITS 32 | ||
105 | #define FD_SETSIZE 32 | ||
106 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) | ||
107 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) | ||
108 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) | ||
109 | #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) | ||
110 | #endif | ||
111 | |||
112 | #define CAN_RECONNECT 1 | ||
113 | |||
114 | #ifndef DEBUG | ||
115 | # define Dprint(cond, args) /*empty*/ | ||
116 | # define DprintQ(cond, args, query, size) /*empty*/ | ||
117 | # define Aerror(file, string, error, address) /*empty*/ | ||
118 | # define Perror(file, string, error) /*empty*/ | ||
119 | #else | ||
120 | # define Dprint(cond, args) if (cond) {fprintf args;} else {} | ||
121 | # define DprintQ(cond, args, query, size) if (cond) {\ | ||
122 | fprintf args;\ | ||
123 | __fp_nquery(query, size, stdout);\ | ||
124 | } else {} | ||
125 | static char abuf[NI_MAXHOST]; | ||
126 | static char pbuf[NI_MAXSERV]; | ||
127 | static void Aerror(FILE *, char *, int, struct sockaddr *); | ||
128 | static void Perror(FILE *, char *, int); | ||
129 | |||
130 | static void | ||
131 | Aerror(file, string, error, address) | ||
132 | FILE *file; | ||
133 | char *string; | ||
134 | int error; | ||
135 | struct sockaddr *address; | ||
136 | { | ||
137 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
138 | int save = errno; | ||
139 | |||
140 | if (_resp->options & RES_DEBUG) { | ||
141 | if (getnameinfo(address, address->sa_len, abuf, sizeof(abuf), | ||
142 | pbuf, sizeof(pbuf), | ||
143 | NI_NUMERICHOST|NI_NUMERICSERV|NI_WITHSCOPEID) != 0) { | ||
144 | strlcpy(abuf, "?", sizeof(abuf)); | ||
145 | strlcpy(pbuf, "?", sizeof(pbuf)); | ||
146 | } | ||
147 | fprintf(file, "res_send: %s ([%s].%s): %s\n", | ||
148 | string, abuf, pbuf, strerror(error)); | ||
149 | } | ||
150 | errno = save; | ||
151 | } | ||
152 | static void | ||
153 | Perror(file, string, error) | ||
154 | FILE *file; | ||
155 | char *string; | ||
156 | int error; | ||
157 | { | ||
158 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
159 | int save = errno; | ||
160 | |||
161 | if (_resp->options & RES_DEBUG) { | ||
162 | fprintf(file, "res_send: %s: %s\n", | ||
163 | string, strerror(error)); | ||
164 | } | ||
165 | errno = save; | ||
166 | } | ||
167 | #endif | ||
168 | |||
169 | static res_send_qhook Qhook = NULL; | ||
170 | static res_send_rhook Rhook = NULL; | ||
171 | |||
172 | void | ||
173 | res_send_setqhook(hook) | ||
174 | res_send_qhook hook; | ||
175 | { | ||
176 | |||
177 | Qhook = hook; | ||
178 | } | ||
179 | |||
180 | void | ||
181 | res_send_setrhook(hook) | ||
182 | res_send_rhook hook; | ||
183 | { | ||
184 | |||
185 | Rhook = hook; | ||
186 | } | ||
187 | |||
188 | #ifdef INET6 | ||
189 | static struct sockaddr * get_nsaddr(size_t); | ||
190 | |||
191 | /* | ||
192 | * pick appropriate nsaddr_list for use. see res_init() for initialization. | ||
193 | */ | ||
194 | static struct sockaddr * | ||
195 | get_nsaddr(n) | ||
196 | size_t n; | ||
197 | { | ||
198 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
199 | struct __res_state_ext *_res_extp = _THREAD_PRIVATE(_res_ext, _res_ext, | ||
200 | &_res_ext); | ||
201 | |||
202 | if (!_resp->nsaddr_list[n].sin_family) { | ||
203 | /* | ||
204 | * - _res_extp->nsaddr_list[n] holds an address that is larger | ||
205 | * than struct sockaddr, and | ||
206 | * - user code did not update _resp->nsaddr_list[n]. | ||
207 | */ | ||
208 | return (struct sockaddr *)&_res_extp->nsaddr_list[n]; | ||
209 | } else { | ||
210 | /* | ||
211 | * - user code updated _res.nsaddr_list[n], or | ||
212 | * - _resp->nsaddr_list[n] has the same content as | ||
213 | * _res_extp->nsaddr_list[n]. | ||
214 | */ | ||
215 | return (struct sockaddr *)&_resp->nsaddr_list[n]; | ||
216 | } | ||
217 | } | ||
218 | #else | ||
219 | #define get_nsaddr(n) ((struct sockaddr *)&_resp->nsaddr_list[(n)]) | ||
220 | #endif | ||
221 | |||
222 | /* int | ||
223 | * res_isourserver(ina) | ||
224 | * looks up "ina" in _resp->ns_addr_list[] | ||
225 | * returns: | ||
226 | * 0 : not found | ||
227 | * >0 : found | ||
228 | * author: | ||
229 | * paul vixie, 29may94 | ||
230 | */ | ||
231 | int | ||
232 | res_isourserver(inp) | ||
233 | const struct sockaddr_in *inp; | ||
234 | { | ||
235 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
236 | #ifdef INET6 | ||
237 | const struct sockaddr_in6 *in6p = (const struct sockaddr_in6 *)inp; | ||
238 | const struct sockaddr_in6 *srv6; | ||
239 | #endif | ||
240 | const struct sockaddr_in *srv; | ||
241 | int ns, ret; | ||
242 | |||
243 | ret = 0; | ||
244 | switch (inp->sin_family) { | ||
245 | #ifdef INET6 | ||
246 | case AF_INET6: | ||
247 | for (ns = 0; ns < _resp->nscount; ns++) { | ||
248 | srv6 = (struct sockaddr_in6 *)get_nsaddr(ns); | ||
249 | if (srv6->sin6_family == in6p->sin6_family && | ||
250 | srv6->sin6_port == in6p->sin6_port && | ||
251 | srv6->sin6_scope_id == in6p->sin6_scope_id && | ||
252 | (IN6_IS_ADDR_UNSPECIFIED(&srv6->sin6_addr) || | ||
253 | IN6_ARE_ADDR_EQUAL(&srv6->sin6_addr, | ||
254 | &in6p->sin6_addr))) { | ||
255 | ret++; | ||
256 | break; | ||
257 | } | ||
258 | } | ||
259 | break; | ||
260 | #endif | ||
261 | case AF_INET: | ||
262 | for (ns = 0; ns < _resp->nscount; ns++) { | ||
263 | srv = (struct sockaddr_in *)get_nsaddr(ns); | ||
264 | if (srv->sin_family == inp->sin_family && | ||
265 | srv->sin_port == inp->sin_port && | ||
266 | (srv->sin_addr.s_addr == INADDR_ANY || | ||
267 | srv->sin_addr.s_addr == inp->sin_addr.s_addr)) { | ||
268 | ret++; | ||
269 | break; | ||
270 | } | ||
271 | } | ||
272 | break; | ||
273 | } | ||
274 | return (ret); | ||
275 | } | ||
276 | |||
277 | /* int | ||
278 | * res_nameinquery(name, type, class, buf, eom) | ||
279 | * look for (name,type,class) in the query section of packet (buf,eom) | ||
280 | * returns: | ||
281 | * -1 : format error | ||
282 | * 0 : not found | ||
283 | * >0 : found | ||
284 | * author: | ||
285 | * paul vixie, 29may94 | ||
286 | */ | ||
287 | int | ||
288 | res_nameinquery(name, type, class, buf, eom) | ||
289 | const char *name; | ||
290 | register int type, class; | ||
291 | const u_char *buf, *eom; | ||
292 | { | ||
293 | register const u_char *cp = buf + HFIXEDSZ; | ||
294 | int qdcount = ntohs(((HEADER*)buf)->qdcount); | ||
295 | |||
296 | while (qdcount-- > 0) { | ||
297 | char tname[MAXDNAME+1]; | ||
298 | register int n, ttype, tclass; | ||
299 | |||
300 | n = dn_expand(buf, eom, cp, tname, sizeof tname); | ||
301 | if (n < 0) | ||
302 | return (-1); | ||
303 | cp += n; | ||
304 | ttype = _getshort(cp); cp += INT16SZ; | ||
305 | tclass = _getshort(cp); cp += INT16SZ; | ||
306 | if (ttype == type && | ||
307 | tclass == class && | ||
308 | strcasecmp(tname, name) == 0) | ||
309 | return (1); | ||
310 | } | ||
311 | return (0); | ||
312 | } | ||
313 | |||
314 | /* int | ||
315 | * res_queriesmatch(buf1, eom1, buf2, eom2) | ||
316 | * is there a 1:1 mapping of (name,type,class) | ||
317 | * in (buf1,eom1) and (buf2,eom2)? | ||
318 | * returns: | ||
319 | * -1 : format error | ||
320 | * 0 : not a 1:1 mapping | ||
321 | * >0 : is a 1:1 mapping | ||
322 | * author: | ||
323 | * paul vixie, 29may94 | ||
324 | */ | ||
325 | int | ||
326 | res_queriesmatch(buf1, eom1, buf2, eom2) | ||
327 | const u_char *buf1, *eom1; | ||
328 | const u_char *buf2, *eom2; | ||
329 | { | ||
330 | register const u_char *cp = buf1 + HFIXEDSZ; | ||
331 | int qdcount = ntohs(((HEADER*)buf1)->qdcount); | ||
332 | |||
333 | if (qdcount != ntohs(((HEADER*)buf2)->qdcount)) | ||
334 | return (0); | ||
335 | while (qdcount-- > 0) { | ||
336 | char tname[MAXDNAME+1]; | ||
337 | register int n, ttype, tclass; | ||
338 | |||
339 | n = dn_expand(buf1, eom1, cp, tname, sizeof tname); | ||
340 | if (n < 0) | ||
341 | return (-1); | ||
342 | cp += n; | ||
343 | ttype = _getshort(cp); cp += INT16SZ; | ||
344 | tclass = _getshort(cp); cp += INT16SZ; | ||
345 | if (!res_nameinquery(tname, ttype, tclass, buf2, eom2)) | ||
346 | return (0); | ||
347 | } | ||
348 | return (1); | ||
349 | } | ||
350 | |||
351 | int | ||
352 | res_send(buf, buflen, ans, anssiz) | ||
353 | const u_char *buf; | ||
354 | int buflen; | ||
355 | u_char *ans; | ||
356 | int anssiz; | ||
357 | { | ||
358 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
359 | HEADER *hp = (HEADER *) buf; | ||
360 | HEADER *anhp = (HEADER *) ans; | ||
361 | int gotsomewhere, connreset, terrno, try, v_circuit, resplen, ns; | ||
362 | register int n; | ||
363 | u_int badns; /* XXX NSMAX can't exceed #/bits in this var */ | ||
364 | |||
365 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) { | ||
366 | /* errno should have been set by res_init() in this case. */ | ||
367 | return (-1); | ||
368 | } | ||
369 | DprintQ((_resp->options & RES_DEBUG) || (_resp->pfcode & RES_PRF_QUERY), | ||
370 | (stdout, ";; res_send()\n"), buf, buflen); | ||
371 | v_circuit = (_resp->options & RES_USEVC) || buflen > PACKETSZ; | ||
372 | gotsomewhere = 0; | ||
373 | connreset = 0; | ||
374 | terrno = ETIMEDOUT; | ||
375 | badns = 0; | ||
376 | |||
377 | /* | ||
378 | * Send request, RETRY times, or until successful | ||
379 | */ | ||
380 | for (try = 0; try < _resp->retry; try++) { | ||
381 | for (ns = 0; ns < _resp->nscount; ns++) { | ||
382 | struct sockaddr *nsap = get_nsaddr(ns); | ||
383 | socklen_t salen; | ||
384 | |||
385 | if (nsap->sa_len) | ||
386 | salen = nsap->sa_len; | ||
387 | #ifdef INET6 | ||
388 | else if (nsap->sa_family == AF_INET6) | ||
389 | salen = sizeof(struct sockaddr_in6); | ||
390 | #endif | ||
391 | else if (nsap->sa_family == AF_INET) | ||
392 | salen = sizeof(struct sockaddr_in); | ||
393 | else | ||
394 | salen = 0; /*unknown, die on connect*/ | ||
395 | |||
396 | same_ns: | ||
397 | if (badns & (1 << ns)) { | ||
398 | res_close(); | ||
399 | goto next_ns; | ||
400 | } | ||
401 | |||
402 | if (Qhook) { | ||
403 | int done = 0, loops = 0; | ||
404 | |||
405 | do { | ||
406 | res_sendhookact act; | ||
407 | |||
408 | act = (*Qhook)((struct sockaddr_in **)&nsap, | ||
409 | &buf, &buflen, | ||
410 | ans, anssiz, &resplen); | ||
411 | switch (act) { | ||
412 | case res_goahead: | ||
413 | done = 1; | ||
414 | break; | ||
415 | case res_nextns: | ||
416 | res_close(); | ||
417 | goto next_ns; | ||
418 | case res_done: | ||
419 | return (resplen); | ||
420 | case res_modified: | ||
421 | /* give the hook another try */ | ||
422 | if (++loops < 42) /*doug adams*/ | ||
423 | break; | ||
424 | /*FALLTHROUGH*/ | ||
425 | case res_error: | ||
426 | /*FALLTHROUGH*/ | ||
427 | default: | ||
428 | return (-1); | ||
429 | } | ||
430 | } while (!done); | ||
431 | } | ||
432 | |||
433 | Dprint((_resp->options & RES_DEBUG) && | ||
434 | getnameinfo(nsap, salen, abuf, sizeof(abuf), | ||
435 | NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) == 0, | ||
436 | (stdout, ";; Querying server (# %d) address = %s\n", | ||
437 | ns + 1, abuf)); | ||
438 | |||
439 | if (v_circuit) { | ||
440 | int truncated; | ||
441 | struct iovec iov[2]; | ||
442 | u_short len; | ||
443 | u_char *cp; | ||
444 | |||
445 | /* | ||
446 | * Use virtual circuit; | ||
447 | * at most one attempt per server. | ||
448 | */ | ||
449 | try = _resp->retry; | ||
450 | truncated = 0; | ||
451 | if ((s < 0) || (!vc) || (af != nsap->sa_family)) { | ||
452 | if (s >= 0) | ||
453 | res_close(); | ||
454 | |||
455 | af = nsap->sa_family; | ||
456 | s = socket(af, SOCK_STREAM, 0); | ||
457 | if (s < 0) { | ||
458 | terrno = errno; | ||
459 | Perror(stderr, "socket(vc)", errno); | ||
460 | #if 0 | ||
461 | return (-1); | ||
462 | #else | ||
463 | badns |= (1 << ns); | ||
464 | res_close(); | ||
465 | goto next_ns; | ||
466 | #endif | ||
467 | } | ||
468 | errno = 0; | ||
469 | if (connect(s, nsap, salen) < 0) { | ||
470 | terrno = errno; | ||
471 | Aerror(stderr, "connect/vc", | ||
472 | errno, nsap); | ||
473 | badns |= (1 << ns); | ||
474 | res_close(); | ||
475 | goto next_ns; | ||
476 | } | ||
477 | vc = 1; | ||
478 | } | ||
479 | /* | ||
480 | * Send length & message | ||
481 | */ | ||
482 | putshort((u_short)buflen, (u_char*)&len); | ||
483 | iov[0].iov_base = (caddr_t)&len; | ||
484 | iov[0].iov_len = INT16SZ; | ||
485 | iov[1].iov_base = (caddr_t)buf; | ||
486 | iov[1].iov_len = buflen; | ||
487 | if (writev(s, iov, 2) != (INT16SZ + buflen)) { | ||
488 | terrno = errno; | ||
489 | Perror(stderr, "write failed", errno); | ||
490 | badns |= (1 << ns); | ||
491 | res_close(); | ||
492 | goto next_ns; | ||
493 | } | ||
494 | /* | ||
495 | * Receive length & response | ||
496 | */ | ||
497 | read_len: | ||
498 | cp = ans; | ||
499 | len = INT16SZ; | ||
500 | while ((n = read(s, (char *)cp, (int)len)) > 0) { | ||
501 | cp += n; | ||
502 | if ((len -= n) <= 0) | ||
503 | break; | ||
504 | } | ||
505 | if (n <= 0) { | ||
506 | terrno = errno; | ||
507 | Perror(stderr, "read failed", errno); | ||
508 | res_close(); | ||
509 | /* | ||
510 | * A long running process might get its TCP | ||
511 | * connection reset if the remote server was | ||
512 | * restarted. Requery the server instead of | ||
513 | * trying a new one. When there is only one | ||
514 | * server, this means that a query might work | ||
515 | * instead of failing. We only allow one reset | ||
516 | * per query to prevent looping. | ||
517 | */ | ||
518 | if (terrno == ECONNRESET && !connreset) { | ||
519 | connreset = 1; | ||
520 | res_close(); | ||
521 | goto same_ns; | ||
522 | } | ||
523 | res_close(); | ||
524 | goto next_ns; | ||
525 | } | ||
526 | resplen = _getshort(ans); | ||
527 | if (resplen > anssiz) { | ||
528 | Dprint(_resp->options & RES_DEBUG, | ||
529 | (stdout, ";; response truncated\n") | ||
530 | ); | ||
531 | truncated = 1; | ||
532 | len = anssiz; | ||
533 | } else | ||
534 | len = resplen; | ||
535 | cp = ans; | ||
536 | while (len != 0 && | ||
537 | (n = read(s, (char *)cp, (int)len)) > 0) { | ||
538 | cp += n; | ||
539 | len -= n; | ||
540 | } | ||
541 | if (n <= 0) { | ||
542 | terrno = errno; | ||
543 | Perror(stderr, "read(vc)", errno); | ||
544 | res_close(); | ||
545 | goto next_ns; | ||
546 | } | ||
547 | if (truncated) { | ||
548 | /* | ||
549 | * Flush rest of answer | ||
550 | * so connection stays in synch. | ||
551 | */ | ||
552 | anhp->tc = 1; | ||
553 | len = resplen - anssiz; | ||
554 | while (len != 0) { | ||
555 | char junk[PACKETSZ]; | ||
556 | |||
557 | n = (len > sizeof(junk) | ||
558 | ? sizeof(junk) | ||
559 | : len); | ||
560 | if ((n = read(s, junk, n)) > 0) | ||
561 | len -= n; | ||
562 | else | ||
563 | break; | ||
564 | } | ||
565 | } | ||
566 | /* | ||
567 | * The calling applicating has bailed out of | ||
568 | * a previous call and failed to arrange to have | ||
569 | * the circuit closed or the server has got | ||
570 | * itself confused. Anyway drop the packet and | ||
571 | * wait for the correct one. | ||
572 | */ | ||
573 | if (hp->id != anhp->id) { | ||
574 | DprintQ((_resp->options & RES_DEBUG) || | ||
575 | (_resp->pfcode & RES_PRF_REPLY), | ||
576 | (stdout, ";; old answer (unexpected):\n"), | ||
577 | ans, (resplen>anssiz)?anssiz:resplen); | ||
578 | goto read_len; | ||
579 | } | ||
580 | } else { | ||
581 | /* | ||
582 | * Use datagrams. | ||
583 | */ | ||
584 | struct timeval timeout; | ||
585 | fd_set *dsmaskp; | ||
586 | struct sockaddr_storage from; | ||
587 | socklen_t fromlen; | ||
588 | |||
589 | if ((s < 0) || vc || (af != nsap->sa_family)) { | ||
590 | if (vc) | ||
591 | res_close(); | ||
592 | af = nsap->sa_family; | ||
593 | s = socket(af, SOCK_DGRAM, 0); | ||
594 | if (s < 0) { | ||
595 | #if !CAN_RECONNECT | ||
596 | bad_dg_sock: | ||
597 | #endif | ||
598 | terrno = errno; | ||
599 | Perror(stderr, "socket(dg)", errno); | ||
600 | #if 0 | ||
601 | return (-1); | ||
602 | #else | ||
603 | badns |= (1 << ns); | ||
604 | res_close(); | ||
605 | goto next_ns; | ||
606 | #endif | ||
607 | } | ||
608 | #ifdef IPV6_MINMTU | ||
609 | if (af == AF_INET6) { | ||
610 | const int yes = 1; | ||
611 | (void)setsockopt(s, IPPROTO_IPV6, | ||
612 | IPV6_USE_MIN_MTU, &yes, | ||
613 | sizeof(yes)); | ||
614 | } | ||
615 | #endif | ||
616 | connected = 0; | ||
617 | } | ||
618 | /* | ||
619 | * On a 4.3BSD+ machine (client and server, | ||
620 | * actually), sending to a nameserver datagram | ||
621 | * port with no nameserver will cause an | ||
622 | * ICMP port unreachable message to be returned. | ||
623 | * If our datagram socket is "connected" to the | ||
624 | * server, we get an ECONNREFUSED error on the next | ||
625 | * socket operation, and select returns if the | ||
626 | * error message is received. We can thus detect | ||
627 | * the absence of a nameserver without timing out. | ||
628 | * If we have sent queries to at least two servers, | ||
629 | * however, we don't want to remain connected, | ||
630 | * as we wish to receive answers from the first | ||
631 | * server to respond. | ||
632 | */ | ||
633 | if (!(_resp->options & RES_INSECURE1) && | ||
634 | (_resp->nscount == 1 || (try == 0 && ns == 0))) { | ||
635 | /* | ||
636 | * Connect only if we are sure we won't | ||
637 | * receive a response from another server. | ||
638 | */ | ||
639 | if (!connected) { | ||
640 | if (connect(s, nsap, salen) < 0) { | ||
641 | Aerror(stderr, | ||
642 | "connect(dg)", | ||
643 | errno, nsap); | ||
644 | badns |= (1 << ns); | ||
645 | res_close(); | ||
646 | goto next_ns; | ||
647 | } | ||
648 | connected = 1; | ||
649 | } | ||
650 | if (send(s, (char*)buf, buflen, 0) != buflen) { | ||
651 | Perror(stderr, "send", errno); | ||
652 | badns |= (1 << ns); | ||
653 | res_close(); | ||
654 | goto next_ns; | ||
655 | } | ||
656 | } else { | ||
657 | /* | ||
658 | * Disconnect if we want to listen | ||
659 | * for responses from more than one server. | ||
660 | */ | ||
661 | if (connected) { | ||
662 | #if CAN_RECONNECT | ||
663 | #ifdef INET6 | ||
664 | /* XXX: any errornous address */ | ||
665 | #endif /* INET6 */ | ||
666 | struct sockaddr_in no_addr; | ||
667 | |||
668 | no_addr.sin_family = AF_INET; | ||
669 | no_addr.sin_addr.s_addr = INADDR_ANY; | ||
670 | no_addr.sin_port = 0; | ||
671 | (void) connect(s, | ||
672 | (struct sockaddr *) | ||
673 | &no_addr, | ||
674 | sizeof(no_addr)); | ||
675 | #else | ||
676 | int s1 = socket(af, SOCK_DGRAM,0); | ||
677 | if (s1 < 0) | ||
678 | goto bad_dg_sock; | ||
679 | (void) dup2(s1, s); | ||
680 | (void) close(s1); | ||
681 | Dprint(_resp->options & RES_DEBUG, | ||
682 | (stdout, ";; new DG socket\n")) | ||
683 | #endif | ||
684 | #ifdef IPV6_MINMTU | ||
685 | if (af == AF_INET6) { | ||
686 | const int yes = 1; | ||
687 | (void)setsockopt(s, IPPROTO_IPV6, | ||
688 | IPV6_USE_MIN_MTU, &yes, | ||
689 | sizeof(yes)); | ||
690 | } | ||
691 | #endif | ||
692 | connected = 0; | ||
693 | errno = 0; | ||
694 | } | ||
695 | if (sendto(s, (char*)buf, buflen, 0, | ||
696 | nsap, salen) != buflen) { | ||
697 | Aerror(stderr, "sendto", errno, nsap); | ||
698 | badns |= (1 << ns); | ||
699 | res_close(); | ||
700 | goto next_ns; | ||
701 | } | ||
702 | } | ||
703 | |||
704 | /* | ||
705 | * Wait for reply | ||
706 | */ | ||
707 | timeout.tv_sec = (_resp->retrans << try); | ||
708 | if (try > 0) | ||
709 | timeout.tv_sec /= _resp->nscount; | ||
710 | if ((long) timeout.tv_sec <= 0) | ||
711 | timeout.tv_sec = 1; | ||
712 | timeout.tv_usec = 0; | ||
713 | wait: | ||
714 | dsmaskp = (fd_set *)calloc(howmany(s+1, NFDBITS), | ||
715 | sizeof(fd_mask)); | ||
716 | if (dsmaskp == NULL) { | ||
717 | res_close(); | ||
718 | goto next_ns; | ||
719 | } | ||
720 | FD_SET(s, dsmaskp); | ||
721 | n = select(s+1, dsmaskp, (fd_set *)NULL, | ||
722 | (fd_set *)NULL, &timeout); | ||
723 | free(dsmaskp); | ||
724 | if (n < 0) { | ||
725 | if (errno == EINTR) | ||
726 | goto wait; | ||
727 | Perror(stderr, "select", errno); | ||
728 | res_close(); | ||
729 | goto next_ns; | ||
730 | } | ||
731 | if (n == 0) { | ||
732 | /* | ||
733 | * timeout | ||
734 | */ | ||
735 | Dprint(_resp->options & RES_DEBUG, | ||
736 | (stdout, ";; timeout\n")); | ||
737 | gotsomewhere = 1; | ||
738 | res_close(); | ||
739 | goto next_ns; | ||
740 | } | ||
741 | errno = 0; | ||
742 | fromlen = sizeof(from); | ||
743 | resplen = recvfrom(s, (char*)ans, anssiz, 0, | ||
744 | (struct sockaddr *)&from, &fromlen); | ||
745 | if (resplen <= 0) { | ||
746 | Perror(stderr, "recvfrom", errno); | ||
747 | res_close(); | ||
748 | goto next_ns; | ||
749 | } | ||
750 | gotsomewhere = 1; | ||
751 | if (hp->id != anhp->id) { | ||
752 | /* | ||
753 | * response from old query, ignore it. | ||
754 | * XXX - potential security hazard could | ||
755 | * be detected here. | ||
756 | */ | ||
757 | DprintQ((_resp->options & RES_DEBUG) || | ||
758 | (_resp->pfcode & RES_PRF_REPLY), | ||
759 | (stdout, ";; old answer:\n"), | ||
760 | ans, (resplen>anssiz)?anssiz:resplen); | ||
761 | goto wait; | ||
762 | } | ||
763 | #if CHECK_SRVR_ADDR | ||
764 | if (!(_resp->options & RES_INSECURE1) && | ||
765 | !res_isourserver((struct sockaddr_in *)&from)) { | ||
766 | /* | ||
767 | * response from wrong server? ignore it. | ||
768 | * XXX - potential security hazard could | ||
769 | * be detected here. | ||
770 | */ | ||
771 | DprintQ((_resp->options & RES_DEBUG) || | ||
772 | (_resp->pfcode & RES_PRF_REPLY), | ||
773 | (stdout, ";; not our server:\n"), | ||
774 | ans, (resplen>anssiz)?anssiz:resplen); | ||
775 | goto wait; | ||
776 | } | ||
777 | #endif | ||
778 | if (!(_resp->options & RES_INSECURE2) && | ||
779 | !res_queriesmatch(buf, buf + buflen, | ||
780 | ans, ans + anssiz)) { | ||
781 | /* | ||
782 | * response contains wrong query? ignore it. | ||
783 | * XXX - potential security hazard could | ||
784 | * be detected here. | ||
785 | */ | ||
786 | DprintQ((_resp->options & RES_DEBUG) || | ||
787 | (_resp->pfcode & RES_PRF_REPLY), | ||
788 | (stdout, ";; wrong query name:\n"), | ||
789 | ans, (resplen>anssiz)?anssiz:resplen); | ||
790 | goto wait; | ||
791 | } | ||
792 | if (anhp->rcode == SERVFAIL || | ||
793 | anhp->rcode == NOTIMP || | ||
794 | anhp->rcode == REFUSED) { | ||
795 | DprintQ(_resp->options & RES_DEBUG, | ||
796 | (stdout, "server rejected query:\n"), | ||
797 | ans, (resplen>anssiz)?anssiz:resplen); | ||
798 | badns |= (1 << ns); | ||
799 | res_close(); | ||
800 | /* don't retry if called from dig */ | ||
801 | if (!_resp->pfcode) | ||
802 | goto next_ns; | ||
803 | } | ||
804 | if (!(_resp->options & RES_IGNTC) && anhp->tc) { | ||
805 | /* | ||
806 | * get rest of answer; | ||
807 | * use TCP with same server. | ||
808 | */ | ||
809 | Dprint(_resp->options & RES_DEBUG, | ||
810 | (stdout, ";; truncated answer\n")); | ||
811 | v_circuit = 1; | ||
812 | res_close(); | ||
813 | goto same_ns; | ||
814 | } | ||
815 | } /*if vc/dg*/ | ||
816 | Dprint((_resp->options & RES_DEBUG) || | ||
817 | ((_resp->pfcode & RES_PRF_REPLY) && | ||
818 | (_resp->pfcode & RES_PRF_HEAD1)), | ||
819 | (stdout, ";; got answer:\n")); | ||
820 | DprintQ((_resp->options & RES_DEBUG) || | ||
821 | (_resp->pfcode & RES_PRF_REPLY), | ||
822 | (stdout, "%s", ""), | ||
823 | ans, (resplen>anssiz)?anssiz:resplen); | ||
824 | /* | ||
825 | * If using virtual circuits, we assume that the first server | ||
826 | * is preferred over the rest (i.e. it is on the local | ||
827 | * machine) and only keep that one open. | ||
828 | * If we have temporarily opened a virtual circuit, | ||
829 | * or if we haven't been asked to keep a socket open, | ||
830 | * close the socket. | ||
831 | */ | ||
832 | if ((v_circuit && (!(_resp->options & RES_USEVC) || ns != 0)) || | ||
833 | !(_resp->options & RES_STAYOPEN)) { | ||
834 | res_close(); | ||
835 | } | ||
836 | if (Rhook) { | ||
837 | int done = 0, loops = 0; | ||
838 | |||
839 | do { | ||
840 | res_sendhookact act; | ||
841 | |||
842 | act = (*Rhook)((struct sockaddr_in *)nsap, | ||
843 | buf, buflen, | ||
844 | ans, anssiz, &resplen); | ||
845 | switch (act) { | ||
846 | case res_goahead: | ||
847 | case res_done: | ||
848 | done = 1; | ||
849 | break; | ||
850 | case res_nextns: | ||
851 | res_close(); | ||
852 | goto next_ns; | ||
853 | case res_modified: | ||
854 | /* give the hook another try */ | ||
855 | if (++loops < 42) /*doug adams*/ | ||
856 | break; | ||
857 | /*FALLTHROUGH*/ | ||
858 | case res_error: | ||
859 | /*FALLTHROUGH*/ | ||
860 | default: | ||
861 | return (-1); | ||
862 | } | ||
863 | } while (!done); | ||
864 | |||
865 | } | ||
866 | return (resplen); | ||
867 | next_ns: ; | ||
868 | } /*foreach ns*/ | ||
869 | } /*foreach retry*/ | ||
870 | res_close(); | ||
871 | if (!v_circuit) { | ||
872 | if (!gotsomewhere) | ||
873 | errno = ECONNREFUSED; /* no nameservers found */ | ||
874 | else | ||
875 | errno = ETIMEDOUT; /* no answer obtained */ | ||
876 | } else | ||
877 | errno = terrno; | ||
878 | return (-1); | ||
879 | } | ||
880 | |||
881 | /* | ||
882 | * This routine is for closing the socket if a virtual circuit is used and | ||
883 | * the program wants to close it. This provides support for endhostent() | ||
884 | * which expects to close the socket. | ||
885 | * | ||
886 | * This routine is not expected to be user visible. | ||
887 | */ | ||
888 | void | ||
889 | res_close() | ||
890 | { | ||
891 | if (s >= 0) { | ||
892 | (void) close(s); | ||
893 | s = -1; | ||
894 | connected = 0; | ||
895 | vc = 0; | ||
896 | af = 0; | ||
897 | } | ||
898 | } | ||
diff --git a/src/lib/libc/net/resolver.3 b/src/lib/libc/net/resolver.3 new file mode 100644 index 0000000000..577420e9f1 --- /dev/null +++ b/src/lib/libc/net/resolver.3 | |||
@@ -0,0 +1,341 @@ | |||
1 | .\" $OpenBSD: resolver.3,v 1.17 2003/08/08 09:26:02 jmc Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1985, 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt RESOLVER 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm res_query , | ||
35 | .Nm res_search , | ||
36 | .Nm res_mkquery , | ||
37 | .Nm res_send , | ||
38 | .Nm res_init , | ||
39 | .Nm dn_comp , | ||
40 | .Nm dn_expand | ||
41 | .Nd resolver routines | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <sys/types.h> | ||
44 | .Fd #include <netinet/in.h> | ||
45 | .Fd #include <arpa/nameser.h> | ||
46 | .Fd #include <resolv.h> | ||
47 | .Ft int | ||
48 | .Fo res_query | ||
49 | .Fa "char *dname" | ||
50 | .Fa "int class" | ||
51 | .Fa "int type" | ||
52 | .Fa "u_char *answer" | ||
53 | .Fa "int anslen" | ||
54 | .Fc | ||
55 | .Ft int | ||
56 | .Fo res_search | ||
57 | .Fa "char *dname" | ||
58 | .Fa "int class" | ||
59 | .Fa "int type" | ||
60 | .Fa "u_char *answer" | ||
61 | .Fa "int anslen" | ||
62 | .Fc | ||
63 | .Ft int | ||
64 | .Fo res_mkquery | ||
65 | .Fa "int op" | ||
66 | .Fa "char *dname" | ||
67 | .Fa "int class" | ||
68 | .Fa "int type" | ||
69 | .Fa "char *data" | ||
70 | .Fa "int datalen" | ||
71 | .Fa "struct rrec *newrr" | ||
72 | .Fa "char *buf" | ||
73 | .Fa "int buflen" | ||
74 | .Fc | ||
75 | .Ft int | ||
76 | .Fo res_send | ||
77 | .Fa "char *msg" | ||
78 | .Fa "int msglen" | ||
79 | .Fa "char *answer" | ||
80 | .Fa "int anslen" | ||
81 | .Fc | ||
82 | .Ft int | ||
83 | .Fn res_init "void" | ||
84 | .Ft int | ||
85 | .Fo dn_comp | ||
86 | .Fa "char *exp_dn" | ||
87 | .Fa "char *comp_dn" | ||
88 | .Fa "int length" | ||
89 | .Fa "char **dnptrs" | ||
90 | .Fa "char **lastdnptr" | ||
91 | .Fc | ||
92 | .Ft int | ||
93 | .Fo dn_expand | ||
94 | .Fa "u_char *msg" | ||
95 | .Fa "u_char *eomorig" | ||
96 | .Fa "u_char *comp_dn" | ||
97 | .Fa "u_char *exp_dn" | ||
98 | .Fa "int length" | ||
99 | .Fc | ||
100 | .Sh DESCRIPTION | ||
101 | These routines are used for making, sending, and interpreting | ||
102 | query and reply messages with Internet domain name servers. | ||
103 | .Pp | ||
104 | Global configuration and state information that is used by the | ||
105 | resolver routines is kept in the structure | ||
106 | .Li _res . | ||
107 | Most of the values have reasonable defaults and can be ignored. | ||
108 | Options stored in | ||
109 | .Li _res.options | ||
110 | are defined in | ||
111 | .Aq Pa resolv.h | ||
112 | and are as follows. | ||
113 | Options are stored as a simple bit mask containing the bitwise | ||
114 | .Tn OR | ||
115 | of the options enabled. | ||
116 | .Bl -tag -width RES_USE_INET6 | ||
117 | .It Dv RES_INIT | ||
118 | True if the initial name server address and default domain name are | ||
119 | initialized (i.e., | ||
120 | .Fn res_init | ||
121 | has been called). | ||
122 | .It Dv RES_DEBUG | ||
123 | Print debugging messages. | ||
124 | .It Dv RES_AAONLY | ||
125 | Accept authoritative answers only. | ||
126 | With this option, | ||
127 | .Fn res_send | ||
128 | should continue until it finds an authoritative answer or finds an error. | ||
129 | Currently this is not implemented. | ||
130 | .It Dv RES_USEVC | ||
131 | Use | ||
132 | .Tn TCP | ||
133 | connections for queries instead of | ||
134 | .Tn UDP | ||
135 | datagrams. | ||
136 | .It Dv RES_STAYOPEN | ||
137 | Used with | ||
138 | .Dv RES_USEVC | ||
139 | to keep the | ||
140 | .Tn TCP | ||
141 | connection open between queries. | ||
142 | This is useful only in programs that regularly do many queries. | ||
143 | .Tn UDP | ||
144 | should be the normal mode used. | ||
145 | .It Dv RES_IGNTC | ||
146 | Unused currently (ignore truncation errors, i.e., don't retry with | ||
147 | .Tn TCP ) . | ||
148 | .It Dv RES_RECURSE | ||
149 | Set the recursion-desired bit in queries. | ||
150 | This is the default. | ||
151 | .Pf ( Fn res_send | ||
152 | does not do iterative queries and expects the name server | ||
153 | to handle recursion.) | ||
154 | .It Dv RES_DEFNAMES | ||
155 | If set, | ||
156 | .Fn res_search | ||
157 | will append the default domain name to single-component names | ||
158 | (those that do not contain a dot). | ||
159 | This option is enabled by default. | ||
160 | .It Dv RES_DNSRCH | ||
161 | If this option is set, | ||
162 | .Fn res_search | ||
163 | will search for host names in the current domain and in parent domains; see | ||
164 | .Xr hostname 7 . | ||
165 | This is used by the standard host lookup routine | ||
166 | .Xr gethostbyname 3 . | ||
167 | This option is enabled by default. | ||
168 | .It Dv RES_USE_INET6 | ||
169 | Enables support for IPv6-only applications. | ||
170 | This causes IPv4 addresses to be returned as an IPv4 mapped address. | ||
171 | For example, 10.1.1.1 will be returned as ::ffff:10.1.1.1. | ||
172 | The option is not meaningful on | ||
173 | .Ox . | ||
174 | .El | ||
175 | .Pp | ||
176 | The | ||
177 | .Fn res_init | ||
178 | routine reads the configuration file (if any; see | ||
179 | .Xr resolv.conf 5 ) | ||
180 | to get the default domain name, search list, and the Internet address | ||
181 | of the local name server(s). | ||
182 | If no server is configured, the host running | ||
183 | the resolver is tried. | ||
184 | The current domain name is defined by the hostname | ||
185 | if not specified in the configuration file; | ||
186 | it can be overridden by the environment variable | ||
187 | .Ev LOCALDOMAIN . | ||
188 | This environment variable may contain several blank-separated | ||
189 | tokens if you wish to override the | ||
190 | .Fa search list | ||
191 | on a per-process basis. | ||
192 | This is similar to the | ||
193 | .Fa search | ||
194 | command in the configuration file. | ||
195 | Another environment variable | ||
196 | .Ev RES_OPTIONS | ||
197 | can be set to override certain internal resolver options which | ||
198 | are otherwise set by changing fields in the | ||
199 | .Fa _res | ||
200 | structure or are inherited from the configuration file's | ||
201 | .Fa options | ||
202 | command. | ||
203 | The syntax of the | ||
204 | .Ev RES_OPTIONS | ||
205 | environment variable is explained in | ||
206 | .Xr resolv.conf 5 . | ||
207 | Initialization normally occurs on the first call | ||
208 | to one of the following routines. | ||
209 | .Pp | ||
210 | The | ||
211 | .Fn res_query | ||
212 | function provides an interface to the server query mechanism. | ||
213 | It constructs a query, sends it to the local server, | ||
214 | awaits a response, and makes preliminary checks on the reply. | ||
215 | The query requests information of the specified | ||
216 | .Fa type | ||
217 | and | ||
218 | .Fa class | ||
219 | for the specified fully qualified domain name | ||
220 | .Fa dname . | ||
221 | The reply message is left in the | ||
222 | .Fa answer | ||
223 | buffer with length | ||
224 | .Fa anslen | ||
225 | supplied by the caller. | ||
226 | .Pp | ||
227 | The | ||
228 | .Fn res_search | ||
229 | routine makes a query and awaits a response like | ||
230 | .Fn res_query , | ||
231 | but in addition, it implements the default and search rules controlled by the | ||
232 | .Dv RES_DEFNAMES | ||
233 | and | ||
234 | .Dv RES_DNSRCH | ||
235 | options. | ||
236 | It returns the first successful reply. | ||
237 | .Pp | ||
238 | The remaining routines are lower-level routines used by | ||
239 | .Fn res_query . | ||
240 | The | ||
241 | .Fn res_mkquery | ||
242 | function constructs a standard query message and places it in | ||
243 | .Fa buf . | ||
244 | It returns the size of the query, or \-1 if the query is larger than | ||
245 | .Fa buflen . | ||
246 | The query type | ||
247 | .Fa op | ||
248 | is usually | ||
249 | .Dv QUERY , | ||
250 | but can be any of the query types defined in | ||
251 | .Aq Pa arpa/nameser.h . | ||
252 | The domain name for the query is given by | ||
253 | .Fa dname . | ||
254 | .Fa newrr | ||
255 | is currently unused but is intended for making update messages. | ||
256 | .Pp | ||
257 | The | ||
258 | .Fn res_send | ||
259 | routine sends a pre-formatted query and returns an answer. | ||
260 | It will call | ||
261 | .Fn res_init | ||
262 | if | ||
263 | .Dv RES_INIT | ||
264 | is not set, send the query to the local name server, and | ||
265 | handle timeouts and retries. | ||
266 | The length of the reply message is returned, or \-1 if there were errors. | ||
267 | .Pp | ||
268 | The | ||
269 | .Fn dn_comp | ||
270 | function compresses the domain name | ||
271 | .Fa exp_dn | ||
272 | and stores it in | ||
273 | .Fa comp_dn . | ||
274 | The size of the compressed name is returned or \-1 if there were errors. | ||
275 | The size of the array pointed to by | ||
276 | .Fa comp_dn | ||
277 | is given by | ||
278 | .Fa length . | ||
279 | The compression uses an array of pointers | ||
280 | .Fa dnptrs | ||
281 | to previously compressed names in the current message. | ||
282 | The first pointer points | ||
283 | to the beginning of the message and the list ends with | ||
284 | .Dv NULL . | ||
285 | The limit to the array is specified by | ||
286 | .Fa lastdnptr . | ||
287 | A side effect of | ||
288 | .Fn dn_comp | ||
289 | is to update the list of pointers for labels inserted into the message | ||
290 | as the name is compressed. | ||
291 | If | ||
292 | .Em dnptr | ||
293 | is | ||
294 | .Dv NULL , | ||
295 | names are not compressed. | ||
296 | If | ||
297 | .Fa lastdnptr | ||
298 | is | ||
299 | .Dv NULL , | ||
300 | the list of labels is not updated. | ||
301 | .Pp | ||
302 | The | ||
303 | .Fn dn_expand | ||
304 | entry expands the compressed domain name | ||
305 | .Fa comp_dn | ||
306 | to a full domain name | ||
307 | The compressed name is contained in a query or reply message; | ||
308 | .Fa msg | ||
309 | is a pointer to the beginning of the message. | ||
310 | The uncompressed name is placed in the buffer indicated by | ||
311 | .Fa exp_dn | ||
312 | which is of size | ||
313 | .Fa length . | ||
314 | The size of compressed name is returned or \-1 if there was an error. | ||
315 | .Sh FILES | ||
316 | .Bl -tag -width Pa | ||
317 | /etc/resolv.conf | ||
318 | configuration file | ||
319 | see | ||
320 | .Xr resolv.conf 5 . | ||
321 | .El | ||
322 | .Sh SEE ALSO | ||
323 | .Xr gethostbyname 3 , | ||
324 | .Xr resolv.conf 5 , | ||
325 | .Xr hostname 7 , | ||
326 | .Xr named 8 | ||
327 | .Pp | ||
328 | .%T RFC 1032 , | ||
329 | .%T RFC 1033 , | ||
330 | .%T RFC 1034 , | ||
331 | .%T RFC 1035 , | ||
332 | .%T RFC 1535 , | ||
333 | .%T RFC 974 | ||
334 | .Rs | ||
335 | .%T "Name Server Operations Guide for BIND" | ||
336 | .Re | ||
337 | .Sh HISTORY | ||
338 | The | ||
339 | .Nm | ||
340 | function appeared in | ||
341 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/net/rresvport.c b/src/lib/libc/net/rresvport.c new file mode 100644 index 0000000000..6e4698e3be --- /dev/null +++ b/src/lib/libc/net/rresvport.c | |||
@@ -0,0 +1,113 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1995, 1996, 1998 Theo de Raadt. All rights reserved. | ||
3 | * Copyright (c) 1983, 1993, 1994 | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #if defined(LIBC_SCCS) && !defined(lint) | ||
32 | static char *rcsid = "$OpenBSD: rresvport.c,v 1.6 2003/06/03 02:11:35 deraadt Exp $"; | ||
33 | #endif /* LIBC_SCCS and not lint */ | ||
34 | |||
35 | #include <sys/param.h> | ||
36 | #include <sys/socket.h> | ||
37 | #include <sys/stat.h> | ||
38 | |||
39 | #include <netinet/in.h> | ||
40 | #include <arpa/inet.h> | ||
41 | |||
42 | #include <signal.h> | ||
43 | #include <fcntl.h> | ||
44 | #include <netdb.h> | ||
45 | #include <unistd.h> | ||
46 | #include <pwd.h> | ||
47 | #include <errno.h> | ||
48 | #include <stdio.h> | ||
49 | #include <ctype.h> | ||
50 | #include <string.h> | ||
51 | #include <syslog.h> | ||
52 | #include <stdlib.h> | ||
53 | #include <netgroup.h> | ||
54 | |||
55 | int | ||
56 | rresvport(alport) | ||
57 | int *alport; | ||
58 | { | ||
59 | return rresvport_af(alport, AF_INET); | ||
60 | } | ||
61 | |||
62 | |||
63 | int | ||
64 | rresvport_af(alport, af) | ||
65 | int *alport; | ||
66 | int af; | ||
67 | { | ||
68 | struct sockaddr_storage ss; | ||
69 | struct sockaddr *sa; | ||
70 | u_int16_t *portp; | ||
71 | int s; | ||
72 | |||
73 | bzero(&ss, sizeof ss); | ||
74 | sa = (struct sockaddr *)&ss; | ||
75 | |||
76 | switch (af) { | ||
77 | case AF_INET: | ||
78 | sa->sa_len = sizeof(struct sockaddr_in); | ||
79 | portp = &((struct sockaddr_in *)sa)->sin_port; | ||
80 | break; | ||
81 | case AF_INET6: | ||
82 | sa->sa_len = sizeof(struct sockaddr_in6); | ||
83 | portp = &((struct sockaddr_in6 *)sa)->sin6_port; | ||
84 | break; | ||
85 | default: | ||
86 | errno = EPFNOSUPPORT; | ||
87 | return (-1); | ||
88 | } | ||
89 | sa->sa_family = af; | ||
90 | |||
91 | s = socket(af, SOCK_STREAM, 0); | ||
92 | if (s < 0) | ||
93 | return (-1); | ||
94 | |||
95 | *portp = htons(*alport); | ||
96 | if (*alport < IPPORT_RESERVED - 1) { | ||
97 | if (bind(s, sa, sa->sa_len) >= 0) | ||
98 | return (s); | ||
99 | if (errno != EADDRINUSE) { | ||
100 | (void)close(s); | ||
101 | return (-1); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | *portp = 0; | ||
106 | sa->sa_family = af; | ||
107 | if (bindresvport_sa(s, sa) == -1) { | ||
108 | (void)close(s); | ||
109 | return (-1); | ||
110 | } | ||
111 | *alport = ntohs(*portp); | ||
112 | return (s); | ||
113 | } | ||
diff --git a/src/lib/libc/net/rthdr.c b/src/lib/libc/net/rthdr.c new file mode 100644 index 0000000000..366ca9e6ba --- /dev/null +++ b/src/lib/libc/net/rthdr.c | |||
@@ -0,0 +1,228 @@ | |||
1 | /* $OpenBSD: rthdr.c,v 1.6 2003/06/11 02:54:02 itojun Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
5 | * 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. Neither the name of the project nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <sys/param.h> | ||
33 | #include <sys/types.h> | ||
34 | #include <sys/socket.h> | ||
35 | |||
36 | #include <netinet/in.h> | ||
37 | #include <netinet/ip6.h> | ||
38 | |||
39 | #include <string.h> | ||
40 | #include <stdio.h> | ||
41 | |||
42 | size_t | ||
43 | inet6_rthdr_space(type, seg) | ||
44 | int type, seg; | ||
45 | { | ||
46 | switch (type) { | ||
47 | case IPV6_RTHDR_TYPE_0: | ||
48 | if (seg < 1 || seg > 23) | ||
49 | return (0); | ||
50 | return (CMSG_SPACE(sizeof(struct in6_addr) * seg + | ||
51 | sizeof(struct ip6_rthdr0))); | ||
52 | default: | ||
53 | return (0); | ||
54 | } | ||
55 | } | ||
56 | |||
57 | struct cmsghdr * | ||
58 | inet6_rthdr_init(bp, type) | ||
59 | void *bp; | ||
60 | int type; | ||
61 | { | ||
62 | struct cmsghdr *ch = (struct cmsghdr *)bp; | ||
63 | struct ip6_rthdr *rthdr; | ||
64 | |||
65 | rthdr = (struct ip6_rthdr *)CMSG_DATA(ch); | ||
66 | |||
67 | ch->cmsg_level = IPPROTO_IPV6; | ||
68 | ch->cmsg_type = IPV6_RTHDR; | ||
69 | |||
70 | switch (type) { | ||
71 | case IPV6_RTHDR_TYPE_0: | ||
72 | ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0)); | ||
73 | bzero(rthdr, sizeof(struct ip6_rthdr0)); | ||
74 | rthdr->ip6r_type = IPV6_RTHDR_TYPE_0; | ||
75 | return (ch); | ||
76 | default: | ||
77 | return (NULL); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | int | ||
82 | inet6_rthdr_add(cmsg, addr, flags) | ||
83 | struct cmsghdr *cmsg; | ||
84 | const struct in6_addr *addr; | ||
85 | u_int flags; | ||
86 | { | ||
87 | struct ip6_rthdr *rthdr; | ||
88 | |||
89 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
90 | |||
91 | switch (rthdr->ip6r_type) { | ||
92 | case IPV6_RTHDR_TYPE_0: | ||
93 | { | ||
94 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
95 | if (flags != IPV6_RTHDR_LOOSE) | ||
96 | return (-1); | ||
97 | if (rt0->ip6r0_segleft == 23) | ||
98 | return (-1); | ||
99 | rt0->ip6r0_segleft++; | ||
100 | bcopy(addr, (caddr_t)rt0 + ((rt0->ip6r0_len + 1) << 3), | ||
101 | sizeof(struct in6_addr)); | ||
102 | rt0->ip6r0_len += sizeof(struct in6_addr) >> 3; | ||
103 | cmsg->cmsg_len = CMSG_LEN((rt0->ip6r0_len + 1) << 3); | ||
104 | break; | ||
105 | } | ||
106 | default: | ||
107 | return (-1); | ||
108 | } | ||
109 | |||
110 | return (0); | ||
111 | } | ||
112 | |||
113 | int | ||
114 | inet6_rthdr_lasthop(cmsg, flags) | ||
115 | struct cmsghdr *cmsg; | ||
116 | unsigned int flags; | ||
117 | { | ||
118 | struct ip6_rthdr *rthdr; | ||
119 | |||
120 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
121 | |||
122 | switch (rthdr->ip6r_type) { | ||
123 | case IPV6_RTHDR_TYPE_0: | ||
124 | { | ||
125 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
126 | if (flags != IPV6_RTHDR_LOOSE) | ||
127 | return (-1); | ||
128 | if (rt0->ip6r0_segleft > 23) | ||
129 | return (-1); | ||
130 | break; | ||
131 | } | ||
132 | default: | ||
133 | return (-1); | ||
134 | } | ||
135 | |||
136 | return (0); | ||
137 | } | ||
138 | |||
139 | #if 0 | ||
140 | int | ||
141 | inet6_rthdr_reverse(in, out) | ||
142 | const struct cmsghdr *in; | ||
143 | struct cmsghdr *out; | ||
144 | { | ||
145 | |||
146 | return (-1); | ||
147 | } | ||
148 | #endif | ||
149 | |||
150 | int | ||
151 | inet6_rthdr_segments(cmsg) | ||
152 | const struct cmsghdr *cmsg; | ||
153 | { | ||
154 | struct ip6_rthdr *rthdr; | ||
155 | |||
156 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
157 | |||
158 | switch (rthdr->ip6r_type) { | ||
159 | case IPV6_RTHDR_TYPE_0: | ||
160 | { | ||
161 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
162 | |||
163 | if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) | ||
164 | return (-1); | ||
165 | |||
166 | return (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); | ||
167 | } | ||
168 | |||
169 | default: | ||
170 | return (-1); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | struct in6_addr * | ||
175 | inet6_rthdr_getaddr(cmsg, index) | ||
176 | struct cmsghdr *cmsg; | ||
177 | int index; | ||
178 | { | ||
179 | struct ip6_rthdr *rthdr; | ||
180 | |||
181 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
182 | |||
183 | switch (rthdr->ip6r_type) { | ||
184 | case IPV6_RTHDR_TYPE_0: | ||
185 | { | ||
186 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
187 | int naddr; | ||
188 | |||
189 | if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) | ||
190 | return NULL; | ||
191 | naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); | ||
192 | if (index <= 0 || naddr < index) | ||
193 | return NULL; | ||
194 | return ((struct in6_addr *)(rt0 + 1)) + index; | ||
195 | } | ||
196 | |||
197 | default: | ||
198 | return NULL; | ||
199 | } | ||
200 | } | ||
201 | |||
202 | int | ||
203 | inet6_rthdr_getflags(cmsg, index) | ||
204 | const struct cmsghdr *cmsg; | ||
205 | int index; | ||
206 | { | ||
207 | struct ip6_rthdr *rthdr; | ||
208 | |||
209 | rthdr = (struct ip6_rthdr *)CMSG_DATA(cmsg); | ||
210 | |||
211 | switch (rthdr->ip6r_type) { | ||
212 | case IPV6_RTHDR_TYPE_0: | ||
213 | { | ||
214 | struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)rthdr; | ||
215 | int naddr; | ||
216 | |||
217 | if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) | ||
218 | return (-1); | ||
219 | naddr = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); | ||
220 | if (index < 0 || naddr < index) | ||
221 | return (-1); | ||
222 | return IPV6_RTHDR_LOOSE; | ||
223 | } | ||
224 | |||
225 | default: | ||
226 | return (-1); | ||
227 | } | ||
228 | } | ||
diff --git a/src/lib/libc/net/send.c b/src/lib/libc/net/send.c new file mode 100644 index 0000000000..8a7cc74e2d --- /dev/null +++ b/src/lib/libc/net/send.c | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: send.c,v 1.3 2003/06/02 20:18:36 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/socket.h> | ||
36 | |||
37 | #include <stddef.h> | ||
38 | |||
39 | ssize_t | ||
40 | send(s, msg, len, flags) | ||
41 | int s, flags; | ||
42 | size_t len; | ||
43 | const void *msg; | ||
44 | { | ||
45 | return (sendto(s, msg, len, flags, NULL, 0)); | ||
46 | } | ||
diff --git a/src/lib/libc/net/sethostent.c b/src/lib/libc/net/sethostent.c new file mode 100644 index 0000000000..2a8df101d4 --- /dev/null +++ b/src/lib/libc/net/sethostent.c | |||
@@ -0,0 +1,61 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1985, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char rcsid[] = "$OpenBSD: sethostent.c,v 1.6 2003/06/02 20:18:36 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/param.h> | ||
35 | #include <netinet/in.h> | ||
36 | #include <arpa/nameser.h> | ||
37 | #include <netdb.h> | ||
38 | #include <resolv.h> | ||
39 | |||
40 | #include "thread_private.h" | ||
41 | |||
42 | void | ||
43 | sethostent(stayopen) | ||
44 | int stayopen; | ||
45 | { | ||
46 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
47 | |||
48 | if ((_resp->options & RES_INIT) == 0 && res_init() == -1) | ||
49 | return; | ||
50 | if (stayopen) | ||
51 | _resp->options |= RES_STAYOPEN | RES_USEVC; | ||
52 | } | ||
53 | |||
54 | void | ||
55 | endhostent() | ||
56 | { | ||
57 | struct __res_state *_resp = _THREAD_PRIVATE(_res, _res, &_res); | ||
58 | |||
59 | _resp->options &= ~(RES_STAYOPEN | RES_USEVC); | ||
60 | res_close(); | ||
61 | } | ||
diff --git a/src/lib/libc/net/vars6.c b/src/lib/libc/net/vars6.c new file mode 100644 index 0000000000..a22d49d047 --- /dev/null +++ b/src/lib/libc/net/vars6.c | |||
@@ -0,0 +1,42 @@ | |||
1 | /* $OpenBSD: vars6.c,v 1.2 2003/05/10 10:56:47 jmc Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | ||
5 | * 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. Neither the name of the project nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <sys/types.h> | ||
33 | #include <netinet/in.h> | ||
34 | |||
35 | /* | ||
36 | * Definitions of some constant IPv6 addresses. | ||
37 | */ | ||
38 | const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; | ||
39 | const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; | ||
40 | const struct in6_addr in6addr_nodelocal_allnodes = IN6ADDR_NODELOCAL_ALLNODES_INIT; | ||
41 | const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; | ||
42 | |||
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc new file mode 100644 index 0000000000..ad46094309 --- /dev/null +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -0,0 +1,68 @@ | |||
1 | # $OpenBDS: Makefile.inc,v 1.6 1996/08/21 03:47:21 tholo Exp $ | ||
2 | |||
3 | # stdlib sources | ||
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib | ||
5 | |||
6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ | ||
7 | calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ | ||
8 | getsubopt.c heapsort.c l64a.c llabs.c lsearch.c malloc.c merge.c \ | ||
9 | multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ | ||
10 | setenv.c strtod.c strtol.c strtoll.c strtoul.c strtoull.c system.c \ | ||
11 | tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ | ||
12 | lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c | ||
13 | |||
14 | .if (${MACHINE_ARCH} == "m68k") | ||
15 | SRCS+= abs.S div.c labs.c ldiv.c | ||
16 | LSRCS+= abs.c | ||
17 | .elif (${MACHINE_ARCH} == "i386") | ||
18 | SRCS+= abs.S div.S labs.S ldiv.S | ||
19 | LSRCS+= abs.c div.c labs.c ldiv.c | ||
20 | .elif (${MACHINE_ARCH} == "ns32k") | ||
21 | SRCS+= abs.S div.c labs.c ldiv.c | ||
22 | LSRCS+= abs.c | ||
23 | .elif (${MACHINE_ARCH} == "tahoe") | ||
24 | SRCS+= abs.S div.c labs.c ldiv.c | ||
25 | LSRCS+= abs.c | ||
26 | .elif (${MACHINE_ARCH} == "vax") | ||
27 | SRCS+= abs.c div.c labs.c ldiv.c | ||
28 | .elif (${MACHINE_ARCH} == "alpha") | ||
29 | # XXX should be .S's | ||
30 | SRCS+= abs.c div.c labs.c ldiv.c | ||
31 | .else | ||
32 | SRCS+= abs.c div.c labs.c ldiv.c | ||
33 | .endif | ||
34 | |||
35 | .if (${MACHINE_ARCH} == "vax") || (${MACHINE_ARCH} == "m68k") | ||
36 | SRCS+= insque.S remque.S | ||
37 | .else | ||
38 | SRCS+= insque.c remque.c | ||
39 | .endif | ||
40 | |||
41 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ | ||
42 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ | ||
43 | getsubopt.3 insque.3 labs.3 ldiv.3 lsearch.3 malloc.3 memory.3 qabs.3 \ | ||
44 | qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \ | ||
45 | strtod.3 strtol.3 strtoul.3 system.3 tsearch.3 | ||
46 | |||
47 | MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 | ||
48 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 | ||
49 | MLINKS+=getopt_long.3 getopt_long_only.3 | ||
50 | MLINKS+=insque.3 remque.3 | ||
51 | MLINKS+=labs.3 llabs.3 | ||
52 | MLINKS+=lsearch.3 lfind.3 | ||
53 | MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 | ||
54 | MLINKS+=malloc.3 cfree.3 malloc.3 malloc.conf.5 | ||
55 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 | ||
56 | MLINKS+=radixsort.3 sradixsort.3 | ||
57 | MLINKS+=rand.3 srand.3 rand.3 rand_r.3 | ||
58 | MLINKS+=random.3 initstate.3 random.3 setstate.3 | ||
59 | MLINKS+=random.3 srandom.3 random.3 srandomdev.3 | ||
60 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 | ||
61 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 | ||
62 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 | ||
63 | MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 | ||
64 | MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 | ||
65 | MLINKS+=tsearch.3 tfind.3 | ||
66 | MLINKS+=tsearch.3 tdelete.3 | ||
67 | MLINKS+=tsearch.3 twalk.3 | ||
68 | MLINKS+=a64l.3 l64a.3 | ||
diff --git a/src/lib/libc/stdlib/_rand48.c b/src/lib/libc/stdlib/_rand48.c new file mode 100644 index 0000000000..fed7372f68 --- /dev/null +++ b/src/lib/libc/stdlib/_rand48.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: _rand48.c,v 1.2 1996/08/19 08:33:19 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | unsigned short __rand48_seed[3] = { | ||
21 | RAND48_SEED_0, | ||
22 | RAND48_SEED_1, | ||
23 | RAND48_SEED_2 | ||
24 | }; | ||
25 | unsigned short __rand48_mult[3] = { | ||
26 | RAND48_MULT_0, | ||
27 | RAND48_MULT_1, | ||
28 | RAND48_MULT_2 | ||
29 | }; | ||
30 | unsigned short __rand48_add = RAND48_ADD; | ||
31 | |||
32 | void | ||
33 | __dorand48(unsigned short xseed[3]) | ||
34 | { | ||
35 | unsigned long accu; | ||
36 | unsigned short temp[2]; | ||
37 | |||
38 | accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] + | ||
39 | (unsigned long) __rand48_add; | ||
40 | temp[0] = (unsigned short) accu; /* lower 16 bits */ | ||
41 | accu >>= sizeof(unsigned short) * 8; | ||
42 | accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] + | ||
43 | (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0]; | ||
44 | temp[1] = (unsigned short) accu; /* middle 16 bits */ | ||
45 | accu >>= sizeof(unsigned short) * 8; | ||
46 | accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0]; | ||
47 | xseed[0] = temp[0]; | ||
48 | xseed[1] = temp[1]; | ||
49 | xseed[2] = (unsigned short) accu; | ||
50 | } | ||
diff --git a/src/lib/libc/stdlib/a64l.3 b/src/lib/libc/stdlib/a64l.3 new file mode 100644 index 0000000000..de70d0af71 --- /dev/null +++ b/src/lib/libc/stdlib/a64l.3 | |||
@@ -0,0 +1,133 @@ | |||
1 | .\" $OpenBSD: a64l.3,v 1.8 2003/06/17 21:56:24 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | .\" | ||
17 | .Dd August 17, 1997 | ||
18 | .Dt A64L 3 | ||
19 | .Os | ||
20 | .Sh NAME | ||
21 | .Nm a64l , | ||
22 | .Nm l64a | ||
23 | .Nd convert between 32-bit integer and radix-64 ASCII string | ||
24 | .Sh SYNOPSIS | ||
25 | .Fd #include <stdlib.h> | ||
26 | .Ft long | ||
27 | .Fn a64l "const char *s" | ||
28 | .Ft char * | ||
29 | .Fn l64a "long l" | ||
30 | .Sh DESCRIPTION | ||
31 | The | ||
32 | .Fn a64l | ||
33 | and | ||
34 | .Fn l64a | ||
35 | functions are used to maintain numbers stored in radix-64 | ||
36 | .Tn ASCII | ||
37 | characters. | ||
38 | This is a notation by which 32-bit integers | ||
39 | can be represented by up to six characters; each character represents a | ||
40 | .Dq digit | ||
41 | in a radix-64 notation. | ||
42 | .Pp | ||
43 | The characters used to represent digits are | ||
44 | .Ql \&. | ||
45 | for 0, | ||
46 | .Ql / | ||
47 | for 1, | ||
48 | .Ql 0 | ||
49 | through | ||
50 | .Ql 9 | ||
51 | for 2-11, | ||
52 | .Ql A | ||
53 | through | ||
54 | .Ql Z | ||
55 | for 12-37, and | ||
56 | .Ql a | ||
57 | through | ||
58 | .Ql z | ||
59 | for 38-63. | ||
60 | .Pp | ||
61 | The | ||
62 | .Fn a64l | ||
63 | function takes a pointer to a null-terminated radix-64 representation | ||
64 | and returns a corresponding 32-bit value. | ||
65 | If the string pointed to by | ||
66 | .Fa s | ||
67 | contains more than six characters, | ||
68 | .Fn a64l | ||
69 | will use the first six. | ||
70 | .Fn a64l | ||
71 | scans the character string from left to right, decoding | ||
72 | each character as a 6-bit radix-64 number. | ||
73 | If a long integer is | ||
74 | larger than 32 bits, the return value will be sign-extended. | ||
75 | .Pp | ||
76 | .Fn l64a | ||
77 | takes a long integer argument | ||
78 | .Fa l | ||
79 | and returns a pointer to the corresponding radix-64 representation. | ||
80 | .Sh RETURN VALUES | ||
81 | On success, | ||
82 | .Fn a64l | ||
83 | returns a 32-bit representation of | ||
84 | .Fa s . | ||
85 | If | ||
86 | .Fa s | ||
87 | is a null pointer or if it contains digits other than those described above, | ||
88 | .Fn a64l | ||
89 | returns \-1 and sets the global variable | ||
90 | .Va errno | ||
91 | to | ||
92 | .Er EINVAL . | ||
93 | .Pp | ||
94 | On success, | ||
95 | .Fn l64a | ||
96 | returns a pointer to a string containing the radix-64 representation of | ||
97 | .Fa l . | ||
98 | If | ||
99 | .Fa l | ||
100 | is 0, | ||
101 | .Fn l64a | ||
102 | returns a pointer to the empty string. | ||
103 | If | ||
104 | .Fa l | ||
105 | is negative, | ||
106 | .Fn l64a | ||
107 | returns a null pointer and sets the global variable | ||
108 | .Va errno | ||
109 | to | ||
110 | .Er EINVAL . | ||
111 | .Sh WARNINGS | ||
112 | The value returned by | ||
113 | .Fn l64a | ||
114 | is a pointer into a static buffer, the contents of which | ||
115 | will be overwritten by subsequent calls. | ||
116 | .Pp | ||
117 | The value returned by | ||
118 | .Fn a64l | ||
119 | may be incorrect if the value is too large; for that reason, only strings | ||
120 | that resulted from a call to | ||
121 | .Fn l64a | ||
122 | should be used to call | ||
123 | .Fn a64l . | ||
124 | .Pp | ||
125 | If a long integer is larger than 32 bits, only the low-order | ||
126 | 32 bits are used. | ||
127 | .Sh STANDARDS | ||
128 | The | ||
129 | .Fn a64l | ||
130 | and | ||
131 | .Fn l64a | ||
132 | functions conform to | ||
133 | .St -xpg4.2 . | ||
diff --git a/src/lib/libc/stdlib/a64l.c b/src/lib/libc/stdlib/a64l.c new file mode 100644 index 0000000000..a68f0a6dcd --- /dev/null +++ b/src/lib/libc/stdlib/a64l.c | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
3 | * Public domain. | ||
4 | */ | ||
5 | |||
6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
7 | static char *rcsid = "$OpenBSD: a64l.c,v 1.3 1997/08/17 22:58:34 millert Exp $"; | ||
8 | #endif /* LIBC_SCCS and not lint */ | ||
9 | |||
10 | #include <errno.h> | ||
11 | #include <stdlib.h> | ||
12 | |||
13 | long | ||
14 | a64l(s) | ||
15 | const char *s; | ||
16 | { | ||
17 | long value, digit, shift; | ||
18 | int i; | ||
19 | |||
20 | if (s == NULL) { | ||
21 | errno = EINVAL; | ||
22 | return(-1L); | ||
23 | } | ||
24 | |||
25 | value = 0; | ||
26 | shift = 0; | ||
27 | for (i = 0; *s && i < 6; i++, s++) { | ||
28 | if (*s >= '.' && *s <= '/') | ||
29 | digit = *s - '.'; | ||
30 | else if (*s >= '0' && *s <= '9') | ||
31 | digit = *s - '0' + 2; | ||
32 | else if (*s >= 'A' && *s <= 'Z') | ||
33 | digit = *s - 'A' + 12; | ||
34 | else if (*s >= 'a' && *s <= 'z') | ||
35 | digit = *s - 'a' + 38; | ||
36 | else { | ||
37 | errno = EINVAL; | ||
38 | return(-1L); | ||
39 | } | ||
40 | |||
41 | value |= digit << shift; | ||
42 | shift += 6; | ||
43 | } | ||
44 | |||
45 | return(value); | ||
46 | } | ||
diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3 new file mode 100644 index 0000000000..d411b859eb --- /dev/null +++ b/src/lib/libc/stdlib/abort.3 | |||
@@ -0,0 +1,63 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: abort.3,v 1.7 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt ABORT 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm abort | ||
39 | .Nd cause abnormal program termination | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft void | ||
43 | .Fn abort void | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn abort | ||
47 | function causes abnormal program termination to occur, unless the signal | ||
48 | .Dv SIGABRT | ||
49 | is being caught and the signal handler does not return. | ||
50 | .Pp | ||
51 | Any open streams are flushed and closed. | ||
52 | .Sh RETURN VALUES | ||
53 | The | ||
54 | .Fn abort | ||
55 | function never returns. | ||
56 | .Sh SEE ALSO | ||
57 | .Xr sigaction 2 , | ||
58 | .Xr exit 3 | ||
59 | .Sh STANDARDS | ||
60 | The | ||
61 | .Fn abort | ||
62 | function conforms to | ||
63 | .St -p1003.1-90 . | ||
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c new file mode 100644 index 0000000000..3f32dc5dc1 --- /dev/null +++ b/src/lib/libc/stdlib/abort.c | |||
@@ -0,0 +1,78 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1985 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: abort.c,v 1.12 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <signal.h> | ||
35 | #include <stdlib.h> | ||
36 | #include <unistd.h> | ||
37 | #include "thread_private.h" | ||
38 | #include "atexit.h" | ||
39 | |||
40 | void | ||
41 | abort() | ||
42 | { | ||
43 | struct atexit *p = __atexit; | ||
44 | static int cleanup_called = 0; | ||
45 | sigset_t mask; | ||
46 | |||
47 | |||
48 | sigfillset(&mask); | ||
49 | /* | ||
50 | * don't block SIGABRT to give any handler a chance; we ignore | ||
51 | * any errors -- X311J doesn't allow abort to return anyway. | ||
52 | */ | ||
53 | sigdelset(&mask, SIGABRT); | ||
54 | (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
55 | |||
56 | /* | ||
57 | * POSIX requires we flush stdio buffers on abort | ||
58 | */ | ||
59 | if (cleanup_called == 0) { | ||
60 | while (p != NULL && p->next != NULL) | ||
61 | p = p->next; | ||
62 | if (p != NULL && p->fns[0] != NULL) { | ||
63 | cleanup_called = 1; | ||
64 | (*p->fns[0])(); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | (void)kill(getpid(), SIGABRT); | ||
69 | |||
70 | /* | ||
71 | * if SIGABRT ignored, or caught and the handler returns, do | ||
72 | * it again, only harder. | ||
73 | */ | ||
74 | (void)signal(SIGABRT, SIG_DFL); | ||
75 | (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
76 | (void)kill(getpid(), SIGABRT); | ||
77 | exit(1); | ||
78 | } | ||
diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3 new file mode 100644 index 0000000000..f0a6b8a23a --- /dev/null +++ b/src/lib/libc/stdlib/abs.3 | |||
@@ -0,0 +1,65 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: abs.3,v 1.6 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt ABS 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm abs | ||
39 | .Nd integer absolute value function | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft int | ||
43 | .Fn abs "int j" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn abs | ||
47 | function computes the absolute value of the integer | ||
48 | .Fa j . | ||
49 | .Sh RETURN VALUES | ||
50 | The | ||
51 | .Fn abs | ||
52 | function returns the absolute value. | ||
53 | .Sh SEE ALSO | ||
54 | .Xr cabs 3 , | ||
55 | .Xr floor 3 , | ||
56 | .Xr hypot 3 , | ||
57 | .Xr labs 3 , | ||
58 | .Xr math 3 | ||
59 | .Sh STANDARDS | ||
60 | The | ||
61 | .Fn abs | ||
62 | function conforms to | ||
63 | .St -ansiC . | ||
64 | .Sh BUGS | ||
65 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/abs.c b/src/lib/libc/stdlib/abs.c new file mode 100644 index 0000000000..c67ad9494f --- /dev/null +++ b/src/lib/libc/stdlib/abs.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: abs.c,v 1.3 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | int | ||
37 | abs(j) | ||
38 | int j; | ||
39 | { | ||
40 | return(j < 0 ? -j : j); | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3 new file mode 100644 index 0000000000..4136dc405c --- /dev/null +++ b/src/lib/libc/stdlib/alloca.3 | |||
@@ -0,0 +1,80 @@ | |||
1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: alloca.3,v 1.10 2003/06/02 20:18:37 millert Exp $ | ||
29 | .\" | ||
30 | .Dd May 2, 1991 | ||
31 | .Dt ALLOCA 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm alloca | ||
35 | .Nd memory allocator | ||
36 | .Sh SYNOPSIS | ||
37 | .Fd #include <stdlib.h> | ||
38 | .Ft void * | ||
39 | .Fn alloca "size_t size" | ||
40 | .Sh DESCRIPTION | ||
41 | The | ||
42 | .Fn alloca | ||
43 | function allocates | ||
44 | .Fa size | ||
45 | bytes of space in the stack frame of the caller. | ||
46 | This temporary space is automatically freed on return. | ||
47 | .Sh RETURN VALUES | ||
48 | The | ||
49 | .Fn alloca | ||
50 | function returns a pointer to the beginning of the allocated space. | ||
51 | .Sh SEE ALSO | ||
52 | .Xr pagesize 1 , | ||
53 | .Xr brk 2 , | ||
54 | .Xr calloc 3 , | ||
55 | .Xr malloc 3 , | ||
56 | .Xr realloc 3 | ||
57 | .Sh BUGS | ||
58 | The | ||
59 | .Fn alloca | ||
60 | function is machine dependent; its use is discouraged. | ||
61 | .\" .Sh HISTORY | ||
62 | .\" The | ||
63 | .\" .Fn alloca | ||
64 | .\" function appeared in | ||
65 | .\" .Bx ?? . | ||
66 | .\" The function appeared in 32v, pwb and pwb.2 and in 3bsd 4bsd | ||
67 | .\" The first man page (or link to a man page that I can find at the | ||
68 | .\" moment is 4.3... | ||
69 | .Pp | ||
70 | The | ||
71 | .Fn alloca | ||
72 | function is slightly unsafe because it cannot ensure that the pointer | ||
73 | returned points to a valid and usable block of memory. | ||
74 | The allocation made may exceed the bounds of the stack, or even go | ||
75 | further into other objects in memory, and | ||
76 | .Fn alloca | ||
77 | cannot determine such an error. | ||
78 | Avoid | ||
79 | .Fn alloca | ||
80 | with large unbounded allocations. | ||
diff --git a/src/lib/libc/stdlib/atexit.3 b/src/lib/libc/stdlib/atexit.3 new file mode 100644 index 0000000000..c8d4ce63d5 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.3 | |||
@@ -0,0 +1,71 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: atexit.3,v 1.5 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt ATEXIT 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm atexit | ||
39 | .Nd register a function to be called on exit | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft int | ||
43 | .Fn atexit "void (*function)(void)" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn atexit | ||
47 | function registers the given | ||
48 | .Fa function | ||
49 | to be called at program exit, whether via | ||
50 | .Xr exit 3 | ||
51 | or via return from the program's | ||
52 | .Fn main . | ||
53 | Functions so registered are called in reverse order; | ||
54 | no arguments are passed. | ||
55 | At least 32 functions can always be registered, | ||
56 | and more are allowed as long as sufficient memory can be allocated. | ||
57 | .Sh RETURN VALUES | ||
58 | .Rv -std atexit | ||
59 | .Sh ERRORS | ||
60 | .Bl -tag -width Er | ||
61 | .It Bq Er ENOMEM | ||
62 | No memory was available to add the function to the list. | ||
63 | The existing list of functions is unmodified. | ||
64 | .El | ||
65 | .Sh SEE ALSO | ||
66 | .Xr exit 3 | ||
67 | .Sh STANDARDS | ||
68 | The | ||
69 | .Fn atexit | ||
70 | function conforms to | ||
71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c new file mode 100644 index 0000000000..98564d0dd3 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.c | |||
@@ -0,0 +1,135 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2002 Daniel Hartmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * | ||
9 | * - Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * - Redistributions in binary form must reproduce the above | ||
12 | * copyright notice, this list of conditions and the following | ||
13 | * disclaimer in the documentation and/or other materials provided | ||
14 | * with the distribution. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
17 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
18 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
19 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
20 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
22 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
23 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
24 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
26 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
27 | * POSSIBILITY OF SUCH DAMAGE. | ||
28 | * | ||
29 | */ | ||
30 | |||
31 | #if defined(LIBC_SCCS) && !defined(lint) | ||
32 | static char *rcsid = "$OpenBSD: atexit.c,v 1.7 2002/09/14 22:03:14 dhartmei Exp $"; | ||
33 | #endif /* LIBC_SCCS and not lint */ | ||
34 | |||
35 | #include <sys/types.h> | ||
36 | #include <sys/mman.h> | ||
37 | #include <stdlib.h> | ||
38 | #include <unistd.h> | ||
39 | #include "atexit.h" | ||
40 | |||
41 | int __atexit_invalid = 1; | ||
42 | struct atexit *__atexit; | ||
43 | |||
44 | /* | ||
45 | * Function pointers are stored in a linked list of pages. The list | ||
46 | * is initially empty, and pages are allocated on demand. The first | ||
47 | * function pointer in the first allocated page (the last one in | ||
48 | * the linked list) is reserved for the cleanup function. | ||
49 | * | ||
50 | * Outside the following two functions, all pages are mprotect()'ed | ||
51 | * to prevent unintentional/malicious corruption. | ||
52 | * | ||
53 | * The free(malloc(1)) is a workaround causing malloc_init() to | ||
54 | * ensure that malloc.c gets the first mmap() call for its sbrk() | ||
55 | * games. | ||
56 | */ | ||
57 | |||
58 | /* | ||
59 | * Register a function to be performed at exit. | ||
60 | */ | ||
61 | int | ||
62 | atexit(fn) | ||
63 | void (*fn)(); | ||
64 | { | ||
65 | register struct atexit *p = __atexit; | ||
66 | register int pgsize = getpagesize(); | ||
67 | |||
68 | if (pgsize < sizeof(*p)) | ||
69 | return (-1); | ||
70 | if (p != NULL) { | ||
71 | if (p->ind + 1 >= p->max) | ||
72 | p = NULL; | ||
73 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
74 | return (-1); | ||
75 | } | ||
76 | if (p == NULL) { | ||
77 | if (__atexit_invalid) { | ||
78 | free(malloc(1)); | ||
79 | __atexit_invalid = 0; | ||
80 | } | ||
81 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
82 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
83 | if (p == MAP_FAILED) | ||
84 | return (-1); | ||
85 | if (__atexit == NULL) { | ||
86 | p->fns[0] = NULL; | ||
87 | p->ind = 1; | ||
88 | } else | ||
89 | p->ind = 0; | ||
90 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
91 | sizeof(p->fns[0]); | ||
92 | p->next = __atexit; | ||
93 | __atexit = p; | ||
94 | } | ||
95 | p->fns[p->ind++] = fn; | ||
96 | if (mprotect(p, pgsize, PROT_READ)) | ||
97 | return (-1); | ||
98 | return (0); | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | * Register the cleanup function | ||
103 | */ | ||
104 | void | ||
105 | __atexit_register_cleanup(fn) | ||
106 | void (*fn)(); | ||
107 | { | ||
108 | register struct atexit *p = __atexit; | ||
109 | register int pgsize = getpagesize(); | ||
110 | |||
111 | if (pgsize < sizeof(*p)) | ||
112 | return; | ||
113 | while (p != NULL && p->next != NULL) | ||
114 | p = p->next; | ||
115 | if (p == NULL) { | ||
116 | if (__atexit_invalid) { | ||
117 | free(malloc(1)); | ||
118 | __atexit_invalid = 0; | ||
119 | } | ||
120 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
121 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
122 | if (p == MAP_FAILED) | ||
123 | return; | ||
124 | p->ind = 1; | ||
125 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
126 | sizeof(p->fns[0]); | ||
127 | p->next = NULL; | ||
128 | __atexit = p; | ||
129 | } else { | ||
130 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
131 | return; | ||
132 | } | ||
133 | p->fns[0] = fn; | ||
134 | mprotect(p, pgsize, PROT_READ); | ||
135 | } | ||
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h new file mode 100644 index 0000000000..21b0c2e532 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.h | |||
@@ -0,0 +1,41 @@ | |||
1 | /* $OpenBSD: atexit.h,v 1.6 2003/07/31 07:08:42 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2002 Daniel Hartmeier | ||
5 | * 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 | * | ||
11 | * - Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * - Redistributions in binary form must reproduce the above | ||
14 | * copyright notice, this list of conditions and the following | ||
15 | * disclaimer in the documentation and/or other materials provided | ||
16 | * with the distribution. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
22 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
28 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | * POSSIBILITY OF SUCH DAMAGE. | ||
30 | * | ||
31 | */ | ||
32 | |||
33 | struct atexit { | ||
34 | struct atexit *next; /* next in list */ | ||
35 | int ind; /* next index in this table */ | ||
36 | int max; /* max entries >= ATEXIT_SIZE */ | ||
37 | void (*fns[1])(void); /* the table itself */ | ||
38 | }; | ||
39 | |||
40 | extern int __atexit_invalid; | ||
41 | extern struct atexit *__atexit; /* points to head of LIFO stack */ | ||
diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3 new file mode 100644 index 0000000000..c60b9e9fd7 --- /dev/null +++ b/src/lib/libc/stdlib/atof.3 | |||
@@ -0,0 +1,69 @@ | |||
1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: atof.3,v 1.4 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt ATOF 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm atof | ||
39 | .Nd convert | ||
40 | .Tn ASCII | ||
41 | string to double | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Ft double | ||
45 | .Fn atof "const char *nptr" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn atof | ||
49 | function converts the initial portion of the string pointed to by | ||
50 | .Fa nptr | ||
51 | to | ||
52 | .Li double | ||
53 | representation. | ||
54 | .Pp | ||
55 | It is equivalent to: | ||
56 | .Bd -literal -offset indent | ||
57 | strtod(nptr, (char **)NULL); | ||
58 | .Ed | ||
59 | .Sh SEE ALSO | ||
60 | .Xr atoi 3 , | ||
61 | .Xr atol 3 , | ||
62 | .Xr strtod 3 , | ||
63 | .Xr strtol 3 , | ||
64 | .Xr strtoul 3 | ||
65 | .Sh STANDARDS | ||
66 | The | ||
67 | .Fn atof | ||
68 | function conforms to | ||
69 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atof.c b/src/lib/libc/stdlib/atof.c new file mode 100644 index 0000000000..dad2b77bfd --- /dev/null +++ b/src/lib/libc/stdlib/atof.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: atof.c,v 1.3 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | double | ||
37 | atof(ascii) | ||
38 | const char *ascii; | ||
39 | { | ||
40 | return(strtod(ascii, (char **)NULL)); | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3 new file mode 100644 index 0000000000..070344a810 --- /dev/null +++ b/src/lib/libc/stdlib/atoi.3 | |||
@@ -0,0 +1,81 @@ | |||
1 | .\" Copyright (c) 1990, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: atoi.3,v 1.7 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 4, 1993 | ||
35 | .Dt ATOI 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm atoi | ||
39 | .Nd convert | ||
40 | .Tn ASCII | ||
41 | string to integer | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Ft int | ||
45 | .Fn atoi "const char *nptr" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn atoi | ||
49 | function converts the initial portion of the string pointed to by | ||
50 | .Fa nptr | ||
51 | to | ||
52 | .Li integer | ||
53 | representation. | ||
54 | .Pp | ||
55 | It is equivalent to: | ||
56 | .Bd -literal -offset indent | ||
57 | (int)strtol(nptr, (char **)NULL, 10); | ||
58 | .Ed | ||
59 | .Sh SEE ALSO | ||
60 | .Xr atof 3 , | ||
61 | .Xr atol 3 , | ||
62 | .Xr strtod 3 , | ||
63 | .Xr strtol 3 , | ||
64 | .Xr strtoul 3 | ||
65 | .Sh STANDARDS | ||
66 | The | ||
67 | .Fn atoi | ||
68 | function conforms to | ||
69 | .St -ansiC . | ||
70 | .Sh CAVEATS | ||
71 | .Nm | ||
72 | does no overflow checking, handles unsigned numbers poorly, | ||
73 | and handles strings containing trailing extra characters | ||
74 | (like | ||
75 | .Dq "123abc" ) | ||
76 | poorly. | ||
77 | Careful use of | ||
78 | .Xr strtol 3 | ||
79 | and | ||
80 | .Xr strtoul 3 | ||
81 | can alleviate these problems. | ||
diff --git a/src/lib/libc/stdlib/atoi.c b/src/lib/libc/stdlib/atoi.c new file mode 100644 index 0000000000..b1fe789fd5 --- /dev/null +++ b/src/lib/libc/stdlib/atoi.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: atoi.c,v 1.3 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | int | ||
37 | atoi(str) | ||
38 | const char *str; | ||
39 | { | ||
40 | return((int)strtol(str, (char **)NULL, 10)); | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3 new file mode 100644 index 0000000000..80e54c892d --- /dev/null +++ b/src/lib/libc/stdlib/atol.3 | |||
@@ -0,0 +1,70 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: atol.3,v 1.6 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt ATOL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm atol | ||
39 | .Nd convert | ||
40 | .Tn ASCII | ||
41 | string to long integer | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Ft long | ||
45 | .Fn atol "const char *nptr" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn atol | ||
49 | function converts the initial portion of the string pointed to by | ||
50 | .Fa nptr | ||
51 | to | ||
52 | .Li long integer | ||
53 | representation. | ||
54 | .Pp | ||
55 | It is equivalent to: | ||
56 | .Bd -literal -offset indent | ||
57 | strtol(nptr, (char **)NULL, 10); | ||
58 | .Ed | ||
59 | .Sh SEE ALSO | ||
60 | .Xr atof 3 , | ||
61 | .Xr atoi 3 , | ||
62 | .Xr atoll 3 , | ||
63 | .Xr strtod 3 , | ||
64 | .Xr strtol 3 , | ||
65 | .Xr strtoul 3 | ||
66 | .Sh STANDARDS | ||
67 | The | ||
68 | .Fn atol | ||
69 | function conforms to | ||
70 | .St -ansiC-99 . | ||
diff --git a/src/lib/libc/stdlib/atol.c b/src/lib/libc/stdlib/atol.c new file mode 100644 index 0000000000..83adad3eae --- /dev/null +++ b/src/lib/libc/stdlib/atol.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: atol.c,v 1.3 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | long | ||
37 | atol(str) | ||
38 | const char *str; | ||
39 | { | ||
40 | return(strtol(str, (char **)NULL, 10)); | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/atoll.3 b/src/lib/libc/stdlib/atoll.3 new file mode 100644 index 0000000000..40584e9d47 --- /dev/null +++ b/src/lib/libc/stdlib/atoll.3 | |||
@@ -0,0 +1,70 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: atoll.3,v 1.3 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt ATOLL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm atoll | ||
39 | .Nd convert | ||
40 | .Tn ASCII | ||
41 | string to long long integer | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Ft long long | ||
45 | .Fn atoll "const char *nptr" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn atoll | ||
49 | function converts the initial portion of the string pointed to by | ||
50 | .Fa nptr | ||
51 | to | ||
52 | .Li long integer | ||
53 | representation. | ||
54 | .Pp | ||
55 | It is equivalent to: | ||
56 | .Bd -literal -offset indent | ||
57 | strtoll(nptr, (char **)NULL, 10); | ||
58 | .Ed | ||
59 | .Sh SEE ALSO | ||
60 | .Xr atof 3 , | ||
61 | .Xr atoi 3 , | ||
62 | .Xr atol 3 , | ||
63 | .Xr strtod 3 , | ||
64 | .Xr strtol 3 , | ||
65 | .Xr strtoul 3 | ||
66 | .Sh STANDARDS | ||
67 | The | ||
68 | .Fn atoll | ||
69 | function conforms to | ||
70 | .St -ansiC-99 . | ||
diff --git a/src/lib/libc/stdlib/atoll.c b/src/lib/libc/stdlib/atoll.c new file mode 100644 index 0000000000..6786b62e1a --- /dev/null +++ b/src/lib/libc/stdlib/atoll.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: atoll.c,v 1.2 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | long long | ||
37 | atoll(str) | ||
38 | const char *str; | ||
39 | { | ||
40 | return(strtoll(str, (char **)NULL, 10)); | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/bsearch.3 b/src/lib/libc/stdlib/bsearch.3 new file mode 100644 index 0000000000..d1dd9173db --- /dev/null +++ b/src/lib/libc/stdlib/bsearch.3 | |||
@@ -0,0 +1,84 @@ | |||
1 | .\" Copyright (c) 1990, 1991, 1993, 1994 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: bsearch.3,v 1.6 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd April 19, 1994 | ||
35 | .Dt BSEARCH 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm bsearch | ||
39 | .Nd binary search of a sorted table | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft void * | ||
43 | .Fn bsearch "const void *key" "const void *base" "size_t nmemb" "size_t size" "int (*compar) (const void *, const void *)" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn bsearch | ||
47 | function searches an array of | ||
48 | .Fa nmemb | ||
49 | objects, the initial member of which is | ||
50 | pointed to by | ||
51 | .Fa base , | ||
52 | for a member that matches the object pointed to by | ||
53 | .Fa key . | ||
54 | The size of each member of the array is specified by | ||
55 | .Fa size . | ||
56 | .Pp | ||
57 | The contents of the array should be in ascending sorted order according | ||
58 | to the comparison function referenced by | ||
59 | .Fa compar . | ||
60 | The | ||
61 | .Fa compar | ||
62 | routine is expected to have two arguments which point to the | ||
63 | .Fa key | ||
64 | object and to an array member, in that order, and should return an integer | ||
65 | less than, equal to, or greater than zero if the | ||
66 | .Fa key | ||
67 | object is found, respectively, to be less than, to match, or be | ||
68 | greater than the array member. | ||
69 | .Sh RETURN VALUES | ||
70 | The | ||
71 | .Fn bsearch | ||
72 | function returns a pointer to a matching member of the array, or a null | ||
73 | pointer if no match is found. | ||
74 | If two members compare as equal, which member is matched is unspecified. | ||
75 | .Sh SEE ALSO | ||
76 | .Xr db 3 , | ||
77 | .Xr lsearch 3 , | ||
78 | .Xr qsort 3 , | ||
79 | .Xr tsearch 3 | ||
80 | .Sh STANDARDS | ||
81 | The | ||
82 | .Fn bsearch | ||
83 | function conforms to | ||
84 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/bsearch.c b/src/lib/libc/stdlib/bsearch.c new file mode 100644 index 0000000000..9bfbf64256 --- /dev/null +++ b/src/lib/libc/stdlib/bsearch.c | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1990 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: bsearch.c,v 1.4 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | /* | ||
37 | * Perform a binary search. | ||
38 | * | ||
39 | * The code below is a bit sneaky. After a comparison fails, we | ||
40 | * divide the work in half by moving either left or right. If lim | ||
41 | * is odd, moving left simply involves halving lim: e.g., when lim | ||
42 | * is 5 we look at item 2, so we change lim to 2 so that we will | ||
43 | * look at items 0 & 1. If lim is even, the same applies. If lim | ||
44 | * is odd, moving right again involes halving lim, this time moving | ||
45 | * the base up one item past p: e.g., when lim is 5 we change base | ||
46 | * to item 3 and make lim 2 so that we will look at items 3 and 4. | ||
47 | * If lim is even, however, we have to shrink it by one before | ||
48 | * halving: e.g., when lim is 4, we still looked at item 2, so we | ||
49 | * have to make lim 3, then halve, obtaining 1, so that we will only | ||
50 | * look at item 3. | ||
51 | */ | ||
52 | void * | ||
53 | bsearch(key, base0, nmemb, size, compar) | ||
54 | register const void *key; | ||
55 | const void *base0; | ||
56 | size_t nmemb; | ||
57 | register size_t size; | ||
58 | register int (*compar)(const void *, const void *); | ||
59 | { | ||
60 | register const char *base = base0; | ||
61 | register int lim, cmp; | ||
62 | register const void *p; | ||
63 | |||
64 | for (lim = nmemb; lim != 0; lim >>= 1) { | ||
65 | p = base + (lim >> 1) * size; | ||
66 | cmp = (*compar)(key, p); | ||
67 | if (cmp == 0) | ||
68 | return ((void *)p); | ||
69 | if (cmp > 0) { /* key > p: move right */ | ||
70 | base = (char *)p + size; | ||
71 | lim--; | ||
72 | } /* else move left */ | ||
73 | } | ||
74 | return (NULL); | ||
75 | } | ||
diff --git a/src/lib/libc/stdlib/calloc.c b/src/lib/libc/stdlib/calloc.c new file mode 100644 index 0000000000..b0703cc884 --- /dev/null +++ b/src/lib/libc/stdlib/calloc.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: calloc.c,v 1.8 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <limits.h> | ||
37 | #include <errno.h> | ||
38 | |||
39 | void * | ||
40 | calloc(num, size) | ||
41 | size_t num; | ||
42 | register size_t size; | ||
43 | { | ||
44 | register void *p; | ||
45 | |||
46 | if (num && size && SIZE_T_MAX / num < size) { | ||
47 | errno = ENOMEM; | ||
48 | return NULL; | ||
49 | } | ||
50 | size *= num; | ||
51 | p = malloc(size); | ||
52 | if (p) | ||
53 | memset(p, 0, size); | ||
54 | return(p); | ||
55 | } | ||
diff --git a/src/lib/libc/stdlib/cfree.c b/src/lib/libc/stdlib/cfree.c new file mode 100644 index 0000000000..ecbc11d6c3 --- /dev/null +++ b/src/lib/libc/stdlib/cfree.c | |||
@@ -0,0 +1,45 @@ | |||
1 | /* $OpenBSD: cfree.c,v 1.3 2003/07/18 23:05:13 david Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996 SigmaSoft, Th. Lockert <tholo@sigmasoft.com> | ||
5 | * 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 | * | ||
16 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
17 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY | ||
18 | * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | ||
19 | * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
20 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
21 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | ||
22 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
23 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | ||
24 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | ||
25 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | #if defined(LIBC_SCCS) && !defined(lint) | ||
29 | static char rcsid[] = "$OpenBSD: cfree.c,v 1.3 2003/07/18 23:05:13 david Exp $"; | ||
30 | #endif /* LIBC_SCCS and not lint */ | ||
31 | |||
32 | #include <sys/cdefs.h> | ||
33 | #include <stdlib.h> | ||
34 | |||
35 | #ifdef __indr_reference | ||
36 | __indr_reference(free, cfree); | ||
37 | #else | ||
38 | |||
39 | void | ||
40 | cfree(p) | ||
41 | void *p; | ||
42 | { | ||
43 | free(p); | ||
44 | } | ||
45 | #endif | ||
diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3 new file mode 100644 index 0000000000..460ab53137 --- /dev/null +++ b/src/lib/libc/stdlib/div.3 | |||
@@ -0,0 +1,63 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" $OpenBSD: div.3,v 1.6 2003/06/02 20:18:37 millert Exp $ | ||
31 | .\" | ||
32 | .Dd April 19, 1991 | ||
33 | .Dt DIV 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm div | ||
37 | .Nd return quotient and remainder from division | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <stdlib.h> | ||
40 | .Ft div_t | ||
41 | .Fn div "int num" "int denom" | ||
42 | .Sh DESCRIPTION | ||
43 | The | ||
44 | .Fn div | ||
45 | function computes the value | ||
46 | .Fa num Ns No / Ns Fa denom | ||
47 | and returns the quotient and remainder in a structure named | ||
48 | .Fa div_t | ||
49 | that contains two | ||
50 | .Li int | ||
51 | members named | ||
52 | .Fa quot | ||
53 | and | ||
54 | .Fa rem . | ||
55 | .Sh SEE ALSO | ||
56 | .Xr ldiv 3 , | ||
57 | .Xr math 3 , | ||
58 | .Xr qdiv 3 | ||
59 | .Sh STANDARDS | ||
60 | The | ||
61 | .Fn div | ||
62 | function conforms to | ||
63 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c new file mode 100644 index 0000000000..9e070dcd64 --- /dev/null +++ b/src/lib/libc/stdlib/div.c | |||
@@ -0,0 +1,75 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1990 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: div.c,v 1.3 2003/06/02 20:18:37 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <stdlib.h> /* div_t */ | ||
38 | |||
39 | div_t | ||
40 | div(num, denom) | ||
41 | int num, denom; | ||
42 | { | ||
43 | div_t r; | ||
44 | |||
45 | r.quot = num / denom; | ||
46 | r.rem = num % denom; | ||
47 | /* | ||
48 | * The ANSI standard says that |r.quot| <= |n/d|, where | ||
49 | * n/d is to be computed in infinite precision. In other | ||
50 | * words, we should always truncate the quotient towards | ||
51 | * 0, never -infinity. | ||
52 | * | ||
53 | * Machine division and remainer may work either way when | ||
54 | * one or both of n or d is negative. If only one is | ||
55 | * negative and r.quot has been truncated towards -inf, | ||
56 | * r.rem will have the same sign as denom and the opposite | ||
57 | * sign of num; if both are negative and r.quot has been | ||
58 | * truncated towards -inf, r.rem will be positive (will | ||
59 | * have the opposite sign of num). These are considered | ||
60 | * `wrong'. | ||
61 | * | ||
62 | * If both are num and denom are positive, r will always | ||
63 | * be positive. | ||
64 | * | ||
65 | * This all boils down to: | ||
66 | * if num >= 0, but r.rem < 0, we got the wrong answer. | ||
67 | * In that case, to get the right answer, add 1 to r.quot and | ||
68 | * subtract denom from r.rem. | ||
69 | */ | ||
70 | if (num >= 0 && r.rem < 0) { | ||
71 | r.quot++; | ||
72 | r.rem -= denom; | ||
73 | } | ||
74 | return (r); | ||
75 | } | ||
diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c new file mode 100644 index 0000000000..02886d5b62 --- /dev/null +++ b/src/lib/libc/stdlib/drand48.c | |||
@@ -0,0 +1,26 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: drand48.c,v 1.2 1996/08/19 08:33:29 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | extern unsigned short __rand48_seed[3]; | ||
21 | |||
22 | double | ||
23 | drand48(void) | ||
24 | { | ||
25 | return erand48(__rand48_seed); | ||
26 | } | ||
diff --git a/src/lib/libc/stdlib/ecvt.3 b/src/lib/libc/stdlib/ecvt.3 new file mode 100644 index 0000000000..da48a33957 --- /dev/null +++ b/src/lib/libc/stdlib/ecvt.3 | |||
@@ -0,0 +1,165 @@ | |||
1 | .\" $OpenBSD: ecvt.3,v 1.6 2003/06/17 21:56:24 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | .\" | ||
17 | .\" Sponsored in part by the Defense Advanced Research Projects | ||
18 | .\" Agency (DARPA) and Air Force Research Laboratory, Air Force | ||
19 | .\" Materiel Command, USAF, under agreement number F39502-99-1-0512. | ||
20 | .\" | ||
21 | .Dd December 1, 2002 | ||
22 | .Dt ECVT 3 | ||
23 | .Os | ||
24 | .Sh NAME | ||
25 | .Nm ecvt , | ||
26 | .Nm fcvt , | ||
27 | .Nm gcvt | ||
28 | .Nd convert double to | ||
29 | .Tn ASCII | ||
30 | string | ||
31 | .Sh SYNOPSIS | ||
32 | .Fd #include <stdlib.h> | ||
33 | .Ft char * | ||
34 | .Fn ecvt "double value" "int ndigit" "int *decpt" "int *sign" | ||
35 | .Ft char * | ||
36 | .Fn fcvt "double value" "int ndigit" "int *decpt" "int *sign" | ||
37 | .Ft char * | ||
38 | .Fn gcvt "double value" "int ndigit" "char *buf" | ||
39 | .Sh DESCRIPTION | ||
40 | .Bf -symbolic | ||
41 | These functions are provided for compatibility with legacy code. | ||
42 | New code should use the | ||
43 | .Xr snprintf 3 | ||
44 | function for improved safety and portability. | ||
45 | .Ef | ||
46 | .Pp | ||
47 | The | ||
48 | .Fn ecvt , | ||
49 | .Fn fcvt | ||
50 | and | ||
51 | .Fn gcvt | ||
52 | functions convert the double precision floating-point number | ||
53 | .Fa value | ||
54 | to a NUL-terminated | ||
55 | .Tn ASCII | ||
56 | string. | ||
57 | .Pp | ||
58 | The | ||
59 | .Fn ecvt | ||
60 | function converts | ||
61 | .Fa value | ||
62 | to a NUL-terminated string of exactly | ||
63 | .Fa ndigit | ||
64 | digits and returns a pointer to that string. | ||
65 | The result is padded with zeroes from left to right as needed. | ||
66 | There are no leading zeroes unless | ||
67 | .Fa value | ||
68 | itself is 0. | ||
69 | The least significant digit is rounded in an implementation-dependent manner. | ||
70 | The position of the decimal point relative to the beginning of the string | ||
71 | is stored in | ||
72 | .Fa decpt . | ||
73 | A negative value indicates that the decimal point is located | ||
74 | to the left of the returned digits (this occurs when there is no | ||
75 | whole number component to | ||
76 | .Fa value ) . | ||
77 | If | ||
78 | .Fa value | ||
79 | is zero, it is unspecified whether the integer pointed to by | ||
80 | .Fa decpt | ||
81 | will be 0 or 1. | ||
82 | The decimal point itself is not included in the returned string. | ||
83 | If the sign of the result is negative, the integer pointed to by | ||
84 | .Fa sign | ||
85 | is non-zero; otherwise, it is 0. | ||
86 | .Pp | ||
87 | If the converted value is out of range or is not representable, | ||
88 | the contents of the returned string are unspecified. | ||
89 | .Pp | ||
90 | The | ||
91 | .Fn fcvt | ||
92 | function is identical to | ||
93 | .Fn ecvt | ||
94 | with the exception that | ||
95 | .Fa ndigit | ||
96 | specifies the number of digits after the decimal point (zero-padded as | ||
97 | needed). | ||
98 | .Pp | ||
99 | The | ||
100 | .Fn gcvt | ||
101 | function converts | ||
102 | .Fa value | ||
103 | to a NUL-terminated string similar to the %g | ||
104 | .Xr printf 3 | ||
105 | format specifier and stores the result in | ||
106 | .Fa buf . | ||
107 | It produces | ||
108 | .Fa ndigit | ||
109 | significant digits similar to the %f | ||
110 | .Xr printf 3 | ||
111 | format specifier where possible. | ||
112 | If | ||
113 | .Fa ndigit | ||
114 | does allow sufficient precision, the result is stored in | ||
115 | exponential notation similar to the %e | ||
116 | .Xr printf 3 | ||
117 | format specifier. | ||
118 | If | ||
119 | .Fa value | ||
120 | is less than zero, | ||
121 | .Fa buf | ||
122 | will be prefixed with a minus sign. | ||
123 | A decimal point is included in the returned string if | ||
124 | .Fa value | ||
125 | is not a whole number. | ||
126 | Unlike the | ||
127 | .Fn ecvt | ||
128 | and | ||
129 | .Fn fcvt | ||
130 | functions, | ||
131 | .Fa buf | ||
132 | is not zero-padded. | ||
133 | .Sh RETURN VALUES | ||
134 | The | ||
135 | .Fn ecvt , | ||
136 | .Fn fcvt | ||
137 | and | ||
138 | .Fn gcvt | ||
139 | functions return a NUL-terminated string representation of | ||
140 | .Fa value . | ||
141 | .Sh WARNINGS | ||
142 | The | ||
143 | .Fn ecvt | ||
144 | and | ||
145 | .Fn fcvt | ||
146 | functions return a pointer to internal storage space that will be | ||
147 | overwritten by subsequent calls to either function. | ||
148 | .Pp | ||
149 | The maximum possible precision of the return value is limited by the | ||
150 | precision of a double and may not be the same on all architectures. | ||
151 | .Pp | ||
152 | The | ||
153 | .Xr snprintf 3 | ||
154 | function is preferred over these functions for new code. | ||
155 | .Sh SEE ALSO | ||
156 | .Xr printf 3 , | ||
157 | .Xr strtod 3 | ||
158 | .Sh STANDARDS | ||
159 | The | ||
160 | .Fn ecvt , | ||
161 | .Fn fcvt | ||
162 | and | ||
163 | .Fn gcvt | ||
164 | functions conform to | ||
165 | .St -p1003.1-01 . | ||
diff --git a/src/lib/libc/stdlib/ecvt.c b/src/lib/libc/stdlib/ecvt.c new file mode 100644 index 0000000000..657c531808 --- /dev/null +++ b/src/lib/libc/stdlib/ecvt.c | |||
@@ -0,0 +1,103 @@ | |||
1 | /* $OpenBSD: ecvt.c,v 1.3 2003/06/17 21:56:24 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | * | ||
18 | * Sponsored in part by the Defense Advanced Research Projects | ||
19 | * Agency (DARPA) and Air Force Research Laboratory, Air Force | ||
20 | * Materiel Command, USAF, under agreement number F39502-99-1-0512. | ||
21 | */ | ||
22 | |||
23 | #if defined(LIBC_SCCS) && !defined(lint) | ||
24 | static char rcsid[] = "$OpenBSD: ecvt.c,v 1.3 2003/06/17 21:56:24 millert Exp $"; | ||
25 | #endif /* LIBC_SCCS and not lint */ | ||
26 | |||
27 | #include <stdio.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <string.h> | ||
30 | |||
31 | extern char *__dtoa(double, int, int, int *, int *, char **); | ||
32 | static char *__cvt(double, int, int *, int *, int, int); | ||
33 | |||
34 | static char * | ||
35 | __cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad) | ||
36 | { | ||
37 | static char *s; | ||
38 | char *p, *rve; | ||
39 | size_t siz; | ||
40 | |||
41 | if (ndigit == 0) { | ||
42 | *sign = value < 0.0; | ||
43 | *decpt = 0; | ||
44 | return (""); | ||
45 | } | ||
46 | |||
47 | if (s) { | ||
48 | free(s); | ||
49 | s = NULL; | ||
50 | } | ||
51 | |||
52 | if (ndigit < 0) | ||
53 | siz = -ndigit + 1; | ||
54 | else | ||
55 | siz = ndigit + 1; | ||
56 | |||
57 | |||
58 | /* __dtoa() doesn't allocate space for 0 so we do it by hand */ | ||
59 | if (value == 0.0) { | ||
60 | *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */ | ||
61 | *sign = 0; | ||
62 | if ((rve = s = (char *)malloc(siz)) == NULL) | ||
63 | return(NULL); | ||
64 | *rve++ = '0'; | ||
65 | *rve = '\0'; | ||
66 | } else { | ||
67 | p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve); | ||
68 | if (*decpt == 9999) { | ||
69 | /* Nan or Infinity */ | ||
70 | *decpt = 0; | ||
71 | return(p); | ||
72 | } | ||
73 | /* make a local copy and adjust rve to be in terms of s */ | ||
74 | if (pad && fmode) | ||
75 | siz += *decpt; | ||
76 | if ((s = (char *)malloc(siz)) == NULL) | ||
77 | return(NULL); | ||
78 | (void) strlcpy(s, p, siz); | ||
79 | rve = s + (rve - p); | ||
80 | } | ||
81 | |||
82 | /* Add trailing zeros (unless we got NaN or Inf) */ | ||
83 | if (pad && *decpt != 9999) { | ||
84 | siz -= rve - s; | ||
85 | while (--siz) | ||
86 | *rve++ = '0'; | ||
87 | *rve = '\0'; | ||
88 | } | ||
89 | |||
90 | return(s); | ||
91 | } | ||
92 | |||
93 | char * | ||
94 | ecvt(double value, int ndigit, int *decpt, int *sign) | ||
95 | { | ||
96 | return(__cvt(value, ndigit, decpt, sign, 0, 1)); | ||
97 | } | ||
98 | |||
99 | char * | ||
100 | fcvt(double value, int ndigit, int *decpt, int *sign) | ||
101 | { | ||
102 | return(__cvt(value, ndigit, decpt, sign, 1, 1)); | ||
103 | } | ||
diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c new file mode 100644 index 0000000000..b92dacffcc --- /dev/null +++ b/src/lib/libc/stdlib/erand48.c | |||
@@ -0,0 +1,27 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: erand48.c,v 1.2 1996/08/19 08:33:29 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | double | ||
21 | erand48(unsigned short xseed[3]) | ||
22 | { | ||
23 | __dorand48(xseed); | ||
24 | return ldexp((double) xseed[0], -48) + | ||
25 | ldexp((double) xseed[1], -32) + | ||
26 | ldexp((double) xseed[2], -16); | ||
27 | } | ||
diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3 new file mode 100644 index 0000000000..8c4b2eed2c --- /dev/null +++ b/src/lib/libc/stdlib/exit.3 | |||
@@ -0,0 +1,89 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: exit.3,v 1.9 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt EXIT 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm exit | ||
39 | .Nd perform normal program termination | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft void | ||
43 | .Fn exit "int status" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn exit | ||
47 | function terminates a process. | ||
48 | .Pp | ||
49 | Before termination it performs the following functions in the | ||
50 | order listed: | ||
51 | .Bl -enum -offset indent | ||
52 | .It | ||
53 | Call the functions registered with the | ||
54 | .Xr atexit 3 | ||
55 | function, in the reverse order of their registration. | ||
56 | .It | ||
57 | Flush all open output streams. | ||
58 | .It | ||
59 | Close all open streams. | ||
60 | .It | ||
61 | Unlink all files created with the | ||
62 | .Xr tmpfile 3 | ||
63 | function. | ||
64 | .El | ||
65 | .Pp | ||
66 | Following this, | ||
67 | .Fn exit | ||
68 | calls | ||
69 | .Xr _exit 2 . | ||
70 | Note that typically | ||
71 | .Xr _exit 2 | ||
72 | only passes the lower 8 bits of | ||
73 | .Fa status | ||
74 | on to the parent, thus negative values have less meaning. | ||
75 | .Sh RETURN VALUES | ||
76 | The | ||
77 | .Fn exit | ||
78 | function never returns. | ||
79 | .Sh SEE ALSO | ||
80 | .Xr _exit 2 , | ||
81 | .Xr atexit 3 , | ||
82 | .Xr intro 3 , | ||
83 | .Xr sysexits 3 , | ||
84 | .Xr tmpfile 3 | ||
85 | .Sh STANDARDS | ||
86 | The | ||
87 | .Fn exit | ||
88 | function conforms to | ||
89 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c new file mode 100644 index 0000000000..a75b32abeb --- /dev/null +++ b/src/lib/libc/stdlib/exit.c | |||
@@ -0,0 +1,73 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: exit.c,v 1.9 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/mman.h> | ||
36 | #include <stdlib.h> | ||
37 | #include <unistd.h> | ||
38 | #include "atexit.h" | ||
39 | #include "thread_private.h" | ||
40 | |||
41 | /* | ||
42 | * This variable is zero until a process has created a thread. | ||
43 | * It is used to avoid calling locking functions in libc when they | ||
44 | * are not required. By default, libc is intended to be(come) | ||
45 | * thread-safe, but without a (significant) penalty to non-threaded | ||
46 | * processes. | ||
47 | */ | ||
48 | int __isthreaded = 0; | ||
49 | |||
50 | /* | ||
51 | * Exit, flushing stdio buffers if necessary. | ||
52 | */ | ||
53 | void | ||
54 | exit(status) | ||
55 | int status; | ||
56 | { | ||
57 | register struct atexit *p, *q; | ||
58 | register int n, pgsize = getpagesize(); | ||
59 | |||
60 | if (!__atexit_invalid) { | ||
61 | p = __atexit; | ||
62 | while (p != NULL) { | ||
63 | for (n = p->ind; --n >= 0;) | ||
64 | if (p->fns[n] != NULL) | ||
65 | (*p->fns[n])(); | ||
66 | q = p; | ||
67 | p = p->next; | ||
68 | munmap(q, pgsize); | ||
69 | } | ||
70 | } | ||
71 | /* cleanup, if registered, was called through fns[0] in the last page */ | ||
72 | _exit(status); | ||
73 | } | ||
diff --git a/src/lib/libc/stdlib/gcvt.c b/src/lib/libc/stdlib/gcvt.c new file mode 100644 index 0000000000..9fd664b346 --- /dev/null +++ b/src/lib/libc/stdlib/gcvt.c | |||
@@ -0,0 +1,99 @@ | |||
1 | /* $OpenBSD: gcvt.c,v 1.5 2003/06/17 21:56:24 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2002, 2003 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | * | ||
18 | * Sponsored in part by the Defense Advanced Research Projects | ||
19 | * Agency (DARPA) and Air Force Research Laboratory, Air Force | ||
20 | * Materiel Command, USAF, under agreement number F39502-99-1-0512. | ||
21 | */ | ||
22 | |||
23 | #if defined(LIBC_SCCS) && !defined(lint) | ||
24 | static char rcsid[] = "$OpenBSD: gcvt.c,v 1.5 2003/06/17 21:56:24 millert Exp $"; | ||
25 | #endif /* LIBC_SCCS and not lint */ | ||
26 | |||
27 | #include <stdio.h> | ||
28 | #include <stdlib.h> | ||
29 | #include <string.h> | ||
30 | |||
31 | extern char *__dtoa(double, int, int, int *, int *, char **); | ||
32 | |||
33 | char * | ||
34 | gcvt(double value, int ndigit, char *buf) | ||
35 | { | ||
36 | char *digits, *dst, *src; | ||
37 | int i, decpt, sign; | ||
38 | |||
39 | if (ndigit == 0) { | ||
40 | buf[0] = '\0'; | ||
41 | return (buf); | ||
42 | } | ||
43 | |||
44 | digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL); | ||
45 | if (decpt == 9999) { | ||
46 | /* Infinity or NaN, assume buffer is at least ndigit long. */ | ||
47 | strlcpy(buf, digits, ndigit + 1); | ||
48 | return (buf); | ||
49 | } | ||
50 | |||
51 | dst = buf; | ||
52 | if (sign) | ||
53 | *dst++ = '-'; | ||
54 | |||
55 | if (decpt < 0 || decpt > ndigit) { | ||
56 | /* exponential format */ | ||
57 | if (--decpt < 0) { | ||
58 | sign = 1; | ||
59 | decpt = -decpt; | ||
60 | } else | ||
61 | sign = 0; | ||
62 | for (src = digits; *src != '\0'; ) | ||
63 | *dst++ = *src++; | ||
64 | *dst++ = 'e'; | ||
65 | if (sign) | ||
66 | *dst++ = '-'; | ||
67 | else | ||
68 | *dst++ = '+'; | ||
69 | if (decpt < 10) { | ||
70 | *dst++ = '0'; | ||
71 | *dst++ = '0' + decpt; | ||
72 | *dst = '\0'; | ||
73 | } else { | ||
74 | /* XXX - optimize */ | ||
75 | for (sign = decpt, i = 0; (sign /= 10) != 0; i++) | ||
76 | sign /= 10; | ||
77 | while (decpt != 0) { | ||
78 | dst[i--] = '0' + decpt % 10; | ||
79 | decpt /= 10; | ||
80 | } | ||
81 | } | ||
82 | } else { | ||
83 | /* standard format */ | ||
84 | for (i = 0, src = digits; i < decpt; i++) { | ||
85 | if (*src != '\0') | ||
86 | *dst++ = *src++; | ||
87 | else | ||
88 | *dst++ = '0'; | ||
89 | } | ||
90 | if (*src != '\0') { | ||
91 | *dst++ = '.'; /* XXX - locale-specific (LC_NUMERIC) */ | ||
92 | for (i = decpt; digits[i] != '\0'; i++) { | ||
93 | *dst++ = digits[i]; | ||
94 | } | ||
95 | } | ||
96 | *dst = '\0'; | ||
97 | } | ||
98 | return (buf); | ||
99 | } | ||
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3 new file mode 100644 index 0000000000..25c325307d --- /dev/null +++ b/src/lib/libc/stdlib/getenv.3 | |||
@@ -0,0 +1,142 @@ | |||
1 | .\" Copyright (c) 1988, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: getenv.3,v 1.8 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd December 11, 1993 | ||
35 | .Dt GETENV 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm getenv , | ||
39 | .Nm putenv , | ||
40 | .Nm setenv , | ||
41 | .Nm unsetenv | ||
42 | .Nd environment variable functions | ||
43 | .Sh SYNOPSIS | ||
44 | .Fd #include <stdlib.h> | ||
45 | .Ft char * | ||
46 | .Fn getenv "const char *name" | ||
47 | .Ft int | ||
48 | .Fn setenv "const char *name" "const char *value" "int overwrite" | ||
49 | .Ft int | ||
50 | .Fn putenv "const char *string" | ||
51 | .Ft void | ||
52 | .Fn unsetenv "const char *name" | ||
53 | .Sh DESCRIPTION | ||
54 | These functions set, unset, and fetch environment variables from the host | ||
55 | .Em environment list . | ||
56 | For compatibility with differing environment conventions, the given arguments | ||
57 | .Fa name | ||
58 | and | ||
59 | .Fa value | ||
60 | may be appended and prepended, respectively, with an equal sign | ||
61 | .Dq Li \&= . | ||
62 | .Pp | ||
63 | The | ||
64 | .Fn getenv | ||
65 | function obtains the current value of the environment variable, | ||
66 | .Fa name . | ||
67 | If the variable | ||
68 | .Fa name | ||
69 | is not in the current environment, a null pointer is returned. | ||
70 | .Pp | ||
71 | The | ||
72 | .Fn setenv | ||
73 | function inserts or resets the environment variable | ||
74 | .Fa name | ||
75 | in the current environment list. | ||
76 | If the variable | ||
77 | .Fa name | ||
78 | does not exist in the list, it is inserted with the given | ||
79 | .Fa value . | ||
80 | If the variable does exist, the argument | ||
81 | .Fa overwrite | ||
82 | is tested; if | ||
83 | .Fa overwrite | ||
84 | is zero, the variable is not reset, otherwise it is reset to the given | ||
85 | .Fa value . | ||
86 | .Pp | ||
87 | The | ||
88 | .Fn putenv | ||
89 | function takes an argument of the form | ||
90 | .Ar name Ns No = Ns Ar value | ||
91 | and is equivalent to: | ||
92 | .Bd -literal -offset indent | ||
93 | setenv(name, value, 1); | ||
94 | .Ed | ||
95 | .Pp | ||
96 | The | ||
97 | .Fn unsetenv | ||
98 | function deletes all instances of the variable name pointed to by | ||
99 | .Fa name | ||
100 | from the list. | ||
101 | .Sh RETURN VALUES | ||
102 | The functions | ||
103 | .Fn setenv | ||
104 | and | ||
105 | .Fn putenv | ||
106 | return zero if successful; otherwise the global variable | ||
107 | .Va errno | ||
108 | is set to indicate the error and \-1 is returned. | ||
109 | .Pp | ||
110 | If | ||
111 | .Fn getenv | ||
112 | is successful, the string returned should be considered read-only. | ||
113 | .Sh ERRORS | ||
114 | .Bl -tag -width [ENOMEM] | ||
115 | .It Bq Er ENOMEM | ||
116 | The function | ||
117 | .Fn setenv | ||
118 | or | ||
119 | .Fn putenv | ||
120 | failed because they were unable to allocate memory for the environment. | ||
121 | .El | ||
122 | .Sh SEE ALSO | ||
123 | .Xr csh 1 , | ||
124 | .Xr sh 1 , | ||
125 | .Xr execve 2 , | ||
126 | .Xr environ 7 | ||
127 | .Sh STANDARDS | ||
128 | The | ||
129 | .Fn getenv | ||
130 | function conforms to | ||
131 | .St -ansiC . | ||
132 | .Sh HISTORY | ||
133 | The functions | ||
134 | .Fn setenv | ||
135 | and | ||
136 | .Fn unsetenv | ||
137 | appeared in | ||
138 | .At v7 . | ||
139 | The | ||
140 | .Fn putenv | ||
141 | function appeared in | ||
142 | .Bx 4.3 Reno . | ||
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c new file mode 100644 index 0000000000..c597e468a9 --- /dev/null +++ b/src/lib/libc/stdlib/getenv.c | |||
@@ -0,0 +1,84 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1987, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: getenv.c,v 1.6 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | char *__findenv(const char *name, int *offset); | ||
38 | |||
39 | /* | ||
40 | * __findenv -- | ||
41 | * Returns pointer to value associated with name, if any, else NULL. | ||
42 | * Sets offset to be the offset of the name/value combination in the | ||
43 | * environmental array, for use by setenv(3) and unsetenv(3). | ||
44 | * Explicitly removes '=' in argument name. | ||
45 | * | ||
46 | * This routine *should* be a static; don't use it. | ||
47 | */ | ||
48 | char * | ||
49 | __findenv(const char *name, int *offset) | ||
50 | { | ||
51 | extern char **environ; | ||
52 | register int len, i; | ||
53 | register const char *np; | ||
54 | register char **p, *cp; | ||
55 | |||
56 | if (name == NULL || environ == NULL) | ||
57 | return (NULL); | ||
58 | for (np = name; *np && *np != '='; ++np) | ||
59 | ; | ||
60 | len = np - name; | ||
61 | for (p = environ; (cp = *p) != NULL; ++p) { | ||
62 | for (np = name, i = len; i && *cp; i--) | ||
63 | if (*cp++ != *np++) | ||
64 | break; | ||
65 | if (i == 0 && *cp++ == '=') { | ||
66 | *offset = p - environ; | ||
67 | return (cp); | ||
68 | } | ||
69 | } | ||
70 | return (NULL); | ||
71 | } | ||
72 | |||
73 | /* | ||
74 | * getenv -- | ||
75 | * Returns ptr to value associated with name, if any, else NULL. | ||
76 | */ | ||
77 | char * | ||
78 | getenv(name) | ||
79 | const char *name; | ||
80 | { | ||
81 | int offset; | ||
82 | |||
83 | return (__findenv(name, &offset)); | ||
84 | } | ||
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 new file mode 100644 index 0000000000..d210852c6b --- /dev/null +++ b/src/lib/libc/stdlib/getopt.3 | |||
@@ -0,0 +1,340 @@ | |||
1 | .\" Copyright (c) 1988, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: getopt.3,v 1.28 2003/09/22 23:47:26 millert Exp $ | ||
29 | .\" | ||
30 | .Dd December 17, 2002 | ||
31 | .Dt GETOPT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm getopt | ||
35 | .Nd get option character from command line argument list | ||
36 | .Sh SYNOPSIS | ||
37 | .Fd #include <unistd.h> | ||
38 | .Vt extern char *optarg; | ||
39 | .Vt extern int optind; | ||
40 | .Vt extern int optopt; | ||
41 | .Vt extern int opterr; | ||
42 | .Vt extern int optreset; | ||
43 | .Ft int | ||
44 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | ||
45 | .Sh DESCRIPTION | ||
46 | The | ||
47 | .Fn getopt | ||
48 | function incrementally parses a command line argument list | ||
49 | .Fa argv | ||
50 | and returns the next | ||
51 | .Em known | ||
52 | option character. | ||
53 | An option character is | ||
54 | .Em known | ||
55 | if it has been specified in the string of accepted option characters, | ||
56 | .Fa optstring . | ||
57 | .Pp | ||
58 | The option string | ||
59 | .Fa optstring | ||
60 | may contain the following elements: individual characters, | ||
61 | characters followed by a colon, and characters followed by two colons. | ||
62 | A character followed by a single colon indicates that an argument | ||
63 | is to follow the option on the command line. | ||
64 | Two colons indicates that the argument is optional--this is an | ||
65 | extension not covered by | ||
66 | .Px . | ||
67 | For example, an option string | ||
68 | .Qq x | ||
69 | recognizes an option | ||
70 | .Fl x , | ||
71 | and an option string | ||
72 | .Qq Li x: | ||
73 | recognizes an option and argument | ||
74 | .Fl x Ar argument . | ||
75 | It does not matter to | ||
76 | .Fn getopt | ||
77 | if a following argument has leading whitespace. | ||
78 | .Pp | ||
79 | On return from | ||
80 | .Fn getopt , | ||
81 | .Va optarg | ||
82 | points to an option argument, if it is anticipated, | ||
83 | and the variable | ||
84 | .Va optind | ||
85 | contains the index to the next | ||
86 | .Fa argv | ||
87 | argument for a subsequent call | ||
88 | to | ||
89 | .Fn getopt . | ||
90 | .Pp | ||
91 | The variables | ||
92 | .Va opterr | ||
93 | and | ||
94 | .Va optind | ||
95 | are both initialized to 1. | ||
96 | The | ||
97 | .Va optind | ||
98 | variable may be set to another value before a set of calls to | ||
99 | .Fn getopt | ||
100 | in order to skip over more or less argv entries. | ||
101 | .Pp | ||
102 | In order to use | ||
103 | .Fn getopt | ||
104 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
105 | arguments multiple times, | ||
106 | the variable | ||
107 | .Va optreset | ||
108 | must be set to 1 before the second and each additional set of calls to | ||
109 | .Fn getopt , | ||
110 | and the variable | ||
111 | .Va optind | ||
112 | must be reinitialized. | ||
113 | .Pp | ||
114 | The | ||
115 | .Fn getopt | ||
116 | function returns \-1 when the argument list is exhausted. | ||
117 | The interpretation of options in the argument list may be cancelled | ||
118 | by the option | ||
119 | .Ql -- | ||
120 | (double dash) which causes | ||
121 | .Fn getopt | ||
122 | to signal the end of argument processing and return \-1. | ||
123 | When all options have been processed (i.e., up to the first non-option | ||
124 | argument), | ||
125 | .Fn getopt | ||
126 | returns \-1. | ||
127 | .Sh RETURN VALUES | ||
128 | The | ||
129 | .Fn getopt | ||
130 | function returns the next known option character in | ||
131 | .Fa optstring . | ||
132 | If | ||
133 | .Fn getopt | ||
134 | encounters a character not found in | ||
135 | .Fa optstring | ||
136 | or if it detects a missing option argument, | ||
137 | it returns | ||
138 | .Sq \&? | ||
139 | (question mark). | ||
140 | If | ||
141 | .Fa optstring | ||
142 | has a leading | ||
143 | .Sq \&: | ||
144 | then a missing option argument causes | ||
145 | .Sq \&: | ||
146 | to be returned instead of | ||
147 | .Sq \&? . | ||
148 | In either case, the variable | ||
149 | .Va optopt | ||
150 | is set to the character that caused the error. | ||
151 | The | ||
152 | .Fn getopt | ||
153 | function returns \-1 when the argument list is exhausted. | ||
154 | .Sh ENVIRONMENT | ||
155 | .Bl -tag -width POSIXLY_CORRECTXX | ||
156 | .It Ev POSIXLY_CORRECT | ||
157 | If set, a leading | ||
158 | .Sq - | ||
159 | in | ||
160 | .Ar optstring | ||
161 | is ignored. | ||
162 | .El | ||
163 | .Sh EXAMPLES | ||
164 | .Bd -literal -compact | ||
165 | extern char *optarg; | ||
166 | extern int optind; | ||
167 | int bflag, ch, fd; | ||
168 | |||
169 | bflag = 0; | ||
170 | while ((ch = getopt(argc, argv, "bf:")) != -1) { | ||
171 | switch (ch) { | ||
172 | case 'b': | ||
173 | bflag = 1; | ||
174 | break; | ||
175 | case 'f': | ||
176 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | ||
177 | (void)fprintf(stderr, | ||
178 | "myname: %s: %s\en", optarg, strerror(errno)); | ||
179 | exit(1); | ||
180 | } | ||
181 | break; | ||
182 | case '?': | ||
183 | default: | ||
184 | usage(); | ||
185 | } | ||
186 | } | ||
187 | argc -= optind; | ||
188 | argv += optind; | ||
189 | .Ed | ||
190 | .Sh DIAGNOSTICS | ||
191 | If the | ||
192 | .Fn getopt | ||
193 | function encounters a character not found in the string | ||
194 | .Fa optstring | ||
195 | or detects | ||
196 | a missing option argument it writes an error message to | ||
197 | .Em stderr | ||
198 | and returns | ||
199 | .Ql \&? . | ||
200 | Setting | ||
201 | .Va opterr | ||
202 | to a zero will disable these error messages. | ||
203 | If | ||
204 | .Fa optstring | ||
205 | has a leading | ||
206 | .Ql \&: | ||
207 | then a missing option argument causes a | ||
208 | .Ql \&: | ||
209 | to be returned in addition to suppressing any error messages. | ||
210 | .Pp | ||
211 | Option arguments are allowed to begin with | ||
212 | .Ql - ; | ||
213 | this is reasonable but reduces the amount of error checking possible. | ||
214 | .Sh SEE ALSO | ||
215 | .Xr getopt 1 , | ||
216 | .Xr getopt_long 3 , | ||
217 | .Xr getsubopt 3 | ||
218 | .Sh STANDARDS | ||
219 | The | ||
220 | .Fn getopt | ||
221 | function implements a superset of the functionality specified by | ||
222 | .St -p1003.1 . | ||
223 | .Pp | ||
224 | The following extensions are supported: | ||
225 | .Bl -tag -width "xxx" | ||
226 | .It Li o | ||
227 | The | ||
228 | .Va optreset | ||
229 | variable was added to make it possible to call the | ||
230 | .Fn getopt | ||
231 | function multiple times. | ||
232 | .It Li o | ||
233 | If the first character of | ||
234 | .Fa optstring | ||
235 | is a plus sign | ||
236 | .Pq Ql + , | ||
237 | it will be ignored. | ||
238 | This is for compatibility with | ||
239 | .Tn GNU | ||
240 | .Fn getopt . | ||
241 | .It Li o | ||
242 | If the first character of | ||
243 | .Fa optstring | ||
244 | is a dash | ||
245 | .Pq Ql - , | ||
246 | non-options will be returned as arguments to the option character | ||
247 | .Ql \e1 . | ||
248 | This is for compatibility with | ||
249 | .Tn GNU | ||
250 | .Fn getopt . | ||
251 | .It Li o | ||
252 | A single dash | ||
253 | .Pq Ql - | ||
254 | may be specified as a character in | ||
255 | .Fa optstring , | ||
256 | however it should | ||
257 | .Em never | ||
258 | have an argument associated with it. | ||
259 | This allows | ||
260 | .Fn getopt | ||
261 | to be used with programs that expect | ||
262 | .Ql - | ||
263 | as an option flag. | ||
264 | This practice is wrong, and should not be used in any current development. | ||
265 | It is provided for backward compatibility | ||
266 | .Em only . | ||
267 | Care should be taken not to use | ||
268 | .Ql - | ||
269 | as the first character in | ||
270 | .Fa optstring | ||
271 | to avoid a semantic conflict with | ||
272 | .Tn GNU | ||
273 | .Fn getopt | ||
274 | semantics (see above). | ||
275 | By default, a single dash causes | ||
276 | .Fn getopt | ||
277 | to return \-1. | ||
278 | .El | ||
279 | .Pp | ||
280 | Unlike | ||
281 | .Tn GNU | ||
282 | .Fn getopt , | ||
283 | .Ox | ||
284 | does not permute the argument vector to allow non-options to be | ||
285 | interspersed with options on the command line. | ||
286 | Programs requiring this behavior should use | ||
287 | .Xr getopt_long 3 | ||
288 | instead. | ||
289 | Because of this (and unlike | ||
290 | .Tn GNU ) , | ||
291 | the | ||
292 | .Ox | ||
293 | .Fn getopt | ||
294 | supports optional arguments separated by whitespace. | ||
295 | .Sh HISTORY | ||
296 | The | ||
297 | .Fn getopt | ||
298 | function appeared in | ||
299 | .Bx 4.3 . | ||
300 | .Sh BUGS | ||
301 | The | ||
302 | .Fn getopt | ||
303 | function was once specified to return | ||
304 | .Dv EOF | ||
305 | instead of \-1. | ||
306 | This was changed by | ||
307 | .St -p1003.2-92 | ||
308 | to decouple | ||
309 | .Fn getopt | ||
310 | from | ||
311 | .Aq Pa stdio.h . | ||
312 | .Pp | ||
313 | It is possible to handle digits as option letters. | ||
314 | This allows | ||
315 | .Fn getopt | ||
316 | to be used with programs that expect a number | ||
317 | .Pq Dq Li \-3 | ||
318 | as an option. | ||
319 | This practice is wrong, and should not be used in any current development. | ||
320 | It is provided for backward compatibility | ||
321 | .Em only . | ||
322 | The following code fragment works in most cases. | ||
323 | .Bd -literal -offset indent | ||
324 | int ch; | ||
325 | long length; | ||
326 | char *p; | ||
327 | |||
328 | while ((ch = getopt(argc, argv, "0123456789")) != -1) { | ||
329 | switch (ch) { | ||
330 | case '0': case '1': case '2': case '3': case '4': | ||
331 | case '5': case '6': case '7': case '8': case '9': | ||
332 | p = argv[optind - 1]; | ||
333 | if (p[0] == '-' && p[1] == ch && !p[2]) | ||
334 | length = ch - '0'; | ||
335 | else | ||
336 | length = strtol(argv[optind] + 1, NULL, 10); | ||
337 | break; | ||
338 | } | ||
339 | } | ||
340 | .Ed | ||
diff --git a/src/lib/libc/stdlib/getopt.c b/src/lib/libc/stdlib/getopt.c new file mode 100644 index 0000000000..ee75f9d2ef --- /dev/null +++ b/src/lib/libc/stdlib/getopt.c | |||
@@ -0,0 +1,116 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1987, 1993, 1994 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: getopt.c,v 1.5 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdio.h> | ||
35 | #include <stdlib.h> | ||
36 | #include <string.h> | ||
37 | |||
38 | int opterr = 1, /* if error message should be printed */ | ||
39 | optind = 1, /* index into parent argv vector */ | ||
40 | optopt, /* character checked for validity */ | ||
41 | optreset; /* reset getopt */ | ||
42 | char *optarg; /* argument associated with option */ | ||
43 | |||
44 | #define BADCH (int)'?' | ||
45 | #define BADARG (int)':' | ||
46 | #define EMSG "" | ||
47 | |||
48 | /* | ||
49 | * getopt -- | ||
50 | * Parse argc/argv argument vector. | ||
51 | */ | ||
52 | int | ||
53 | getopt(nargc, nargv, ostr) | ||
54 | int nargc; | ||
55 | char * const *nargv; | ||
56 | const char *ostr; | ||
57 | { | ||
58 | extern char *__progname; | ||
59 | static char *place = EMSG; /* option letter processing */ | ||
60 | char *oli; /* option letter list index */ | ||
61 | |||
62 | if (ostr == NULL) | ||
63 | return (-1); | ||
64 | |||
65 | if (optreset || !*place) { /* update scanning pointer */ | ||
66 | optreset = 0; | ||
67 | if (optind >= nargc || *(place = nargv[optind]) != '-') { | ||
68 | place = EMSG; | ||
69 | return (-1); | ||
70 | } | ||
71 | if (place[1] && *++place == '-') { /* found "--" */ | ||
72 | ++optind; | ||
73 | place = EMSG; | ||
74 | return (-1); | ||
75 | } | ||
76 | } /* option letter okay? */ | ||
77 | if ((optopt = (int)*place++) == (int)':' || | ||
78 | !(oli = strchr(ostr, optopt))) { | ||
79 | /* | ||
80 | * if the user didn't specify '-' as an option, | ||
81 | * assume it means -1. | ||
82 | */ | ||
83 | if (optopt == (int)'-') | ||
84 | return (-1); | ||
85 | if (!*place) | ||
86 | ++optind; | ||
87 | if (opterr && *ostr != ':') | ||
88 | (void)fprintf(stderr, | ||
89 | "%s: illegal option -- %c\n", __progname, optopt); | ||
90 | return (BADCH); | ||
91 | } | ||
92 | if (*++oli != ':') { /* don't need argument */ | ||
93 | optarg = NULL; | ||
94 | if (!*place) | ||
95 | ++optind; | ||
96 | } | ||
97 | else { /* need an argument */ | ||
98 | if (*place) /* no white space */ | ||
99 | optarg = place; | ||
100 | else if (nargc <= ++optind) { /* no arg */ | ||
101 | place = EMSG; | ||
102 | if (*ostr == ':') | ||
103 | return (BADARG); | ||
104 | if (opterr) | ||
105 | (void)fprintf(stderr, | ||
106 | "%s: option requires an argument -- %c\n", | ||
107 | __progname, optopt); | ||
108 | return (BADCH); | ||
109 | } | ||
110 | else /* white space */ | ||
111 | optarg = nargv[optind]; | ||
112 | place = EMSG; | ||
113 | ++optind; | ||
114 | } | ||
115 | return (optopt); /* dump back option letter */ | ||
116 | } | ||
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3 new file mode 100644 index 0000000000..6226dfe99f --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.3 | |||
@@ -0,0 +1,366 @@ | |||
1 | .\" $OpenBSD: getopt_long.3,v 1.9 2003/09/02 18:24:21 jmc Exp $ | ||
2 | .\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1988, 1991, 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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 | ||
32 | .\" | ||
33 | .Dd April 1, 2000 | ||
34 | .Dt GETOPT_LONG 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm getopt_long , | ||
38 | .Nm getopt_long_only | ||
39 | .Nd get long options from command line argument list | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <getopt.h> | ||
42 | .Vt extern char *optarg; | ||
43 | .Vt extern int optind; | ||
44 | .Vt extern int optopt; | ||
45 | .Vt extern int opterr; | ||
46 | .Vt extern int optreset; | ||
47 | .Ft int | ||
48 | .Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *index" | ||
49 | .Ft int | ||
50 | .Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *index" | ||
51 | .Sh DESCRIPTION | ||
52 | The | ||
53 | .Fn getopt_long | ||
54 | function is similar to | ||
55 | .Xr getopt 3 | ||
56 | but it accepts options in two forms: words and characters. | ||
57 | The | ||
58 | .Fn getopt_long | ||
59 | function provides a superset of the functionality of | ||
60 | .Xr getopt 3 . | ||
61 | .Fn getopt_long | ||
62 | can be used in two ways. | ||
63 | In the first way, every long option understood by the program has a | ||
64 | corresponding short option, and the option structure is only used to | ||
65 | translate from long options to short options. | ||
66 | When used in this fashion, | ||
67 | .Fn getopt_long | ||
68 | behaves identically to | ||
69 | .Xr getopt 3 . | ||
70 | This is a good way to add long option processing to an existing program | ||
71 | with the minimum of rewriting. | ||
72 | .Pp | ||
73 | In the second mechanism, a long option sets a flag in the | ||
74 | .Fa option | ||
75 | structure passed, or will store a pointer to the command line argument | ||
76 | in the | ||
77 | .Fa option | ||
78 | structure passed to it for options that take arguments. | ||
79 | Additionally, the long option's argument may be specified as a single | ||
80 | argument with an equal sign, e.g. | ||
81 | .Bd -literal | ||
82 | myprogram --myoption=somevalue | ||
83 | .Ed | ||
84 | .Pp | ||
85 | When a long option is processed the call to | ||
86 | .Fn getopt_long | ||
87 | will return 0. | ||
88 | For this reason, long option processing without | ||
89 | shortcuts is not backwards compatible with | ||
90 | .Xr getopt 3 . | ||
91 | .Pp | ||
92 | It is possible to combine these methods, providing for long options | ||
93 | processing with short option equivalents for some options. | ||
94 | Less frequently used options would be processed as long options only. | ||
95 | .Pp | ||
96 | The | ||
97 | .Fn getopt_long | ||
98 | call requires a structure to be initialized describing the long | ||
99 | options. | ||
100 | The structure is: | ||
101 | .Bd -literal | ||
102 | struct option { | ||
103 | char *name; | ||
104 | int has_arg; | ||
105 | int *flag; | ||
106 | int val; | ||
107 | }; | ||
108 | .Ed | ||
109 | .Pp | ||
110 | The | ||
111 | .Fa name | ||
112 | field should contain the option name without the leading double dash. | ||
113 | .Pp | ||
114 | The | ||
115 | .Fa has_arg | ||
116 | field should be one of: | ||
117 | .Bl -tag -width "optional_argument" | ||
118 | .It Li no_argument | ||
119 | no argument to the option is expect. | ||
120 | .It Li required_argument | ||
121 | an argument to the option is required. | ||
122 | .It Li optional_argument | ||
123 | an argument to the option may be presented. | ||
124 | .El | ||
125 | .Pp | ||
126 | If | ||
127 | .Fa flag | ||
128 | is not | ||
129 | .Dv NULL , | ||
130 | then the integer pointed to by it will be set to the value in the | ||
131 | .Fa val | ||
132 | field. | ||
133 | If the | ||
134 | .Fa flag | ||
135 | field is | ||
136 | .Dv NULL , | ||
137 | then the | ||
138 | .Fa val | ||
139 | field will be returned. | ||
140 | Setting | ||
141 | .Fa flag | ||
142 | to | ||
143 | .Dv NULL | ||
144 | and setting | ||
145 | .Fa val | ||
146 | to the corresponding short option will make this function act just | ||
147 | like | ||
148 | .Xr getopt 3 . | ||
149 | .Pp | ||
150 | The | ||
151 | .Fn getopt_long_only | ||
152 | function behaves identically to | ||
153 | .Fn getopt_long | ||
154 | with the exception that long options may start with | ||
155 | .Sq - | ||
156 | in addition to | ||
157 | .Sq -- . | ||
158 | If an option starting with | ||
159 | .Sq - | ||
160 | does not match a long option but does match a single-character option, | ||
161 | the single-character option is returned. | ||
162 | .Sh RETURN VALUES | ||
163 | If the | ||
164 | .Fa flag | ||
165 | field in | ||
166 | .Li struct option | ||
167 | is | ||
168 | .Dv NULL , | ||
169 | .Fn getopt_long | ||
170 | and | ||
171 | .Fn getopt_long_only | ||
172 | return the value specified in the | ||
173 | .Fa val | ||
174 | field, which is usually just the corresponding short option. | ||
175 | If | ||
176 | .Fa flag | ||
177 | is not | ||
178 | .Dv NULL , | ||
179 | these functions return 0 and store | ||
180 | .Fa val | ||
181 | in the location pointed to by | ||
182 | .Fa flag . | ||
183 | These functions return | ||
184 | .Sq \: | ||
185 | if there was a missing option argument, | ||
186 | .Sq \&? | ||
187 | if the user specified an unknown or ambiguous option, and | ||
188 | \-1 when the argument list has been exhausted. | ||
189 | .Sh EXAMPLES | ||
190 | .Bd -literal -compact | ||
191 | int bflag, ch, fd; | ||
192 | int daggerset; | ||
193 | |||
194 | /* options descriptor */ | ||
195 | static struct option longopts[] = { | ||
196 | { "buffy", no_argument, 0, 'b' }, | ||
197 | { "fluoride", required_argument, 0, 'f' }, | ||
198 | { "daggerset", no_argument, &daggerset, 1 }, | ||
199 | { 0, 0, 0, 0 } | ||
200 | }; | ||
201 | |||
202 | bflag = 0; | ||
203 | while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) | ||
204 | switch(ch) { | ||
205 | case 'b': | ||
206 | bflag = 1; | ||
207 | break; | ||
208 | case 'f': | ||
209 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
210 | err(1, "unable to open %s", optarg); | ||
211 | break; | ||
212 | case 0: | ||
213 | if (daggerset) { | ||
214 | fprintf(stderr,"Buffy will use her dagger to " | ||
215 | "apply fluoride to dracula's teeth\en"); | ||
216 | } | ||
217 | break; | ||
218 | case '?': | ||
219 | default: | ||
220 | usage(); | ||
221 | } | ||
222 | argc -= optind; | ||
223 | argv += optind; | ||
224 | .Ed | ||
225 | .Sh IMPLEMENTATION DIFFERENCES | ||
226 | This section describes differences to the GNU implementation | ||
227 | found in glibc-2.1.3: | ||
228 | .Bl -tag -width "xxx" | ||
229 | .It Li o | ||
230 | handling of - as first char of option string in presence of | ||
231 | environment variable POSIXLY_CORRECT: | ||
232 | .Bl -tag -width "OpenBSD" | ||
233 | .It Li GNU | ||
234 | ignores POSIXLY_CORRECT and returns non-options as | ||
235 | arguments to option '\e1'. | ||
236 | .It Li OpenBSD | ||
237 | honors POSIXLY_CORRECT and stops at the first non-option. | ||
238 | .El | ||
239 | .It Li o | ||
240 | handling of - within the option string (not the first character): | ||
241 | .Bl -tag -width "OpenBSD" | ||
242 | .It Li GNU | ||
243 | treats a | ||
244 | .Ql - | ||
245 | on the command line as a non-argument. | ||
246 | .It Li OpenBSD | ||
247 | a | ||
248 | .Ql - | ||
249 | within the option string matches a | ||
250 | .Ql - | ||
251 | (single dash) on the command line. | ||
252 | This functionality is provided for backward compatibility with | ||
253 | programs, such as | ||
254 | .Xr su 1 , | ||
255 | that use | ||
256 | .Ql - | ||
257 | as an option flag. | ||
258 | This practice is wrong, and should not be used in any current development. | ||
259 | .El | ||
260 | .It Li o | ||
261 | handling of :: in options string in presence of POSIXLY_CORRECT: | ||
262 | .Bl -tag -width "OpenBSD" | ||
263 | .It Li Both | ||
264 | GNU and OpenBSD ignore POSIXLY_CORRECT here and take :: to | ||
265 | mean the preceding option takes an optional argument. | ||
266 | .El | ||
267 | .It Li o | ||
268 | return value in case of missing argument if first character | ||
269 | (after + or -) in option string is not ':': | ||
270 | .Bl -tag -width "OpenBSD" | ||
271 | .It Li GNU | ||
272 | returns '?' | ||
273 | .It OpenBSD | ||
274 | returns ':' (since OpenBSD's getopt does). | ||
275 | .El | ||
276 | .It Li o | ||
277 | handling of --a in getopt: | ||
278 | .Bl -tag -width "OpenBSD" | ||
279 | .It Li GNU | ||
280 | parses this as option '-', option 'a'. | ||
281 | .It Li OpenBSD | ||
282 | parses this as '--', and returns \-1 (ignoring the a). | ||
283 | (Because the original getopt does.) | ||
284 | .El | ||
285 | .It Li o | ||
286 | setting of optopt for long options with flag != | ||
287 | .Dv NULL : | ||
288 | .Bl -tag -width "OpenBSD" | ||
289 | .It Li GNU | ||
290 | sets optopt to val. | ||
291 | .It Li OpenBSD | ||
292 | sets optopt to 0 (since val would never be returned). | ||
293 | .El | ||
294 | .It Li o | ||
295 | handling of -W with W; in option string in getopt (not getopt_long): | ||
296 | .Bl -tag -width "OpenBSD" | ||
297 | .It Li GNU | ||
298 | causes a segfault. | ||
299 | .It Li OpenBSD | ||
300 | no special handling is done; | ||
301 | .Dq W; | ||
302 | is interpreted as two separate options, neither of which take an argument. | ||
303 | .El | ||
304 | .It Li o | ||
305 | setting of optarg for long options without an argument that are | ||
306 | invoked via -W (W; in option string): | ||
307 | .Bl -tag -width "OpenBSD" | ||
308 | .It Li GNU | ||
309 | sets optarg to the option name (the argument of -W). | ||
310 | .It Li OpenBSD | ||
311 | sets optarg to | ||
312 | .Dv NULL | ||
313 | (the argument of the long option). | ||
314 | .El | ||
315 | .It Li o | ||
316 | handling of -W with an argument that is not (a prefix to) a known | ||
317 | long option (W; in option string): | ||
318 | .Bl -tag -width "OpenBSD" | ||
319 | .It Li GNU | ||
320 | returns -W with optarg set to the unknown option. | ||
321 | .It Li OpenBSD | ||
322 | treats this as an error (unknown option) and returns '?' with | ||
323 | optopt set to 0 and optarg set to | ||
324 | .Dv NULL | ||
325 | (as GNU's man page documents). | ||
326 | .El | ||
327 | .It Li o | ||
328 | The error messages are different. | ||
329 | .It Li o | ||
330 | OpenBSD does not permute the argument vector at the same points in | ||
331 | the calling sequence as GNU does. | ||
332 | The aspects normally used by the caller | ||
333 | (ordering after \-1 is returned, value of optind relative | ||
334 | to current positions) are the same, though. | ||
335 | (We do fewer variable swaps.) | ||
336 | .El | ||
337 | .Sh ENVIRONMENT | ||
338 | .Bl -tag -width POSIXLY_CORRECT | ||
339 | .It Ev POSIXLY_CORRECT | ||
340 | If set, option processing stops when the first non-option is found and | ||
341 | a leading | ||
342 | .Sq - | ||
343 | or | ||
344 | .Sq + | ||
345 | in the | ||
346 | .Ar optstring | ||
347 | is ignored. | ||
348 | .El | ||
349 | .Sh SEE ALSO | ||
350 | .Xr getopt 3 | ||
351 | .Sh HISTORY | ||
352 | The | ||
353 | .Fn getopt_long | ||
354 | and | ||
355 | .Fn getopt_long_only | ||
356 | functions first appeared in GNU libiberty. | ||
357 | This implementation first appeared in | ||
358 | .Ox 3.3 . | ||
359 | .Sh BUGS | ||
360 | The | ||
361 | .Ar argv | ||
362 | argument is not really | ||
363 | .Dv const | ||
364 | as its elements may be permuted (unless | ||
365 | .Ev POSIXLY_CORRECT | ||
366 | is set). | ||
diff --git a/src/lib/libc/stdlib/getopt_long.c b/src/lib/libc/stdlib/getopt_long.c new file mode 100644 index 0000000000..1f7f5baa8b --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.c | |||
@@ -0,0 +1,545 @@ | |||
1 | /* $OpenBSD: getopt_long.c,v 1.15 2003/09/22 23:45:22 millert Exp $ */ | ||
2 | /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
18 | * | ||
19 | * Sponsored in part by the Defense Advanced Research Projects | ||
20 | * Agency (DARPA) and Air Force Research Laboratory, Air Force | ||
21 | * Materiel Command, USAF, under agreement number F39502-99-1-0512. | ||
22 | */ | ||
23 | /*- | ||
24 | * Copyright (c) 2000 The NetBSD Foundation, Inc. | ||
25 | * All rights reserved. | ||
26 | * | ||
27 | * This code is derived from software contributed to The NetBSD Foundation | ||
28 | * by Dieter Baron and Thomas Klausner. | ||
29 | * | ||
30 | * Redistribution and use in source and binary forms, with or without | ||
31 | * modification, are permitted provided that the following conditions | ||
32 | * are met: | ||
33 | * 1. Redistributions of source code must retain the above copyright | ||
34 | * notice, this list of conditions and the following disclaimer. | ||
35 | * 2. Redistributions in binary form must reproduce the above copyright | ||
36 | * notice, this list of conditions and the following disclaimer in the | ||
37 | * documentation and/or other materials provided with the distribution. | ||
38 | * 3. All advertising materials mentioning features or use of this software | ||
39 | * must display the following acknowledgement: | ||
40 | * This product includes software developed by the NetBSD | ||
41 | * Foundation, Inc. and its contributors. | ||
42 | * 4. Neither the name of The NetBSD Foundation nor the names of its | ||
43 | * contributors may be used to endorse or promote products derived | ||
44 | * from this software without specific prior written permission. | ||
45 | * | ||
46 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
47 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
48 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
49 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
50 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
51 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
52 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
53 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
54 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
55 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
56 | * POSSIBILITY OF SUCH DAMAGE. | ||
57 | */ | ||
58 | |||
59 | #if defined(LIBC_SCCS) && !defined(lint) | ||
60 | static char *rcsid = "$OpenBSD: getopt_long.c,v 1.15 2003/09/22 23:45:22 millert Exp $"; | ||
61 | #endif /* LIBC_SCCS and not lint */ | ||
62 | |||
63 | #include <err.h> | ||
64 | #include <errno.h> | ||
65 | #include <getopt.h> | ||
66 | #include <stdlib.h> | ||
67 | #include <string.h> | ||
68 | |||
69 | #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ | ||
70 | |||
71 | #ifdef REPLACE_GETOPT | ||
72 | int opterr = 1; /* if error message should be printed */ | ||
73 | int optind = 1; /* index into parent argv vector */ | ||
74 | int optopt = '?'; /* character checked for validity */ | ||
75 | int optreset; /* reset getopt */ | ||
76 | char *optarg; /* argument associated with option */ | ||
77 | #endif | ||
78 | |||
79 | #define PRINT_ERROR ((opterr) && (*options != ':')) | ||
80 | |||
81 | #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ | ||
82 | #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ | ||
83 | #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ | ||
84 | |||
85 | /* return values */ | ||
86 | #define BADCH (int)'?' | ||
87 | #define BADARG ((*options == ':') ? (int)':' : (int)'?') | ||
88 | #define INORDER (int)1 | ||
89 | |||
90 | #define EMSG "" | ||
91 | |||
92 | static int getopt_internal(int, char * const *, const char *, | ||
93 | const struct option *, int *, int); | ||
94 | static int parse_long_options(char * const *, const char *, | ||
95 | const struct option *, int *, int); | ||
96 | static int gcd(int, int); | ||
97 | static void permute_args(int, int, int, char * const *); | ||
98 | |||
99 | static char *place = EMSG; /* option letter processing */ | ||
100 | |||
101 | /* XXX: set optreset to 1 rather than these two */ | ||
102 | static int nonopt_start = -1; /* first non option argument (for permute) */ | ||
103 | static int nonopt_end = -1; /* first option after non options (for permute) */ | ||
104 | |||
105 | /* Error messages */ | ||
106 | static const char recargchar[] = "option requires an argument -- %c"; | ||
107 | static const char recargstring[] = "option requires an argument -- %s"; | ||
108 | static const char ambig[] = "ambiguous option -- %.*s"; | ||
109 | static const char noarg[] = "option doesn't take an argument -- %.*s"; | ||
110 | static const char illoptchar[] = "unknown option -- %c"; | ||
111 | static const char illoptstring[] = "unknown option -- %s"; | ||
112 | |||
113 | /* | ||
114 | * Compute the greatest common divisor of a and b. | ||
115 | */ | ||
116 | static int | ||
117 | gcd(int a, int b) | ||
118 | { | ||
119 | int c; | ||
120 | |||
121 | c = a % b; | ||
122 | while (c != 0) { | ||
123 | a = b; | ||
124 | b = c; | ||
125 | c = a % b; | ||
126 | } | ||
127 | |||
128 | return (b); | ||
129 | } | ||
130 | |||
131 | /* | ||
132 | * Exchange the block from nonopt_start to nonopt_end with the block | ||
133 | * from nonopt_end to opt_end (keeping the same order of arguments | ||
134 | * in each block). | ||
135 | */ | ||
136 | static void | ||
137 | permute_args(int panonopt_start, int panonopt_end, int opt_end, | ||
138 | char * const *nargv) | ||
139 | { | ||
140 | int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; | ||
141 | char *swap; | ||
142 | |||
143 | /* | ||
144 | * compute lengths of blocks and number and size of cycles | ||
145 | */ | ||
146 | nnonopts = panonopt_end - panonopt_start; | ||
147 | nopts = opt_end - panonopt_end; | ||
148 | ncycle = gcd(nnonopts, nopts); | ||
149 | cyclelen = (opt_end - panonopt_start) / ncycle; | ||
150 | |||
151 | for (i = 0; i < ncycle; i++) { | ||
152 | cstart = panonopt_end+i; | ||
153 | pos = cstart; | ||
154 | for (j = 0; j < cyclelen; j++) { | ||
155 | if (pos >= panonopt_end) | ||
156 | pos -= nnonopts; | ||
157 | else | ||
158 | pos += nopts; | ||
159 | swap = nargv[pos]; | ||
160 | /* LINTED const cast */ | ||
161 | ((char **) nargv)[pos] = nargv[cstart]; | ||
162 | /* LINTED const cast */ | ||
163 | ((char **)nargv)[cstart] = swap; | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | |||
168 | /* | ||
169 | * parse_long_options -- | ||
170 | * Parse long options in argc/argv argument vector. | ||
171 | * Returns -1 if short_too is set and the option does not match long_options. | ||
172 | */ | ||
173 | static int | ||
174 | parse_long_options(char * const *nargv, const char *options, | ||
175 | const struct option *long_options, int *idx, int short_too) | ||
176 | { | ||
177 | char *current_argv, *has_equal; | ||
178 | size_t current_argv_len; | ||
179 | int i, match; | ||
180 | |||
181 | current_argv = place; | ||
182 | match = -1; | ||
183 | |||
184 | optind++; | ||
185 | |||
186 | if ((has_equal = strchr(current_argv, '=')) != NULL) { | ||
187 | /* argument found (--option=arg) */ | ||
188 | current_argv_len = has_equal - current_argv; | ||
189 | has_equal++; | ||
190 | } else | ||
191 | current_argv_len = strlen(current_argv); | ||
192 | |||
193 | for (i = 0; long_options[i].name; i++) { | ||
194 | /* find matching long option */ | ||
195 | if (strncmp(current_argv, long_options[i].name, | ||
196 | current_argv_len)) | ||
197 | continue; | ||
198 | |||
199 | if (strlen(long_options[i].name) == current_argv_len) { | ||
200 | /* exact match */ | ||
201 | match = i; | ||
202 | break; | ||
203 | } | ||
204 | /* | ||
205 | * If this is a known short option, don't allow | ||
206 | * a partial match of a single character. | ||
207 | */ | ||
208 | if (short_too && current_argv_len == 1) | ||
209 | continue; | ||
210 | |||
211 | if (match == -1) /* partial match */ | ||
212 | match = i; | ||
213 | else { | ||
214 | /* ambiguous abbreviation */ | ||
215 | if (PRINT_ERROR) | ||
216 | warnx(ambig, (int)current_argv_len, | ||
217 | current_argv); | ||
218 | optopt = 0; | ||
219 | return (BADCH); | ||
220 | } | ||
221 | } | ||
222 | if (match != -1) { /* option found */ | ||
223 | if (long_options[match].has_arg == no_argument | ||
224 | && has_equal) { | ||
225 | if (PRINT_ERROR) | ||
226 | warnx(noarg, (int)current_argv_len, | ||
227 | current_argv); | ||
228 | /* | ||
229 | * XXX: GNU sets optopt to val regardless of flag | ||
230 | */ | ||
231 | if (long_options[match].flag == NULL) | ||
232 | optopt = long_options[match].val; | ||
233 | else | ||
234 | optopt = 0; | ||
235 | return (BADARG); | ||
236 | } | ||
237 | if (long_options[match].has_arg == required_argument || | ||
238 | long_options[match].has_arg == optional_argument) { | ||
239 | if (has_equal) | ||
240 | optarg = has_equal; | ||
241 | else if (long_options[match].has_arg == | ||
242 | required_argument) { | ||
243 | /* | ||
244 | * optional argument doesn't use next nargv | ||
245 | */ | ||
246 | optarg = nargv[optind++]; | ||
247 | } | ||
248 | } | ||
249 | if ((long_options[match].has_arg == required_argument) | ||
250 | && (optarg == NULL)) { | ||
251 | /* | ||
252 | * Missing argument; leading ':' indicates no error | ||
253 | * should be generated. | ||
254 | */ | ||
255 | if (PRINT_ERROR) | ||
256 | warnx(recargstring, | ||
257 | current_argv); | ||
258 | /* | ||
259 | * XXX: GNU sets optopt to val regardless of flag | ||
260 | */ | ||
261 | if (long_options[match].flag == NULL) | ||
262 | optopt = long_options[match].val; | ||
263 | else | ||
264 | optopt = 0; | ||
265 | --optind; | ||
266 | return (BADARG); | ||
267 | } | ||
268 | } else { /* unknown option */ | ||
269 | if (short_too) { | ||
270 | --optind; | ||
271 | return (-1); | ||
272 | } | ||
273 | if (PRINT_ERROR) | ||
274 | warnx(illoptstring, current_argv); | ||
275 | optopt = 0; | ||
276 | return (BADCH); | ||
277 | } | ||
278 | if (idx) | ||
279 | *idx = match; | ||
280 | if (long_options[match].flag) { | ||
281 | *long_options[match].flag = long_options[match].val; | ||
282 | return (0); | ||
283 | } else | ||
284 | return (long_options[match].val); | ||
285 | } | ||
286 | |||
287 | /* | ||
288 | * getopt_internal -- | ||
289 | * Parse argc/argv argument vector. Called by user level routines. | ||
290 | */ | ||
291 | static int | ||
292 | getopt_internal(int nargc, char * const *nargv, const char *options, | ||
293 | const struct option *long_options, int *idx, int flags) | ||
294 | { | ||
295 | char *oli; /* option letter list index */ | ||
296 | int optchar, short_too; | ||
297 | static int posixly_correct = -1; | ||
298 | |||
299 | if (options == NULL) | ||
300 | return (-1); | ||
301 | |||
302 | /* | ||
303 | * Disable GNU extensions if POSIXLY_CORRECT is set or options | ||
304 | * string begins with a '+'. | ||
305 | */ | ||
306 | if (posixly_correct == -1) | ||
307 | posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); | ||
308 | if (posixly_correct || *options == '+') | ||
309 | flags &= ~FLAG_PERMUTE; | ||
310 | else if (*options == '-') | ||
311 | flags |= FLAG_ALLARGS; | ||
312 | if (*options == '+' || *options == '-') | ||
313 | options++; | ||
314 | |||
315 | /* | ||
316 | * XXX Some GNU programs (like cvs) set optind to 0 instead of | ||
317 | * XXX using optreset. Work around this braindamage. | ||
318 | */ | ||
319 | if (optind == 0) | ||
320 | optind = optreset = 1; | ||
321 | |||
322 | optarg = NULL; | ||
323 | if (optreset) | ||
324 | nonopt_start = nonopt_end = -1; | ||
325 | start: | ||
326 | if (optreset || !*place) { /* update scanning pointer */ | ||
327 | optreset = 0; | ||
328 | if (optind >= nargc) { /* end of argument vector */ | ||
329 | place = EMSG; | ||
330 | if (nonopt_end != -1) { | ||
331 | /* do permutation, if we have to */ | ||
332 | permute_args(nonopt_start, nonopt_end, | ||
333 | optind, nargv); | ||
334 | optind -= nonopt_end - nonopt_start; | ||
335 | } | ||
336 | else if (nonopt_start != -1) { | ||
337 | /* | ||
338 | * If we skipped non-options, set optind | ||
339 | * to the first of them. | ||
340 | */ | ||
341 | optind = nonopt_start; | ||
342 | } | ||
343 | nonopt_start = nonopt_end = -1; | ||
344 | return (-1); | ||
345 | } | ||
346 | if (*(place = nargv[optind]) != '-' || | ||
347 | (place[1] == '\0' && strchr(options, '-') == NULL)) { | ||
348 | place = EMSG; /* found non-option */ | ||
349 | if (flags & FLAG_ALLARGS) { | ||
350 | /* | ||
351 | * GNU extension: | ||
352 | * return non-option as argument to option 1 | ||
353 | */ | ||
354 | optarg = nargv[optind++]; | ||
355 | return (INORDER); | ||
356 | } | ||
357 | if (!(flags & FLAG_PERMUTE)) { | ||
358 | /* | ||
359 | * If no permutation wanted, stop parsing | ||
360 | * at first non-option. | ||
361 | */ | ||
362 | return (-1); | ||
363 | } | ||
364 | /* do permutation */ | ||
365 | if (nonopt_start == -1) | ||
366 | nonopt_start = optind; | ||
367 | else if (nonopt_end != -1) { | ||
368 | permute_args(nonopt_start, nonopt_end, | ||
369 | optind, nargv); | ||
370 | nonopt_start = optind - | ||
371 | (nonopt_end - nonopt_start); | ||
372 | nonopt_end = -1; | ||
373 | } | ||
374 | optind++; | ||
375 | /* process next argument */ | ||
376 | goto start; | ||
377 | } | ||
378 | if (nonopt_start != -1 && nonopt_end == -1) | ||
379 | nonopt_end = optind; | ||
380 | |||
381 | /* | ||
382 | * Check for "--" or "--foo" with no long options | ||
383 | * but if place is simply "-" leave it unmolested. | ||
384 | */ | ||
385 | if (place[1] != '\0' && *++place == '-' && | ||
386 | (place[1] == '\0' || long_options == NULL)) { | ||
387 | optind++; | ||
388 | place = EMSG; | ||
389 | /* | ||
390 | * We found an option (--), so if we skipped | ||
391 | * non-options, we have to permute. | ||
392 | */ | ||
393 | if (nonopt_end != -1) { | ||
394 | permute_args(nonopt_start, nonopt_end, | ||
395 | optind, nargv); | ||
396 | optind -= nonopt_end - nonopt_start; | ||
397 | } | ||
398 | nonopt_start = nonopt_end = -1; | ||
399 | return (-1); | ||
400 | } | ||
401 | } | ||
402 | |||
403 | /* | ||
404 | * Check long options if: | ||
405 | * 1) we were passed some | ||
406 | * 2) the arg is not just "-" | ||
407 | * 3) either the arg starts with -- we are getopt_long_only() | ||
408 | */ | ||
409 | if (long_options != NULL && place != nargv[optind] && | ||
410 | (*place == '-' || (flags & FLAG_LONGONLY))) { | ||
411 | short_too = 0; | ||
412 | if (*place == '-') | ||
413 | place++; /* --foo long option */ | ||
414 | else if (*place != ':' && strchr(options, *place) != NULL) | ||
415 | short_too = 1; /* could be short option too */ | ||
416 | |||
417 | optchar = parse_long_options(nargv, options, long_options, | ||
418 | idx, short_too); | ||
419 | if (optchar != -1) { | ||
420 | place = EMSG; | ||
421 | return (optchar); | ||
422 | } | ||
423 | } | ||
424 | |||
425 | if ((optchar = (int)*place++) == (int)':' || | ||
426 | (oli = strchr(options, optchar)) == NULL) { | ||
427 | /* | ||
428 | * If the user didn't specify '-' as an option, | ||
429 | * assume it means -1 as POSIX specifies. | ||
430 | */ | ||
431 | if (optchar == (int)'-') | ||
432 | return (-1); | ||
433 | /* option letter unknown or ':' */ | ||
434 | if (!*place) | ||
435 | ++optind; | ||
436 | if (PRINT_ERROR) | ||
437 | warnx(illoptchar, optchar); | ||
438 | optopt = optchar; | ||
439 | return (BADCH); | ||
440 | } | ||
441 | if (long_options != NULL && optchar == 'W' && oli[1] == ';') { | ||
442 | /* -W long-option */ | ||
443 | if (*place) /* no space */ | ||
444 | /* NOTHING */; | ||
445 | else if (++optind >= nargc) { /* no arg */ | ||
446 | place = EMSG; | ||
447 | if (PRINT_ERROR) | ||
448 | warnx(recargchar, optchar); | ||
449 | optopt = optchar; | ||
450 | return (BADARG); | ||
451 | } else /* white space */ | ||
452 | place = nargv[optind]; | ||
453 | optchar = parse_long_options(nargv, options, long_options, | ||
454 | idx, 0); | ||
455 | place = EMSG; | ||
456 | return (optchar); | ||
457 | } | ||
458 | if (*++oli != ':') { /* doesn't take argument */ | ||
459 | if (!*place) | ||
460 | ++optind; | ||
461 | } else { /* takes (optional) argument */ | ||
462 | optarg = NULL; | ||
463 | if (*place) /* no white space */ | ||
464 | optarg = place; | ||
465 | /* XXX: disable test for :: if PC? (GNU doesn't) */ | ||
466 | else if (oli[1] != ':') { /* arg not optional */ | ||
467 | if (++optind >= nargc) { /* no arg */ | ||
468 | place = EMSG; | ||
469 | if (PRINT_ERROR) | ||
470 | warnx(recargchar, optchar); | ||
471 | optopt = optchar; | ||
472 | return (BADARG); | ||
473 | } else | ||
474 | optarg = nargv[optind]; | ||
475 | } else if (!(flags & FLAG_PERMUTE)) { | ||
476 | /* | ||
477 | * If permutation is disabled, we can accept an | ||
478 | * optional arg separated by whitespace. | ||
479 | */ | ||
480 | if (optind + 1 < nargc) | ||
481 | optarg = nargv[++optind]; | ||
482 | } | ||
483 | place = EMSG; | ||
484 | ++optind; | ||
485 | } | ||
486 | /* dump back option letter */ | ||
487 | return (optchar); | ||
488 | } | ||
489 | |||
490 | #ifdef REPLACE_GETOPT | ||
491 | /* | ||
492 | * getopt -- | ||
493 | * Parse argc/argv argument vector. | ||
494 | * | ||
495 | * [eventually this will replace the BSD getopt] | ||
496 | */ | ||
497 | int | ||
498 | getopt(int nargc, char * const *nargv, const char *options) | ||
499 | { | ||
500 | |||
501 | /* | ||
502 | * We dont' pass FLAG_PERMUTE to getopt_internal() since | ||
503 | * the BSD getopt(3) (unlike GNU) has never done this. | ||
504 | * | ||
505 | * Furthermore, since many privileged programs call getopt() | ||
506 | * before dropping privileges it makes sense to keep things | ||
507 | * as simple (and bug-free) as possible. | ||
508 | */ | ||
509 | return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); | ||
510 | } | ||
511 | #endif /* REPLACE_GETOPT */ | ||
512 | |||
513 | /* | ||
514 | * getopt_long -- | ||
515 | * Parse argc/argv argument vector. | ||
516 | */ | ||
517 | int | ||
518 | getopt_long(nargc, nargv, options, long_options, idx) | ||
519 | int nargc; | ||
520 | char * const *nargv; | ||
521 | const char *options; | ||
522 | const struct option *long_options; | ||
523 | int *idx; | ||
524 | { | ||
525 | |||
526 | return (getopt_internal(nargc, nargv, options, long_options, idx, | ||
527 | FLAG_PERMUTE)); | ||
528 | } | ||
529 | |||
530 | /* | ||
531 | * getopt_long_only -- | ||
532 | * Parse argc/argv argument vector. | ||
533 | */ | ||
534 | int | ||
535 | getopt_long_only(nargc, nargv, options, long_options, idx) | ||
536 | int nargc; | ||
537 | char * const *nargv; | ||
538 | const char *options; | ||
539 | const struct option *long_options; | ||
540 | int *idx; | ||
541 | { | ||
542 | |||
543 | return (getopt_internal(nargc, nargv, options, long_options, idx, | ||
544 | FLAG_PERMUTE|FLAG_LONGONLY)); | ||
545 | } | ||
diff --git a/src/lib/libc/stdlib/getsubopt.3 b/src/lib/libc/stdlib/getsubopt.3 new file mode 100644 index 0000000000..83530653d2 --- /dev/null +++ b/src/lib/libc/stdlib/getsubopt.3 | |||
@@ -0,0 +1,143 @@ | |||
1 | .\" $OpenBSD: getsubopt.3,v 1.7 2003/06/02 20:18:37 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93 | ||
31 | .\" | ||
32 | .Dd June 9, 1993 | ||
33 | .Dt GETSUBOPT 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm getsubopt | ||
37 | .Nd get sub options from an argument | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <stdlib.h> | ||
40 | .Vt extern char *suboptarg; | ||
41 | .Ft int | ||
42 | .Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep" | ||
43 | .Sh DESCRIPTION | ||
44 | The | ||
45 | .Fn getsubopt | ||
46 | function parses a string containing tokens delimited by one or more | ||
47 | tab, space or comma | ||
48 | .Pq Ql \&, | ||
49 | characters. | ||
50 | It is intended for use in parsing groups of option arguments provided | ||
51 | as part of a utility command line. | ||
52 | .Pp | ||
53 | The argument | ||
54 | .Fa optionp | ||
55 | is a pointer to a pointer to the string. | ||
56 | The argument | ||
57 | .Fa tokens | ||
58 | is a pointer to a null-terminated array of pointers to strings. | ||
59 | .Pp | ||
60 | The | ||
61 | .Fn getsubopt | ||
62 | function returns the zero-based offset of the pointer in the | ||
63 | .Fa tokens | ||
64 | array referencing a string which matches the first token | ||
65 | in the string, or \-1 if the string contains no tokens or | ||
66 | .Fa tokens | ||
67 | does not contain a matching string. | ||
68 | .Pp | ||
69 | If the token is of the form | ||
70 | .Ar name Ns No = Ns Ar value , | ||
71 | the location referenced by | ||
72 | .Fa valuep | ||
73 | will be set to point to the start of the | ||
74 | .Dq value | ||
75 | portion of the token. | ||
76 | .Pp | ||
77 | On return from | ||
78 | .Fn getsubopt , | ||
79 | .Fa optionp | ||
80 | will be set to point to the start of the next token in the string, | ||
81 | or the null at the end of the string if no more tokens are present. | ||
82 | The external variable | ||
83 | .Fa suboptarg | ||
84 | will be set to point to the start of the current token, or | ||
85 | .Dv NULL | ||
86 | if no tokens were present. | ||
87 | The argument | ||
88 | .Fa valuep | ||
89 | will be set to point to the value portion of the token, or | ||
90 | .Dv NULL | ||
91 | if no value portion was present. | ||
92 | .Sh EXAMPLES | ||
93 | .Bd -literal -compact | ||
94 | char *tokens[] = { | ||
95 | #define ONE 0 | ||
96 | "one", | ||
97 | #define TWO 1 | ||
98 | "two", | ||
99 | NULL | ||
100 | }; | ||
101 | |||
102 | \&... | ||
103 | |||
104 | extern char *optarg, *suboptarg; | ||
105 | char *options, *value; | ||
106 | |||
107 | while ((ch = getopt(argc, argv, "ab:")) != \-1) { | ||
108 | switch(ch) { | ||
109 | case 'a': | ||
110 | /* process ``a'' option */ | ||
111 | break; | ||
112 | case 'b': | ||
113 | options = optarg; | ||
114 | while (*options) { | ||
115 | switch(getsubopt(&options, tokens, &value)) { | ||
116 | case ONE: | ||
117 | /* process ``one'' sub option */ | ||
118 | break; | ||
119 | case TWO: | ||
120 | /* process ``two'' sub option */ | ||
121 | if (!value) | ||
122 | error("no value for two"); | ||
123 | i = atoi(value); | ||
124 | break; | ||
125 | case \-1: | ||
126 | if (suboptarg) | ||
127 | error("illegal sub option %s", | ||
128 | suboptarg); | ||
129 | else | ||
130 | error("missing sub option"); | ||
131 | break; | ||
132 | } | ||
133 | break; | ||
134 | } | ||
135 | .Ed | ||
136 | .Sh SEE ALSO | ||
137 | .Xr getopt 3 , | ||
138 | .Xr strsep 3 | ||
139 | .Sh HISTORY | ||
140 | The | ||
141 | .Fn getsubopt | ||
142 | function first appeared in | ||
143 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/stdlib/getsubopt.c b/src/lib/libc/stdlib/getsubopt.c new file mode 100644 index 0000000000..308458ea75 --- /dev/null +++ b/src/lib/libc/stdlib/getsubopt.c | |||
@@ -0,0 +1,102 @@ | |||
1 | /* $OpenBSD: getsubopt.c,v 1.2 2003/06/02 20:18:37 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. Neither the name of the University nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #ifndef lint | ||
33 | #if 0 | ||
34 | static char sccsid[] = "@(#)getsubopt.c 8.1 (Berkeley) 6/4/93"; | ||
35 | #else | ||
36 | static char rcsid[] = "$OpenBSD: getsubopt.c,v 1.2 2003/06/02 20:18:37 millert Exp $"; | ||
37 | #endif | ||
38 | #endif /* not lint */ | ||
39 | |||
40 | #include <unistd.h> | ||
41 | #include <stdlib.h> | ||
42 | #include <string.h> | ||
43 | |||
44 | /* | ||
45 | * The SVID interface to getsubopt provides no way of figuring out which | ||
46 | * part of the suboptions list wasn't matched. This makes error messages | ||
47 | * tricky... The extern variable suboptarg is a pointer to the token | ||
48 | * which didn't match. | ||
49 | */ | ||
50 | char *suboptarg; | ||
51 | |||
52 | int | ||
53 | getsubopt(optionp, tokens, valuep) | ||
54 | register char **optionp, **valuep; | ||
55 | register char * const *tokens; | ||
56 | { | ||
57 | register int cnt; | ||
58 | register char *p; | ||
59 | |||
60 | suboptarg = *valuep = NULL; | ||
61 | |||
62 | if (!optionp || !*optionp) | ||
63 | return(-1); | ||
64 | |||
65 | /* skip leading white-space, commas */ | ||
66 | for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); | ||
67 | |||
68 | if (!*p) { | ||
69 | *optionp = p; | ||
70 | return(-1); | ||
71 | } | ||
72 | |||
73 | /* save the start of the token, and skip the rest of the token. */ | ||
74 | for (suboptarg = p; | ||
75 | *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';); | ||
76 | |||
77 | if (*p) { | ||
78 | /* | ||
79 | * If there's an equals sign, set the value pointer, and | ||
80 | * skip over the value part of the token. Terminate the | ||
81 | * token. | ||
82 | */ | ||
83 | if (*p == '=') { | ||
84 | *p = '\0'; | ||
85 | for (*valuep = ++p; | ||
86 | *p && *p != ',' && *p != ' ' && *p != '\t'; ++p); | ||
87 | if (*p) | ||
88 | *p++ = '\0'; | ||
89 | } else | ||
90 | *p++ = '\0'; | ||
91 | /* Skip any whitespace or commas after this token. */ | ||
92 | for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); | ||
93 | } | ||
94 | |||
95 | /* set optionp for next round. */ | ||
96 | *optionp = p; | ||
97 | |||
98 | for (cnt = 0; *tokens; ++tokens, ++cnt) | ||
99 | if (!strcmp(suboptarg, *tokens)) | ||
100 | return(cnt); | ||
101 | return(-1); | ||
102 | } | ||
diff --git a/src/lib/libc/stdlib/heapsort.c b/src/lib/libc/stdlib/heapsort.c new file mode 100644 index 0000000000..f4aeeef7a3 --- /dev/null +++ b/src/lib/libc/stdlib/heapsort.c | |||
@@ -0,0 +1,179 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1991, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: heapsort.c,v 1.6 2003/09/08 16:24:05 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <sys/types.h> | ||
38 | #include <errno.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | /* | ||
42 | * Swap two areas of size number of bytes. Although qsort(3) permits random | ||
43 | * blocks of memory to be sorted, sorting pointers is almost certainly the | ||
44 | * common case (and, were it not, could easily be made so). Regardless, it | ||
45 | * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer | ||
46 | * arithmetic gets lost in the time required for comparison function calls. | ||
47 | */ | ||
48 | #define SWAP(a, b, count, size, tmp) { \ | ||
49 | count = size; \ | ||
50 | do { \ | ||
51 | tmp = *a; \ | ||
52 | *a++ = *b; \ | ||
53 | *b++ = tmp; \ | ||
54 | } while (--count); \ | ||
55 | } | ||
56 | |||
57 | /* Copy one block of size size to another. */ | ||
58 | #define COPY(a, b, count, size, tmp1, tmp2) { \ | ||
59 | count = size; \ | ||
60 | tmp1 = a; \ | ||
61 | tmp2 = b; \ | ||
62 | do { \ | ||
63 | *tmp1++ = *tmp2++; \ | ||
64 | } while (--count); \ | ||
65 | } | ||
66 | |||
67 | /* | ||
68 | * Build the list into a heap, where a heap is defined such that for | ||
69 | * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N. | ||
70 | * | ||
71 | * There are two cases. If j == nmemb, select largest of Ki and Kj. If | ||
72 | * j < nmemb, select largest of Ki, Kj and Kj+1. | ||
73 | */ | ||
74 | #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ | ||
75 | for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ | ||
76 | par_i = child_i) { \ | ||
77 | child = base + child_i * size; \ | ||
78 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
79 | child += size; \ | ||
80 | ++child_i; \ | ||
81 | } \ | ||
82 | par = base + par_i * size; \ | ||
83 | if (compar(child, par) <= 0) \ | ||
84 | break; \ | ||
85 | SWAP(par, child, count, size, tmp); \ | ||
86 | } \ | ||
87 | } | ||
88 | |||
89 | /* | ||
90 | * Select the top of the heap and 'heapify'. Since by far the most expensive | ||
91 | * action is the call to the compar function, a considerable optimization | ||
92 | * in the average case can be achieved due to the fact that k, the displaced | ||
93 | * element, is usually quite small, so it would be preferable to first | ||
94 | * heapify, always maintaining the invariant that the larger child is copied | ||
95 | * over its parent's record. | ||
96 | * | ||
97 | * Then, starting from the *bottom* of the heap, finding k's correct place, | ||
98 | * again maintaining the invariant. As a result of the invariant no element | ||
99 | * is 'lost' when k is assigned its correct place in the heap. | ||
100 | * | ||
101 | * The time savings from this optimization are on the order of 15-20% for the | ||
102 | * average case. See Knuth, Vol. 3, page 158, problem 18. | ||
103 | * | ||
104 | * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset. | ||
105 | */ | ||
106 | #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \ | ||
107 | for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \ | ||
108 | child = base + child_i * size; \ | ||
109 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
110 | child += size; \ | ||
111 | ++child_i; \ | ||
112 | } \ | ||
113 | par = base + par_i * size; \ | ||
114 | COPY(par, child, count, size, tmp1, tmp2); \ | ||
115 | } \ | ||
116 | for (;;) { \ | ||
117 | child_i = par_i; \ | ||
118 | par_i = child_i / 2; \ | ||
119 | child = base + child_i * size; \ | ||
120 | par = base + par_i * size; \ | ||
121 | if (child_i == 1 || compar(k, par) < 0) { \ | ||
122 | COPY(child, k, count, size, tmp1, tmp2); \ | ||
123 | break; \ | ||
124 | } \ | ||
125 | COPY(child, par, count, size, tmp1, tmp2); \ | ||
126 | } \ | ||
127 | } | ||
128 | |||
129 | /* | ||
130 | * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average | ||
131 | * and worst. While heapsort is faster than the worst case of quicksort, | ||
132 | * the BSD quicksort does median selection so that the chance of finding | ||
133 | * a data set that will trigger the worst case is nonexistent. Heapsort's | ||
134 | * only advantage over quicksort is that it requires little additional memory. | ||
135 | */ | ||
136 | int | ||
137 | heapsort(vbase, nmemb, size, compar) | ||
138 | void *vbase; | ||
139 | size_t nmemb, size; | ||
140 | int (*compar)(const void *, const void *); | ||
141 | { | ||
142 | register int cnt, i, j, l; | ||
143 | register char tmp, *tmp1, *tmp2; | ||
144 | char *base, *k, *p, *t; | ||
145 | |||
146 | if (nmemb <= 1) | ||
147 | return (0); | ||
148 | |||
149 | if (!size) { | ||
150 | errno = EINVAL; | ||
151 | return (-1); | ||
152 | } | ||
153 | |||
154 | if ((k = malloc(size)) == NULL) | ||
155 | return (-1); | ||
156 | |||
157 | /* | ||
158 | * Items are numbered from 1 to nmemb, so offset from size bytes | ||
159 | * below the starting address. | ||
160 | */ | ||
161 | base = (char *)vbase - size; | ||
162 | |||
163 | for (l = nmemb / 2 + 1; --l;) | ||
164 | CREATE(l, nmemb, i, j, t, p, size, cnt, tmp); | ||
165 | |||
166 | /* | ||
167 | * For each element of the heap, save the largest element into its | ||
168 | * final slot, save the displaced element (k), then recreate the | ||
169 | * heap. | ||
170 | */ | ||
171 | while (nmemb > 1) { | ||
172 | COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2); | ||
173 | COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2); | ||
174 | --nmemb; | ||
175 | SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2); | ||
176 | } | ||
177 | free(k); | ||
178 | return (0); | ||
179 | } | ||
diff --git a/src/lib/libc/stdlib/insque.3 b/src/lib/libc/stdlib/insque.3 new file mode 100644 index 0000000000..b09f1fdc04 --- /dev/null +++ b/src/lib/libc/stdlib/insque.3 | |||
@@ -0,0 +1,100 @@ | |||
1 | .\" $OpenBSD: insque.3,v 1.2 2003/06/01 19:27:27 jmc Exp $ | ||
2 | .\" Copyright (c) 1993 John Brezak | ||
3 | .\" All rights reserved. | ||
4 | .\" | ||
5 | .\" Redistribution and use in source and binary forms, with or without | ||
6 | .\" modification, are permitted provided that the following conditions | ||
7 | .\" are met: | ||
8 | .\" 1. Redistributions of source code must retain the above copyright | ||
9 | .\" notice, this list of conditions and the following disclaimer. | ||
10 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer in the | ||
12 | .\" documentation and/or other materials provided with the distribution. | ||
13 | .\" 3. The name of the author may be used to endorse or promote products | ||
14 | .\" derived from this software without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" | ||
29 | .Dd August 12, 1993 | ||
30 | .Dt INSQUE 3 | ||
31 | .Os | ||
32 | .Sh NAME | ||
33 | .Nm insque , | ||
34 | .Nm remque | ||
35 | .Nd insert/remove element from a queue | ||
36 | .Sh SYNOPSIS | ||
37 | .Fd #include <search.h> | ||
38 | .Ft void | ||
39 | .Fn insque "void *elem" "void *pred" | ||
40 | .Ft void | ||
41 | .Fn remque "void *elem" | ||
42 | .Sh DESCRIPTION | ||
43 | .Bf -symbolic | ||
44 | These interfaces have been superceded by the | ||
45 | .Xr queue 3 | ||
46 | macros and are provided for compatibility with legacy code. | ||
47 | .Ef | ||
48 | .Pp | ||
49 | .Fn insque | ||
50 | and | ||
51 | .Fn remque | ||
52 | manipulate queues built from doubly linked lists. | ||
53 | The queue can be either circular or linear. | ||
54 | Each element in the queue must be of the following form: | ||
55 | .Bd -literal | ||
56 | struct qelem { | ||
57 | struct qelem *q_forw; | ||
58 | struct qelem *q_back; | ||
59 | char q_data[]; | ||
60 | }; | ||
61 | .Ed | ||
62 | .Pp | ||
63 | The first two elements in the struct must be pointers of the | ||
64 | same type that point to the previous and next elements in | ||
65 | the queue respectively. | ||
66 | Any subsequent data in the struct is application-dependent. | ||
67 | .Pp | ||
68 | The | ||
69 | .Fn insque | ||
70 | function inserts | ||
71 | .Fa elem | ||
72 | into a queue immediately after | ||
73 | .Fa pred . | ||
74 | .Pp | ||
75 | The | ||
76 | .Fn remque | ||
77 | function removes | ||
78 | .Fa elem | ||
79 | from the queue. | ||
80 | .Sh DIAGNOSTICS | ||
81 | These functions are not atomic unless that machine architecture allows it. | ||
82 | .Sh SEE ALSO | ||
83 | .Xr queue 3 | ||
84 | .Sh STANDARDS | ||
85 | The | ||
86 | .Fn lsearch | ||
87 | and | ||
88 | .Fn lfind | ||
89 | functions conform to the | ||
90 | .St -p1003.1-01 | ||
91 | and | ||
92 | .St -xpg4.3 . | ||
93 | specifications. | ||
94 | .Sh HISTORY | ||
95 | The | ||
96 | .Fn insque | ||
97 | and | ||
98 | .Fn remque | ||
99 | functions are derived from the insque and remque instructions on a | ||
100 | .Tn VAX . | ||
diff --git a/src/lib/libc/stdlib/insque.c b/src/lib/libc/stdlib/insque.c new file mode 100644 index 0000000000..549246c71f --- /dev/null +++ b/src/lib/libc/stdlib/insque.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* $OpenBSD: insque.c,v 1.1 2003/05/08 23:26:30 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1993 John Brezak | ||
5 | * 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. The name of the author may be used to endorse or promote products | ||
16 | * derived from this software without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR | ||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | ||
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
27 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
28 | * POSSIBILITY OF SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #if defined(LIBC_SCCS) && !defined(lint) | ||
32 | static char *rcsid = "$OpenBSD: insque.c,v 1.1 2003/05/08 23:26:30 millert Exp $"; | ||
33 | #endif /* LIBC_SCCS and not lint */ | ||
34 | |||
35 | #include <search.h> | ||
36 | |||
37 | struct qelem { | ||
38 | struct qelem *q_forw; | ||
39 | struct qelem *q_back; | ||
40 | }; | ||
41 | |||
42 | void | ||
43 | insque(void *entry, void *pred) | ||
44 | { | ||
45 | struct qelem *e = (struct qelem *) entry; | ||
46 | struct qelem *p = (struct qelem *) pred; | ||
47 | |||
48 | e->q_forw = p->q_forw; | ||
49 | e->q_back = p; | ||
50 | p->q_forw->q_back = e; | ||
51 | p->q_forw = e; | ||
52 | } | ||
diff --git a/src/lib/libc/stdlib/jrand48.c b/src/lib/libc/stdlib/jrand48.c new file mode 100644 index 0000000000..99cddb71e5 --- /dev/null +++ b/src/lib/libc/stdlib/jrand48.c | |||
@@ -0,0 +1,25 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: jrand48.c,v 1.2 1996/08/19 08:33:33 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | long | ||
21 | jrand48(unsigned short xseed[3]) | ||
22 | { | ||
23 | __dorand48(xseed); | ||
24 | return ((long) xseed[2] << 16) + (long) xseed[1]; | ||
25 | } | ||
diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c new file mode 100644 index 0000000000..4e99391254 --- /dev/null +++ b/src/lib/libc/stdlib/l64a.c | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
3 | * Public domain. | ||
4 | */ | ||
5 | |||
6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
7 | static char *rcsid = "$OpenBSD: l64a.c,v 1.3 1997/08/17 22:58:34 millert Exp $"; | ||
8 | #endif /* LIBC_SCCS and not lint */ | ||
9 | |||
10 | #include <errno.h> | ||
11 | #include <stdlib.h> | ||
12 | |||
13 | char * | ||
14 | l64a(value) | ||
15 | long value; | ||
16 | { | ||
17 | static char buf[8]; | ||
18 | char *s = buf; | ||
19 | int digit; | ||
20 | int i; | ||
21 | |||
22 | if (value < 0) { | ||
23 | errno = EINVAL; | ||
24 | return(NULL); | ||
25 | } | ||
26 | |||
27 | for (i = 0; value != 0 && i < 6; i++) { | ||
28 | digit = value & 0x3f; | ||
29 | |||
30 | if (digit < 2) | ||
31 | *s = digit + '.'; | ||
32 | else if (digit < 12) | ||
33 | *s = digit + '0' - 2; | ||
34 | else if (digit < 38) | ||
35 | *s = digit + 'A' - 12; | ||
36 | else | ||
37 | *s = digit + 'a' - 38; | ||
38 | |||
39 | value >>= 6; | ||
40 | s++; | ||
41 | } | ||
42 | |||
43 | *s = '\0'; | ||
44 | |||
45 | return(buf); | ||
46 | } | ||
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3 new file mode 100644 index 0000000000..742f3eedba --- /dev/null +++ b/src/lib/libc/stdlib/labs.3 | |||
@@ -0,0 +1,68 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: labs.3,v 1.7 2003/07/21 20:20:04 millert Exp $ | ||
33 | .\" | ||
34 | .Dd May 14, 2003 | ||
35 | .Dt LABS 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm labs, llabs | ||
39 | .Nd return the absolute value of a long integer | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft long | ||
43 | .Fn labs "long i" | ||
44 | .Ft long long | ||
45 | .Fn llabs "long long j" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn labs | ||
49 | function returns the absolute value of the long integer | ||
50 | .Fa i . | ||
51 | The | ||
52 | .Fn llabs | ||
53 | function returns the absolute value of the long long integer | ||
54 | .Fa j . | ||
55 | .Sh SEE ALSO | ||
56 | .Xr abs 3 , | ||
57 | .Xr cabs 3 , | ||
58 | .Xr floor 3 , | ||
59 | .Xr math 3 | ||
60 | .Sh STANDARDS | ||
61 | The | ||
62 | .Fn labs | ||
63 | and | ||
64 | .Fn llabs | ||
65 | functions conform to | ||
66 | .St -ansiC-99 . | ||
67 | .Sh BUGS | ||
68 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/labs.c b/src/lib/libc/stdlib/labs.c new file mode 100644 index 0000000000..51ef490d97 --- /dev/null +++ b/src/lib/libc/stdlib/labs.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: labs.c,v 1.3 2003/06/02 20:18:37 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | long | ||
37 | labs(j) | ||
38 | long j; | ||
39 | { | ||
40 | return(j < 0 ? -j : j); | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c new file mode 100644 index 0000000000..44bd74e48a --- /dev/null +++ b/src/lib/libc/stdlib/lcong48.c | |||
@@ -0,0 +1,34 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: lcong48.c,v 1.2 1996/08/19 08:33:35 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | extern unsigned short __rand48_seed[3]; | ||
21 | extern unsigned short __rand48_mult[3]; | ||
22 | extern unsigned short __rand48_add; | ||
23 | |||
24 | void | ||
25 | lcong48(unsigned short p[7]) | ||
26 | { | ||
27 | __rand48_seed[0] = p[0]; | ||
28 | __rand48_seed[1] = p[1]; | ||
29 | __rand48_seed[2] = p[2]; | ||
30 | __rand48_mult[0] = p[3]; | ||
31 | __rand48_mult[1] = p[4]; | ||
32 | __rand48_mult[2] = p[5]; | ||
33 | __rand48_add = p[6]; | ||
34 | } | ||
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3 new file mode 100644 index 0000000000..f4b5a5fa45 --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.3 | |||
@@ -0,0 +1,65 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: ldiv.3,v 1.6 2003/06/02 20:18:37 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt LDIV 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm ldiv | ||
39 | .Nd return quotient and remainder from division | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft ldiv_t | ||
43 | .Fn ldiv "long num" "long denom" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn ldiv | ||
47 | function computes the value | ||
48 | .Fa num Ns No / Ns Fa denom | ||
49 | and returns the quotient and remainder in a structure named | ||
50 | .Li ldiv_t | ||
51 | that contains two | ||
52 | .Li long integer | ||
53 | members named | ||
54 | .Fa quot | ||
55 | and | ||
56 | .Fa rem . | ||
57 | .Sh SEE ALSO | ||
58 | .Xr div 3 , | ||
59 | .Xr math 3 , | ||
60 | .Xr qdiv 3 | ||
61 | .Sh STANDARDS | ||
62 | The | ||
63 | .Fn ldiv | ||
64 | function conforms to | ||
65 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c new file mode 100644 index 0000000000..e005ff77de --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1990 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: ldiv.c,v 1.3 2003/06/02 20:18:37 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <stdlib.h> /* ldiv_t */ | ||
38 | |||
39 | ldiv_t | ||
40 | ldiv(num, denom) | ||
41 | long num, denom; | ||
42 | { | ||
43 | ldiv_t r; | ||
44 | |||
45 | /* see div.c for comments */ | ||
46 | |||
47 | r.quot = num / denom; | ||
48 | r.rem = num % denom; | ||
49 | if (num >= 0 && r.rem < 0) { | ||
50 | r.quot++; | ||
51 | r.rem -= denom; | ||
52 | } | ||
53 | return (r); | ||
54 | } | ||
diff --git a/src/lib/libc/stdlib/llabs.c b/src/lib/libc/stdlib/llabs.c new file mode 100644 index 0000000000..9611b5aefd --- /dev/null +++ b/src/lib/libc/stdlib/llabs.c | |||
@@ -0,0 +1,45 @@ | |||
1 | /* $OpenBSD: llabs.c,v 1.1 2003/07/21 20:20:04 millert Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1990 The Regents of the University of California. | ||
5 | * 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 | #if defined(LIBC_SCCS) && !defined(lint) | ||
37 | static char *rcsid = "$OpenBSD: llabs.c,v 1.1 2003/07/21 20:20:04 millert Exp $"; | ||
38 | #endif /* LIBC_SCCS and not lint */ | ||
39 | |||
40 | #include <stdlib.h> | ||
41 | |||
42 | long long llabs(long long j) | ||
43 | { | ||
44 | return (j < 0 ? -j : j); | ||
45 | } | ||
diff --git a/src/lib/libc/stdlib/lrand48.c b/src/lib/libc/stdlib/lrand48.c new file mode 100644 index 0000000000..6b7524a51b --- /dev/null +++ b/src/lib/libc/stdlib/lrand48.c | |||
@@ -0,0 +1,27 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: lrand48.c,v 1.2 1996/08/19 08:33:36 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | extern unsigned short __rand48_seed[3]; | ||
21 | |||
22 | long | ||
23 | lrand48(void) | ||
24 | { | ||
25 | __dorand48(__rand48_seed); | ||
26 | return ((long) __rand48_seed[2] << 15) + ((long) __rand48_seed[1] >> 1); | ||
27 | } | ||
diff --git a/src/lib/libc/stdlib/lsearch.3 b/src/lib/libc/stdlib/lsearch.3 new file mode 100644 index 0000000000..2bef4c9aad --- /dev/null +++ b/src/lib/libc/stdlib/lsearch.3 | |||
@@ -0,0 +1,103 @@ | |||
1 | .\" Copyright (c) 1989, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" @(#)lsearch.3 8.1 (Berkeley) 6/4/93 | ||
29 | .\" | ||
30 | .Dd June 4, 1993 | ||
31 | .Dt LSEARCH 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm lsearch , | ||
35 | .Nm lfind | ||
36 | .Nd linear searching routines | ||
37 | .Sh SYNOPSIS | ||
38 | .Ft char * | ||
39 | .Fn lsearch "const void *key" "const void *base" "size_t *nelp" "size_t width" "int (*compar)(void *, void *)" | ||
40 | .Ft char * | ||
41 | .Fn lfind "const void *key" "const void *base" "size_t *nelp" "size_t width" "int (*compar)(void *, void *)" | ||
42 | .Sh DESCRIPTION | ||
43 | The functions | ||
44 | .Fn lsearch , | ||
45 | and | ||
46 | .Fn lfind | ||
47 | provide basic linear searching functionality. | ||
48 | .Pp | ||
49 | .Fa base | ||
50 | is the pointer to the beginning of an array. | ||
51 | The argument | ||
52 | .Fa nelp | ||
53 | is the current number of elements in the array, where each element | ||
54 | is | ||
55 | .Fa width | ||
56 | bytes long. | ||
57 | The | ||
58 | .Fa compar | ||
59 | function | ||
60 | is a comparison routine which is used to compare two elements. | ||
61 | It takes two arguments which point to the | ||
62 | .Fa key | ||
63 | object and to an array member, in that order, and must return an integer | ||
64 | less than, equivalent to, or greater than zero if the | ||
65 | .Fa key | ||
66 | object is considered, respectively, to be less than, equal to, or greater | ||
67 | than the array member. | ||
68 | .Pp | ||
69 | The | ||
70 | .Fn lsearch | ||
71 | and | ||
72 | .Fn lfind | ||
73 | functions | ||
74 | return a pointer into the array referenced by | ||
75 | .Fa base | ||
76 | where | ||
77 | .Fa key | ||
78 | is located. | ||
79 | If | ||
80 | .Fa key | ||
81 | does not exist, | ||
82 | .Fn lfind | ||
83 | will return a null pointer and | ||
84 | .Fn lsearch | ||
85 | will add it to the array. | ||
86 | When an element is added to the array by | ||
87 | .Fn lsearch | ||
88 | the location referenced by the argument | ||
89 | .Fa nelp | ||
90 | is incremented by one. | ||
91 | .Sh SEE ALSO | ||
92 | .Xr bsearch 3 , | ||
93 | .Xr db 3 | ||
94 | .Sh STANDARDS | ||
95 | The | ||
96 | .Fn lsearch | ||
97 | and | ||
98 | .Fn lfind | ||
99 | functions conform to the | ||
100 | .St -p1003.1-01 | ||
101 | and | ||
102 | .St -xpg4.3 . | ||
103 | specifications. | ||
diff --git a/src/lib/libc/stdlib/lsearch.c b/src/lib/libc/stdlib/lsearch.c new file mode 100644 index 0000000000..82483d1c82 --- /dev/null +++ b/src/lib/libc/stdlib/lsearch.c | |||
@@ -0,0 +1,86 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1989, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Roger L. Snyder. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char sccsid[] = "@(#)lsearch.c 8.1 (Berkeley) 6/4/93"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <sys/types.h> | ||
38 | #include <string.h> | ||
39 | #include <search.h> | ||
40 | |||
41 | typedef int (*cmp_fn_t)(const void *, const void *); | ||
42 | static void *linear_base(const void *, const void *, size_t *, size_t, | ||
43 | cmp_fn_t, int); | ||
44 | |||
45 | void * | ||
46 | lsearch(const void *key, const void *base, size_t *nelp, size_t width, | ||
47 | cmp_fn_t compar) | ||
48 | { | ||
49 | |||
50 | return(linear_base(key, base, nelp, width, compar, 1)); | ||
51 | } | ||
52 | |||
53 | void * | ||
54 | lfind(const void *key, const void *base, size_t *nelp, size_t width, | ||
55 | cmp_fn_t compar) | ||
56 | { | ||
57 | return(linear_base(key, base, nelp, width, compar, 0)); | ||
58 | } | ||
59 | |||
60 | static void * | ||
61 | linear_base(const void *key, const void *base, size_t *nelp, size_t width, | ||
62 | cmp_fn_t compar, int add_flag) | ||
63 | { | ||
64 | const char *element, *end; | ||
65 | |||
66 | end = (const char *)base + *nelp * width; | ||
67 | for (element = base; element < end; element += width) | ||
68 | if (!compar(key, element)) /* key found */ | ||
69 | return((void *)element); | ||
70 | |||
71 | if (!add_flag) /* key not found */ | ||
72 | return(NULL); | ||
73 | |||
74 | /* | ||
75 | * The UNIX System User's Manual, 1986 edition claims that | ||
76 | * a NULL pointer is returned by lsearch with errno set | ||
77 | * appropriately, if there is not enough room in the table | ||
78 | * to add a new item. This can't be done as none of these | ||
79 | * routines have any method of determining the size of the | ||
80 | * table. This comment isn't in the 1986-87 System V | ||
81 | * manual. | ||
82 | */ | ||
83 | ++*nelp; | ||
84 | memcpy((void *)end, key, width); | ||
85 | return((void *)end); | ||
86 | } | ||
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 new file mode 100644 index 0000000000..2af8900656 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -0,0 +1,426 @@ | |||
1 | .\" | ||
2 | .\" Copyright (c) 1980, 1991, 1993 | ||
3 | .\" The Regents of the University of California. All rights reserved. | ||
4 | .\" | ||
5 | .\" This code is derived from software contributed to Berkeley by | ||
6 | .\" the American National Standards Committee X3, on Information | ||
7 | .\" Processing Systems. | ||
8 | .\" | ||
9 | .\" Redistribution and use in source and binary forms, with or without | ||
10 | .\" modification, are permitted provided that the following conditions | ||
11 | .\" are met: | ||
12 | .\" 1. Redistributions of source code must retain the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer. | ||
14 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
15 | .\" notice, this list of conditions and the following disclaimer in the | ||
16 | .\" documentation and/or other materials provided with the distribution. | ||
17 | .\" 3. Neither the name of the University nor the names of its contributors | ||
18 | .\" may be used to endorse or promote products derived from this software | ||
19 | .\" without specific prior written permission. | ||
20 | .\" | ||
21 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | .\" SUCH DAMAGE. | ||
32 | .\" | ||
33 | .\" $OpenBSD: malloc.3,v 1.32 2003/10/16 17:05:04 tedu Exp $ | ||
34 | .\" | ||
35 | .Dd August 27, 1996 | ||
36 | .Dt MALLOC 3 | ||
37 | .Os | ||
38 | .Sh NAME | ||
39 | .Nm malloc , | ||
40 | .Nm calloc , | ||
41 | .Nm realloc , | ||
42 | .Nm free , | ||
43 | .Nm cfree | ||
44 | .Nd memory allocation and deallocation | ||
45 | .Sh SYNOPSIS | ||
46 | .Fd #include <stdlib.h> | ||
47 | .Ft void * | ||
48 | .Fn malloc "size_t size" | ||
49 | .Ft void * | ||
50 | .Fn calloc "size_t nmemb" "size_t size" | ||
51 | .Ft void * | ||
52 | .Fn realloc "void *ptr" "size_t size" | ||
53 | .Ft void | ||
54 | .Fn free "void *ptr" | ||
55 | .Ft void | ||
56 | .Fn cfree "void *ptr" | ||
57 | .Ft char * | ||
58 | .Va malloc_options | ||
59 | .Sh DESCRIPTION | ||
60 | The | ||
61 | .Fn malloc | ||
62 | function allocates uninitialized space for an object whose | ||
63 | size is specified by | ||
64 | .Fa size . | ||
65 | The | ||
66 | .Fn malloc | ||
67 | function maintains multiple lists of free blocks according to size, allocating | ||
68 | space from the appropriate list. | ||
69 | .Pp | ||
70 | The allocated space is | ||
71 | suitably aligned (after possible pointer | ||
72 | coercion) for storage of any type of object. | ||
73 | If the space is of | ||
74 | .Em pagesize | ||
75 | or larger, the memory returned will be page-aligned. | ||
76 | .Pp | ||
77 | Allocation of a zero size object returns a pointer to a zero size object. | ||
78 | This zero size object is access protected, so any access to it will | ||
79 | generate an exception (SIGSEGV). | ||
80 | Many zero-sized objects can be placed consecutively in shared | ||
81 | protected pages. | ||
82 | The minimum size of the protection on each object is suitably aligned and | ||
83 | sized as previously stated, but the protection may extend further depending | ||
84 | on where in a protected zone the object lands. | ||
85 | .Pp | ||
86 | The | ||
87 | .Fn calloc | ||
88 | function allocates space for an array of | ||
89 | .Fa nmemb | ||
90 | objects, each of whose size is | ||
91 | .Fa size . | ||
92 | The space is initialized to all bits zero. | ||
93 | .Pp | ||
94 | The | ||
95 | .Fn free | ||
96 | function causes the space pointed to by | ||
97 | .Fa ptr | ||
98 | to be deallocated, that is, at least made available for further allocation, | ||
99 | but if possible, it will passed back to the kernel with | ||
100 | .Xr sbrk 2 . | ||
101 | If | ||
102 | .Fa ptr | ||
103 | is a null pointer, no action occurs. | ||
104 | .Pp | ||
105 | A | ||
106 | .Fn cfree | ||
107 | function is also provided for compatibility with old systems and other | ||
108 | .Nm malloc | ||
109 | libraries; it is simply an alias for | ||
110 | .Fn free . | ||
111 | .Pp | ||
112 | The | ||
113 | .Fn realloc | ||
114 | function changes the size of the object pointed to by | ||
115 | .Fa ptr | ||
116 | to | ||
117 | .Fa size | ||
118 | bytes and returns a pointer to the (possibly moved) object. | ||
119 | The contents of the object are unchanged up to the lesser | ||
120 | of the new and old sizes. | ||
121 | If the new size is larger, the value of the newly allocated portion | ||
122 | of the object is indeterminate and uninitialized. | ||
123 | If | ||
124 | .Fa ptr | ||
125 | is a null pointer, the | ||
126 | .Fn realloc | ||
127 | function behaves like the | ||
128 | .Fn malloc | ||
129 | function for the specified size. | ||
130 | If the space cannot be allocated, the object | ||
131 | pointed to by | ||
132 | .Fa ptr | ||
133 | is unchanged. | ||
134 | If | ||
135 | .Fa size | ||
136 | is zero and | ||
137 | .Fa ptr | ||
138 | is not a null pointer, the object it points to is freed and a new zero size | ||
139 | object is returned. | ||
140 | .Pp | ||
141 | When using | ||
142 | .Fn realloc | ||
143 | one must be careful to avoid the following idiom: | ||
144 | .Pp | ||
145 | .Bd -literal -offset indent | ||
146 | size += 50; | ||
147 | if ((p = realloc(p, size)) == NULL) | ||
148 | return (NULL); | ||
149 | .Ed | ||
150 | .Pp | ||
151 | Do not adjust the variable describing how much memory has been allocated | ||
152 | until one knows the allocation has been successful. | ||
153 | This can cause aberrant program behavior if the incorrect size value is used. | ||
154 | In most cases, the above sample will also result in a leak of memory. | ||
155 | As stated earlier, a return value of | ||
156 | .Dv NULL | ||
157 | indicates that the old object still remains allocated. | ||
158 | Better code looks like this: | ||
159 | .Bd -literal -offset indent | ||
160 | newsize = size + 50; | ||
161 | if ((newp = realloc(p, newsize)) == NULL) { | ||
162 | free(p); | ||
163 | p = NULL; | ||
164 | size = 0; | ||
165 | return (NULL); | ||
166 | } | ||
167 | p = newp; | ||
168 | size = newsize; | ||
169 | .Ed | ||
170 | .Pp | ||
171 | Malloc will first look for a symbolic link called | ||
172 | .Pa /etc/malloc.conf | ||
173 | and next check the environment for a variable called | ||
174 | .Ev MALLOC_OPTIONS | ||
175 | and finally for the global variable | ||
176 | .Va malloc_options | ||
177 | and scan them for flags in that order. | ||
178 | Flags are single letters, uppercase means on, lowercase means off. | ||
179 | .Bl -tag -width indent | ||
180 | .It Cm A | ||
181 | .Dq Abort . | ||
182 | .Fn malloc | ||
183 | will coredump the process, rather than tolerate failure. | ||
184 | This is a very handy debugging aid, since the core file will represent the | ||
185 | time of failure, rather than when the null pointer was accessed. | ||
186 | .Pp | ||
187 | .It Cm D | ||
188 | .Dq Dump . | ||
189 | .Fn malloc | ||
190 | will dump statistics in a file called | ||
191 | .Pa malloc.out | ||
192 | at exit. | ||
193 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
194 | order to have any effect. | ||
195 | .Pp | ||
196 | .It Cm G | ||
197 | Enable guard pages and chunk randomization. | ||
198 | Each page size or larger allocation is followed by a guard page that will | ||
199 | cause a segmentation fault upon any access. | ||
200 | Smaller than page size chunks are returned in a random order. | ||
201 | .Pp | ||
202 | .It Cm J | ||
203 | .Dq Junk . | ||
204 | Fill some junk into the area allocated. | ||
205 | Currently junk is bytes of 0xd0; this is pronounced | ||
206 | .Dq Duh . | ||
207 | \&:-) | ||
208 | .Pp | ||
209 | .It Cm H | ||
210 | .Dq Hint . | ||
211 | Pass a hint to the kernel about pages we don't use. | ||
212 | If the machine is paging a lot this may help a bit. | ||
213 | .Pp | ||
214 | .It Cm N | ||
215 | Do not output warning messages when encountering possible corruption | ||
216 | or bad pointers. | ||
217 | .Pp | ||
218 | .It Cm R | ||
219 | .Dq realloc . | ||
220 | Always reallocate when | ||
221 | .Fn realloc | ||
222 | is called, even if the initial allocation was big enough. | ||
223 | This can substantially aid in compacting memory. | ||
224 | .\".Pp | ||
225 | .\".It Cm U | ||
226 | .\".Dq utrace . | ||
227 | .\"Generate entries for | ||
228 | .\".Xr ktrace 1 | ||
229 | .\"for all operations. | ||
230 | .\"Consult the source for this one. | ||
231 | .Pp | ||
232 | .It Cm X | ||
233 | .Dq xmalloc . | ||
234 | Rather than return failure, | ||
235 | .Xr abort 3 | ||
236 | the program with a diagnostic message on stderr. | ||
237 | It is the intention that this option be set at compile time by | ||
238 | including in the source: | ||
239 | .Bd -literal -offset indent | ||
240 | extern char *malloc_options; | ||
241 | malloc_options = "X"; | ||
242 | .Ed | ||
243 | .Pp | ||
244 | .It Cm Z | ||
245 | .Dq Zero . | ||
246 | Fill some junk into the area allocated (see | ||
247 | .Cm J ) , | ||
248 | except for the exact length the user asked for, which is zeroed. | ||
249 | .Pp | ||
250 | .It Cm < | ||
251 | .Dq Half the cache size . | ||
252 | Reduce the size of the cache by a factor of two. | ||
253 | .Pp | ||
254 | .It Cm > | ||
255 | .Dq Double the cache size . | ||
256 | Double the size of the cache by a factor of two. | ||
257 | .El | ||
258 | .Pp | ||
259 | So to set a systemwide reduction of cache size and coredumps on problems | ||
260 | one would: | ||
261 | .Li ln -s 'A<' /etc/malloc.conf | ||
262 | .Pp | ||
263 | The | ||
264 | .Cm J | ||
265 | and | ||
266 | .Cm Z | ||
267 | flags are mostly for testing and debugging. | ||
268 | If a program changes behavior if either of these options are used, | ||
269 | it is buggy. | ||
270 | .Pp | ||
271 | The default cache size is 16 pages. | ||
272 | .Sh RETURN VALUES | ||
273 | The | ||
274 | .Fn malloc | ||
275 | and | ||
276 | .Fn calloc | ||
277 | functions return a pointer to the allocated space if successful; otherwise, | ||
278 | a null pointer is returned and | ||
279 | .Va errno | ||
280 | is set to | ||
281 | .Er ENOMEM . | ||
282 | .Pp | ||
283 | The | ||
284 | .Fn free | ||
285 | and | ||
286 | .Fn cfree | ||
287 | functions return no value. | ||
288 | .Pp | ||
289 | The | ||
290 | .Fn realloc | ||
291 | function returns a pointer to the (possibly moved) allocated space | ||
292 | if successful; otherwise, a null pointer is returned and | ||
293 | .Va errno | ||
294 | is set to | ||
295 | .Er ENOMEM . | ||
296 | .Sh ENVIRONMENT | ||
297 | See above. | ||
298 | .Sh FILES | ||
299 | .Bl -tag -width "/etc/malloc.conf" | ||
300 | .It Pa /etc/malloc.conf | ||
301 | symbolic link to filename containing option flags | ||
302 | .El | ||
303 | .Sh DIAGNOSTICS | ||
304 | If | ||
305 | .Fn malloc , | ||
306 | .Fn calloc , | ||
307 | .Fn realloc , | ||
308 | or | ||
309 | .Fn free | ||
310 | detect an error or warning condition, | ||
311 | a message will be printed to file descriptor | ||
312 | 2 (not using stdio). | ||
313 | Errors will always result in the process being | ||
314 | .Xr abort 3 'ed. | ||
315 | If the | ||
316 | .Cm A | ||
317 | option has been specified, warnings will also | ||
318 | .Xr abort 3 | ||
319 | the process. | ||
320 | .Pp | ||
321 | Here is a brief description of the error messages and what they mean: | ||
322 | .Bl -tag -width Fl | ||
323 | .It Dq (ES): mumble mumble mumble | ||
324 | .Fn malloc | ||
325 | has been compiled with | ||
326 | .Dv \&-DEXTRA_SANITY | ||
327 | and something looks fishy in there. | ||
328 | Consult sources and/or wizards. | ||
329 | .It Dq allocation failed | ||
330 | If the | ||
331 | .Cm A | ||
332 | option is specified it is an error for | ||
333 | .Fn malloc , | ||
334 | .Fn calloc , | ||
335 | or | ||
336 | .Fn realloc | ||
337 | to return | ||
338 | .Dv NULL . | ||
339 | .It Dq mmap(2) failed, check limits. | ||
340 | This is a rather weird condition that is most likely to indicate a | ||
341 | seriously overloaded system or a | ||
342 | .Xr ulimit 1 | ||
343 | restriction. | ||
344 | .It Dq freelist is destroyed. | ||
345 | .Fn malloc Ns 's | ||
346 | internal freelist has been stomped on. | ||
347 | .El | ||
348 | .Pp | ||
349 | Here is a brief description of the warning messages and what they mean: | ||
350 | .Bl -tag -width Fl | ||
351 | .It Dq chunk/page is already free. | ||
352 | A pointer to a free chunk is attempted freed again. | ||
353 | .It Dq junk pointer, too high to make sense. | ||
354 | The pointer doesn't make sense. | ||
355 | It's above the area of memory that | ||
356 | .Fn malloc | ||
357 | knows something about. | ||
358 | This could be a pointer from some | ||
359 | .Xr mmap 2 'ed | ||
360 | memory. | ||
361 | .It Dq junk pointer, too low to make sense. | ||
362 | The pointer doesn't make sense. | ||
363 | It's below the area of memory that | ||
364 | .Fn malloc | ||
365 | knows something about. | ||
366 | This pointer probably came from your data or bss segments. | ||
367 | .It Dq malloc() has never been called. | ||
368 | Nothing has ever been allocated, yet something is being freed or | ||
369 | realloc'ed. | ||
370 | .It Dq modified (chunk-/page-) pointer. | ||
371 | The pointer passed to free or realloc has been modified. | ||
372 | .It Dq pointer to wrong page. | ||
373 | The pointer that | ||
374 | .Fn malloc | ||
375 | is trying to free is not pointing to | ||
376 | a sensible page. | ||
377 | .It Dq recursive call. | ||
378 | An attempt was made to call recursively into these functions, i.e., from a | ||
379 | signal handler. | ||
380 | This behavior is not supported. | ||
381 | In particular, signal handlers should | ||
382 | .Em not | ||
383 | use any of the | ||
384 | .Fn malloc | ||
385 | functions nor utilize any other functions which may call | ||
386 | .Fn malloc | ||
387 | (e.g., | ||
388 | .Xr stdio 3 | ||
389 | routines). | ||
390 | .It Dq unknown char in MALLOC_OPTIONS | ||
391 | We found something we didn't understand. | ||
392 | .El | ||
393 | .Sh SEE ALSO | ||
394 | .Xr brk 2 , | ||
395 | .Xr alloca 3 , | ||
396 | .Xr getpagesize 3 , | ||
397 | .Xr memory 3 | ||
398 | .Sh STANDARDS | ||
399 | The | ||
400 | .Fn malloc | ||
401 | function conforms to | ||
402 | .St -ansiC . | ||
403 | .Sh HISTORY | ||
404 | The present implementation of | ||
405 | .Fn malloc | ||
406 | started out as a filesystem on a drum | ||
407 | attached to a 20-bit binary challenged computer built with discrete germanium | ||
408 | transistors, and it has since graduated to handle primary storage rather than | ||
409 | secondary. | ||
410 | .Pp | ||
411 | The main difference from other | ||
412 | .Fn malloc | ||
413 | implementations are believed to be that | ||
414 | the free pages are not accessed until allocated. | ||
415 | Most | ||
416 | .Fn malloc | ||
417 | implementations will store a data structure containing a, | ||
418 | possibly double-, linked list in the free chunks of memory, used to tie | ||
419 | all the free memory together. | ||
420 | That is a quite suboptimal thing to do. | ||
421 | Every time the free-list is traversed, all the otherwise unused, and very | ||
422 | likely paged out, pages get faulted into primary memory, just to see what | ||
423 | lies after them in the list. | ||
424 | .Pp | ||
425 | On systems which are paging, this can make a factor five in difference on the | ||
426 | page-faults of a process. | ||
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c new file mode 100644 index 0000000000..689fa8996a --- /dev/null +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -0,0 +1,1317 @@ | |||
1 | /* | ||
2 | * ---------------------------------------------------------------------------- | ||
3 | * "THE BEER-WARE LICENSE" (Revision 42): | ||
4 | * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you | ||
5 | * can do whatever you want with this stuff. If we meet some day, and you think | ||
6 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp | ||
7 | * ---------------------------------------------------------------------------- | ||
8 | */ | ||
9 | |||
10 | #if defined(LIBC_SCCS) && !defined(lint) | ||
11 | static char rcsid[] = "$OpenBSD: malloc.c,v 1.64 2003/10/16 17:05:05 tedu Exp $"; | ||
12 | #endif /* LIBC_SCCS and not lint */ | ||
13 | |||
14 | /* | ||
15 | * Defining MALLOC_EXTRA_SANITY will enable extra checks which are | ||
16 | * related to internal conditions and consistency in malloc.c. This has | ||
17 | * a noticeable runtime performance hit, and generally will not do you | ||
18 | * any good unless you fiddle with the internals of malloc or want | ||
19 | * to catch random pointer corruption as early as possible. | ||
20 | */ | ||
21 | #ifndef MALLOC_EXTRA_SANITY | ||
22 | #undef MALLOC_EXTRA_SANITY | ||
23 | #endif | ||
24 | |||
25 | /* | ||
26 | * Defining MALLOC_STATS will enable you to call malloc_dump() and set | ||
27 | * the [dD] options in the MALLOC_OPTIONS environment variable. | ||
28 | * It has no run-time performance hit, but does pull in stdio... | ||
29 | */ | ||
30 | #ifndef MALLOC_STATS | ||
31 | #undef MALLOC_STATS | ||
32 | #endif | ||
33 | |||
34 | /* | ||
35 | * What to use for Junk. This is the byte value we use to fill with | ||
36 | * when the 'J' option is enabled. | ||
37 | */ | ||
38 | #define SOME_JUNK 0xd0 /* as in "Duh" :-) */ | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | #include <sys/param.h> | ||
42 | #include <sys/mman.h> | ||
43 | #include <sys/uio.h> | ||
44 | #include <stdio.h> | ||
45 | #include <stdlib.h> | ||
46 | #include <string.h> | ||
47 | #include <unistd.h> | ||
48 | #include <fcntl.h> | ||
49 | #include <limits.h> | ||
50 | #include <errno.h> | ||
51 | |||
52 | #include "thread_private.h" | ||
53 | |||
54 | /* | ||
55 | * The basic parameters you can tweak. | ||
56 | * | ||
57 | * malloc_pageshift pagesize = 1 << malloc_pageshift | ||
58 | * It's probably best if this is the native | ||
59 | * page size, but it shouldn't have to be. | ||
60 | * | ||
61 | * malloc_minsize minimum size of an allocation in bytes. | ||
62 | * If this is too small it's too much work | ||
63 | * to manage them. This is also the smallest | ||
64 | * unit of alignment used for the storage | ||
65 | * returned by malloc/realloc. | ||
66 | * | ||
67 | */ | ||
68 | |||
69 | #if defined(__OpenBSD__) && defined(__sparc__) | ||
70 | # define malloc_pageshift 13U | ||
71 | #endif /* __OpenBSD__ */ | ||
72 | |||
73 | /* | ||
74 | * No user serviceable parts behind this point. | ||
75 | * | ||
76 | * This structure describes a page worth of chunks. | ||
77 | */ | ||
78 | |||
79 | struct pginfo { | ||
80 | struct pginfo *next; /* next on the free list */ | ||
81 | void *page; /* Pointer to the page */ | ||
82 | u_short size; /* size of this page's chunks */ | ||
83 | u_short shift; /* How far to shift for this size chunks */ | ||
84 | u_short free; /* How many free chunks */ | ||
85 | u_short total; /* How many chunk */ | ||
86 | u_long bits[1]; /* Which chunks are free */ | ||
87 | }; | ||
88 | |||
89 | /* | ||
90 | * This structure describes a number of free pages. | ||
91 | */ | ||
92 | |||
93 | struct pgfree { | ||
94 | struct pgfree *next; /* next run of free pages */ | ||
95 | struct pgfree *prev; /* prev run of free pages */ | ||
96 | void *page; /* pointer to free pages */ | ||
97 | void *end; /* pointer to end of free pages */ | ||
98 | u_long size; /* number of bytes free */ | ||
99 | }; | ||
100 | |||
101 | /* | ||
102 | * How many bits per u_long in the bitmap. | ||
103 | * Change only if not 8 bits/byte | ||
104 | */ | ||
105 | #define MALLOC_BITS (8*sizeof(u_long)) | ||
106 | |||
107 | /* | ||
108 | * Magic values to put in the page_directory | ||
109 | */ | ||
110 | #define MALLOC_NOT_MINE ((struct pginfo*) 0) | ||
111 | #define MALLOC_FREE ((struct pginfo*) 1) | ||
112 | #define MALLOC_FIRST ((struct pginfo*) 2) | ||
113 | #define MALLOC_FOLLOW ((struct pginfo*) 3) | ||
114 | #define MALLOC_MAGIC ((struct pginfo*) 4) | ||
115 | |||
116 | #ifndef malloc_pageshift | ||
117 | #define malloc_pageshift (PGSHIFT) | ||
118 | #endif | ||
119 | |||
120 | #ifndef malloc_minsize | ||
121 | #define malloc_minsize 16U | ||
122 | #endif | ||
123 | |||
124 | #ifndef malloc_pageshift | ||
125 | #error "malloc_pageshift undefined" | ||
126 | #endif | ||
127 | |||
128 | #if !defined(malloc_pagesize) | ||
129 | #define malloc_pagesize (1UL<<malloc_pageshift) | ||
130 | #endif | ||
131 | |||
132 | #if ((1UL<<malloc_pageshift) != malloc_pagesize) | ||
133 | #error "(1UL<<malloc_pageshift) != malloc_pagesize" | ||
134 | #endif | ||
135 | |||
136 | #ifndef malloc_maxsize | ||
137 | #define malloc_maxsize ((malloc_pagesize)>>1) | ||
138 | #endif | ||
139 | |||
140 | /* A mask for the offset inside a page. */ | ||
141 | #define malloc_pagemask ((malloc_pagesize)-1) | ||
142 | |||
143 | #define pageround(foo) (((foo) + (malloc_pagemask))&(~(malloc_pagemask))) | ||
144 | #define ptr2index(foo) (((u_long)(foo) >> malloc_pageshift)-malloc_origo) | ||
145 | |||
146 | /* fd of /dev/zero */ | ||
147 | #ifdef USE_DEV_ZERO | ||
148 | static int fdzero; | ||
149 | #define MMAP_FD fdzero | ||
150 | #define INIT_MMAP() \ | ||
151 | { if ((fdzero=open("/dev/zero", O_RDWR, 0000)) == -1) \ | ||
152 | wrterror("open of /dev/zero\n"); } | ||
153 | #else | ||
154 | #define MMAP_FD (-1) | ||
155 | #define INIT_MMAP() | ||
156 | #endif | ||
157 | |||
158 | /* Set when initialization has been done */ | ||
159 | static unsigned int malloc_started; | ||
160 | |||
161 | /* Number of free pages we cache */ | ||
162 | static unsigned int malloc_cache = 16; | ||
163 | |||
164 | /* The offset from pagenumber to index into the page directory */ | ||
165 | static u_long malloc_origo; | ||
166 | |||
167 | /* The last index in the page directory we care about */ | ||
168 | static u_long last_index; | ||
169 | |||
170 | /* Pointer to page directory. Allocated "as if with" malloc */ | ||
171 | static struct pginfo **page_dir; | ||
172 | |||
173 | /* How many slots in the page directory */ | ||
174 | static size_t malloc_ninfo; | ||
175 | |||
176 | /* Free pages line up here */ | ||
177 | static struct pgfree free_list; | ||
178 | |||
179 | /* Abort(), user doesn't handle problems. */ | ||
180 | static int malloc_abort = 2; | ||
181 | |||
182 | /* Are we trying to die ? */ | ||
183 | static int suicide; | ||
184 | |||
185 | #ifdef MALLOC_STATS | ||
186 | /* dump statistics */ | ||
187 | static int malloc_stats; | ||
188 | #endif | ||
189 | |||
190 | /* avoid outputting warnings? */ | ||
191 | static int malloc_silent; | ||
192 | |||
193 | /* always realloc ? */ | ||
194 | static int malloc_realloc; | ||
195 | |||
196 | /* mprotect free pages PROT_NONE? */ | ||
197 | static int malloc_freeprot; | ||
198 | |||
199 | /* use guard pages after allocations? */ | ||
200 | static int malloc_guard = 0; | ||
201 | |||
202 | #if defined(__FreeBSD__) || (defined(__OpenBSD__) && defined(MADV_FREE)) | ||
203 | /* pass the kernel a hint on free pages ? */ | ||
204 | static int malloc_hint; | ||
205 | #endif | ||
206 | |||
207 | /* xmalloc behaviour ? */ | ||
208 | static int malloc_xmalloc; | ||
209 | |||
210 | /* zero fill ? */ | ||
211 | static int malloc_zero; | ||
212 | |||
213 | /* junk fill ? */ | ||
214 | static int malloc_junk; | ||
215 | |||
216 | #ifdef __FreeBSD__ | ||
217 | /* utrace ? */ | ||
218 | static int malloc_utrace; | ||
219 | |||
220 | struct ut { void *p; size_t s; void *r; }; | ||
221 | |||
222 | void utrace(struct ut *, int); | ||
223 | |||
224 | #define UTRACE(a, b, c) \ | ||
225 | if (malloc_utrace) \ | ||
226 | {struct ut u; u.p=a; u.s = b; u.r=c; utrace(&u, sizeof u);} | ||
227 | #else /* !__FreeBSD__ */ | ||
228 | #define UTRACE(a,b,c) | ||
229 | #endif | ||
230 | |||
231 | /* my last break. */ | ||
232 | static void *malloc_brk; | ||
233 | |||
234 | /* one location cache for free-list holders */ | ||
235 | static struct pgfree *px; | ||
236 | |||
237 | /* compile-time options */ | ||
238 | char *malloc_options; | ||
239 | |||
240 | /* Name of the current public function */ | ||
241 | static char *malloc_func; | ||
242 | |||
243 | /* Macro for mmap */ | ||
244 | #define MMAP(size) \ | ||
245 | mmap((void *)0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \ | ||
246 | MMAP_FD, (off_t)0) | ||
247 | |||
248 | /* | ||
249 | * Necessary function declarations | ||
250 | */ | ||
251 | static int extend_pgdir(u_long index); | ||
252 | static void *imalloc(size_t size); | ||
253 | static void ifree(void *ptr); | ||
254 | static void *irealloc(void *ptr, size_t size); | ||
255 | static void *malloc_bytes(size_t size); | ||
256 | |||
257 | #ifdef MALLOC_STATS | ||
258 | void | ||
259 | malloc_dump(FILE *fd) | ||
260 | { | ||
261 | struct pginfo **pd; | ||
262 | struct pgfree *pf; | ||
263 | int j; | ||
264 | |||
265 | pd = page_dir; | ||
266 | |||
267 | /* print out all the pages */ | ||
268 | for(j=0;j<=last_index;j++) { | ||
269 | fprintf(fd, "%08lx %5d ", (j+malloc_origo) << malloc_pageshift, j); | ||
270 | if (pd[j] == MALLOC_NOT_MINE) { | ||
271 | for(j++;j<=last_index && pd[j] == MALLOC_NOT_MINE;j++) | ||
272 | ; | ||
273 | j--; | ||
274 | fprintf(fd, ".. %5d not mine\n", j); | ||
275 | } else if (pd[j] == MALLOC_FREE) { | ||
276 | for(j++;j<=last_index && pd[j] == MALLOC_FREE;j++) | ||
277 | ; | ||
278 | j--; | ||
279 | fprintf(fd, ".. %5d free\n", j); | ||
280 | } else if (pd[j] == MALLOC_FIRST) { | ||
281 | for(j++;j<=last_index && pd[j] == MALLOC_FOLLOW;j++) | ||
282 | ; | ||
283 | j--; | ||
284 | fprintf(fd, ".. %5d in use\n", j); | ||
285 | } else if (pd[j] < MALLOC_MAGIC) { | ||
286 | fprintf(fd, "(%p)\n", pd[j]); | ||
287 | } else { | ||
288 | fprintf(fd, "%p %d (of %d) x %d @ %p --> %p\n", | ||
289 | pd[j], pd[j]->free, pd[j]->total, | ||
290 | pd[j]->size, pd[j]->page, pd[j]->next); | ||
291 | } | ||
292 | } | ||
293 | |||
294 | for(pf=free_list.next; pf; pf=pf->next) { | ||
295 | fprintf(fd, "Free: @%p [%p...%p[ %ld ->%p <-%p\n", | ||
296 | pf, pf->page, pf->end, pf->size, pf->prev, pf->next); | ||
297 | if (pf == pf->next) { | ||
298 | fprintf(fd, "Free_list loops\n"); | ||
299 | break; | ||
300 | } | ||
301 | } | ||
302 | |||
303 | /* print out various info */ | ||
304 | fprintf(fd, "Minsize\t%d\n", malloc_minsize); | ||
305 | fprintf(fd, "Maxsize\t%d\n", malloc_maxsize); | ||
306 | fprintf(fd, "Pagesize\t%lu\n", (u_long)malloc_pagesize); | ||
307 | fprintf(fd, "Pageshift\t%d\n", malloc_pageshift); | ||
308 | fprintf(fd, "FirstPage\t%ld\n", malloc_origo); | ||
309 | fprintf(fd, "LastPage\t%ld %lx\n", last_index+malloc_pageshift, | ||
310 | (last_index + malloc_pageshift) << malloc_pageshift); | ||
311 | fprintf(fd, "Break\t%ld\n", (u_long)sbrk(0) >> malloc_pageshift); | ||
312 | } | ||
313 | #endif /* MALLOC_STATS */ | ||
314 | |||
315 | extern char *__progname; | ||
316 | |||
317 | static void | ||
318 | wrterror(char *p) | ||
319 | { | ||
320 | char *q = " error: "; | ||
321 | struct iovec iov[4]; | ||
322 | |||
323 | iov[0].iov_base = __progname; | ||
324 | iov[0].iov_len = strlen(__progname); | ||
325 | iov[1].iov_base = malloc_func; | ||
326 | iov[1].iov_len = strlen(malloc_func); | ||
327 | iov[2].iov_base = q; | ||
328 | iov[2].iov_len = strlen(q); | ||
329 | iov[3].iov_base = p; | ||
330 | iov[3].iov_len = strlen(p); | ||
331 | writev(STDERR_FILENO, iov, 4); | ||
332 | |||
333 | suicide = 1; | ||
334 | #ifdef MALLOC_STATS | ||
335 | if (malloc_stats) | ||
336 | malloc_dump(stderr); | ||
337 | #endif /* MALLOC_STATS */ | ||
338 | abort(); | ||
339 | } | ||
340 | |||
341 | static void | ||
342 | wrtwarning(char *p) | ||
343 | { | ||
344 | char *q = " warning: "; | ||
345 | struct iovec iov[4]; | ||
346 | |||
347 | if (malloc_abort) | ||
348 | wrterror(p); | ||
349 | else if (malloc_silent) | ||
350 | return; | ||
351 | |||
352 | iov[0].iov_base = __progname; | ||
353 | iov[0].iov_len = strlen(__progname); | ||
354 | iov[1].iov_base = malloc_func; | ||
355 | iov[1].iov_len = strlen(malloc_func); | ||
356 | iov[2].iov_base = q; | ||
357 | iov[2].iov_len = strlen(q); | ||
358 | iov[3].iov_base = p; | ||
359 | iov[3].iov_len = strlen(p); | ||
360 | writev(STDERR_FILENO, iov, 4); | ||
361 | } | ||
362 | |||
363 | #ifdef MALLOC_STATS | ||
364 | static void | ||
365 | malloc_exit(void) | ||
366 | { | ||
367 | FILE *fd = fopen("malloc.out", "a"); | ||
368 | char *q = "malloc() warning: Couldn't dump stats\n"; | ||
369 | if (fd != NULL) { | ||
370 | malloc_dump(fd); | ||
371 | fclose(fd); | ||
372 | } else | ||
373 | write(STDERR_FILENO, q, strlen(q)); | ||
374 | } | ||
375 | #endif /* MALLOC_STATS */ | ||
376 | |||
377 | |||
378 | /* | ||
379 | * Allocate a number of pages from the OS | ||
380 | */ | ||
381 | static void * | ||
382 | map_pages(size_t pages) | ||
383 | { | ||
384 | caddr_t result, tail; | ||
385 | |||
386 | result = (caddr_t)pageround((u_long)sbrk(0)); | ||
387 | pages <<= malloc_pageshift; | ||
388 | if (pages > SIZE_T_MAX - (size_t)result) { | ||
389 | #ifdef MALLOC_EXTRA_SANITY | ||
390 | wrtwarning("(ES): overflow in map_pages fails\n"); | ||
391 | #endif /* MALLOC_EXTRA_SANITY */ | ||
392 | errno = ENOMEM; | ||
393 | return (NULL); | ||
394 | } | ||
395 | tail = result + pages + malloc_guard; | ||
396 | |||
397 | if (brk(tail) == (char *)-1) { | ||
398 | #ifdef MALLOC_EXTRA_SANITY | ||
399 | wrtwarning("(ES): map_pages fails\n"); | ||
400 | #endif /* MALLOC_EXTRA_SANITY */ | ||
401 | return (NULL); | ||
402 | } | ||
403 | if (malloc_guard) | ||
404 | mprotect(result + pages, malloc_pagesize, PROT_NONE); | ||
405 | |||
406 | last_index = ptr2index(tail) - 1; | ||
407 | malloc_brk = tail; | ||
408 | |||
409 | if ((last_index+1) >= malloc_ninfo && !extend_pgdir(last_index)) | ||
410 | return (NULL); | ||
411 | |||
412 | return (result); | ||
413 | } | ||
414 | |||
415 | /* | ||
416 | * Extend page directory | ||
417 | */ | ||
418 | static int | ||
419 | extend_pgdir(u_long index) | ||
420 | { | ||
421 | struct pginfo **new, **old; | ||
422 | size_t i, oldlen; | ||
423 | |||
424 | /* Make it this many pages */ | ||
425 | i = index * sizeof *page_dir; | ||
426 | i /= malloc_pagesize; | ||
427 | i += 2; | ||
428 | |||
429 | /* remember the old mapping size */ | ||
430 | oldlen = malloc_ninfo * sizeof *page_dir; | ||
431 | |||
432 | /* | ||
433 | * NOTE: we allocate new pages and copy the directory rather than tempt | ||
434 | * fate by trying to "grow" the region.. There is nothing to prevent | ||
435 | * us from accidently re-mapping space that's been allocated by our caller | ||
436 | * via dlopen() or other mmap(). | ||
437 | * | ||
438 | * The copy problem is not too bad, as there is 4K of page index per | ||
439 | * 4MB of malloc arena. | ||
440 | * | ||
441 | * We can totally avoid the copy if we open a file descriptor to associate | ||
442 | * the anon mappings with. Then, when we remap the pages at the new | ||
443 | * address, the old pages will be "magically" remapped.. But this means | ||
444 | * keeping open a "secret" file descriptor..... | ||
445 | */ | ||
446 | |||
447 | /* Get new pages */ | ||
448 | new = (struct pginfo**) MMAP(i * malloc_pagesize); | ||
449 | if (new == MAP_FAILED) | ||
450 | return (0); | ||
451 | |||
452 | /* Copy the old stuff */ | ||
453 | memcpy(new, page_dir, | ||
454 | malloc_ninfo * sizeof *page_dir); | ||
455 | |||
456 | /* register the new size */ | ||
457 | malloc_ninfo = i * malloc_pagesize / sizeof *page_dir; | ||
458 | |||
459 | /* swap the pointers */ | ||
460 | old = page_dir; | ||
461 | page_dir = new; | ||
462 | |||
463 | /* Now free the old stuff */ | ||
464 | munmap(old, oldlen); | ||
465 | return (1); | ||
466 | } | ||
467 | |||
468 | /* | ||
469 | * Initialize the world | ||
470 | */ | ||
471 | static void | ||
472 | malloc_init(void) | ||
473 | { | ||
474 | char *p, b[64]; | ||
475 | int i, j; | ||
476 | int save_errno = errno; | ||
477 | |||
478 | _MALLOC_LOCK_INIT(); | ||
479 | |||
480 | INIT_MMAP(); | ||
481 | |||
482 | #ifdef MALLOC_EXTRA_SANITY | ||
483 | malloc_junk = 1; | ||
484 | #endif /* MALLOC_EXTRA_SANITY */ | ||
485 | |||
486 | for (i = 0; i < 3; i++) { | ||
487 | if (i == 0) { | ||
488 | j = readlink("/etc/malloc.conf", b, sizeof b - 1); | ||
489 | if (j <= 0) | ||
490 | continue; | ||
491 | b[j] = '\0'; | ||
492 | p = b; | ||
493 | } else if (i == 1) { | ||
494 | if (issetugid() == 0) | ||
495 | p = getenv("MALLOC_OPTIONS"); | ||
496 | else | ||
497 | continue; | ||
498 | } else if (i == 2) { | ||
499 | p = malloc_options; | ||
500 | } | ||
501 | for (; p != NULL && *p != '\0'; p++) { | ||
502 | switch (*p) { | ||
503 | case '>': malloc_cache <<= 1; break; | ||
504 | case '<': malloc_cache >>= 1; break; | ||
505 | case 'a': malloc_abort = 0; break; | ||
506 | case 'A': malloc_abort = 1; break; | ||
507 | #ifdef MALLOC_STATS | ||
508 | case 'd': malloc_stats = 0; break; | ||
509 | case 'D': malloc_stats = 1; break; | ||
510 | #endif /* MALLOC_STATS */ | ||
511 | case 'f': malloc_freeprot = 0; break; | ||
512 | case 'F': malloc_freeprot = 1; break; | ||
513 | case 'g': malloc_guard = 0; break; | ||
514 | case 'G': malloc_guard = malloc_pagesize; break; | ||
515 | #if defined(__FreeBSD__) || (defined(__OpenBSD__) && defined(MADV_FREE)) | ||
516 | case 'h': malloc_hint = 0; break; | ||
517 | case 'H': malloc_hint = 1; break; | ||
518 | #endif /* __FreeBSD__ */ | ||
519 | case 'r': malloc_realloc = 0; break; | ||
520 | case 'R': malloc_realloc = 1; break; | ||
521 | case 'j': malloc_junk = 0; break; | ||
522 | case 'J': malloc_junk = 1; break; | ||
523 | case 'n': malloc_silent = 0; break; | ||
524 | case 'N': malloc_silent = 1; break; | ||
525 | #ifdef __FreeBSD__ | ||
526 | case 'u': malloc_utrace = 0; break; | ||
527 | case 'U': malloc_utrace = 1; break; | ||
528 | #endif /* __FreeBSD__ */ | ||
529 | case 'x': malloc_xmalloc = 0; break; | ||
530 | case 'X': malloc_xmalloc = 1; break; | ||
531 | case 'z': malloc_zero = 0; break; | ||
532 | case 'Z': malloc_zero = 1; break; | ||
533 | default: | ||
534 | j = malloc_abort; | ||
535 | malloc_abort = 0; | ||
536 | wrtwarning("unknown char in MALLOC_OPTIONS\n"); | ||
537 | malloc_abort = j; | ||
538 | break; | ||
539 | } | ||
540 | } | ||
541 | } | ||
542 | |||
543 | UTRACE(0, 0, 0); | ||
544 | |||
545 | /* | ||
546 | * We want junk in the entire allocation, and zero only in the part | ||
547 | * the user asked for. | ||
548 | */ | ||
549 | if (malloc_zero) | ||
550 | malloc_junk=1; | ||
551 | |||
552 | #ifdef MALLOC_STATS | ||
553 | if (malloc_stats && (atexit(malloc_exit) == -1)) | ||
554 | wrtwarning("atexit(2) failed. Will not be able to dump malloc stats on exit\n"); | ||
555 | #endif /* MALLOC_STATS */ | ||
556 | |||
557 | /* Allocate one page for the page directory */ | ||
558 | page_dir = (struct pginfo **) MMAP(malloc_pagesize); | ||
559 | |||
560 | if (page_dir == MAP_FAILED) | ||
561 | wrterror("mmap(2) failed, check limits\n"); | ||
562 | |||
563 | /* | ||
564 | * We need a maximum of malloc_pageshift buckets, steal these from the | ||
565 | * front of the page_directory; | ||
566 | */ | ||
567 | malloc_origo = ((u_long)pageround((u_long)sbrk(0))) >> malloc_pageshift; | ||
568 | malloc_origo -= malloc_pageshift; | ||
569 | |||
570 | malloc_ninfo = malloc_pagesize / sizeof *page_dir; | ||
571 | |||
572 | /* Been here, done that */ | ||
573 | malloc_started++; | ||
574 | |||
575 | /* Recalculate the cache size in bytes, and make sure it's nonzero */ | ||
576 | |||
577 | if (!malloc_cache) | ||
578 | malloc_cache++; | ||
579 | |||
580 | malloc_cache <<= malloc_pageshift; | ||
581 | |||
582 | /* | ||
583 | * This is a nice hack from Kaleb Keithly (kaleb@x.org). | ||
584 | * We can sbrk(2) further back when we keep this on a low address. | ||
585 | */ | ||
586 | px = (struct pgfree *) imalloc (sizeof *px); | ||
587 | errno = save_errno; | ||
588 | } | ||
589 | |||
590 | /* | ||
591 | * Allocate a number of complete pages | ||
592 | */ | ||
593 | static void * | ||
594 | malloc_pages(size_t size) | ||
595 | { | ||
596 | void *p, *delay_free = NULL; | ||
597 | int i; | ||
598 | struct pgfree *pf; | ||
599 | u_long index; | ||
600 | |||
601 | size = pageround(size) + malloc_guard; | ||
602 | |||
603 | p = NULL; | ||
604 | /* Look for free pages before asking for more */ | ||
605 | for(pf = free_list.next; pf; pf = pf->next) { | ||
606 | |||
607 | #ifdef MALLOC_EXTRA_SANITY | ||
608 | if (pf->size & malloc_pagemask) | ||
609 | wrterror("(ES): junk length entry on free_list\n"); | ||
610 | if (!pf->size) | ||
611 | wrterror("(ES): zero length entry on free_list\n"); | ||
612 | if (pf->page == pf->end) | ||
613 | wrterror("(ES): zero entry on free_list\n"); | ||
614 | if (pf->page > pf->end) | ||
615 | wrterror("(ES): sick entry on free_list\n"); | ||
616 | if ((void*)pf->page >= (void*)sbrk(0)) | ||
617 | wrterror("(ES): entry on free_list past brk\n"); | ||
618 | if (page_dir[ptr2index(pf->page)] != MALLOC_FREE) | ||
619 | wrterror("(ES): non-free first page on free-list\n"); | ||
620 | if (page_dir[ptr2index(pf->end)-1] != MALLOC_FREE) | ||
621 | wrterror("(ES): non-free last page on free-list\n"); | ||
622 | #endif /* MALLOC_EXTRA_SANITY */ | ||
623 | |||
624 | if (pf->size < size) | ||
625 | continue; | ||
626 | |||
627 | if (pf->size == size) { | ||
628 | p = pf->page; | ||
629 | if (pf->next != NULL) | ||
630 | pf->next->prev = pf->prev; | ||
631 | pf->prev->next = pf->next; | ||
632 | delay_free = pf; | ||
633 | break; | ||
634 | } | ||
635 | |||
636 | p = pf->page; | ||
637 | pf->page = (char *)pf->page + size; | ||
638 | pf->size -= size; | ||
639 | break; | ||
640 | } | ||
641 | |||
642 | size -= malloc_guard; | ||
643 | |||
644 | #ifdef MALLOC_EXTRA_SANITY | ||
645 | if (p != NULL && page_dir[ptr2index(p)] != MALLOC_FREE) | ||
646 | wrterror("(ES): allocated non-free page on free-list\n"); | ||
647 | #endif /* MALLOC_EXTRA_SANITY */ | ||
648 | |||
649 | if ((malloc_guard || malloc_freeprot) && p != NULL) | ||
650 | mprotect(p, size, PROT_READ|PROT_WRITE); | ||
651 | |||
652 | size >>= malloc_pageshift; | ||
653 | |||
654 | /* Map new pages */ | ||
655 | if (p == NULL) | ||
656 | p = map_pages(size); | ||
657 | |||
658 | if (p != NULL) { | ||
659 | |||
660 | index = ptr2index(p); | ||
661 | page_dir[index] = MALLOC_FIRST; | ||
662 | for (i=1;i<size;i++) | ||
663 | page_dir[index+i] = MALLOC_FOLLOW; | ||
664 | |||
665 | if (malloc_junk) | ||
666 | memset(p, SOME_JUNK, size << malloc_pageshift); | ||
667 | } | ||
668 | |||
669 | if (delay_free) { | ||
670 | if (px == NULL) | ||
671 | px = delay_free; | ||
672 | else | ||
673 | ifree(delay_free); | ||
674 | } | ||
675 | |||
676 | return (p); | ||
677 | } | ||
678 | |||
679 | /* | ||
680 | * Allocate a page of fragments | ||
681 | */ | ||
682 | |||
683 | static __inline__ int | ||
684 | malloc_make_chunks(int bits) | ||
685 | { | ||
686 | struct pginfo *bp; | ||
687 | void *pp; | ||
688 | int i, k, l; | ||
689 | |||
690 | /* Allocate a new bucket */ | ||
691 | pp = malloc_pages((size_t)malloc_pagesize); | ||
692 | if (pp == NULL) | ||
693 | return (0); | ||
694 | |||
695 | /* Find length of admin structure */ | ||
696 | l = sizeof *bp - sizeof(u_long); | ||
697 | l += sizeof(u_long) * | ||
698 | (((malloc_pagesize >> bits)+MALLOC_BITS-1) / MALLOC_BITS); | ||
699 | |||
700 | /* Don't waste more than two chunks on this */ | ||
701 | /* | ||
702 | * If we are to allocate a memory protected page for the malloc(0) | ||
703 | * case (when bits=0), it must be from a different page than the | ||
704 | * pginfo page. | ||
705 | * --> Treat it like the big chunk alloc, get a second data page. | ||
706 | */ | ||
707 | if (bits != 0 && (1UL<<(bits)) <= l+l) { | ||
708 | bp = (struct pginfo *)pp; | ||
709 | } else { | ||
710 | bp = (struct pginfo *)imalloc(l); | ||
711 | if (bp == NULL) { | ||
712 | ifree(pp); | ||
713 | return (0); | ||
714 | } | ||
715 | } | ||
716 | |||
717 | /* memory protect the page allocated in the malloc(0) case */ | ||
718 | if (bits == 0) { | ||
719 | |||
720 | bp->size = 0; | ||
721 | bp->shift = 1; | ||
722 | i = malloc_minsize-1; | ||
723 | while (i >>= 1) | ||
724 | bp->shift++; | ||
725 | bp->total = bp->free = malloc_pagesize >> bp->shift; | ||
726 | bp->page = pp; | ||
727 | |||
728 | k = mprotect(pp, malloc_pagesize, PROT_NONE); | ||
729 | if (k < 0) { | ||
730 | ifree(pp); | ||
731 | ifree(bp); | ||
732 | return (0); | ||
733 | } | ||
734 | } else { | ||
735 | bp->size = (1UL<<bits); | ||
736 | bp->shift = bits; | ||
737 | bp->total = bp->free = malloc_pagesize >> bits; | ||
738 | bp->page = pp; | ||
739 | } | ||
740 | |||
741 | /* set all valid bits in the bitmap */ | ||
742 | k = bp->total; | ||
743 | i = 0; | ||
744 | |||
745 | /* Do a bunch at a time */ | ||
746 | for(;k-i >= MALLOC_BITS; i += MALLOC_BITS) | ||
747 | bp->bits[i / MALLOC_BITS] = ~0UL; | ||
748 | |||
749 | for(; i < k; i++) | ||
750 | bp->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS); | ||
751 | |||
752 | if (bp == bp->page) { | ||
753 | /* Mark the ones we stole for ourselves */ | ||
754 | for(i=0;l > 0;i++) { | ||
755 | bp->bits[i/MALLOC_BITS] &= ~(1UL<<(i%MALLOC_BITS)); | ||
756 | bp->free--; | ||
757 | bp->total--; | ||
758 | l -= (1 << bits); | ||
759 | } | ||
760 | } | ||
761 | |||
762 | /* MALLOC_LOCK */ | ||
763 | |||
764 | page_dir[ptr2index(pp)] = bp; | ||
765 | |||
766 | bp->next = page_dir[bits]; | ||
767 | page_dir[bits] = bp; | ||
768 | |||
769 | /* MALLOC_UNLOCK */ | ||
770 | |||
771 | return (1); | ||
772 | } | ||
773 | |||
774 | /* | ||
775 | * Allocate a fragment | ||
776 | */ | ||
777 | static void * | ||
778 | malloc_bytes(size_t size) | ||
779 | { | ||
780 | int i,j; | ||
781 | u_long u; | ||
782 | struct pginfo *bp; | ||
783 | int k; | ||
784 | u_long *lp; | ||
785 | |||
786 | /* Don't bother with anything less than this */ | ||
787 | /* unless we have a malloc(0) requests */ | ||
788 | if (size != 0 && size < malloc_minsize) | ||
789 | size = malloc_minsize; | ||
790 | |||
791 | /* Find the right bucket */ | ||
792 | if (size == 0) | ||
793 | j=0; | ||
794 | else { | ||
795 | j = 1; | ||
796 | i = size-1; | ||
797 | while (i >>= 1) | ||
798 | j++; | ||
799 | } | ||
800 | |||
801 | /* If it's empty, make a page more of that size chunks */ | ||
802 | if (page_dir[j] == NULL && !malloc_make_chunks(j)) | ||
803 | return (NULL); | ||
804 | |||
805 | bp = page_dir[j]; | ||
806 | |||
807 | /* Find first word of bitmap which isn't empty */ | ||
808 | for (lp = bp->bits; !*lp; lp++) | ||
809 | ; | ||
810 | |||
811 | /* Find that bit, and tweak it */ | ||
812 | u = 1; | ||
813 | k = 0; | ||
814 | while (!(*lp & u)) { | ||
815 | u += u; | ||
816 | k++; | ||
817 | } | ||
818 | |||
819 | if (malloc_guard) { | ||
820 | /* Walk to a random position. */ | ||
821 | i = arc4random() % bp->free; | ||
822 | while (i > 0) { | ||
823 | u += u; | ||
824 | k++; | ||
825 | if (k >= MALLOC_BITS) { | ||
826 | lp++; | ||
827 | u = 1; | ||
828 | k = 0; | ||
829 | } | ||
830 | #ifdef MALLOC_EXTRA_SANITY | ||
831 | if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) | ||
832 | wrterror("chunk overflow\n"); | ||
833 | #endif /* MALLOC_EXTRA_SANITY */ | ||
834 | if (*lp & u) | ||
835 | i--; | ||
836 | } | ||
837 | } | ||
838 | *lp ^= u; | ||
839 | |||
840 | /* If there are no more free, remove from free-list */ | ||
841 | if (!--bp->free) { | ||
842 | page_dir[j] = bp->next; | ||
843 | bp->next = NULL; | ||
844 | } | ||
845 | |||
846 | /* Adjust to the real offset of that chunk */ | ||
847 | k += (lp-bp->bits)*MALLOC_BITS; | ||
848 | k <<= bp->shift; | ||
849 | |||
850 | if (malloc_junk && bp->size != 0) | ||
851 | memset((char *)bp->page + k, SOME_JUNK, bp->size); | ||
852 | |||
853 | return ((u_char *)bp->page + k); | ||
854 | } | ||
855 | |||
856 | /* | ||
857 | * Allocate a piece of memory | ||
858 | */ | ||
859 | static void * | ||
860 | imalloc(size_t size) | ||
861 | { | ||
862 | void *result; | ||
863 | |||
864 | if (!malloc_started) | ||
865 | malloc_init(); | ||
866 | |||
867 | if (suicide) | ||
868 | abort(); | ||
869 | |||
870 | if ((size + malloc_pagesize) < size) { /* Check for overflow */ | ||
871 | result = NULL; | ||
872 | errno = ENOMEM; | ||
873 | } | ||
874 | else if (size <= malloc_maxsize) | ||
875 | result = malloc_bytes(size); | ||
876 | else | ||
877 | result = malloc_pages(size); | ||
878 | |||
879 | if (malloc_abort == 1 && result == NULL) | ||
880 | wrterror("allocation failed\n"); | ||
881 | |||
882 | if (malloc_zero && result != NULL) | ||
883 | memset(result, 0, size); | ||
884 | |||
885 | return (result); | ||
886 | } | ||
887 | |||
888 | /* | ||
889 | * Change the size of an allocation. | ||
890 | */ | ||
891 | static void * | ||
892 | irealloc(void *ptr, size_t size) | ||
893 | { | ||
894 | void *p; | ||
895 | u_long osize, index; | ||
896 | struct pginfo **mp; | ||
897 | int i; | ||
898 | |||
899 | if (suicide) | ||
900 | abort(); | ||
901 | |||
902 | if (!malloc_started) { | ||
903 | wrtwarning("malloc() has never been called\n"); | ||
904 | return (NULL); | ||
905 | } | ||
906 | |||
907 | index = ptr2index(ptr); | ||
908 | |||
909 | if (index < malloc_pageshift) { | ||
910 | wrtwarning("junk pointer, too low to make sense\n"); | ||
911 | return (NULL); | ||
912 | } | ||
913 | |||
914 | if (index > last_index) { | ||
915 | wrtwarning("junk pointer, too high to make sense\n"); | ||
916 | return (NULL); | ||
917 | } | ||
918 | |||
919 | mp = &page_dir[index]; | ||
920 | |||
921 | if (*mp == MALLOC_FIRST) { /* Page allocation */ | ||
922 | |||
923 | /* Check the pointer */ | ||
924 | if ((u_long)ptr & malloc_pagemask) { | ||
925 | wrtwarning("modified (page-) pointer\n"); | ||
926 | return (NULL); | ||
927 | } | ||
928 | |||
929 | /* Find the size in bytes */ | ||
930 | for (osize = malloc_pagesize; *(++mp) == MALLOC_FOLLOW;) | ||
931 | osize += malloc_pagesize; | ||
932 | |||
933 | if (!malloc_realloc && /* Unless we have to, */ | ||
934 | size <= osize && /* .. or are too small, */ | ||
935 | size > (osize - malloc_pagesize)) { /* .. or can free a page, */ | ||
936 | if (malloc_junk) | ||
937 | memset((char *)ptr + size, SOME_JUNK, osize-size); | ||
938 | return (ptr); /* ..don't do anything else. */ | ||
939 | } | ||
940 | |||
941 | } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */ | ||
942 | |||
943 | /* Check the pointer for sane values */ | ||
944 | if ((u_long)ptr & ((1UL<<((*mp)->shift))-1)) { | ||
945 | wrtwarning("modified (chunk-) pointer\n"); | ||
946 | return (NULL); | ||
947 | } | ||
948 | |||
949 | /* Find the chunk index in the page */ | ||
950 | i = ((u_long)ptr & malloc_pagemask) >> (*mp)->shift; | ||
951 | |||
952 | /* Verify that it isn't a free chunk already */ | ||
953 | if ((*mp)->bits[i/MALLOC_BITS] & (1UL<<(i%MALLOC_BITS))) { | ||
954 | wrtwarning("chunk is already free\n"); | ||
955 | return (NULL); | ||
956 | } | ||
957 | |||
958 | osize = (*mp)->size; | ||
959 | |||
960 | if (!malloc_realloc && /* Unless we have to, */ | ||
961 | size <= osize && /* ..or are too small, */ | ||
962 | (size > osize/2 || /* ..or could use a smaller size, */ | ||
963 | osize == malloc_minsize)) { /* ..(if there is one) */ | ||
964 | if (malloc_junk) | ||
965 | memset((char *)ptr + size, SOME_JUNK, osize-size); | ||
966 | return (ptr); /* ..don't do anything else. */ | ||
967 | } | ||
968 | |||
969 | } else { | ||
970 | wrtwarning("pointer to wrong page\n"); | ||
971 | return (NULL); | ||
972 | } | ||
973 | |||
974 | p = imalloc(size); | ||
975 | |||
976 | if (p != NULL) { | ||
977 | /* copy the lesser of the two sizes, and free the old one */ | ||
978 | /* Don't move from/to 0 sized region !!! */ | ||
979 | if (osize != 0 && size != 0) { | ||
980 | if (osize < size) | ||
981 | memcpy(p, ptr, osize); | ||
982 | else | ||
983 | memcpy(p, ptr, size); | ||
984 | } | ||
985 | ifree(ptr); | ||
986 | } | ||
987 | return (p); | ||
988 | } | ||
989 | |||
990 | /* | ||
991 | * Free a sequence of pages | ||
992 | */ | ||
993 | |||
994 | static __inline__ void | ||
995 | free_pages(void *ptr, int index, struct pginfo *info) | ||
996 | { | ||
997 | int i; | ||
998 | struct pgfree *pf, *pt=NULL; | ||
999 | u_long l; | ||
1000 | void *tail; | ||
1001 | |||
1002 | if (info == MALLOC_FREE) { | ||
1003 | wrtwarning("page is already free\n"); | ||
1004 | return; | ||
1005 | } | ||
1006 | |||
1007 | if (info != MALLOC_FIRST) { | ||
1008 | wrtwarning("pointer to wrong page\n"); | ||
1009 | return; | ||
1010 | } | ||
1011 | |||
1012 | if ((u_long)ptr & malloc_pagemask) { | ||
1013 | wrtwarning("modified (page-) pointer\n"); | ||
1014 | return; | ||
1015 | } | ||
1016 | |||
1017 | /* Count how many pages and mark them free at the same time */ | ||
1018 | page_dir[index] = MALLOC_FREE; | ||
1019 | for (i = 1; page_dir[index+i] == MALLOC_FOLLOW; i++) | ||
1020 | page_dir[index + i] = MALLOC_FREE; | ||
1021 | |||
1022 | l = i << malloc_pageshift; | ||
1023 | |||
1024 | if (malloc_junk) | ||
1025 | memset(ptr, SOME_JUNK, l); | ||
1026 | |||
1027 | #if defined(__FreeBSD__) || (defined(__OpenBSD__) && defined(MADV_FREE)) | ||
1028 | if (malloc_hint) | ||
1029 | madvise(ptr, l, MADV_FREE); | ||
1030 | #endif | ||
1031 | |||
1032 | l += malloc_guard; | ||
1033 | tail = (char *)ptr+l; | ||
1034 | |||
1035 | if (malloc_freeprot) | ||
1036 | mprotect(ptr, tail - ptr, PROT_NONE); | ||
1037 | |||
1038 | /* add to free-list */ | ||
1039 | if (px == NULL) | ||
1040 | px = imalloc(sizeof *px); /* This cannot fail... */ | ||
1041 | px->page = ptr; | ||
1042 | px->end = tail; | ||
1043 | px->size = l; | ||
1044 | |||
1045 | if (free_list.next == NULL) { | ||
1046 | |||
1047 | /* Nothing on free list, put this at head */ | ||
1048 | px->next = free_list.next; | ||
1049 | px->prev = &free_list; | ||
1050 | free_list.next = px; | ||
1051 | pf = px; | ||
1052 | px = NULL; | ||
1053 | |||
1054 | } else { | ||
1055 | |||
1056 | /* Find the right spot, leave pf pointing to the modified entry. */ | ||
1057 | |||
1058 | for(pf = free_list.next; pf->end < ptr && pf->next != NULL; | ||
1059 | pf = pf->next) | ||
1060 | ; /* Race ahead here */ | ||
1061 | |||
1062 | if (pf->page > tail) { | ||
1063 | /* Insert before entry */ | ||
1064 | px->next = pf; | ||
1065 | px->prev = pf->prev; | ||
1066 | pf->prev = px; | ||
1067 | px->prev->next = px; | ||
1068 | pf = px; | ||
1069 | px = NULL; | ||
1070 | } else if (pf->end == ptr ) { | ||
1071 | /* Append to the previous entry */ | ||
1072 | pf->end = (char *)pf->end + l; | ||
1073 | pf->size += l; | ||
1074 | if (pf->next != NULL && pf->end == pf->next->page ) { | ||
1075 | /* And collapse the next too. */ | ||
1076 | pt = pf->next; | ||
1077 | pf->end = pt->end; | ||
1078 | pf->size += pt->size; | ||
1079 | pf->next = pt->next; | ||
1080 | if (pf->next != NULL) | ||
1081 | pf->next->prev = pf; | ||
1082 | } | ||
1083 | } else if (pf->page == tail) { | ||
1084 | /* Prepend to entry */ | ||
1085 | pf->size += l; | ||
1086 | pf->page = ptr; | ||
1087 | } else if (pf->next == NULL) { | ||
1088 | /* Append at tail of chain */ | ||
1089 | px->next = NULL; | ||
1090 | px->prev = pf; | ||
1091 | pf->next = px; | ||
1092 | pf = px; | ||
1093 | px = NULL; | ||
1094 | } else { | ||
1095 | wrterror("freelist is destroyed\n"); | ||
1096 | } | ||
1097 | } | ||
1098 | |||
1099 | /* Return something to OS ? */ | ||
1100 | if (pf->next == NULL && /* If we're the last one, */ | ||
1101 | pf->size > malloc_cache && /* ..and the cache is full, */ | ||
1102 | pf->end == malloc_brk && /* ..and none behind us, */ | ||
1103 | malloc_brk == sbrk(0)) { /* ..and it's OK to do... */ | ||
1104 | |||
1105 | /* | ||
1106 | * Keep the cache intact. Notice that the '>' above guarantees that | ||
1107 | * the pf will always have at least one page afterwards. | ||
1108 | */ | ||
1109 | pf->end = (char *)pf->page + malloc_cache; | ||
1110 | pf->size = malloc_cache; | ||
1111 | |||
1112 | brk(pf->end); | ||
1113 | malloc_brk = pf->end; | ||
1114 | |||
1115 | index = ptr2index(pf->end); | ||
1116 | |||
1117 | for(i=index;i <= last_index;) | ||
1118 | page_dir[i++] = MALLOC_NOT_MINE; | ||
1119 | |||
1120 | last_index = index - 1; | ||
1121 | |||
1122 | /* XXX: We could realloc/shrink the pagedir here I guess. */ | ||
1123 | } | ||
1124 | if (pt != NULL) | ||
1125 | ifree(pt); | ||
1126 | } | ||
1127 | |||
1128 | /* | ||
1129 | * Free a chunk, and possibly the page it's on, if the page becomes empty. | ||
1130 | */ | ||
1131 | |||
1132 | /* ARGSUSED */ | ||
1133 | static __inline__ void | ||
1134 | free_bytes(void *ptr, int index, struct pginfo *info) | ||
1135 | { | ||
1136 | int i; | ||
1137 | struct pginfo **mp; | ||
1138 | void *vp; | ||
1139 | |||
1140 | /* Find the chunk number on the page */ | ||
1141 | i = ((u_long)ptr & malloc_pagemask) >> info->shift; | ||
1142 | |||
1143 | if ((u_long)ptr & ((1UL<<(info->shift))-1)) { | ||
1144 | wrtwarning("modified (chunk-) pointer\n"); | ||
1145 | return; | ||
1146 | } | ||
1147 | |||
1148 | if (info->bits[i/MALLOC_BITS] & (1UL<<(i%MALLOC_BITS))) { | ||
1149 | wrtwarning("chunk is already free\n"); | ||
1150 | return; | ||
1151 | } | ||
1152 | |||
1153 | if (malloc_junk && info->size != 0) | ||
1154 | memset(ptr, SOME_JUNK, info->size); | ||
1155 | |||
1156 | info->bits[i/MALLOC_BITS] |= 1UL<<(i%MALLOC_BITS); | ||
1157 | info->free++; | ||
1158 | |||
1159 | if (info->size != 0) | ||
1160 | mp = page_dir + info->shift; | ||
1161 | else | ||
1162 | mp = page_dir; | ||
1163 | |||
1164 | if (info->free == 1) { | ||
1165 | |||
1166 | /* Page became non-full */ | ||
1167 | |||
1168 | /* Insert in address order */ | ||
1169 | while (*mp && (*mp)->next && (*mp)->next->page < info->page) | ||
1170 | mp = &(*mp)->next; | ||
1171 | info->next = *mp; | ||
1172 | *mp = info; | ||
1173 | return; | ||
1174 | } | ||
1175 | |||
1176 | if (info->free != info->total) | ||
1177 | return; | ||
1178 | |||
1179 | /* Find & remove this page in the queue */ | ||
1180 | while (*mp != info) { | ||
1181 | mp = &((*mp)->next); | ||
1182 | #ifdef MALLOC_EXTRA_SANITY | ||
1183 | if (!*mp) | ||
1184 | wrterror("(ES): Not on queue\n"); | ||
1185 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1186 | } | ||
1187 | *mp = info->next; | ||
1188 | |||
1189 | /* Free the page & the info structure if need be */ | ||
1190 | page_dir[ptr2index(info->page)] = MALLOC_FIRST; | ||
1191 | |||
1192 | /* If the page was mprotected, unprotect it before releasing it */ | ||
1193 | if (info->size == 0) { | ||
1194 | mprotect(info->page, malloc_pagesize, PROT_READ|PROT_WRITE); | ||
1195 | /* Do we have to care if mprotect succeeds here ? */ | ||
1196 | } | ||
1197 | |||
1198 | vp = info->page; /* Order is important ! */ | ||
1199 | if(vp != (void*)info) | ||
1200 | ifree(info); | ||
1201 | ifree(vp); | ||
1202 | } | ||
1203 | |||
1204 | static void | ||
1205 | ifree(void *ptr) | ||
1206 | { | ||
1207 | struct pginfo *info; | ||
1208 | int index; | ||
1209 | |||
1210 | /* This is legal */ | ||
1211 | if (ptr == NULL) | ||
1212 | return; | ||
1213 | |||
1214 | if (!malloc_started) { | ||
1215 | wrtwarning("malloc() has never been called\n"); | ||
1216 | return; | ||
1217 | } | ||
1218 | |||
1219 | /* If we're already sinking, don't make matters any worse. */ | ||
1220 | if (suicide) | ||
1221 | return; | ||
1222 | |||
1223 | index = ptr2index(ptr); | ||
1224 | |||
1225 | if (index < malloc_pageshift) { | ||
1226 | wrtwarning("junk pointer, too low to make sense\n"); | ||
1227 | return; | ||
1228 | } | ||
1229 | |||
1230 | if (index > last_index) { | ||
1231 | wrtwarning("junk pointer, too high to make sense\n"); | ||
1232 | return; | ||
1233 | } | ||
1234 | |||
1235 | info = page_dir[index]; | ||
1236 | |||
1237 | if (info < MALLOC_MAGIC) | ||
1238 | free_pages(ptr, index, info); | ||
1239 | else | ||
1240 | free_bytes(ptr, index, info); | ||
1241 | return; | ||
1242 | } | ||
1243 | |||
1244 | /* | ||
1245 | * These are the public exported interface routines. | ||
1246 | */ | ||
1247 | |||
1248 | static int malloc_active; | ||
1249 | |||
1250 | void * | ||
1251 | malloc(size_t size) | ||
1252 | { | ||
1253 | void *r; | ||
1254 | |||
1255 | _MALLOC_LOCK(); | ||
1256 | malloc_func = " in malloc():"; | ||
1257 | if (malloc_active++) { | ||
1258 | wrtwarning("recursive call\n"); | ||
1259 | malloc_active--; | ||
1260 | _MALLOC_UNLOCK(); | ||
1261 | errno = EDEADLK; | ||
1262 | return (NULL); | ||
1263 | } | ||
1264 | r = imalloc(size); | ||
1265 | UTRACE(0, size, r); | ||
1266 | malloc_active--; | ||
1267 | _MALLOC_UNLOCK(); | ||
1268 | if (malloc_xmalloc && r == NULL) | ||
1269 | wrterror("out of memory\n"); | ||
1270 | return (r); | ||
1271 | } | ||
1272 | |||
1273 | void | ||
1274 | free(void *ptr) | ||
1275 | { | ||
1276 | _MALLOC_LOCK(); | ||
1277 | malloc_func = " in free():"; | ||
1278 | if (malloc_active++) { | ||
1279 | wrtwarning("recursive call\n"); | ||
1280 | malloc_active--; | ||
1281 | _MALLOC_UNLOCK(); | ||
1282 | errno = EDEADLK; | ||
1283 | return; | ||
1284 | } | ||
1285 | ifree(ptr); | ||
1286 | UTRACE(ptr, 0, 0); | ||
1287 | malloc_active--; | ||
1288 | _MALLOC_UNLOCK(); | ||
1289 | return; | ||
1290 | } | ||
1291 | |||
1292 | void * | ||
1293 | realloc(void *ptr, size_t size) | ||
1294 | { | ||
1295 | void *r; | ||
1296 | |||
1297 | _MALLOC_LOCK(); | ||
1298 | malloc_func = " in realloc():"; | ||
1299 | if (malloc_active++) { | ||
1300 | wrtwarning("recursive call\n"); | ||
1301 | malloc_active--; | ||
1302 | _MALLOC_UNLOCK(); | ||
1303 | errno = EDEADLK; | ||
1304 | return (NULL); | ||
1305 | } | ||
1306 | if (ptr == NULL) { | ||
1307 | r = imalloc(size); | ||
1308 | } else { | ||
1309 | r = irealloc(ptr, size); | ||
1310 | } | ||
1311 | UTRACE(ptr, size, r); | ||
1312 | malloc_active--; | ||
1313 | _MALLOC_UNLOCK(); | ||
1314 | if (malloc_xmalloc && r == NULL) | ||
1315 | wrterror("out of memory\n"); | ||
1316 | return (r); | ||
1317 | } | ||
diff --git a/src/lib/libc/stdlib/memory.3 b/src/lib/libc/stdlib/memory.3 new file mode 100644 index 0000000000..84996f76c3 --- /dev/null +++ b/src/lib/libc/stdlib/memory.3 | |||
@@ -0,0 +1,65 @@ | |||
1 | .\" Copyright (c) 1991 Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: memory.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
29 | .\" | ||
30 | .Dd May 2, 1991 | ||
31 | .Dt MEMORY 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm malloc , | ||
35 | .Nm free , | ||
36 | .Nm realloc , | ||
37 | .Nm calloc , | ||
38 | .Nm alloca | ||
39 | .Nd general memory allocation operations | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft void * | ||
43 | .Fn malloc "size_t size" | ||
44 | .Ft void | ||
45 | .Fn free "void *ptr" | ||
46 | .Ft void * | ||
47 | .Fn realloc "void *ptr" "size_t size" | ||
48 | .Ft void * | ||
49 | .Fn calloc "size_t nelem" "size_t elsize" | ||
50 | .Ft void * | ||
51 | .Fn alloca "size_t size" | ||
52 | .Sh DESCRIPTION | ||
53 | These functions allocate and free memory for the calling process. | ||
54 | They are described in the individual man pages. | ||
55 | .Sh SEE ALSO | ||
56 | .Xr alloca 3 , | ||
57 | .Xr calloc 3 , | ||
58 | .Xr free 3 , | ||
59 | .Xr malloc 3 , | ||
60 | .Xr realloc 3 | ||
61 | .Sh STANDARDS | ||
62 | These functions, with the exception of | ||
63 | .Fn alloca , | ||
64 | conform to | ||
65 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c new file mode 100644 index 0000000000..1826acc369 --- /dev/null +++ b/src/lib/libc/stdlib/merge.c | |||
@@ -0,0 +1,343 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1992, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Peter McIlroy. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: merge.c,v 1.6 2003/06/02 20:18:38 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | /* | ||
38 | * Hybrid exponential search/linear search merge sort with hybrid | ||
39 | * natural/pairwise first pass. Requires about .3% more comparisons | ||
40 | * for random data than LSMS with pairwise first pass alone. | ||
41 | * It works for objects as small as two bytes. | ||
42 | */ | ||
43 | |||
44 | #define NATURAL | ||
45 | #define THRESHOLD 16 /* Best choice for natural merge cut-off. */ | ||
46 | |||
47 | /* #define NATURAL to get hybrid natural merge. | ||
48 | * (The default is pairwise merging.) | ||
49 | */ | ||
50 | |||
51 | #include <sys/types.h> | ||
52 | |||
53 | #include <errno.h> | ||
54 | #include <stdlib.h> | ||
55 | #include <string.h> | ||
56 | |||
57 | static void setup(u_char *, u_char *, size_t, size_t, int (*)()); | ||
58 | static void insertionsort(u_char *, size_t, size_t, int (*)()); | ||
59 | |||
60 | #define ISIZE sizeof(int) | ||
61 | #define PSIZE sizeof(u_char *) | ||
62 | #define ICOPY_LIST(src, dst, last) \ | ||
63 | do \ | ||
64 | *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ | ||
65 | while(src < last) | ||
66 | #define ICOPY_ELT(src, dst, i) \ | ||
67 | do \ | ||
68 | *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ | ||
69 | while (i -= ISIZE) | ||
70 | |||
71 | #define CCOPY_LIST(src, dst, last) \ | ||
72 | do \ | ||
73 | *dst++ = *src++; \ | ||
74 | while (src < last) | ||
75 | #define CCOPY_ELT(src, dst, i) \ | ||
76 | do \ | ||
77 | *dst++ = *src++; \ | ||
78 | while (i -= 1) | ||
79 | |||
80 | /* | ||
81 | * Find the next possible pointer head. (Trickery for forcing an array | ||
82 | * to do double duty as a linked list when objects do not align with word | ||
83 | * boundaries. | ||
84 | */ | ||
85 | /* Assumption: PSIZE is a power of 2. */ | ||
86 | #define EVAL(p) (u_char **) \ | ||
87 | ((u_char *)0 + \ | ||
88 | (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1))) | ||
89 | |||
90 | /* | ||
91 | * Arguments are as for qsort. | ||
92 | */ | ||
93 | int | ||
94 | mergesort(base, nmemb, size, cmp) | ||
95 | void *base; | ||
96 | size_t nmemb; | ||
97 | register size_t size; | ||
98 | int (*cmp)(const void *, const void *); | ||
99 | { | ||
100 | register int i, sense; | ||
101 | int big, iflag; | ||
102 | register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; | ||
103 | u_char *list2, *list1, *p2, *p, *last, **p1; | ||
104 | |||
105 | if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ | ||
106 | errno = EINVAL; | ||
107 | return (-1); | ||
108 | } | ||
109 | |||
110 | /* | ||
111 | * XXX | ||
112 | * Stupid subtraction for the Cray. | ||
113 | */ | ||
114 | iflag = 0; | ||
115 | if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) | ||
116 | iflag = 1; | ||
117 | |||
118 | if ((list2 = malloc(nmemb * size + PSIZE)) == NULL) | ||
119 | return (-1); | ||
120 | |||
121 | list1 = base; | ||
122 | setup(list1, list2, nmemb, size, cmp); | ||
123 | last = list2 + nmemb * size; | ||
124 | i = big = 0; | ||
125 | while (*EVAL(list2) != last) { | ||
126 | l2 = list1; | ||
127 | p1 = EVAL(list1); | ||
128 | for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { | ||
129 | p2 = *EVAL(p2); | ||
130 | f1 = l2; | ||
131 | f2 = l1 = list1 + (p2 - list2); | ||
132 | if (p2 != last) | ||
133 | p2 = *EVAL(p2); | ||
134 | l2 = list1 + (p2 - list2); | ||
135 | while (f1 < l1 && f2 < l2) { | ||
136 | if ((*cmp)(f1, f2) <= 0) { | ||
137 | q = f2; | ||
138 | b = f1, t = l1; | ||
139 | sense = -1; | ||
140 | } else { | ||
141 | q = f1; | ||
142 | b = f2, t = l2; | ||
143 | sense = 0; | ||
144 | } | ||
145 | if (!big) { /* here i = 0 */ | ||
146 | while ((b += size) < t && cmp(q, b) >sense) | ||
147 | if (++i == 6) { | ||
148 | big = 1; | ||
149 | goto EXPONENTIAL; | ||
150 | } | ||
151 | } else { | ||
152 | EXPONENTIAL: for (i = size; ; i <<= 1) | ||
153 | if ((p = (b + i)) >= t) { | ||
154 | if ((p = t - size) > b && | ||
155 | (*cmp)(q, p) <= sense) | ||
156 | t = p; | ||
157 | else | ||
158 | b = p; | ||
159 | break; | ||
160 | } else if ((*cmp)(q, p) <= sense) { | ||
161 | t = p; | ||
162 | if (i == size) | ||
163 | big = 0; | ||
164 | goto FASTCASE; | ||
165 | } else | ||
166 | b = p; | ||
167 | while (t > b+size) { | ||
168 | i = (((t - b) / size) >> 1) * size; | ||
169 | if ((*cmp)(q, p = b + i) <= sense) | ||
170 | t = p; | ||
171 | else | ||
172 | b = p; | ||
173 | } | ||
174 | goto COPY; | ||
175 | FASTCASE: while (i > size) | ||
176 | if ((*cmp)(q, | ||
177 | p = b + (i >>= 1)) <= sense) | ||
178 | t = p; | ||
179 | else | ||
180 | b = p; | ||
181 | COPY: b = t; | ||
182 | } | ||
183 | i = size; | ||
184 | if (q == f1) { | ||
185 | if (iflag) { | ||
186 | ICOPY_LIST(f2, tp2, b); | ||
187 | ICOPY_ELT(f1, tp2, i); | ||
188 | } else { | ||
189 | CCOPY_LIST(f2, tp2, b); | ||
190 | CCOPY_ELT(f1, tp2, i); | ||
191 | } | ||
192 | } else { | ||
193 | if (iflag) { | ||
194 | ICOPY_LIST(f1, tp2, b); | ||
195 | ICOPY_ELT(f2, tp2, i); | ||
196 | } else { | ||
197 | CCOPY_LIST(f1, tp2, b); | ||
198 | CCOPY_ELT(f2, tp2, i); | ||
199 | } | ||
200 | } | ||
201 | } | ||
202 | if (f2 < l2) { | ||
203 | if (iflag) | ||
204 | ICOPY_LIST(f2, tp2, l2); | ||
205 | else | ||
206 | CCOPY_LIST(f2, tp2, l2); | ||
207 | } else if (f1 < l1) { | ||
208 | if (iflag) | ||
209 | ICOPY_LIST(f1, tp2, l1); | ||
210 | else | ||
211 | CCOPY_LIST(f1, tp2, l1); | ||
212 | } | ||
213 | *p1 = l2; | ||
214 | } | ||
215 | tp2 = list1; /* swap list1, list2 */ | ||
216 | list1 = list2; | ||
217 | list2 = tp2; | ||
218 | last = list2 + nmemb*size; | ||
219 | } | ||
220 | if (base == list2) { | ||
221 | memmove(list2, list1, nmemb*size); | ||
222 | list2 = list1; | ||
223 | } | ||
224 | free(list2); | ||
225 | return (0); | ||
226 | } | ||
227 | |||
228 | #define swap(a, b) { \ | ||
229 | s = b; \ | ||
230 | i = size; \ | ||
231 | do { \ | ||
232 | tmp = *a; *a++ = *s; *s++ = tmp; \ | ||
233 | } while (--i); \ | ||
234 | a -= size; \ | ||
235 | } | ||
236 | #define reverse(bot, top) { \ | ||
237 | s = top; \ | ||
238 | do { \ | ||
239 | i = size; \ | ||
240 | do { \ | ||
241 | tmp = *bot; *bot++ = *s; *s++ = tmp; \ | ||
242 | } while (--i); \ | ||
243 | s -= size2; \ | ||
244 | } while(bot < s); \ | ||
245 | } | ||
246 | |||
247 | /* | ||
248 | * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of | ||
249 | * increasing order, list2 in a corresponding linked list. Checks for runs | ||
250 | * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL | ||
251 | * is defined. Otherwise simple pairwise merging is used.) | ||
252 | */ | ||
253 | void | ||
254 | setup(list1, list2, n, size, cmp) | ||
255 | size_t n, size; | ||
256 | int (*cmp)(const void *, const void *); | ||
257 | u_char *list1, *list2; | ||
258 | { | ||
259 | int i, length, size2, tmp, sense; | ||
260 | u_char *f1, *f2, *s, *l2, *last, *p2; | ||
261 | |||
262 | size2 = size*2; | ||
263 | if (n <= 5) { | ||
264 | insertionsort(list1, n, size, cmp); | ||
265 | *EVAL(list2) = (u_char*) list2 + n*size; | ||
266 | return; | ||
267 | } | ||
268 | /* | ||
269 | * Avoid running pointers out of bounds; limit n to evens | ||
270 | * for simplicity. | ||
271 | */ | ||
272 | i = 4 + (n & 1); | ||
273 | insertionsort(list1 + (n - i) * size, i, size, cmp); | ||
274 | last = list1 + size * (n - i); | ||
275 | *EVAL(list2 + (last - list1)) = list2 + n * size; | ||
276 | |||
277 | #ifdef NATURAL | ||
278 | p2 = list2; | ||
279 | f1 = list1; | ||
280 | sense = (cmp(f1, f1 + size) > 0); | ||
281 | for (; f1 < last; sense = !sense) { | ||
282 | length = 2; | ||
283 | /* Find pairs with same sense. */ | ||
284 | for (f2 = f1 + size2; f2 < last; f2 += size2) { | ||
285 | if ((cmp(f2, f2+ size) > 0) != sense) | ||
286 | break; | ||
287 | length += 2; | ||
288 | } | ||
289 | if (length < THRESHOLD) { /* Pairwise merge */ | ||
290 | do { | ||
291 | p2 = *EVAL(p2) = f1 + size2 - list1 + list2; | ||
292 | if (sense > 0) | ||
293 | swap (f1, f1 + size); | ||
294 | } while ((f1 += size2) < f2); | ||
295 | } else { /* Natural merge */ | ||
296 | l2 = f2; | ||
297 | for (f2 = f1 + size2; f2 < l2; f2 += size2) { | ||
298 | if ((cmp(f2-size, f2) > 0) != sense) { | ||
299 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
300 | if (sense > 0) | ||
301 | reverse(f1, f2-size); | ||
302 | f1 = f2; | ||
303 | } | ||
304 | } | ||
305 | if (sense > 0) | ||
306 | reverse (f1, f2-size); | ||
307 | f1 = f2; | ||
308 | if (f2 < last || cmp(f2 - size, f2) > 0) | ||
309 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
310 | else | ||
311 | p2 = *EVAL(p2) = list2 + n*size; | ||
312 | } | ||
313 | } | ||
314 | #else /* pairwise merge only. */ | ||
315 | for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { | ||
316 | p2 = *EVAL(p2) = p2 + size2; | ||
317 | if (cmp (f1, f1 + size) > 0) | ||
318 | swap(f1, f1 + size); | ||
319 | } | ||
320 | #endif /* NATURAL */ | ||
321 | } | ||
322 | |||
323 | /* | ||
324 | * This is to avoid out-of-bounds addresses in sorting the | ||
325 | * last 4 elements. | ||
326 | */ | ||
327 | static void | ||
328 | insertionsort(a, n, size, cmp) | ||
329 | u_char *a; | ||
330 | size_t n, size; | ||
331 | int (*cmp)(const void *, const void *); | ||
332 | { | ||
333 | u_char *ai, *s, *t, *u, tmp; | ||
334 | int i; | ||
335 | |||
336 | for (ai = a+size; --n >= 1; ai += size) | ||
337 | for (t = ai; t > a; t -= size) { | ||
338 | u = t - size; | ||
339 | if (cmp(u, t) <= 0) | ||
340 | break; | ||
341 | swap(u, t); | ||
342 | } | ||
343 | } | ||
diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c new file mode 100644 index 0000000000..cd34260b5c --- /dev/null +++ b/src/lib/libc/stdlib/mrand48.c | |||
@@ -0,0 +1,27 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: mrand48.c,v 1.2 1996/08/19 08:33:39 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | extern unsigned short __rand48_seed[3]; | ||
21 | |||
22 | long | ||
23 | mrand48(void) | ||
24 | { | ||
25 | __dorand48(__rand48_seed); | ||
26 | return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1]; | ||
27 | } | ||
diff --git a/src/lib/libc/stdlib/multibyte.c b/src/lib/libc/stdlib/multibyte.c new file mode 100644 index 0000000000..ba878b8fdc --- /dev/null +++ b/src/lib/libc/stdlib/multibyte.c | |||
@@ -0,0 +1,120 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1991 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: multibyte.c,v 1.4 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | /* | ||
37 | * Stub multibyte character functions. | ||
38 | * This cheezy implementation is fixed to the native single-byte | ||
39 | * character set. | ||
40 | */ | ||
41 | |||
42 | int | ||
43 | mblen(s, n) | ||
44 | const char *s; | ||
45 | size_t n; | ||
46 | { | ||
47 | if (s == NULL || *s == '\0') | ||
48 | return 0; | ||
49 | if (n == 0) | ||
50 | return -1; | ||
51 | return 1; | ||
52 | } | ||
53 | |||
54 | /*ARGSUSED*/ | ||
55 | int | ||
56 | mbtowc(pwc, s, n) | ||
57 | wchar_t *pwc; | ||
58 | const char *s; | ||
59 | size_t n; | ||
60 | { | ||
61 | if (s == NULL) | ||
62 | return 0; | ||
63 | if (n == 0) | ||
64 | return -1; | ||
65 | if (pwc) | ||
66 | *pwc = (wchar_t) *s; | ||
67 | return (*s != '\0'); | ||
68 | } | ||
69 | |||
70 | /*ARGSUSED*/ | ||
71 | int | ||
72 | wctomb(char *s, wchar_t wchar) | ||
73 | { | ||
74 | if (s == NULL) | ||
75 | return 0; | ||
76 | |||
77 | *s = (char) wchar; | ||
78 | return 1; | ||
79 | } | ||
80 | |||
81 | /*ARGSUSED*/ | ||
82 | size_t | ||
83 | mbstowcs(pwcs, s, n) | ||
84 | wchar_t *pwcs; | ||
85 | const char *s; | ||
86 | size_t n; | ||
87 | { | ||
88 | int count = 0; | ||
89 | |||
90 | if (n != 0) { | ||
91 | do { | ||
92 | if ((*pwcs++ = (wchar_t) *s++) == 0) | ||
93 | break; | ||
94 | count++; | ||
95 | } while (--n != 0); | ||
96 | } | ||
97 | |||
98 | return count; | ||
99 | } | ||
100 | |||
101 | /*ARGSUSED*/ | ||
102 | size_t | ||
103 | wcstombs(s, pwcs, n) | ||
104 | char *s; | ||
105 | const wchar_t *pwcs; | ||
106 | size_t n; | ||
107 | { | ||
108 | int count = 0; | ||
109 | |||
110 | if (n != 0) { | ||
111 | do { | ||
112 | if ((*s++ = (char) *pwcs++) == 0) | ||
113 | break; | ||
114 | count++; | ||
115 | } while (--n != 0); | ||
116 | } | ||
117 | |||
118 | return count; | ||
119 | } | ||
120 | |||
diff --git a/src/lib/libc/stdlib/nrand48.c b/src/lib/libc/stdlib/nrand48.c new file mode 100644 index 0000000000..b1ec2cebb1 --- /dev/null +++ b/src/lib/libc/stdlib/nrand48.c | |||
@@ -0,0 +1,25 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: nrand48.c,v 1.2 1996/08/19 08:33:40 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | long | ||
21 | nrand48(unsigned short xseed[3]) | ||
22 | { | ||
23 | __dorand48(xseed); | ||
24 | return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); | ||
25 | } | ||
diff --git a/src/lib/libc/stdlib/putenv.c b/src/lib/libc/stdlib/putenv.c new file mode 100644 index 0000000000..1789e686ef --- /dev/null +++ b/src/lib/libc/stdlib/putenv.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1988, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: putenv.c,v 1.3 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | int | ||
38 | putenv(str) | ||
39 | const char *str; | ||
40 | { | ||
41 | char *p, *equal; | ||
42 | int rval; | ||
43 | |||
44 | if ((p = strdup(str)) == NULL) | ||
45 | return (-1); | ||
46 | if ((equal = strchr(p, '=')) == NULL) { | ||
47 | (void)free(p); | ||
48 | return (-1); | ||
49 | } | ||
50 | *equal = '\0'; | ||
51 | rval = setenv(p, equal + 1, 1); | ||
52 | (void)free(p); | ||
53 | return (rval); | ||
54 | } | ||
diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3 new file mode 100644 index 0000000000..093212edf5 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.3 | |||
@@ -0,0 +1,56 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: qabs.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt QABS 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm qabs | ||
39 | .Nd return the absolute value of a quad integer | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft quad_t | ||
43 | .Fn qabs "quad_t j" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn qabs | ||
47 | function returns the absolute value of the quad integer | ||
48 | .Fa j . | ||
49 | .Sh SEE ALSO | ||
50 | .Xr abs 3 , | ||
51 | .Xr cabs 3 , | ||
52 | .Xr floor 3 , | ||
53 | .Xr labs 3 , | ||
54 | .Xr math 3 | ||
55 | .Sh BUGS | ||
56 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/qabs.c b/src/lib/libc/stdlib/qabs.c new file mode 100644 index 0000000000..296d2d4742 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: qabs.c,v 1.3 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | |||
36 | quad_t | ||
37 | qabs(j) | ||
38 | quad_t j; | ||
39 | { | ||
40 | return(j < 0 ? -j : j); | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3 new file mode 100644 index 0000000000..597f951a99 --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.3 | |||
@@ -0,0 +1,60 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: qdiv.3,v 1.5 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt QDIV 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm qdiv | ||
39 | .Nd return quotient and remainder from division | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft qdiv_t | ||
43 | .Fn qdiv "quad_t num" "quad_t denom" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn qdiv | ||
47 | function computes the value | ||
48 | .Fa num Ns No / Ns Fa denom | ||
49 | and returns the quotient and remainder in a structure named | ||
50 | .Li qdiv_t | ||
51 | that contains two | ||
52 | .Li quad integer | ||
53 | members named | ||
54 | .Fa quot | ||
55 | and | ||
56 | .Fa rem . | ||
57 | .Sh SEE ALSO | ||
58 | .Xr div 3 , | ||
59 | .Xr ldiv 3 , | ||
60 | .Xr math 3 | ||
diff --git a/src/lib/libc/stdlib/qdiv.c b/src/lib/libc/stdlib/qdiv.c new file mode 100644 index 0000000000..6688ccb712 --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1990 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: qdiv.c,v 1.3 2003/06/02 20:18:38 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <stdlib.h> /* qdiv_t */ | ||
38 | |||
39 | qdiv_t | ||
40 | qdiv(num, denom) | ||
41 | quad_t num, denom; | ||
42 | { | ||
43 | qdiv_t r; | ||
44 | |||
45 | /* see div.c for comments */ | ||
46 | |||
47 | r.quot = num / denom; | ||
48 | r.rem = num % denom; | ||
49 | if (num >= 0 && r.rem < 0) { | ||
50 | r.quot++; | ||
51 | r.rem -= denom; | ||
52 | } | ||
53 | return (r); | ||
54 | } | ||
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 new file mode 100644 index 0000000000..668ea90243 --- /dev/null +++ b/src/lib/libc/stdlib/qsort.3 | |||
@@ -0,0 +1,233 @@ | |||
1 | .\" Copyright (c) 1990, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: qsort.3,v 1.14 2003/10/01 08:11:58 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd June 4, 1993 | ||
35 | .Dt QSORT 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm qsort , | ||
39 | .Nm heapsort , | ||
40 | .Nm mergesort | ||
41 | .Nd sort functions | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Ft void | ||
45 | .Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
46 | .Ft int | ||
47 | .Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
48 | .Ft int | ||
49 | .Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
50 | .Sh DESCRIPTION | ||
51 | The | ||
52 | .Fn qsort | ||
53 | function is a modified partition-exchange sort, or quicksort. | ||
54 | The | ||
55 | .Fn heapsort | ||
56 | function is a modified selection sort. | ||
57 | The | ||
58 | .Fn mergesort | ||
59 | function is a modified merge sort with exponential search | ||
60 | intended for sorting data with pre-existing order. | ||
61 | .Pp | ||
62 | The | ||
63 | .Fn qsort | ||
64 | and | ||
65 | .Fn heapsort | ||
66 | functions sort an array of | ||
67 | .Fa nmemb | ||
68 | objects, the initial member of which is pointed to by | ||
69 | .Fa base . | ||
70 | The size of each object is specified by | ||
71 | .Fa size . | ||
72 | .Fn mergesort | ||
73 | behaves similarly, but | ||
74 | .Em requires | ||
75 | that | ||
76 | .Fa size | ||
77 | be greater than | ||
78 | .Dq "sizeof(void *) / 2" . | ||
79 | .Pp | ||
80 | The contents of the array | ||
81 | .Fa base | ||
82 | are sorted in ascending order according to | ||
83 | a comparison function pointed to by | ||
84 | .Fa compar , | ||
85 | which requires two arguments pointing to the objects being | ||
86 | compared. | ||
87 | .Pp | ||
88 | The comparison function must return an integer less than, equal to, or | ||
89 | greater than zero if the first argument is considered to be respectively | ||
90 | less than, equal to, or greater than the second. | ||
91 | .Pp | ||
92 | The functions | ||
93 | .Fn qsort | ||
94 | and | ||
95 | .Fn heapsort | ||
96 | are | ||
97 | .Em not | ||
98 | stable, that is, if two members compare as equal, their order in | ||
99 | the sorted array is undefined. | ||
100 | The function | ||
101 | .Fn mergesort | ||
102 | is stable. | ||
103 | .Pp | ||
104 | The | ||
105 | .Fn qsort | ||
106 | function is an implementation of C.A.R. Hoare's | ||
107 | .Dq quicksort | ||
108 | algorithm, | ||
109 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's | ||
110 | Algorithm Q. | ||
111 | .Fn qsort | ||
112 | takes O N lg N average time. | ||
113 | This implementation uses median selection to avoid its | ||
114 | O N**2 worst-case behavior. | ||
115 | .Pp | ||
116 | The | ||
117 | .Fn heapsort | ||
118 | function is an implementation of J.W.J. William's | ||
119 | .Dq heapsort | ||
120 | algorithm, | ||
121 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. | ||
122 | .Fn heapsort | ||
123 | takes O N lg N worst-case time. | ||
124 | This implementation of | ||
125 | .Fn heapsort | ||
126 | is implemented without recursive function calls. | ||
127 | .Pp | ||
128 | The function | ||
129 | .Fn mergesort | ||
130 | requires additional memory of size | ||
131 | .Fa nmemb * | ||
132 | .Fa size | ||
133 | bytes; it should be used only when space is not at a premium. | ||
134 | .Fn mergesort | ||
135 | is optimized for data with pre-existing order; its worst case | ||
136 | time is O N lg N; its best case is O N. | ||
137 | .Pp | ||
138 | Normally, | ||
139 | .Fn qsort | ||
140 | is faster than | ||
141 | .Fn mergesort , | ||
142 | which is faster than | ||
143 | .Fn heapsort . | ||
144 | Memory availability and pre-existing order in the data can make this untrue. | ||
145 | .Sh RETURN VALUES | ||
146 | The | ||
147 | .Fn qsort | ||
148 | function returns no value. | ||
149 | .Pp | ||
150 | Upon successful completion, | ||
151 | .Fn heapsort | ||
152 | and | ||
153 | .Fn mergesort | ||
154 | return 0. | ||
155 | Otherwise, they return \-1 and the global variable | ||
156 | .Va errno | ||
157 | is set to indicate the error. | ||
158 | .Sh ERRORS | ||
159 | The | ||
160 | .Fn heapsort | ||
161 | and | ||
162 | .Fn mergesort | ||
163 | functions succeed unless: | ||
164 | .Bl -tag -width Er | ||
165 | .It Bq Er EINVAL | ||
166 | The | ||
167 | .Fa size | ||
168 | argument is zero, or the | ||
169 | .Fa size | ||
170 | argument to | ||
171 | .Fn mergesort | ||
172 | is less than | ||
173 | .Dq "sizeof(void *) / 2" . | ||
174 | .It Bq Er ENOMEM | ||
175 | .Fn heapsort | ||
176 | or | ||
177 | .Fn mergesort | ||
178 | were unable to allocate memory. | ||
179 | .El | ||
180 | .Sh SEE ALSO | ||
181 | .Xr sort 1 , | ||
182 | .Xr radixsort 3 | ||
183 | .Rs | ||
184 | .%A Hoare, C.A.R. | ||
185 | .%D 1962 | ||
186 | .%T "Quicksort" | ||
187 | .%J "The Computer Journal" | ||
188 | .%V 5:1 | ||
189 | .%P pp. 10-15 | ||
190 | .Re | ||
191 | .Rs | ||
192 | .%A Williams, J.W.J | ||
193 | .%D 1964 | ||
194 | .%T "Heapsort" | ||
195 | .%J "Communications of the ACM" | ||
196 | .%V 7:1 | ||
197 | .%P pp. 347\-348 | ||
198 | .Re | ||
199 | .Rs | ||
200 | .%A Knuth, D.E. | ||
201 | .%D 1968 | ||
202 | .%B "The Art of Computer Programming" | ||
203 | .%V Vol. 3 | ||
204 | .%T "Sorting and Searching" | ||
205 | .%P pp. 114\-123, 145\-149 | ||
206 | .Re | ||
207 | .Rs | ||
208 | .%A McIlroy, P.M. | ||
209 | .%T "Optimistic Sorting and Information Theoretic Complexity" | ||
210 | .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" | ||
211 | .%P pp. 467\-464 | ||
212 | .%D January 1993 | ||
213 | .Re | ||
214 | .Rs | ||
215 | .%A Bentley, J.L. | ||
216 | .%A McIlroy, M.D. | ||
217 | .%T "Engineering a Sort Function" | ||
218 | .%J "Software \- Practice and Experience" | ||
219 | .%V Vol. 23(11) | ||
220 | .%P pp. 1249\-1265 | ||
221 | .%D November 1993 | ||
222 | .Re | ||
223 | .Sh STANDARDS | ||
224 | Previous versions of | ||
225 | .Fn qsort | ||
226 | did not permit the comparison routine itself to call | ||
227 | .Fn qsort . | ||
228 | This is no longer true. | ||
229 | .Pp | ||
230 | The | ||
231 | .Fn qsort | ||
232 | function conforms to | ||
233 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c new file mode 100644 index 0000000000..2a972c0eb0 --- /dev/null +++ b/src/lib/libc/stdlib/qsort.c | |||
@@ -0,0 +1,171 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1992, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: qsort.c,v 1.8 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <stdlib.h> | ||
36 | |||
37 | static __inline char *med3(char *, char *, char *, int (*)()); | ||
38 | static __inline void swapfunc(char *, char *, int, int); | ||
39 | |||
40 | #define min(a, b) (a) < (b) ? a : b | ||
41 | |||
42 | /* | ||
43 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". | ||
44 | */ | ||
45 | #define swapcode(TYPE, parmi, parmj, n) { \ | ||
46 | long i = (n) / sizeof (TYPE); \ | ||
47 | register TYPE *pi = (TYPE *) (parmi); \ | ||
48 | register TYPE *pj = (TYPE *) (parmj); \ | ||
49 | do { \ | ||
50 | register TYPE t = *pi; \ | ||
51 | *pi++ = *pj; \ | ||
52 | *pj++ = t; \ | ||
53 | } while (--i > 0); \ | ||
54 | } | ||
55 | |||
56 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ | ||
57 | es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; | ||
58 | |||
59 | static __inline void | ||
60 | swapfunc(a, b, n, swaptype) | ||
61 | char *a, *b; | ||
62 | int n, swaptype; | ||
63 | { | ||
64 | if (swaptype <= 1) | ||
65 | swapcode(long, a, b, n) | ||
66 | else | ||
67 | swapcode(char, a, b, n) | ||
68 | } | ||
69 | |||
70 | #define swap(a, b) \ | ||
71 | if (swaptype == 0) { \ | ||
72 | long t = *(long *)(a); \ | ||
73 | *(long *)(a) = *(long *)(b); \ | ||
74 | *(long *)(b) = t; \ | ||
75 | } else \ | ||
76 | swapfunc(a, b, es, swaptype) | ||
77 | |||
78 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) | ||
79 | |||
80 | static __inline char * | ||
81 | med3(a, b, c, cmp) | ||
82 | char *a, *b, *c; | ||
83 | int (*cmp)(); | ||
84 | { | ||
85 | return cmp(a, b) < 0 ? | ||
86 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) | ||
87 | :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); | ||
88 | } | ||
89 | |||
90 | void | ||
91 | qsort(aa, n, es, cmp) | ||
92 | void *aa; | ||
93 | size_t n, es; | ||
94 | int (*cmp)(); | ||
95 | { | ||
96 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | ||
97 | int d, r, swaptype, swap_cnt; | ||
98 | register char *a = aa; | ||
99 | |||
100 | loop: SWAPINIT(a, es); | ||
101 | swap_cnt = 0; | ||
102 | if (n < 7) { | ||
103 | for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) | ||
104 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
105 | pl -= es) | ||
106 | swap(pl, pl - es); | ||
107 | return; | ||
108 | } | ||
109 | pm = (char *)a + (n / 2) * es; | ||
110 | if (n > 7) { | ||
111 | pl = (char *)a; | ||
112 | pn = (char *)a + (n - 1) * es; | ||
113 | if (n > 40) { | ||
114 | d = (n / 8) * es; | ||
115 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | ||
116 | pm = med3(pm - d, pm, pm + d, cmp); | ||
117 | pn = med3(pn - 2 * d, pn - d, pn, cmp); | ||
118 | } | ||
119 | pm = med3(pl, pm, pn, cmp); | ||
120 | } | ||
121 | swap(a, pm); | ||
122 | pa = pb = (char *)a + es; | ||
123 | |||
124 | pc = pd = (char *)a + (n - 1) * es; | ||
125 | for (;;) { | ||
126 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { | ||
127 | if (r == 0) { | ||
128 | swap_cnt = 1; | ||
129 | swap(pa, pb); | ||
130 | pa += es; | ||
131 | } | ||
132 | pb += es; | ||
133 | } | ||
134 | while (pb <= pc && (r = cmp(pc, a)) >= 0) { | ||
135 | if (r == 0) { | ||
136 | swap_cnt = 1; | ||
137 | swap(pc, pd); | ||
138 | pd -= es; | ||
139 | } | ||
140 | pc -= es; | ||
141 | } | ||
142 | if (pb > pc) | ||
143 | break; | ||
144 | swap(pb, pc); | ||
145 | swap_cnt = 1; | ||
146 | pb += es; | ||
147 | pc -= es; | ||
148 | } | ||
149 | if (swap_cnt == 0) { /* Switch to insertion sort */ | ||
150 | for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) | ||
151 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
152 | pl -= es) | ||
153 | swap(pl, pl - es); | ||
154 | return; | ||
155 | } | ||
156 | |||
157 | pn = (char *)a + n * es; | ||
158 | r = min(pa - (char *)a, pb - pa); | ||
159 | vecswap(a, pb - r, r); | ||
160 | r = min(pd - pc, pn - pd - es); | ||
161 | vecswap(pb, pn - r, r); | ||
162 | if ((r = pb - pa) > es) | ||
163 | qsort(a, r / es, es, cmp); | ||
164 | if ((r = pd - pc) > es) { | ||
165 | /* Iterate rather than recurse to save stack space */ | ||
166 | a = pn - r; | ||
167 | n = r / es; | ||
168 | goto loop; | ||
169 | } | ||
170 | /* qsort(pn - r, r / es, es, cmp);*/ | ||
171 | } | ||
diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3 new file mode 100644 index 0000000000..028837d4d1 --- /dev/null +++ b/src/lib/libc/stdlib/radixsort.3 | |||
@@ -0,0 +1,155 @@ | |||
1 | .\" Copyright (c) 1990, 1991, 1993 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: radixsort.3,v 1.9 2003/06/02 20:18:38 millert Exp $ | ||
29 | .\" | ||
30 | .Dd January 27, 1994 | ||
31 | .Dt RADIXSORT 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm radixsort , | ||
35 | .Nm sradixsort | ||
36 | .Nd radix sort | ||
37 | .Sh SYNOPSIS | ||
38 | .Fd #include <limits.h> | ||
39 | .Fd #include <stdlib.h> | ||
40 | .Ft int | ||
41 | .Fn radixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte" | ||
42 | .Ft int | ||
43 | .Fn sradixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn radixsort | ||
47 | and | ||
48 | .Fn sradixsort | ||
49 | functions are implementations of radix sort. | ||
50 | .Pp | ||
51 | These functions sort an array of | ||
52 | .Fa nmemb | ||
53 | pointers to byte strings. | ||
54 | The initial member is referenced by | ||
55 | .Fa base . | ||
56 | The byte strings may contain any values; the end of each string | ||
57 | is denoted by the user-specified value | ||
58 | .Fa endbyte . | ||
59 | .Pp | ||
60 | Applications may specify a sort order by providing the | ||
61 | .Fa table | ||
62 | argument. | ||
63 | If non-null, | ||
64 | .Fa table | ||
65 | must reference an array of | ||
66 | .Dv UCHAR_MAX | ||
67 | + 1 bytes which contains the sort weight of each possible byte value. | ||
68 | The end-of-string byte must have a sort weight of 0 or 255 | ||
69 | (for sorting in reverse order). | ||
70 | More than one byte may have the same sort weight. | ||
71 | The | ||
72 | .Fa table | ||
73 | argument is useful for applications which wish to sort different characters | ||
74 | equally, for example, providing a table with the same weights | ||
75 | for A-Z as for a-z will result in a case-insensitive sort. | ||
76 | If | ||
77 | .Fa table | ||
78 | is | ||
79 | .Dv NULL , | ||
80 | the contents of the array are sorted in ascending order according to the | ||
81 | .Tn ASCII | ||
82 | order of the byte strings they reference and | ||
83 | .Fa endbyte | ||
84 | has a sorting weight of 0. | ||
85 | .Pp | ||
86 | The | ||
87 | .Fn sradixsort | ||
88 | function is stable; that is, if two elements compare as equal, their | ||
89 | order in the sorted array is unchanged. | ||
90 | The | ||
91 | .Fn sradixsort | ||
92 | function uses additional memory sufficient to hold | ||
93 | .Fa nmemb | ||
94 | pointers. | ||
95 | .Pp | ||
96 | The | ||
97 | .Fn radixsort | ||
98 | function is not stable, but uses no additional memory. | ||
99 | .Pp | ||
100 | These functions are variants of most-significant-byte radix sorting; in | ||
101 | particular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10. | ||
102 | They take linear time relative to the number of bytes in the strings. | ||
103 | .Sh RETURN VALUES | ||
104 | Upon successful completion 0 is returned. | ||
105 | Otherwise, \-1 is returned and the global variable | ||
106 | .Va errno | ||
107 | is set to indicate the error. | ||
108 | .Sh ERRORS | ||
109 | .Bl -tag -width Er | ||
110 | .It Bq Er EINVAL | ||
111 | The value of the | ||
112 | .Fa endbyte | ||
113 | element of | ||
114 | .Fa table | ||
115 | is not 0 or 255. | ||
116 | .El | ||
117 | .Pp | ||
118 | Additionally, the | ||
119 | .Fn sradixsort | ||
120 | function may fail and set | ||
121 | .Va errno | ||
122 | for any of the errors specified for the library routine | ||
123 | .Xr malloc 3 . | ||
124 | .Sh SEE ALSO | ||
125 | .Xr sort 1 , | ||
126 | .Xr qsort 3 | ||
127 | .Rs | ||
128 | .%A Knuth, D.E. | ||
129 | .%D 1968 | ||
130 | .%B "The Art of Computer Programming" | ||
131 | .%T "Sorting and Searching" | ||
132 | .%V Vol. 3 | ||
133 | .%P pp. 170-178 | ||
134 | .Re | ||
135 | .Rs | ||
136 | .%A Paige, R. | ||
137 | .%D 1987 | ||
138 | .%T "Three Partition Refinement Algorithms" | ||
139 | .%J "SIAM J. Comput." | ||
140 | .%V Vol. 16 | ||
141 | .%N No. 6 | ||
142 | .Re | ||
143 | .Rs | ||
144 | .%A McIlroy, P. | ||
145 | .%D 1993 | ||
146 | .%B "Engineering Radix Sort" | ||
147 | .%T "Computing Systems" | ||
148 | .%V Vol. 6:1 | ||
149 | .%P pp. 5-27 | ||
150 | .Re | ||
151 | .Sh HISTORY | ||
152 | The | ||
153 | .Fn radixsort | ||
154 | function first appeared in | ||
155 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/stdlib/radixsort.c b/src/lib/libc/stdlib/radixsort.c new file mode 100644 index 0000000000..1ff30416d9 --- /dev/null +++ b/src/lib/libc/stdlib/radixsort.c | |||
@@ -0,0 +1,313 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990, 1993 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Peter McIlroy and by Dan Bernstein at New York University, | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: radixsort.c,v 1.6 2003/06/02 20:18:38 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | /* | ||
38 | * Radixsort routines. | ||
39 | * | ||
40 | * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. | ||
41 | * Use radixsort(a, n, trace, endchar) for this case. | ||
42 | * | ||
43 | * For stable sorting (using N extra pointers) use sradixsort(), which calls | ||
44 | * r_sort_b(). | ||
45 | * | ||
46 | * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, | ||
47 | * "Engineering Radix Sort". | ||
48 | */ | ||
49 | |||
50 | #include <sys/types.h> | ||
51 | #include <stdlib.h> | ||
52 | #include <errno.h> | ||
53 | |||
54 | typedef struct { | ||
55 | const u_char **sa; | ||
56 | int sn, si; | ||
57 | } stack; | ||
58 | |||
59 | static __inline void simplesort | ||
60 | (const u_char **, int, int, const u_char *, u_int); | ||
61 | static void r_sort_a(const u_char **, int, int, const u_char *, u_int); | ||
62 | static void r_sort_b(const u_char **, | ||
63 | const u_char **, int, int, const u_char *, u_int); | ||
64 | |||
65 | #define THRESHOLD 20 /* Divert to simplesort(). */ | ||
66 | #define SIZE 512 /* Default stack size. */ | ||
67 | |||
68 | #define SETUP { \ | ||
69 | if (tab == NULL) { \ | ||
70 | tr = tr0; \ | ||
71 | for (c = 0; c < endch; c++) \ | ||
72 | tr0[c] = c + 1; \ | ||
73 | tr0[c] = 0; \ | ||
74 | for (c++; c < 256; c++) \ | ||
75 | tr0[c] = c; \ | ||
76 | endch = 0; \ | ||
77 | } else { \ | ||
78 | endch = tab[endch]; \ | ||
79 | tr = tab; \ | ||
80 | if (endch != 0 && endch != 255) { \ | ||
81 | errno = EINVAL; \ | ||
82 | return (-1); \ | ||
83 | } \ | ||
84 | } \ | ||
85 | } | ||
86 | |||
87 | int | ||
88 | radixsort(a, n, tab, endch) | ||
89 | const u_char **a, *tab; | ||
90 | int n; | ||
91 | u_int endch; | ||
92 | { | ||
93 | const u_char *tr; | ||
94 | int c; | ||
95 | u_char tr0[256]; | ||
96 | |||
97 | SETUP; | ||
98 | r_sort_a(a, n, 0, tr, endch); | ||
99 | return (0); | ||
100 | } | ||
101 | |||
102 | int | ||
103 | sradixsort(a, n, tab, endch) | ||
104 | const u_char **a, *tab; | ||
105 | int n; | ||
106 | u_int endch; | ||
107 | { | ||
108 | const u_char *tr, **ta; | ||
109 | int c; | ||
110 | u_char tr0[256]; | ||
111 | |||
112 | SETUP; | ||
113 | if (n < THRESHOLD) | ||
114 | simplesort(a, n, 0, tr, endch); | ||
115 | else { | ||
116 | if ((ta = malloc(n * sizeof(a))) == NULL) | ||
117 | return (-1); | ||
118 | r_sort_b(a, ta, n, 0, tr, endch); | ||
119 | free(ta); | ||
120 | } | ||
121 | return (0); | ||
122 | } | ||
123 | |||
124 | #define empty(s) (s >= sp) | ||
125 | #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si | ||
126 | #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i | ||
127 | #define swap(a, b, t) t = a, a = b, b = t | ||
128 | |||
129 | /* Unstable, in-place sort. */ | ||
130 | void | ||
131 | r_sort_a(a, n, i, tr, endch) | ||
132 | const u_char **a; | ||
133 | int n, i; | ||
134 | const u_char *tr; | ||
135 | u_int endch; | ||
136 | { | ||
137 | static int count[256], nc, bmin; | ||
138 | register int c; | ||
139 | register const u_char **ak, *r; | ||
140 | stack s[SIZE], *sp, *sp0, *sp1, temp; | ||
141 | int *cp, bigc; | ||
142 | const u_char **an, *t, **aj, **top[256]; | ||
143 | |||
144 | /* Set up stack. */ | ||
145 | sp = s; | ||
146 | push(a, n, i); | ||
147 | while (!empty(s)) { | ||
148 | pop(a, n, i); | ||
149 | if (n < THRESHOLD) { | ||
150 | simplesort(a, n, i, tr, endch); | ||
151 | continue; | ||
152 | } | ||
153 | an = a + n; | ||
154 | |||
155 | /* Make character histogram. */ | ||
156 | if (nc == 0) { | ||
157 | bmin = 255; /* First occupied bin, excluding eos. */ | ||
158 | for (ak = a; ak < an;) { | ||
159 | c = tr[(*ak++)[i]]; | ||
160 | if (++count[c] == 1 && c != endch) { | ||
161 | if (c < bmin) | ||
162 | bmin = c; | ||
163 | nc++; | ||
164 | } | ||
165 | } | ||
166 | if (sp + nc > s + SIZE) { /* Get more stack. */ | ||
167 | r_sort_a(a, n, i, tr, endch); | ||
168 | continue; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | /* | ||
173 | * Set top[]; push incompletely sorted bins onto stack. | ||
174 | * top[] = pointers to last out-of-place element in bins. | ||
175 | * count[] = counts of elements in bins. | ||
176 | * Before permuting: top[c-1] + count[c] = top[c]; | ||
177 | * during deal: top[c] counts down to top[c-1]. | ||
178 | */ | ||
179 | sp0 = sp1 = sp; /* Stack position of biggest bin. */ | ||
180 | bigc = 2; /* Size of biggest bin. */ | ||
181 | if (endch == 0) /* Special case: set top[eos]. */ | ||
182 | top[0] = ak = a + count[0]; | ||
183 | else { | ||
184 | ak = a; | ||
185 | top[255] = an; | ||
186 | } | ||
187 | for (cp = count + bmin; nc > 0; cp++) { | ||
188 | while (*cp == 0) /* Find next non-empty pile. */ | ||
189 | cp++; | ||
190 | if (*cp > 1) { | ||
191 | if (*cp > bigc) { | ||
192 | bigc = *cp; | ||
193 | sp1 = sp; | ||
194 | } | ||
195 | push(ak, *cp, i+1); | ||
196 | } | ||
197 | top[cp-count] = ak += *cp; | ||
198 | nc--; | ||
199 | } | ||
200 | swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ | ||
201 | |||
202 | /* | ||
203 | * Permute misplacements home. Already home: everything | ||
204 | * before aj, and in bin[c], items from top[c] on. | ||
205 | * Inner loop: | ||
206 | * r = next element to put in place; | ||
207 | * ak = top[r[i]] = location to put the next element. | ||
208 | * aj = bottom of 1st disordered bin. | ||
209 | * Outer loop: | ||
210 | * Once the 1st disordered bin is done, ie. aj >= ak, | ||
211 | * aj<-aj + count[c] connects the bins in a linked list; | ||
212 | * reset count[c]. | ||
213 | */ | ||
214 | for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) | ||
215 | for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) | ||
216 | swap(*ak, r, t); | ||
217 | } | ||
218 | } | ||
219 | |||
220 | /* Stable sort, requiring additional memory. */ | ||
221 | void | ||
222 | r_sort_b(a, ta, n, i, tr, endch) | ||
223 | const u_char **a, **ta; | ||
224 | int n, i; | ||
225 | const u_char *tr; | ||
226 | u_int endch; | ||
227 | { | ||
228 | static int count[256], nc, bmin; | ||
229 | register int c; | ||
230 | register const u_char **ak, **ai; | ||
231 | stack s[512], *sp, *sp0, *sp1, temp; | ||
232 | const u_char **top[256]; | ||
233 | int *cp, bigc; | ||
234 | |||
235 | sp = s; | ||
236 | push(a, n, i); | ||
237 | while (!empty(s)) { | ||
238 | pop(a, n, i); | ||
239 | if (n < THRESHOLD) { | ||
240 | simplesort(a, n, i, tr, endch); | ||
241 | continue; | ||
242 | } | ||
243 | |||
244 | if (nc == 0) { | ||
245 | bmin = 255; | ||
246 | for (ak = a + n; --ak >= a;) { | ||
247 | c = tr[(*ak)[i]]; | ||
248 | if (++count[c] == 1 && c != endch) { | ||
249 | if (c < bmin) | ||
250 | bmin = c; | ||
251 | nc++; | ||
252 | } | ||
253 | } | ||
254 | if (sp + nc > s + SIZE) { | ||
255 | r_sort_b(a, ta, n, i, tr, endch); | ||
256 | continue; | ||
257 | } | ||
258 | } | ||
259 | |||
260 | sp0 = sp1 = sp; | ||
261 | bigc = 2; | ||
262 | if (endch == 0) { | ||
263 | top[0] = ak = a + count[0]; | ||
264 | count[0] = 0; | ||
265 | } else { | ||
266 | ak = a; | ||
267 | top[255] = a + n; | ||
268 | count[255] = 0; | ||
269 | } | ||
270 | for (cp = count + bmin; nc > 0; cp++) { | ||
271 | while (*cp == 0) | ||
272 | cp++; | ||
273 | if ((c = *cp) > 1) { | ||
274 | if (c > bigc) { | ||
275 | bigc = c; | ||
276 | sp1 = sp; | ||
277 | } | ||
278 | push(ak, c, i+1); | ||
279 | } | ||
280 | top[cp-count] = ak += c; | ||
281 | *cp = 0; /* Reset count[]. */ | ||
282 | nc--; | ||
283 | } | ||
284 | swap(*sp0, *sp1, temp); | ||
285 | |||
286 | for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ | ||
287 | *--ak = *--ai; | ||
288 | for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ | ||
289 | *--top[tr[(*ak)[i]]] = *ak; | ||
290 | } | ||
291 | } | ||
292 | |||
293 | static __inline void | ||
294 | simplesort(a, n, b, tr, endch) /* insertion sort */ | ||
295 | register const u_char **a; | ||
296 | int n, b; | ||
297 | register const u_char *tr; | ||
298 | u_int endch; | ||
299 | { | ||
300 | register u_char ch; | ||
301 | const u_char **ak, **ai, *s, *t; | ||
302 | |||
303 | for (ak = a+1; --n >= 1; ak++) | ||
304 | for (ai = ak; ai > a; ai--) { | ||
305 | for (s = ai[0] + b, t = ai[-1] + b; | ||
306 | (ch = tr[*s]) != endch; s++, t++) | ||
307 | if (ch != tr[*t]) | ||
308 | break; | ||
309 | if (ch >= tr[*t]) | ||
310 | break; | ||
311 | swap(ai[0], ai[-1], s); | ||
312 | } | ||
313 | } | ||
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3 new file mode 100644 index 0000000000..0d9c52a3b9 --- /dev/null +++ b/src/lib/libc/stdlib/rand.3 | |||
@@ -0,0 +1,98 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: rand.3,v 1.9 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt RAND 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm rand , | ||
39 | .Nm srand | ||
40 | .Nd bad random number generator | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <stdlib.h> | ||
43 | .Ft void | ||
44 | .Fn srand "unsigned int seed" | ||
45 | .Ft int | ||
46 | .Fn rand void | ||
47 | .Ft int | ||
48 | .Fn rand_r "unsigned int *seed" | ||
49 | .Sh DESCRIPTION | ||
50 | .Bf -symbolic | ||
51 | These interfaces are obsoleted by | ||
52 | .Xr random 3 . | ||
53 | .Ef | ||
54 | .Pp | ||
55 | The | ||
56 | .Fn rand | ||
57 | function computes a sequence of pseudo-random integers in the range | ||
58 | of 0 to | ||
59 | .Dv RAND_MAX | ||
60 | (as defined by the header file | ||
61 | .Aq Pa stdlib.h ) . | ||
62 | .Pp | ||
63 | The | ||
64 | .Fn srand | ||
65 | function sets its argument as the seed for a new sequence of | ||
66 | pseudo-random numbers to be returned by | ||
67 | .Fn rand . | ||
68 | These sequences are repeatable by calling | ||
69 | .Fn srand | ||
70 | with the same seed value. | ||
71 | .Pp | ||
72 | If no seed value is provided, the functions are automatically | ||
73 | seeded with a value of 1. | ||
74 | .Pp | ||
75 | The | ||
76 | .Fn rand_r | ||
77 | is a thread-safe version of | ||
78 | .Fn rand . | ||
79 | Storage for the seed must be provided through the | ||
80 | .Fa seed | ||
81 | argument, and needs to have been initialized by the caller. | ||
82 | .Sh SEE ALSO | ||
83 | .Xr arc4random 3 , | ||
84 | .Xr rand48 3 , | ||
85 | .Xr random 3 | ||
86 | .Sh STANDARDS | ||
87 | The | ||
88 | .Fn rand | ||
89 | and | ||
90 | .Fn srand | ||
91 | functions conform to | ||
92 | .St -ansiC . | ||
93 | .Pp | ||
94 | The | ||
95 | .Fn rand_r | ||
96 | function conforms to ISO/IEC 9945-1 ANSI/IEEE | ||
97 | .Pq Dq Tn POSIX | ||
98 | Std 1003.1c Draft 10. | ||
diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c new file mode 100644 index 0000000000..d58d040ad5 --- /dev/null +++ b/src/lib/libc/stdlib/rand.c | |||
@@ -0,0 +1,61 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: rand.c,v 1.7 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <stdlib.h> | ||
36 | |||
37 | static u_int next = 1; | ||
38 | |||
39 | int | ||
40 | rand_r(seed) | ||
41 | u_int *seed; | ||
42 | { | ||
43 | |||
44 | *seed = *seed * 1103515245 + 12345; | ||
45 | return (*seed % ((u_int)RAND_MAX + 1)); | ||
46 | } | ||
47 | |||
48 | int | ||
49 | rand() | ||
50 | { | ||
51 | |||
52 | return (rand_r(&next)); | ||
53 | } | ||
54 | |||
55 | void | ||
56 | srand(seed) | ||
57 | u_int seed; | ||
58 | { | ||
59 | |||
60 | next = seed; | ||
61 | } | ||
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 new file mode 100644 index 0000000000..78cd0a7cd5 --- /dev/null +++ b/src/lib/libc/stdlib/rand48.3 | |||
@@ -0,0 +1,165 @@ | |||
1 | .\" Copyright (c) 1993 Martin Birgmeier | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" You may redistribute unmodified or modified versions of this source | ||
5 | .\" code provided that the above copyright notice and this and the | ||
6 | .\" following conditions are retained. | ||
7 | .\" | ||
8 | .\" This software is provided ``as is'', and comes with no warranties | ||
9 | .\" of any kind. I shall in no event be liable for anything that happens | ||
10 | .\" to anyone/anything when using this software. | ||
11 | .\" | ||
12 | .\" $OpenBSD: rand48.3,v 1.10 2003/06/01 19:27:27 jmc Exp $ | ||
13 | .\" | ||
14 | .Dd October 8, 1993 | ||
15 | .Dt RAND48 3 | ||
16 | .Os | ||
17 | .Sh NAME | ||
18 | .Nm drand48 , | ||
19 | .Nm erand48 , | ||
20 | .Nm lrand48 , | ||
21 | .Nm nrand48 , | ||
22 | .Nm mrand48 , | ||
23 | .Nm jrand48 , | ||
24 | .Nm srand48 , | ||
25 | .Nm seed48 , | ||
26 | .Nm lcong48 | ||
27 | .Nd pseudo-random number generators and initialization routines | ||
28 | .Sh SYNOPSIS | ||
29 | .Fd #include <stdlib.h> | ||
30 | .Ft double | ||
31 | .Fn drand48 void | ||
32 | .Ft double | ||
33 | .Fn erand48 "unsigned short xseed[3]" | ||
34 | .Ft long | ||
35 | .Fn lrand48 void | ||
36 | .Ft long | ||
37 | .Fn nrand48 "unsigned short xseed[3]" | ||
38 | .Ft long | ||
39 | .Fn mrand48 void | ||
40 | .Ft long | ||
41 | .Fn jrand48 "unsigned short xseed[3]" | ||
42 | .Ft void | ||
43 | .Fn srand48 "long seed" | ||
44 | .Ft "unsigned short *" | ||
45 | .Fn seed48 "unsigned short xseed[3]" | ||
46 | .Ft void | ||
47 | .Fn lcong48 "unsigned short p[7]" | ||
48 | .Sh DESCRIPTION | ||
49 | The | ||
50 | .Fn rand48 | ||
51 | family of functions generates pseudo-random numbers using a linear | ||
52 | congruential algorithm working on integers 48 bits in size. | ||
53 | The particular formula employed is | ||
54 | r(n+1) = (a * r(n) + c) mod m | ||
55 | where the default values are | ||
56 | for the multiplicand a = 0xfdeece66d = 25214903917 and | ||
57 | the addend c = 0xb = 11. | ||
58 | The modulus is always fixed at m = 2 ** 48. | ||
59 | r(n) is called the seed of the random number generator. | ||
60 | .Pp | ||
61 | For all the six generator routines described next, the first | ||
62 | computational step is to perform a single iteration of the algorithm. | ||
63 | .Pp | ||
64 | .Fn drand48 | ||
65 | and | ||
66 | .Fn erand48 | ||
67 | return values of type double. | ||
68 | The full 48 bits of r(n+1) are | ||
69 | loaded into the mantissa of the returned value, with the exponent set | ||
70 | such that the values produced lie in the interval [0.0, 1.0]. | ||
71 | .Pp | ||
72 | .Fn lrand48 | ||
73 | and | ||
74 | .Fn nrand48 | ||
75 | return values of type long in the range | ||
76 | [0, 2**31-1]. | ||
77 | The high-order (31) bits of | ||
78 | r(n+1) are loaded into the lower bits of the returned value, with | ||
79 | the topmost (sign) bit set to zero. | ||
80 | .Pp | ||
81 | .Fn mrand48 | ||
82 | and | ||
83 | .Fn jrand48 | ||
84 | return values of type long in the range | ||
85 | [-2**31, 2**31-1]. | ||
86 | The high-order (32) bits of r(n+1) are loaded into the returned value. | ||
87 | .Pp | ||
88 | .Fn drand48 , | ||
89 | .Fn lrand48 , | ||
90 | and | ||
91 | .Fn mrand48 | ||
92 | use an internal buffer to store r(n). | ||
93 | For these functions | ||
94 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. | ||
95 | .Pp | ||
96 | On the other hand, | ||
97 | .Fn erand48 , | ||
98 | .Fn nrand48 , | ||
99 | and | ||
100 | .Fn jrand48 | ||
101 | use a user-supplied buffer to store the seed r(n), | ||
102 | which consists of an array of 3 shorts, where the zeroth member | ||
103 | holds the least significant bits. | ||
104 | .Pp | ||
105 | All functions share the same multiplicand and addend. | ||
106 | .Pp | ||
107 | .Fn srand48 | ||
108 | is used to initialize the internal buffer r(n) of | ||
109 | .Fn drand48 , | ||
110 | .Fn lrand48 , | ||
111 | and | ||
112 | .Fn mrand48 | ||
113 | such that the 32 bits of the seed value are copied into the upper 32 bits | ||
114 | of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. | ||
115 | Additionally, the constant multiplicand and addend of the algorithm are | ||
116 | reset to the default values given above. | ||
117 | .Pp | ||
118 | .Fn seed48 | ||
119 | also initializes the internal buffer r(n) of | ||
120 | .Fn drand48 , | ||
121 | .Fn lrand48 , | ||
122 | and | ||
123 | .Fn mrand48 , | ||
124 | but here all 48 bits of the seed can be specified in an array of 3 shorts, | ||
125 | where the zeroth member specifies the lowest bits. | ||
126 | Again, the constant multiplicand and addend of the algorithm are | ||
127 | reset to the default values given above. | ||
128 | .Fn seed48 | ||
129 | returns a pointer to an array of 3 shorts which contains the old seed. | ||
130 | This array is statically allocated, so its contents are lost after | ||
131 | each new call to | ||
132 | .Fn seed48 . | ||
133 | .Pp | ||
134 | Finally, | ||
135 | .Fn lcong48 | ||
136 | allows full control over the multiplicand and addend used in | ||
137 | .Fn drand48 , | ||
138 | .Fn erand48 , | ||
139 | .Fn lrand48 , | ||
140 | .Fn nrand48 , | ||
141 | .Fn mrand48 , | ||
142 | and | ||
143 | .Fn jrand48 , | ||
144 | and the seed used in | ||
145 | .Fn drand48 , | ||
146 | .Fn lrand48 , | ||
147 | and | ||
148 | .Fn mrand48 . | ||
149 | An array of 7 shorts is passed as parameter; the first three shorts are | ||
150 | used to initialize the seed; the second three are used to initialize the | ||
151 | multiplicand; and the last short is used to initialize the addend. | ||
152 | It is thus not possible to use values greater than 0xffff as the addend. | ||
153 | .Pp | ||
154 | Note that all three methods of seeding the random number generator | ||
155 | always also set the multiplicand and addend for any of the six | ||
156 | generator calls. | ||
157 | .Pp | ||
158 | For a more powerful random number generator, see | ||
159 | .Xr random 3 . | ||
160 | .Sh SEE ALSO | ||
161 | .Xr arc4random 3 , | ||
162 | .Xr rand 3 , | ||
163 | .Xr random 3 | ||
164 | .Sh AUTHORS | ||
165 | Martin Birgmeier | ||
diff --git a/src/lib/libc/stdlib/rand48.h b/src/lib/libc/stdlib/rand48.h new file mode 100644 index 0000000000..afa49f65f3 --- /dev/null +++ b/src/lib/libc/stdlib/rand48.h | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | * | ||
13 | * $OpenBSD: rand48.h,v 1.3 2002/02/16 21:27:24 millert Exp $ | ||
14 | */ | ||
15 | |||
16 | #ifndef _RAND48_H_ | ||
17 | #define _RAND48_H_ | ||
18 | |||
19 | #include <math.h> | ||
20 | #include <stdlib.h> | ||
21 | |||
22 | void __dorand48(unsigned short[3]); | ||
23 | |||
24 | #define RAND48_SEED_0 (0x330e) | ||
25 | #define RAND48_SEED_1 (0xabcd) | ||
26 | #define RAND48_SEED_2 (0x1234) | ||
27 | #define RAND48_MULT_0 (0xe66d) | ||
28 | #define RAND48_MULT_1 (0xdeec) | ||
29 | #define RAND48_MULT_2 (0x0005) | ||
30 | #define RAND48_ADD (0x000b) | ||
31 | |||
32 | #endif /* _RAND48_H_ */ | ||
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3 new file mode 100644 index 0000000000..f43f06420d --- /dev/null +++ b/src/lib/libc/stdlib/random.3 | |||
@@ -0,0 +1,186 @@ | |||
1 | .\" Copyright (c) 1983, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: random.3,v 1.17 2003/06/02 20:18:38 millert Exp $ | ||
29 | .\" | ||
30 | .Dd April 19, 1991 | ||
31 | .Dt RANDOM 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm random , | ||
35 | .Nm srandom , | ||
36 | .Nm srandomdev , | ||
37 | .Nm initstate , | ||
38 | .Nm setstate | ||
39 | .Nd better random number generator; routines for changing generators | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft long | ||
43 | .Fn random void | ||
44 | .Ft void | ||
45 | .Fn srandom "unsigned int seed" | ||
46 | .Ft void | ||
47 | .Fn srandomdev void | ||
48 | .Ft char * | ||
49 | .Fn initstate "unsigned int seed" "char *state" "size_t n" | ||
50 | .Ft char * | ||
51 | .Fn setstate "const char *state" | ||
52 | .Sh DESCRIPTION | ||
53 | The | ||
54 | .Fn random | ||
55 | function uses a non-linear additive feedback random number generator employing | ||
56 | a default table of size 31 long integers to return successive pseudo-random | ||
57 | numbers in the range from 0 to (2**31)\-1. | ||
58 | The period of this random number generator is very large, approximately | ||
59 | 16*((2**31)\-1). | ||
60 | .Pp | ||
61 | The | ||
62 | .Fn random | ||
63 | and | ||
64 | .Fn srandom | ||
65 | functions have (almost) the same calling sequence and initialization | ||
66 | properties as | ||
67 | .Xr rand 3 Ns / Ns Xr srand 3 . | ||
68 | The difference is that | ||
69 | .Xr rand | ||
70 | produces a much less random sequence \(em in fact, the low dozen bits | ||
71 | generated by rand go through a cyclic pattern. | ||
72 | All the bits generated by | ||
73 | .Fn random | ||
74 | are usable. | ||
75 | For example, | ||
76 | .Sq Li random()&01 | ||
77 | will produce a random binary | ||
78 | value. | ||
79 | .Pp | ||
80 | Like | ||
81 | .Xr rand 3 , | ||
82 | .Fn random | ||
83 | will by default produce a sequence of numbers that can be duplicated | ||
84 | by calling | ||
85 | .Fn srandom | ||
86 | with | ||
87 | .Ql 1 | ||
88 | as the seed. | ||
89 | .Pp | ||
90 | The | ||
91 | .Fn srandomdev | ||
92 | routine initializes a state array using the | ||
93 | .Xr arandom 4 | ||
94 | random number device which returns good random numbers, | ||
95 | suitable for cryptographic use. | ||
96 | Note that this particular seeding procedure can generate | ||
97 | states which are impossible to reproduce by calling | ||
98 | .Fn srandom | ||
99 | with any value, since the succeeding terms in the | ||
100 | state buffer are no longer derived from the LC algorithm applied to | ||
101 | a fixed seed. | ||
102 | .Pp | ||
103 | The | ||
104 | .Fn initstate | ||
105 | routine allows a state array, passed in as an argument, to be initialized | ||
106 | for future use. | ||
107 | The size of the state array (in bytes) is used by | ||
108 | .Fn initstate | ||
109 | to decide how sophisticated a random number generator it should use \(em the | ||
110 | more state, the better the random numbers will be. | ||
111 | (Current "optimal" values for the amount of state information are | ||
112 | 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to | ||
113 | the nearest known amount. | ||
114 | Using less than 8 bytes will cause an error.) | ||
115 | The seed for the initialization (which specifies a starting point for | ||
116 | the random number sequence, and provides for restarting at the same | ||
117 | point) is also an argument. | ||
118 | The | ||
119 | .Fn initstate | ||
120 | function returns a pointer to the previous state information array. | ||
121 | .Pp | ||
122 | Once a state has been initialized, the | ||
123 | .Fn setstate | ||
124 | routine provides for rapid switching between states. | ||
125 | The | ||
126 | .Fn setstate | ||
127 | function returns a pointer to the previous state array; its | ||
128 | argument state array is used for further random number generation | ||
129 | until the next call to | ||
130 | .Fn initstate | ||
131 | or | ||
132 | .Fn setstate . | ||
133 | .Pp | ||
134 | Once a state array has been initialized, it may be restarted at a | ||
135 | different point either by calling | ||
136 | .Fn initstate | ||
137 | (with the desired seed, the state array, and its size) or by calling | ||
138 | both | ||
139 | .Fn setstate | ||
140 | (with the state array) and | ||
141 | .Fn srandom | ||
142 | (with the desired seed). | ||
143 | The advantage of calling both | ||
144 | .Fn setstate | ||
145 | and | ||
146 | .Fn srandom | ||
147 | is that the size of the state array does not have to be remembered after | ||
148 | it is initialized. | ||
149 | .Pp | ||
150 | With 256 bytes of state information, the period of the random number | ||
151 | generator is greater than 2**69 | ||
152 | which should be sufficient for most purposes. | ||
153 | .Sh DIAGNOSTICS | ||
154 | If | ||
155 | .Fn initstate | ||
156 | is called with less than 8 bytes of state information, or if | ||
157 | .Fn setstate | ||
158 | detects that the state information has been garbled, error | ||
159 | messages are printed on the standard error output. | ||
160 | .Sh SEE ALSO | ||
161 | .Xr arc4random 3 , | ||
162 | .Xr drand48 3 , | ||
163 | .Xr rand 3 , | ||
164 | .Xr random 4 | ||
165 | .Sh STANDARDS | ||
166 | The | ||
167 | .Fn random , | ||
168 | .Fn srandom , | ||
169 | .Fn initstate , | ||
170 | and | ||
171 | .Fn setstate | ||
172 | functions conform to | ||
173 | .St -xpg4.2 . | ||
174 | .Pp | ||
175 | The | ||
176 | .Fn srandomdev | ||
177 | function is an extension. | ||
178 | .Sh HISTORY | ||
179 | These | ||
180 | functions appeared in | ||
181 | .Bx 4.2 . | ||
182 | .Sh AUTHORS | ||
183 | .An Earl T. Cohen | ||
184 | .Sh BUGS | ||
185 | About 2/3 the speed of | ||
186 | .Xr rand 3 . | ||
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c new file mode 100644 index 0000000000..4807d2f27d --- /dev/null +++ b/src/lib/libc/stdlib/random.c | |||
@@ -0,0 +1,430 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1983 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: random.c,v 1.12 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/param.h> | ||
35 | #include <sys/sysctl.h> | ||
36 | #include <sys/time.h> | ||
37 | #include <fcntl.h> | ||
38 | #include <stdio.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <unistd.h> | ||
41 | |||
42 | /* | ||
43 | * random.c: | ||
44 | * | ||
45 | * An improved random number generation package. In addition to the standard | ||
46 | * rand()/srand() like interface, this package also has a special state info | ||
47 | * interface. The initstate() routine is called with a seed, an array of | ||
48 | * bytes, and a count of how many bytes are being passed in; this array is | ||
49 | * then initialized to contain information for random number generation with | ||
50 | * that much state information. Good sizes for the amount of state | ||
51 | * information are 32, 64, 128, and 256 bytes. The state can be switched by | ||
52 | * calling the setstate() routine with the same array as was initiallized | ||
53 | * with initstate(). By default, the package runs with 128 bytes of state | ||
54 | * information and generates far better random numbers than a linear | ||
55 | * congruential generator. If the amount of state information is less than | ||
56 | * 32 bytes, a simple linear congruential R.N.G. is used. | ||
57 | * | ||
58 | * Internally, the state information is treated as an array of int32_t; the | ||
59 | * zeroeth element of the array is the type of R.N.G. being used (small | ||
60 | * integer); the remainder of the array is the state information for the | ||
61 | * R.N.G. Thus, 32 bytes of state information will give 7 int32_ts worth of | ||
62 | * state information, which will allow a degree seven polynomial. (Note: | ||
63 | * the zeroeth word of state information also has some other information | ||
64 | * stored in it -- see setstate() for details). | ||
65 | * | ||
66 | * The random number generation technique is a linear feedback shift register | ||
67 | * approach, employing trinomials (since there are fewer terms to sum up that | ||
68 | * way). In this approach, the least significant bit of all the numbers in | ||
69 | * the state table will act as a linear feedback shift register, and will | ||
70 | * have period 2^deg - 1 (where deg is the degree of the polynomial being | ||
71 | * used, assuming that the polynomial is irreducible and primitive). The | ||
72 | * higher order bits will have longer periods, since their values are also | ||
73 | * influenced by pseudo-random carries out of the lower bits. The total | ||
74 | * period of the generator is approximately deg*(2**deg - 1); thus doubling | ||
75 | * the amount of state information has a vast influence on the period of the | ||
76 | * generator. Note: the deg*(2**deg - 1) is an approximation only good for | ||
77 | * large deg, when the period of the shift register is the dominant factor. | ||
78 | * With deg equal to seven, the period is actually much longer than the | ||
79 | * 7*(2**7 - 1) predicted by this formula. | ||
80 | */ | ||
81 | |||
82 | /* | ||
83 | * For each of the currently supported random number generators, we have a | ||
84 | * break value on the amount of state information (you need at least this | ||
85 | * many bytes of state info to support this random number generator), a degree | ||
86 | * for the polynomial (actually a trinomial) that the R.N.G. is based on, and | ||
87 | * the separation between the two lower order coefficients of the trinomial. | ||
88 | */ | ||
89 | #define TYPE_0 0 /* linear congruential */ | ||
90 | #define BREAK_0 8 | ||
91 | #define DEG_0 0 | ||
92 | #define SEP_0 0 | ||
93 | |||
94 | #define TYPE_1 1 /* x**7 + x**3 + 1 */ | ||
95 | #define BREAK_1 32 | ||
96 | #define DEG_1 7 | ||
97 | #define SEP_1 3 | ||
98 | |||
99 | #define TYPE_2 2 /* x**15 + x + 1 */ | ||
100 | #define BREAK_2 64 | ||
101 | #define DEG_2 15 | ||
102 | #define SEP_2 1 | ||
103 | |||
104 | #define TYPE_3 3 /* x**31 + x**3 + 1 */ | ||
105 | #define BREAK_3 128 | ||
106 | #define DEG_3 31 | ||
107 | #define SEP_3 3 | ||
108 | |||
109 | #define TYPE_4 4 /* x**63 + x + 1 */ | ||
110 | #define BREAK_4 256 | ||
111 | #define DEG_4 63 | ||
112 | #define SEP_4 1 | ||
113 | |||
114 | /* | ||
115 | * Array versions of the above information to make code run faster -- | ||
116 | * relies on fact that TYPE_i == i. | ||
117 | */ | ||
118 | #define MAX_TYPES 5 /* max number of types above */ | ||
119 | |||
120 | static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; | ||
121 | static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; | ||
122 | |||
123 | /* | ||
124 | * Initially, everything is set up as if from: | ||
125 | * | ||
126 | * initstate(1, &randtbl, 128); | ||
127 | * | ||
128 | * Note that this initialization takes advantage of the fact that srandom() | ||
129 | * advances the front and rear pointers 10*rand_deg times, and hence the | ||
130 | * rear pointer which starts at 0 will also end up at zero; thus the zeroeth | ||
131 | * element of the state information, which contains info about the current | ||
132 | * position of the rear pointer is just | ||
133 | * | ||
134 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. | ||
135 | */ | ||
136 | |||
137 | static int32_t randtbl[DEG_3 + 1] = { | ||
138 | TYPE_3, | ||
139 | 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, | ||
140 | 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, | ||
141 | 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471, | ||
142 | 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1, | ||
143 | 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41, | ||
144 | 0xf3bec5da, | ||
145 | }; | ||
146 | |||
147 | /* | ||
148 | * fptr and rptr are two pointers into the state info, a front and a rear | ||
149 | * pointer. These two pointers are always rand_sep places aparts, as they | ||
150 | * cycle cyclically through the state information. (Yes, this does mean we | ||
151 | * could get away with just one pointer, but the code for random() is more | ||
152 | * efficient this way). The pointers are left positioned as they would be | ||
153 | * from the call | ||
154 | * | ||
155 | * initstate(1, randtbl, 128); | ||
156 | * | ||
157 | * (The position of the rear pointer, rptr, is really 0 (as explained above | ||
158 | * in the initialization of randtbl) because the state table pointer is set | ||
159 | * to point to randtbl[1] (as explained below). | ||
160 | */ | ||
161 | static int32_t *fptr = &randtbl[SEP_3 + 1]; | ||
162 | static int32_t *rptr = &randtbl[1]; | ||
163 | |||
164 | /* | ||
165 | * The following things are the pointer to the state information table, the | ||
166 | * type of the current generator, the degree of the current polynomial being | ||
167 | * used, and the separation between the two pointers. Note that for efficiency | ||
168 | * of random(), we remember the first location of the state information, not | ||
169 | * the zeroeth. Hence it is valid to access state[-1], which is used to | ||
170 | * store the type of the R.N.G. Also, we remember the last location, since | ||
171 | * this is more efficient than indexing every time to find the address of | ||
172 | * the last element to see if the front and rear pointers have wrapped. | ||
173 | */ | ||
174 | static int32_t *state = &randtbl[1]; | ||
175 | static int32_t *end_ptr = &randtbl[DEG_3 + 1]; | ||
176 | static int rand_type = TYPE_3; | ||
177 | static int rand_deg = DEG_3; | ||
178 | static int rand_sep = SEP_3; | ||
179 | |||
180 | /* | ||
181 | * srandom: | ||
182 | * | ||
183 | * Initialize the random number generator based on the given seed. If the | ||
184 | * type is the trivial no-state-information type, just remember the seed. | ||
185 | * Otherwise, initializes state[] based on the given "seed" via a linear | ||
186 | * congruential generator. Then, the pointers are set to known locations | ||
187 | * that are exactly rand_sep places apart. Lastly, it cycles the state | ||
188 | * information a given number of times to get rid of any initial dependencies | ||
189 | * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] | ||
190 | * for default usage relies on values produced by this routine. | ||
191 | */ | ||
192 | void | ||
193 | srandom(x) | ||
194 | unsigned int x; | ||
195 | { | ||
196 | int i; | ||
197 | int32_t test; | ||
198 | div_t val; | ||
199 | |||
200 | if (rand_type == TYPE_0) | ||
201 | state[0] = x; | ||
202 | else { | ||
203 | state[0] = x; | ||
204 | for (i = 1; i < rand_deg; i++) { | ||
205 | /* | ||
206 | * Implement the following, without overflowing 31 bits: | ||
207 | * | ||
208 | * state[i] = (16807 * state[i - 1]) % 2147483647; | ||
209 | * | ||
210 | * 2^31-1 (prime) = 2147483647 = 127773*16807+2836 | ||
211 | */ | ||
212 | val = div(state[i-1], 127773); | ||
213 | test = 16807 * val.rem - 2836 * val.quot; | ||
214 | state[i] = test + (test < 0 ? 2147483647 : 0); | ||
215 | } | ||
216 | fptr = &state[rand_sep]; | ||
217 | rptr = &state[0]; | ||
218 | for (i = 0; i < 10 * rand_deg; i++) | ||
219 | (void)random(); | ||
220 | } | ||
221 | } | ||
222 | |||
223 | /* | ||
224 | * srandomdev: | ||
225 | * | ||
226 | * Many programs choose the seed value in a totally predictable manner. | ||
227 | * This often causes problems. We seed the generator using the much more | ||
228 | * secure arandom(4) interface. Note that this particular seeding | ||
229 | * procedure can generate states which are impossible to reproduce by | ||
230 | * calling srandom() with any value, since the succeeding terms in the | ||
231 | * state buffer are no longer derived from the LC algorithm applied to | ||
232 | * a fixed seed. | ||
233 | */ | ||
234 | void | ||
235 | srandomdev() | ||
236 | { | ||
237 | int fd, i, mib[2], n; | ||
238 | size_t len; | ||
239 | |||
240 | if (rand_type == TYPE_0) | ||
241 | len = sizeof(state[0]); | ||
242 | else | ||
243 | len = rand_deg * sizeof(state[0]); | ||
244 | |||
245 | /* | ||
246 | * To get seed data, first try reading from /dev/arandom. | ||
247 | * If that fails, try the KERN_ARND sysctl() (one int at a time). | ||
248 | * As a last resort, call srandom(). | ||
249 | */ | ||
250 | if ((fd = open("/dev/arandom", O_RDONLY, 0)) != -1 && | ||
251 | read(fd, (void *) state, len) == (ssize_t) len) { | ||
252 | close(fd); | ||
253 | } else { | ||
254 | if (fd != -1) | ||
255 | close(fd); | ||
256 | mib[0] = CTL_KERN; | ||
257 | mib[1] = KERN_ARND; | ||
258 | n = len / sizeof(int); | ||
259 | len = sizeof(int); | ||
260 | for (i = 0; i < n; i++) { | ||
261 | if (sysctl(mib, 2, (char *)((int *)state + i), &len, | ||
262 | NULL, 0) == -1) | ||
263 | break; | ||
264 | } | ||
265 | if (i != n) { | ||
266 | struct timeval tv; | ||
267 | u_int junk; | ||
268 | |||
269 | /* XXX - this could be better */ | ||
270 | gettimeofday(&tv, NULL); | ||
271 | srandom(getpid() ^ tv.tv_sec ^ tv.tv_usec ^ junk); | ||
272 | return; | ||
273 | } | ||
274 | } | ||
275 | |||
276 | if (rand_type != TYPE_0) { | ||
277 | fptr = &state[rand_sep]; | ||
278 | rptr = &state[0]; | ||
279 | } | ||
280 | } | ||
281 | |||
282 | /* | ||
283 | * initstate: | ||
284 | * | ||
285 | * Initialize the state information in the given array of n bytes for future | ||
286 | * random number generation. Based on the number of bytes we are given, and | ||
287 | * the break values for the different R.N.G.'s, we choose the best (largest) | ||
288 | * one we can and set things up for it. srandom() is then called to | ||
289 | * initialize the state information. | ||
290 | * | ||
291 | * Note that on return from srandom(), we set state[-1] to be the type | ||
292 | * multiplexed with the current value of the rear pointer; this is so | ||
293 | * successive calls to initstate() won't lose this information and will be | ||
294 | * able to restart with setstate(). | ||
295 | * | ||
296 | * Note: the first thing we do is save the current state, if any, just like | ||
297 | * setstate() so that it doesn't matter when initstate is called. | ||
298 | * | ||
299 | * Returns a pointer to the old state. | ||
300 | */ | ||
301 | char * | ||
302 | initstate(seed, arg_state, n) | ||
303 | u_int seed; /* seed for R.N.G. */ | ||
304 | char *arg_state; /* pointer to state array */ | ||
305 | size_t n; /* # bytes of state info */ | ||
306 | { | ||
307 | char *ostate = (char *)(&state[-1]); | ||
308 | |||
309 | if (rand_type == TYPE_0) | ||
310 | state[-1] = rand_type; | ||
311 | else | ||
312 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
313 | if (n < BREAK_0) | ||
314 | return(NULL); | ||
315 | if (n < BREAK_1) { | ||
316 | rand_type = TYPE_0; | ||
317 | rand_deg = DEG_0; | ||
318 | rand_sep = SEP_0; | ||
319 | } else if (n < BREAK_2) { | ||
320 | rand_type = TYPE_1; | ||
321 | rand_deg = DEG_1; | ||
322 | rand_sep = SEP_1; | ||
323 | } else if (n < BREAK_3) { | ||
324 | rand_type = TYPE_2; | ||
325 | rand_deg = DEG_2; | ||
326 | rand_sep = SEP_2; | ||
327 | } else if (n < BREAK_4) { | ||
328 | rand_type = TYPE_3; | ||
329 | rand_deg = DEG_3; | ||
330 | rand_sep = SEP_3; | ||
331 | } else { | ||
332 | rand_type = TYPE_4; | ||
333 | rand_deg = DEG_4; | ||
334 | rand_sep = SEP_4; | ||
335 | } | ||
336 | state = &(((int32_t *)arg_state)[1]); /* first location */ | ||
337 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ | ||
338 | srandom(seed); | ||
339 | if (rand_type == TYPE_0) | ||
340 | state[-1] = rand_type; | ||
341 | else | ||
342 | state[-1] = MAX_TYPES*(rptr - state) + rand_type; | ||
343 | return(ostate); | ||
344 | } | ||
345 | |||
346 | /* | ||
347 | * setstate: | ||
348 | * | ||
349 | * Restore the state from the given state array. | ||
350 | * | ||
351 | * Note: it is important that we also remember the locations of the pointers | ||
352 | * in the current state information, and restore the locations of the pointers | ||
353 | * from the old state information. This is done by multiplexing the pointer | ||
354 | * location into the zeroeth word of the state information. | ||
355 | * | ||
356 | * Note that due to the order in which things are done, it is OK to call | ||
357 | * setstate() with the same state as the current state. | ||
358 | * | ||
359 | * Returns a pointer to the old state information. | ||
360 | */ | ||
361 | char * | ||
362 | setstate(arg_state) | ||
363 | const char *arg_state; | ||
364 | { | ||
365 | int32_t *new_state = (int32_t *)arg_state; | ||
366 | int32_t type = new_state[0] % MAX_TYPES; | ||
367 | int32_t rear = new_state[0] / MAX_TYPES; | ||
368 | char *ostate = (char *)(&state[-1]); | ||
369 | |||
370 | if (rand_type == TYPE_0) | ||
371 | state[-1] = rand_type; | ||
372 | else | ||
373 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
374 | switch(type) { | ||
375 | case TYPE_0: | ||
376 | case TYPE_1: | ||
377 | case TYPE_2: | ||
378 | case TYPE_3: | ||
379 | case TYPE_4: | ||
380 | rand_type = type; | ||
381 | rand_deg = degrees[type]; | ||
382 | rand_sep = seps[type]; | ||
383 | break; | ||
384 | default: | ||
385 | return(NULL); | ||
386 | } | ||
387 | state = &new_state[1]; | ||
388 | if (rand_type != TYPE_0) { | ||
389 | rptr = &state[rear]; | ||
390 | fptr = &state[(rear + rand_sep) % rand_deg]; | ||
391 | } | ||
392 | end_ptr = &state[rand_deg]; /* set end_ptr too */ | ||
393 | return(ostate); | ||
394 | } | ||
395 | |||
396 | /* | ||
397 | * random: | ||
398 | * | ||
399 | * If we are using the trivial TYPE_0 R.N.G., just do the old linear | ||
400 | * congruential bit. Otherwise, we do our fancy trinomial stuff, which is | ||
401 | * the same in all the other cases due to all the global variables that have | ||
402 | * been set up. The basic operation is to add the number at the rear pointer | ||
403 | * into the one at the front pointer. Then both pointers are advanced to | ||
404 | * the next location cyclically in the table. The value returned is the sum | ||
405 | * generated, reduced to 31 bits by throwing away the "least random" low bit. | ||
406 | * | ||
407 | * Note: the code takes advantage of the fact that both the front and | ||
408 | * rear pointers can't wrap on the same call by not testing the rear | ||
409 | * pointer if the front one has wrapped. | ||
410 | * | ||
411 | * Returns a 31-bit random number. | ||
412 | */ | ||
413 | long | ||
414 | random() | ||
415 | { | ||
416 | int32_t i; | ||
417 | |||
418 | if (rand_type == TYPE_0) | ||
419 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; | ||
420 | else { | ||
421 | *fptr += *rptr; | ||
422 | i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ | ||
423 | if (++fptr >= end_ptr) { | ||
424 | fptr = state; | ||
425 | ++rptr; | ||
426 | } else if (++rptr >= end_ptr) | ||
427 | rptr = state; | ||
428 | } | ||
429 | return((long)i); | ||
430 | } | ||
diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3 new file mode 100644 index 0000000000..b8093b51e1 --- /dev/null +++ b/src/lib/libc/stdlib/realpath.3 | |||
@@ -0,0 +1,120 @@ | |||
1 | .\" Copyright (c) 1994 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Jan-Simon Pendry. | ||
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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" $OpenBSD: realpath.3,v 1.10 2003/06/02 20:18:38 millert Exp $ | ||
32 | .\" | ||
33 | .Dd February 16, 1994 | ||
34 | .Dt REALPATH 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm realpath | ||
38 | .Nd returns the canonicalized absolute pathname | ||
39 | .Sh SYNOPSIS | ||
40 | .Fd #include <sys/param.h> | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft "char *" | ||
43 | .Fn realpath "const char *pathname" "char resolvedname[MAXPATHLEN]" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn realpath | ||
47 | function resolves all symbolic links, extra | ||
48 | .Dq / | ||
49 | characters and references to | ||
50 | .Pa /./ | ||
51 | and | ||
52 | .Pa /../ | ||
53 | in | ||
54 | .Fa pathname , | ||
55 | and copies the resulting absolute pathname into the memory referenced by | ||
56 | .Fa resolvedname . | ||
57 | The | ||
58 | .Fa resolvedname | ||
59 | argument | ||
60 | .Em must | ||
61 | refer to a buffer capable of storing at least | ||
62 | .Dv MAXPATHLEN | ||
63 | characters. | ||
64 | .Pp | ||
65 | The | ||
66 | .Fn realpath | ||
67 | function will resolve both absolute and relative paths | ||
68 | and return the absolute pathname corresponding to | ||
69 | .Fa pathname . | ||
70 | All but the last component of | ||
71 | .Fa pathname | ||
72 | must exist when | ||
73 | .Fn realpath | ||
74 | is called. | ||
75 | .Sh RETURN VALUES | ||
76 | The | ||
77 | .Fn realpath | ||
78 | function returns | ||
79 | .Fa resolvedname | ||
80 | on success. | ||
81 | If an error occurs, | ||
82 | .Fn realpath | ||
83 | returns | ||
84 | .Dv NULL , | ||
85 | and | ||
86 | .Fa resolvedname | ||
87 | contains the pathname which caused the problem. | ||
88 | .Sh ERRORS | ||
89 | The function | ||
90 | .Fn realpath | ||
91 | may fail and set the external variable | ||
92 | .Va errno | ||
93 | for any of the errors specified for the library functions | ||
94 | .Xr chdir 2 , | ||
95 | .Xr close 2 , | ||
96 | .Xr fchdir 2 , | ||
97 | .Xr lstat 2 , | ||
98 | .Xr open 2 , | ||
99 | .Xr readlink 2 , | ||
100 | and | ||
101 | .Xr getcwd 3 . | ||
102 | .Sh SEE ALSO | ||
103 | .Xr getcwd 3 | ||
104 | .Sh HISTORY | ||
105 | The | ||
106 | .Fn realpath | ||
107 | function call first appeared in | ||
108 | .Bx 4.4 . | ||
109 | .Sh CAVEATS | ||
110 | This implementation of | ||
111 | .Fn realpath | ||
112 | differs slightly from the Solaris implementation. | ||
113 | The | ||
114 | .Bx 4.4 | ||
115 | version always returns absolute pathnames, | ||
116 | whereas the Solaris implementation will, | ||
117 | under certain circumstances, return a relative | ||
118 | .Fa resolvedname | ||
119 | when given a relative | ||
120 | .Fa pathname . | ||
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c new file mode 100644 index 0000000000..1525d0372f --- /dev/null +++ b/src/lib/libc/stdlib/realpath.c | |||
@@ -0,0 +1,162 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1994 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Jan-Simon Pendry. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: realpath.c,v 1.10 2003/08/01 21:04:59 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <sys/param.h> | ||
38 | #include <sys/stat.h> | ||
39 | |||
40 | #include <errno.h> | ||
41 | #include <fcntl.h> | ||
42 | #include <stdlib.h> | ||
43 | #include <string.h> | ||
44 | #include <unistd.h> | ||
45 | |||
46 | /* | ||
47 | * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); | ||
48 | * | ||
49 | * Find the real name of path, by removing all ".", ".." and symlink | ||
50 | * components. Returns (resolved) on success, or (NULL) on failure, | ||
51 | * in which case the path which caused trouble is left in (resolved). | ||
52 | */ | ||
53 | char * | ||
54 | realpath(path, resolved) | ||
55 | const char *path; | ||
56 | char *resolved; | ||
57 | { | ||
58 | struct stat sb; | ||
59 | int fd, n, needslash, serrno; | ||
60 | char *p, *q, wbuf[MAXPATHLEN]; | ||
61 | int symlinks = 0; | ||
62 | |||
63 | /* Save the starting point. */ | ||
64 | if ((fd = open(".", O_RDONLY)) < 0) { | ||
65 | (void)strlcpy(resolved, ".", MAXPATHLEN); | ||
66 | return (NULL); | ||
67 | } | ||
68 | |||
69 | /* Convert "." -> "" to optimize away a needless lstat() and chdir() */ | ||
70 | if (path[0] == '.' && path[1] == '\0') | ||
71 | path = ""; | ||
72 | |||
73 | /* | ||
74 | * Find the dirname and basename from the path to be resolved. | ||
75 | * Change directory to the dirname component. | ||
76 | * lstat the basename part. | ||
77 | * if it is a symlink, read in the value and loop. | ||
78 | * if it is a directory, then change to that directory. | ||
79 | * get the current directory name and append the basename. | ||
80 | */ | ||
81 | strlcpy(resolved, path, MAXPATHLEN); | ||
82 | loop: | ||
83 | q = strrchr(resolved, '/'); | ||
84 | if (q != NULL) { | ||
85 | p = q + 1; | ||
86 | if (q == resolved) | ||
87 | q = "/"; | ||
88 | else { | ||
89 | do { | ||
90 | --q; | ||
91 | } while (q > resolved && *q == '/'); | ||
92 | q[1] = '\0'; | ||
93 | q = resolved; | ||
94 | } | ||
95 | if (chdir(q) < 0) | ||
96 | goto err1; | ||
97 | } else | ||
98 | p = resolved; | ||
99 | |||
100 | /* Deal with the last component. */ | ||
101 | if (*p != '\0' && lstat(p, &sb) == 0) { | ||
102 | if (S_ISLNK(sb.st_mode)) { | ||
103 | if (++symlinks > MAXSYMLINKS) { | ||
104 | errno = ELOOP; | ||
105 | goto err1; | ||
106 | } | ||
107 | n = readlink(p, resolved, MAXPATHLEN-1); | ||
108 | if (n < 0) | ||
109 | goto err1; | ||
110 | resolved[n] = '\0'; | ||
111 | goto loop; | ||
112 | } | ||
113 | if (S_ISDIR(sb.st_mode)) { | ||
114 | if (chdir(p) < 0) | ||
115 | goto err1; | ||
116 | p = ""; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * Save the last component name and get the full pathname of | ||
122 | * the current directory. | ||
123 | */ | ||
124 | (void)strlcpy(wbuf, p, sizeof wbuf); | ||
125 | if (getcwd(resolved, MAXPATHLEN) == 0) | ||
126 | goto err1; | ||
127 | |||
128 | /* | ||
129 | * Join the two strings together, ensuring that the right thing | ||
130 | * happens if the last component is empty, or the dirname is root. | ||
131 | */ | ||
132 | if (resolved[0] == '/' && resolved[1] == '\0') | ||
133 | needslash = 0; | ||
134 | else | ||
135 | needslash = 1; | ||
136 | |||
137 | if (*wbuf) { | ||
138 | if (strlen(resolved) + strlen(wbuf) + needslash >= MAXPATHLEN) { | ||
139 | errno = ENAMETOOLONG; | ||
140 | goto err1; | ||
141 | } | ||
142 | if (needslash) | ||
143 | strlcat(resolved, "/", MAXPATHLEN); | ||
144 | strlcat(resolved, wbuf, MAXPATHLEN); | ||
145 | } | ||
146 | |||
147 | /* Go back to where we came from. */ | ||
148 | if (fchdir(fd) < 0) { | ||
149 | serrno = errno; | ||
150 | goto err2; | ||
151 | } | ||
152 | |||
153 | /* It's okay if the close fails, what's an fd more or less? */ | ||
154 | (void)close(fd); | ||
155 | return (resolved); | ||
156 | |||
157 | err1: serrno = errno; | ||
158 | (void)fchdir(fd); | ||
159 | err2: (void)close(fd); | ||
160 | errno = serrno; | ||
161 | return (NULL); | ||
162 | } | ||
diff --git a/src/lib/libc/stdlib/remque.c b/src/lib/libc/stdlib/remque.c new file mode 100644 index 0000000000..f4c5769a8c --- /dev/null +++ b/src/lib/libc/stdlib/remque.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: remque.c,v 1.1 2003/05/08 23:26:30 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1993 John Brezak | ||
5 | * 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. The name of the author may be used to endorse or promote products | ||
16 | * derived from this software without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR | ||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | ||
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | ||
27 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
28 | * POSSIBILITY OF SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #if defined(LIBC_SCCS) && !defined(lint) | ||
32 | static char *rcsid = "$OpenBSD: remque.c,v 1.1 2003/05/08 23:26:30 millert Exp $"; | ||
33 | #endif /* LIBC_SCCS and not lint */ | ||
34 | |||
35 | #include <search.h> | ||
36 | |||
37 | struct qelem { | ||
38 | struct qelem *q_forw; | ||
39 | struct qelem *q_back; | ||
40 | }; | ||
41 | |||
42 | void | ||
43 | remque(void *element) | ||
44 | { | ||
45 | struct qelem *e = (struct qelem *) element; | ||
46 | e->q_forw->q_back = e->q_back; | ||
47 | e->q_back->q_forw = e->q_forw; | ||
48 | } | ||
diff --git a/src/lib/libc/stdlib/seed48.c b/src/lib/libc/stdlib/seed48.c new file mode 100644 index 0000000000..c4dcd0ead8 --- /dev/null +++ b/src/lib/libc/stdlib/seed48.c | |||
@@ -0,0 +1,40 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: seed48.c,v 1.2 1996/08/19 08:33:48 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | extern unsigned short __rand48_seed[3]; | ||
21 | extern unsigned short __rand48_mult[3]; | ||
22 | extern unsigned short __rand48_add; | ||
23 | |||
24 | unsigned short * | ||
25 | seed48(unsigned short xseed[3]) | ||
26 | { | ||
27 | static unsigned short sseed[3]; | ||
28 | |||
29 | sseed[0] = __rand48_seed[0]; | ||
30 | sseed[1] = __rand48_seed[1]; | ||
31 | sseed[2] = __rand48_seed[2]; | ||
32 | __rand48_seed[0] = xseed[0]; | ||
33 | __rand48_seed[1] = xseed[1]; | ||
34 | __rand48_seed[2] = xseed[2]; | ||
35 | __rand48_mult[0] = RAND48_MULT_0; | ||
36 | __rand48_mult[1] = RAND48_MULT_1; | ||
37 | __rand48_mult[2] = RAND48_MULT_2; | ||
38 | __rand48_add = RAND48_ADD; | ||
39 | return sseed; | ||
40 | } | ||
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c new file mode 100644 index 0000000000..2ba072b65c --- /dev/null +++ b/src/lib/libc/stdlib/setenv.c | |||
@@ -0,0 +1,118 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1987 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: setenv.c,v 1.6 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | |||
37 | char *__findenv(const char *name, int *offset); | ||
38 | |||
39 | /* | ||
40 | * setenv -- | ||
41 | * Set the value of the environmental variable "name" to be | ||
42 | * "value". If rewrite is set, replace any current value. | ||
43 | */ | ||
44 | int | ||
45 | setenv(name, value, rewrite) | ||
46 | register const char *name; | ||
47 | register const char *value; | ||
48 | int rewrite; | ||
49 | { | ||
50 | extern char **environ; | ||
51 | static int alloced; /* if allocated space before */ | ||
52 | register char *C; | ||
53 | int l_value, offset; | ||
54 | |||
55 | if (*value == '=') /* no `=' in value */ | ||
56 | ++value; | ||
57 | l_value = strlen(value); | ||
58 | if ((C = __findenv(name, &offset))) { /* find if already exists */ | ||
59 | if (!rewrite) | ||
60 | return (0); | ||
61 | if (strlen(C) >= l_value) { /* old larger; copy over */ | ||
62 | while ((*C++ = *value++)) | ||
63 | ; | ||
64 | return (0); | ||
65 | } | ||
66 | } else { /* create new slot */ | ||
67 | register int cnt; | ||
68 | register char **P; | ||
69 | |||
70 | for (P = environ, cnt = 0; *P; ++P, ++cnt); | ||
71 | if (alloced) { /* just increase size */ | ||
72 | P = (char **)realloc((void *)environ, | ||
73 | (size_t)(sizeof(char *) * (cnt + 2))); | ||
74 | if (!P) | ||
75 | return (-1); | ||
76 | environ = P; | ||
77 | } | ||
78 | else { /* get new space */ | ||
79 | alloced = 1; /* copy old entries into it */ | ||
80 | P = (char **)malloc((size_t)(sizeof(char *) * | ||
81 | (cnt + 2))); | ||
82 | if (!P) | ||
83 | return (-1); | ||
84 | bcopy(environ, P, cnt * sizeof(char *)); | ||
85 | environ = P; | ||
86 | } | ||
87 | environ[cnt + 1] = NULL; | ||
88 | offset = cnt; | ||
89 | } | ||
90 | for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ | ||
91 | if (!(environ[offset] = /* name + `=' + value */ | ||
92 | malloc((size_t)((int)(C - name) + l_value + 2)))) | ||
93 | return (-1); | ||
94 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) | ||
95 | ; | ||
96 | for (*C++ = '='; (*C++ = *value++); ) | ||
97 | ; | ||
98 | return (0); | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | * unsetenv(name) -- | ||
103 | * Delete environmental variable "name". | ||
104 | */ | ||
105 | void | ||
106 | unsetenv(name) | ||
107 | const char *name; | ||
108 | { | ||
109 | extern char **environ; | ||
110 | register char **P; | ||
111 | int offset; | ||
112 | char *__findenv(); | ||
113 | |||
114 | while (__findenv(name, &offset)) /* if set multiple times */ | ||
115 | for (P = &environ[offset];; ++P) | ||
116 | if (!(*P = *(P + 1))) | ||
117 | break; | ||
118 | } | ||
diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c new file mode 100644 index 0000000000..fcff8a172e --- /dev/null +++ b/src/lib/libc/stdlib/srand48.c | |||
@@ -0,0 +1,34 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1993 Martin Birgmeier | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * You may redistribute unmodified or modified versions of this source | ||
6 | * code provided that the above copyright notice and this and the | ||
7 | * following conditions are retained. | ||
8 | * | ||
9 | * This software is provided ``as is'', and comes with no warranties | ||
10 | * of any kind. I shall in no event be liable for anything that happens | ||
11 | * to anyone/anything when using this software. | ||
12 | */ | ||
13 | |||
14 | #if defined(LIBC_SCCS) && !defined(lint) | ||
15 | static char rcsid[] = "$OpenBSD: srand48.c,v 1.2 1996/08/19 08:33:49 tholo Exp $"; | ||
16 | #endif /* LIBC_SCCS and not lint */ | ||
17 | |||
18 | #include "rand48.h" | ||
19 | |||
20 | extern unsigned short __rand48_seed[3]; | ||
21 | extern unsigned short __rand48_mult[3]; | ||
22 | extern unsigned short __rand48_add; | ||
23 | |||
24 | void | ||
25 | srand48(long seed) | ||
26 | { | ||
27 | __rand48_seed[0] = RAND48_SEED_0; | ||
28 | __rand48_seed[1] = (unsigned short) seed; | ||
29 | __rand48_seed[2] = (unsigned short) (seed >> 16); | ||
30 | __rand48_mult[0] = RAND48_MULT_0; | ||
31 | __rand48_mult[1] = RAND48_MULT_1; | ||
32 | __rand48_mult[2] = RAND48_MULT_2; | ||
33 | __rand48_add = RAND48_ADD; | ||
34 | } | ||
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3 new file mode 100644 index 0000000000..e25b0a5511 --- /dev/null +++ b/src/lib/libc/stdlib/strtod.3 | |||
@@ -0,0 +1,113 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strtod.3,v 1.8 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRTOD 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtod | ||
39 | .Nd convert | ||
40 | .Tn ASCII | ||
41 | string to double | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Ft double | ||
45 | .Fn strtod "const char *nptr" "char **endptr" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn strtod | ||
49 | function converts the initial portion of the string pointed to by | ||
50 | .Fa nptr | ||
51 | to | ||
52 | .Li double | ||
53 | representation. | ||
54 | .Pp | ||
55 | The expected form of the string is an optional plus | ||
56 | .Pq Ql + | ||
57 | or minus sign | ||
58 | .Pq Ql - | ||
59 | followed by a sequence of digits optionally containing | ||
60 | a decimal-point character, optionally followed by an exponent. | ||
61 | An exponent consists of an | ||
62 | .Sq E | ||
63 | or | ||
64 | .Sq e , | ||
65 | followed by an optional plus or minus sign, followed by a sequence of digits. | ||
66 | .Pp | ||
67 | Leading whitespace characters in the string (as defined by the | ||
68 | .Xr isspace 3 | ||
69 | function) are skipped. | ||
70 | .Sh RETURN VALUES | ||
71 | The | ||
72 | .Fn strtod | ||
73 | function returns the converted value, if any. | ||
74 | .Pp | ||
75 | If | ||
76 | .Fa endptr | ||
77 | is not | ||
78 | .Dv NULL , | ||
79 | a pointer to the character after the last character used | ||
80 | in the conversion is stored in the location referenced by | ||
81 | .Fa endptr . | ||
82 | .Pp | ||
83 | If no conversion is performed, zero is returned and the value of | ||
84 | .Fa nptr | ||
85 | is stored in the location referenced by | ||
86 | .Fa endptr . | ||
87 | .Pp | ||
88 | If the correct value would cause overflow, plus or minus | ||
89 | .Dv HUGE_VAL | ||
90 | is returned (according to the sign of the value), and | ||
91 | .Er ERANGE | ||
92 | is stored in | ||
93 | .Va errno . | ||
94 | If the correct value would cause underflow, zero is returned and | ||
95 | .Er ERANGE | ||
96 | is stored in | ||
97 | .Va errno . | ||
98 | .Sh ERRORS | ||
99 | .Bl -tag -width Er | ||
100 | .It Bq Er ERANGE | ||
101 | Overflow or underflow occurred. | ||
102 | .El | ||
103 | .Sh SEE ALSO | ||
104 | .Xr atof 3 , | ||
105 | .Xr atoi 3 , | ||
106 | .Xr atol 3 , | ||
107 | .Xr strtol 3 , | ||
108 | .Xr strtoul 3 | ||
109 | .Sh STANDARDS | ||
110 | The | ||
111 | .Fn strtod | ||
112 | function conforms to | ||
113 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c new file mode 100644 index 0000000000..17d26a1dc3 --- /dev/null +++ b/src/lib/libc/stdlib/strtod.c | |||
@@ -0,0 +1,2528 @@ | |||
1 | /**************************************************************** | ||
2 | * | ||
3 | * The author of this software is David M. Gay. | ||
4 | * | ||
5 | * Copyright (c) 1991 by AT&T. | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose without fee is hereby granted, provided that this entire notice | ||
9 | * is included in all copies of any software which is or includes a copy | ||
10 | * or modification of this software and in all copies of the supporting | ||
11 | * documentation for such software. | ||
12 | * | ||
13 | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED | ||
14 | * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY | ||
15 | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY | ||
16 | * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. | ||
17 | * | ||
18 | ***************************************************************/ | ||
19 | |||
20 | /* Please send bug reports to | ||
21 | David M. Gay | ||
22 | AT&T Bell Laboratories, Room 2C-463 | ||
23 | 600 Mountain Avenue | ||
24 | Murray Hill, NJ 07974-2070 | ||
25 | U.S.A. | ||
26 | dmg@research.att.com or research!dmg | ||
27 | */ | ||
28 | |||
29 | /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. | ||
30 | * | ||
31 | * This strtod returns a nearest machine number to the input decimal | ||
32 | * string (or sets errno to ERANGE). With IEEE arithmetic, ties are | ||
33 | * broken by the IEEE round-even rule. Otherwise ties are broken by | ||
34 | * biased rounding (add half and chop). | ||
35 | * | ||
36 | * Inspired loosely by William D. Clinger's paper "How to Read Floating | ||
37 | * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. | ||
38 | * | ||
39 | * Modifications: | ||
40 | * | ||
41 | * 1. We only require IEEE, IBM, or VAX double-precision | ||
42 | * arithmetic (not IEEE double-extended). | ||
43 | * 2. We get by with floating-point arithmetic in a case that | ||
44 | * Clinger missed -- when we're computing d * 10^n | ||
45 | * for a small integer d and the integer n is not too | ||
46 | * much larger than 22 (the maximum integer k for which | ||
47 | * we can represent 10^k exactly), we may be able to | ||
48 | * compute (d*10^k) * 10^(e-k) with just one roundoff. | ||
49 | * 3. Rather than a bit-at-a-time adjustment of the binary | ||
50 | * result in the hard case, we use floating-point | ||
51 | * arithmetic to determine the adjustment to within | ||
52 | * one bit; only in really hard cases do we need to | ||
53 | * compute a second residual. | ||
54 | * 4. Because of 3., we don't need a large table of powers of 10 | ||
55 | * for ten-to-e (just some small tables, e.g. of 10^k | ||
56 | * for 0 <= k <= 22). | ||
57 | */ | ||
58 | |||
59 | /* | ||
60 | * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least | ||
61 | * significant byte has the lowest address. | ||
62 | * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most | ||
63 | * significant byte has the lowest address. | ||
64 | * #define Long int on machines with 32-bit ints and 64-bit longs. | ||
65 | * #define Sudden_Underflow for IEEE-format machines without gradual | ||
66 | * underflow (i.e., that flush to zero on underflow). | ||
67 | * #define IBM for IBM mainframe-style floating-point arithmetic. | ||
68 | * #define VAX for VAX-style floating-point arithmetic. | ||
69 | * #define Unsigned_Shifts if >> does treats its left operand as unsigned. | ||
70 | * #define No_leftright to omit left-right logic in fast floating-point | ||
71 | * computation of dtoa. | ||
72 | * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3. | ||
73 | * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines | ||
74 | * that use extended-precision instructions to compute rounded | ||
75 | * products and quotients) with IBM. | ||
76 | * #define ROUND_BIASED for IEEE-format with biased rounding. | ||
77 | * #define Inaccurate_Divide for IEEE-format with correctly rounded | ||
78 | * products but inaccurate quotients, e.g., for Intel i860. | ||
79 | * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision | ||
80 | * integer arithmetic. Whether this speeds things up or slows things | ||
81 | * down depends on the machine and the number being converted. | ||
82 | * #define KR_headers for old-style C function headers. | ||
83 | * #define Bad_float_h if your system lacks a float.h or if it does not | ||
84 | * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, | ||
85 | * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. | ||
86 | * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) | ||
87 | * if memory is available and otherwise does something you deem | ||
88 | * appropriate. If MALLOC is undefined, malloc will be invoked | ||
89 | * directly -- and assumed always to succeed. | ||
90 | */ | ||
91 | |||
92 | #if defined(LIBC_SCCS) && !defined(lint) | ||
93 | static char *rcsid = "$OpenBSD: strtod.c,v 1.16 2002/12/02 15:38:54 millert Exp $"; | ||
94 | #endif /* LIBC_SCCS and not lint */ | ||
95 | |||
96 | #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ | ||
97 | defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \ | ||
98 | defined(__powerpc__) || defined(__m88k__) || defined(__hppa__) | ||
99 | #include <sys/types.h> | ||
100 | #if BYTE_ORDER == BIG_ENDIAN | ||
101 | #define IEEE_BIG_ENDIAN | ||
102 | #else | ||
103 | #define IEEE_LITTLE_ENDIAN | ||
104 | #endif | ||
105 | #endif | ||
106 | |||
107 | #ifdef __arm32__ | ||
108 | /* | ||
109 | * Although the CPU is little endian the FP has different | ||
110 | * byte and word endianness. The byte order is still little endian | ||
111 | * but the word order is big endian. | ||
112 | */ | ||
113 | #define IEEE_BIG_ENDIAN | ||
114 | #endif | ||
115 | |||
116 | #ifdef __vax__ | ||
117 | #define VAX | ||
118 | #endif | ||
119 | |||
120 | #define Long int32_t | ||
121 | #define ULong u_int32_t | ||
122 | |||
123 | #ifdef DEBUG | ||
124 | #include "stdio.h" | ||
125 | #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} | ||
126 | #endif | ||
127 | |||
128 | #ifdef __cplusplus | ||
129 | #include "malloc.h" | ||
130 | #include "memory.h" | ||
131 | #else | ||
132 | #ifndef KR_headers | ||
133 | #include "stdlib.h" | ||
134 | #include "string.h" | ||
135 | #include "locale.h" | ||
136 | #else | ||
137 | #include "malloc.h" | ||
138 | #include "memory.h" | ||
139 | #endif | ||
140 | #endif | ||
141 | |||
142 | #ifdef MALLOC | ||
143 | #ifdef KR_headers | ||
144 | extern char *MALLOC(); | ||
145 | #else | ||
146 | extern void *MALLOC(size_t); | ||
147 | #endif | ||
148 | #else | ||
149 | #define MALLOC malloc | ||
150 | #endif | ||
151 | |||
152 | #include "ctype.h" | ||
153 | #include "errno.h" | ||
154 | |||
155 | #ifdef Bad_float_h | ||
156 | #ifdef IEEE_BIG_ENDIAN | ||
157 | #define IEEE_ARITHMETIC | ||
158 | #endif | ||
159 | #ifdef IEEE_LITTLE_ENDIAN | ||
160 | #define IEEE_ARITHMETIC | ||
161 | #endif | ||
162 | |||
163 | #ifdef IEEE_ARITHMETIC | ||
164 | #define DBL_DIG 15 | ||
165 | #define DBL_MAX_10_EXP 308 | ||
166 | #define DBL_MAX_EXP 1024 | ||
167 | #define FLT_RADIX 2 | ||
168 | #define FLT_ROUNDS 1 | ||
169 | #define DBL_MAX 1.7976931348623157e+308 | ||
170 | #endif | ||
171 | |||
172 | #ifdef IBM | ||
173 | #define DBL_DIG 16 | ||
174 | #define DBL_MAX_10_EXP 75 | ||
175 | #define DBL_MAX_EXP 63 | ||
176 | #define FLT_RADIX 16 | ||
177 | #define FLT_ROUNDS 0 | ||
178 | #define DBL_MAX 7.2370055773322621e+75 | ||
179 | #endif | ||
180 | |||
181 | #ifdef VAX | ||
182 | #define DBL_DIG 16 | ||
183 | #define DBL_MAX_10_EXP 38 | ||
184 | #define DBL_MAX_EXP 127 | ||
185 | #define FLT_RADIX 2 | ||
186 | #define FLT_ROUNDS 1 | ||
187 | #define DBL_MAX 1.7014118346046923e+38 | ||
188 | #endif | ||
189 | |||
190 | #ifndef LONG_MAX | ||
191 | #define LONG_MAX 2147483647 | ||
192 | #endif | ||
193 | #else | ||
194 | #include "float.h" | ||
195 | #endif | ||
196 | #ifndef __MATH_H__ | ||
197 | #include "math.h" | ||
198 | #endif | ||
199 | |||
200 | #ifdef __cplusplus | ||
201 | extern "C" { | ||
202 | #endif | ||
203 | |||
204 | #ifndef CONST | ||
205 | #ifdef KR_headers | ||
206 | #define CONST /* blank */ | ||
207 | #else | ||
208 | #define CONST const | ||
209 | #endif | ||
210 | #endif | ||
211 | |||
212 | #ifdef Unsigned_Shifts | ||
213 | #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; | ||
214 | #else | ||
215 | #define Sign_Extend(a,b) /*no-op*/ | ||
216 | #endif | ||
217 | |||
218 | #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \ | ||
219 | defined(IBM) != 1 | ||
220 | Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or | ||
221 | IBM should be defined. | ||
222 | #endif | ||
223 | |||
224 | typedef union { | ||
225 | double d; | ||
226 | ULong ul[2]; | ||
227 | } _double; | ||
228 | #define value(x) ((x).d) | ||
229 | #ifdef IEEE_LITTLE_ENDIAN | ||
230 | #define word0(x) ((x).ul[1]) | ||
231 | #define word1(x) ((x).ul[0]) | ||
232 | #else | ||
233 | #define word0(x) ((x).ul[0]) | ||
234 | #define word1(x) ((x).ul[1]) | ||
235 | #endif | ||
236 | |||
237 | /* The following definition of Storeinc is appropriate for MIPS processors. | ||
238 | * An alternative that might be better on some machines is | ||
239 | * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) | ||
240 | */ | ||
241 | #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm32__) | ||
242 | #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ | ||
243 | ((unsigned short *)a)[0] = (unsigned short)c, a++) | ||
244 | #else | ||
245 | #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ | ||
246 | ((unsigned short *)a)[1] = (unsigned short)c, a++) | ||
247 | #endif | ||
248 | |||
249 | /* #define P DBL_MANT_DIG */ | ||
250 | /* Ten_pmax = floor(P*log(2)/log(5)) */ | ||
251 | /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ | ||
252 | /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ | ||
253 | /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ | ||
254 | |||
255 | #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) | ||
256 | #define Exp_shift 20 | ||
257 | #define Exp_shift1 20 | ||
258 | #define Exp_msk1 0x100000 | ||
259 | #define Exp_msk11 0x100000 | ||
260 | #define Exp_mask 0x7ff00000 | ||
261 | #define P 53 | ||
262 | #define Bias 1023 | ||
263 | #define IEEE_Arith | ||
264 | #define Emin (-1022) | ||
265 | #define Exp_1 0x3ff00000 | ||
266 | #define Exp_11 0x3ff00000 | ||
267 | #define Ebits 11 | ||
268 | #define Frac_mask 0xfffff | ||
269 | #define Frac_mask1 0xfffff | ||
270 | #define Ten_pmax 22 | ||
271 | #define Bletch 0x10 | ||
272 | #define Bndry_mask 0xfffff | ||
273 | #define Bndry_mask1 0xfffff | ||
274 | #define LSB 1 | ||
275 | #define Sign_bit 0x80000000 | ||
276 | #define Log2P 1 | ||
277 | #define Tiny0 0 | ||
278 | #define Tiny1 1 | ||
279 | #define Quick_max 14 | ||
280 | #define Int_max 14 | ||
281 | #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */ | ||
282 | #else | ||
283 | #undef Sudden_Underflow | ||
284 | #define Sudden_Underflow | ||
285 | #ifdef IBM | ||
286 | #define Exp_shift 24 | ||
287 | #define Exp_shift1 24 | ||
288 | #define Exp_msk1 0x1000000 | ||
289 | #define Exp_msk11 0x1000000 | ||
290 | #define Exp_mask 0x7f000000 | ||
291 | #define P 14 | ||
292 | #define Bias 65 | ||
293 | #define Exp_1 0x41000000 | ||
294 | #define Exp_11 0x41000000 | ||
295 | #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ | ||
296 | #define Frac_mask 0xffffff | ||
297 | #define Frac_mask1 0xffffff | ||
298 | #define Bletch 4 | ||
299 | #define Ten_pmax 22 | ||
300 | #define Bndry_mask 0xefffff | ||
301 | #define Bndry_mask1 0xffffff | ||
302 | #define LSB 1 | ||
303 | #define Sign_bit 0x80000000 | ||
304 | #define Log2P 4 | ||
305 | #define Tiny0 0x100000 | ||
306 | #define Tiny1 0 | ||
307 | #define Quick_max 14 | ||
308 | #define Int_max 15 | ||
309 | #else /* VAX */ | ||
310 | #define Exp_shift 23 | ||
311 | #define Exp_shift1 7 | ||
312 | #define Exp_msk1 0x80 | ||
313 | #define Exp_msk11 0x800000 | ||
314 | #define Exp_mask 0x7f80 | ||
315 | #define P 56 | ||
316 | #define Bias 129 | ||
317 | #define Exp_1 0x40800000 | ||
318 | #define Exp_11 0x4080 | ||
319 | #define Ebits 8 | ||
320 | #define Frac_mask 0x7fffff | ||
321 | #define Frac_mask1 0xffff007f | ||
322 | #define Ten_pmax 24 | ||
323 | #define Bletch 2 | ||
324 | #define Bndry_mask 0xffff007f | ||
325 | #define Bndry_mask1 0xffff007f | ||
326 | #define LSB 0x10000 | ||
327 | #define Sign_bit 0x8000 | ||
328 | #define Log2P 1 | ||
329 | #define Tiny0 0x80 | ||
330 | #define Tiny1 0 | ||
331 | #define Quick_max 15 | ||
332 | #define Int_max 15 | ||
333 | #endif | ||
334 | #endif | ||
335 | |||
336 | #ifndef IEEE_Arith | ||
337 | #define ROUND_BIASED | ||
338 | #endif | ||
339 | |||
340 | #ifdef RND_PRODQUOT | ||
341 | #define rounded_product(a,b) a = rnd_prod(a, b) | ||
342 | #define rounded_quotient(a,b) a = rnd_quot(a, b) | ||
343 | #ifdef KR_headers | ||
344 | extern double rnd_prod(), rnd_quot(); | ||
345 | #else | ||
346 | extern double rnd_prod(double, double), rnd_quot(double, double); | ||
347 | #endif | ||
348 | #else | ||
349 | #define rounded_product(a,b) a *= b | ||
350 | #define rounded_quotient(a,b) a /= b | ||
351 | #endif | ||
352 | |||
353 | #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) | ||
354 | #define Big1 0xffffffff | ||
355 | |||
356 | #ifndef Just_16 | ||
357 | /* When Pack_32 is not defined, we store 16 bits per 32-bit Long. | ||
358 | * This makes some inner loops simpler and sometimes saves work | ||
359 | * during multiplications, but it often seems to make things slightly | ||
360 | * slower. Hence the default is now to store 32 bits per Long. | ||
361 | */ | ||
362 | #ifndef Pack_32 | ||
363 | #define Pack_32 | ||
364 | #endif | ||
365 | #endif | ||
366 | |||
367 | #define Kmax 15 | ||
368 | |||
369 | #ifdef __cplusplus | ||
370 | extern "C" double strtod(const char *s00, char **se); | ||
371 | extern "C" char *__dtoa(double d, int mode, int ndigits, | ||
372 | int *decpt, int *sign, char **rve); | ||
373 | #endif | ||
374 | |||
375 | struct | ||
376 | Bigint { | ||
377 | struct Bigint *next; | ||
378 | int k, maxwds, sign, wds; | ||
379 | ULong x[1]; | ||
380 | }; | ||
381 | |||
382 | typedef struct Bigint Bigint; | ||
383 | |||
384 | static Bigint *freelist[Kmax+1]; | ||
385 | |||
386 | static Bigint * | ||
387 | Balloc | ||
388 | #ifdef KR_headers | ||
389 | (k) int k; | ||
390 | #else | ||
391 | (int k) | ||
392 | #endif | ||
393 | { | ||
394 | int x; | ||
395 | Bigint *rv; | ||
396 | |||
397 | if ((rv = freelist[k])) { | ||
398 | freelist[k] = rv->next; | ||
399 | } | ||
400 | else { | ||
401 | x = 1 << k; | ||
402 | rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)); | ||
403 | rv->k = k; | ||
404 | rv->maxwds = x; | ||
405 | } | ||
406 | rv->sign = rv->wds = 0; | ||
407 | return rv; | ||
408 | } | ||
409 | |||
410 | static void | ||
411 | Bfree | ||
412 | #ifdef KR_headers | ||
413 | (v) Bigint *v; | ||
414 | #else | ||
415 | (Bigint *v) | ||
416 | #endif | ||
417 | { | ||
418 | if (v) { | ||
419 | v->next = freelist[v->k]; | ||
420 | freelist[v->k] = v; | ||
421 | } | ||
422 | } | ||
423 | |||
424 | #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ | ||
425 | y->wds*sizeof(Long) + 2*sizeof(int)) | ||
426 | |||
427 | static Bigint * | ||
428 | multadd | ||
429 | #ifdef KR_headers | ||
430 | (b, m, a) Bigint *b; int m, a; | ||
431 | #else | ||
432 | (Bigint *b, int m, int a) /* multiply by m and add a */ | ||
433 | #endif | ||
434 | { | ||
435 | int i, wds; | ||
436 | ULong *x, y; | ||
437 | #ifdef Pack_32 | ||
438 | ULong xi, z; | ||
439 | #endif | ||
440 | Bigint *b1; | ||
441 | |||
442 | wds = b->wds; | ||
443 | x = b->x; | ||
444 | i = 0; | ||
445 | do { | ||
446 | #ifdef Pack_32 | ||
447 | xi = *x; | ||
448 | y = (xi & 0xffff) * m + a; | ||
449 | z = (xi >> 16) * m + (y >> 16); | ||
450 | a = (int)(z >> 16); | ||
451 | *x++ = (z << 16) + (y & 0xffff); | ||
452 | #else | ||
453 | y = *x * m + a; | ||
454 | a = (int)(y >> 16); | ||
455 | *x++ = y & 0xffff; | ||
456 | #endif | ||
457 | } | ||
458 | while(++i < wds); | ||
459 | if (a) { | ||
460 | if (wds >= b->maxwds) { | ||
461 | b1 = Balloc(b->k+1); | ||
462 | Bcopy(b1, b); | ||
463 | Bfree(b); | ||
464 | b = b1; | ||
465 | } | ||
466 | b->x[wds++] = a; | ||
467 | b->wds = wds; | ||
468 | } | ||
469 | return b; | ||
470 | } | ||
471 | |||
472 | static Bigint * | ||
473 | s2b | ||
474 | #ifdef KR_headers | ||
475 | (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9; | ||
476 | #else | ||
477 | (CONST char *s, int nd0, int nd, ULong y9) | ||
478 | #endif | ||
479 | { | ||
480 | Bigint *b; | ||
481 | int i, k; | ||
482 | Long x, y; | ||
483 | |||
484 | x = (nd + 8) / 9; | ||
485 | for(k = 0, y = 1; x > y; y <<= 1, k++) ; | ||
486 | #ifdef Pack_32 | ||
487 | b = Balloc(k); | ||
488 | b->x[0] = y9; | ||
489 | b->wds = 1; | ||
490 | #else | ||
491 | b = Balloc(k+1); | ||
492 | b->x[0] = y9 & 0xffff; | ||
493 | b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; | ||
494 | #endif | ||
495 | |||
496 | i = 9; | ||
497 | if (9 < nd0) { | ||
498 | s += 9; | ||
499 | do b = multadd(b, 10, *s++ - '0'); | ||
500 | while(++i < nd0); | ||
501 | s++; | ||
502 | } | ||
503 | else | ||
504 | s += 10; | ||
505 | for(; i < nd; i++) | ||
506 | b = multadd(b, 10, *s++ - '0'); | ||
507 | return b; | ||
508 | } | ||
509 | |||
510 | static int | ||
511 | hi0bits | ||
512 | #ifdef KR_headers | ||
513 | (x) register ULong x; | ||
514 | #else | ||
515 | (register ULong x) | ||
516 | #endif | ||
517 | { | ||
518 | register int k = 0; | ||
519 | |||
520 | if (!(x & 0xffff0000)) { | ||
521 | k = 16; | ||
522 | x <<= 16; | ||
523 | } | ||
524 | if (!(x & 0xff000000)) { | ||
525 | k += 8; | ||
526 | x <<= 8; | ||
527 | } | ||
528 | if (!(x & 0xf0000000)) { | ||
529 | k += 4; | ||
530 | x <<= 4; | ||
531 | } | ||
532 | if (!(x & 0xc0000000)) { | ||
533 | k += 2; | ||
534 | x <<= 2; | ||
535 | } | ||
536 | if (!(x & 0x80000000)) { | ||
537 | k++; | ||
538 | if (!(x & 0x40000000)) | ||
539 | return 32; | ||
540 | } | ||
541 | return k; | ||
542 | } | ||
543 | |||
544 | static int | ||
545 | lo0bits | ||
546 | #ifdef KR_headers | ||
547 | (y) ULong *y; | ||
548 | #else | ||
549 | (ULong *y) | ||
550 | #endif | ||
551 | { | ||
552 | register int k; | ||
553 | register ULong x = *y; | ||
554 | |||
555 | if (x & 7) { | ||
556 | if (x & 1) | ||
557 | return 0; | ||
558 | if (x & 2) { | ||
559 | *y = x >> 1; | ||
560 | return 1; | ||
561 | } | ||
562 | *y = x >> 2; | ||
563 | return 2; | ||
564 | } | ||
565 | k = 0; | ||
566 | if (!(x & 0xffff)) { | ||
567 | k = 16; | ||
568 | x >>= 16; | ||
569 | } | ||
570 | if (!(x & 0xff)) { | ||
571 | k += 8; | ||
572 | x >>= 8; | ||
573 | } | ||
574 | if (!(x & 0xf)) { | ||
575 | k += 4; | ||
576 | x >>= 4; | ||
577 | } | ||
578 | if (!(x & 0x3)) { | ||
579 | k += 2; | ||
580 | x >>= 2; | ||
581 | } | ||
582 | if (!(x & 1)) { | ||
583 | k++; | ||
584 | x >>= 1; | ||
585 | if (!x & 1) | ||
586 | return 32; | ||
587 | } | ||
588 | *y = x; | ||
589 | return k; | ||
590 | } | ||
591 | |||
592 | static Bigint * | ||
593 | i2b | ||
594 | #ifdef KR_headers | ||
595 | (i) int i; | ||
596 | #else | ||
597 | (int i) | ||
598 | #endif | ||
599 | { | ||
600 | Bigint *b; | ||
601 | |||
602 | b = Balloc(1); | ||
603 | b->x[0] = i; | ||
604 | b->wds = 1; | ||
605 | return b; | ||
606 | } | ||
607 | |||
608 | static Bigint * | ||
609 | mult | ||
610 | #ifdef KR_headers | ||
611 | (a, b) Bigint *a, *b; | ||
612 | #else | ||
613 | (Bigint *a, Bigint *b) | ||
614 | #endif | ||
615 | { | ||
616 | Bigint *c; | ||
617 | int k, wa, wb, wc; | ||
618 | ULong carry, y, z; | ||
619 | ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; | ||
620 | #ifdef Pack_32 | ||
621 | ULong z2; | ||
622 | #endif | ||
623 | |||
624 | if (a->wds < b->wds) { | ||
625 | c = a; | ||
626 | a = b; | ||
627 | b = c; | ||
628 | } | ||
629 | k = a->k; | ||
630 | wa = a->wds; | ||
631 | wb = b->wds; | ||
632 | wc = wa + wb; | ||
633 | if (wc > a->maxwds) | ||
634 | k++; | ||
635 | c = Balloc(k); | ||
636 | for(x = c->x, xa = x + wc; x < xa; x++) | ||
637 | *x = 0; | ||
638 | xa = a->x; | ||
639 | xae = xa + wa; | ||
640 | xb = b->x; | ||
641 | xbe = xb + wb; | ||
642 | xc0 = c->x; | ||
643 | #ifdef Pack_32 | ||
644 | for(; xb < xbe; xb++, xc0++) { | ||
645 | if ((y = *xb & 0xffff)) { | ||
646 | x = xa; | ||
647 | xc = xc0; | ||
648 | carry = 0; | ||
649 | do { | ||
650 | z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; | ||
651 | carry = z >> 16; | ||
652 | z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; | ||
653 | carry = z2 >> 16; | ||
654 | Storeinc(xc, z2, z); | ||
655 | } | ||
656 | while(x < xae); | ||
657 | *xc = carry; | ||
658 | } | ||
659 | if ((y = *xb >> 16)) { | ||
660 | x = xa; | ||
661 | xc = xc0; | ||
662 | carry = 0; | ||
663 | z2 = *xc; | ||
664 | do { | ||
665 | z = (*x & 0xffff) * y + (*xc >> 16) + carry; | ||
666 | carry = z >> 16; | ||
667 | Storeinc(xc, z, z2); | ||
668 | z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; | ||
669 | carry = z2 >> 16; | ||
670 | } | ||
671 | while(x < xae); | ||
672 | *xc = z2; | ||
673 | } | ||
674 | } | ||
675 | #else | ||
676 | for(; xb < xbe; xc0++) { | ||
677 | if (y = *xb++) { | ||
678 | x = xa; | ||
679 | xc = xc0; | ||
680 | carry = 0; | ||
681 | do { | ||
682 | z = *x++ * y + *xc + carry; | ||
683 | carry = z >> 16; | ||
684 | *xc++ = z & 0xffff; | ||
685 | } | ||
686 | while(x < xae); | ||
687 | *xc = carry; | ||
688 | } | ||
689 | } | ||
690 | #endif | ||
691 | for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; | ||
692 | c->wds = wc; | ||
693 | return c; | ||
694 | } | ||
695 | |||
696 | static Bigint *p5s; | ||
697 | |||
698 | static Bigint * | ||
699 | pow5mult | ||
700 | #ifdef KR_headers | ||
701 | (b, k) Bigint *b; int k; | ||
702 | #else | ||
703 | (Bigint *b, int k) | ||
704 | #endif | ||
705 | { | ||
706 | Bigint *b1, *p5, *p51; | ||
707 | int i; | ||
708 | static int p05[3] = { 5, 25, 125 }; | ||
709 | |||
710 | if ((i = k & 3)) | ||
711 | b = multadd(b, p05[i-1], 0); | ||
712 | |||
713 | if (!(k >>= 2)) | ||
714 | return b; | ||
715 | if (!(p5 = p5s)) { | ||
716 | /* first time */ | ||
717 | p5 = p5s = i2b(625); | ||
718 | p5->next = 0; | ||
719 | } | ||
720 | for(;;) { | ||
721 | if (k & 1) { | ||
722 | b1 = mult(b, p5); | ||
723 | Bfree(b); | ||
724 | b = b1; | ||
725 | } | ||
726 | if (!(k >>= 1)) | ||
727 | break; | ||
728 | if (!(p51 = p5->next)) { | ||
729 | p51 = p5->next = mult(p5,p5); | ||
730 | p51->next = 0; | ||
731 | } | ||
732 | p5 = p51; | ||
733 | } | ||
734 | return b; | ||
735 | } | ||
736 | |||
737 | static Bigint * | ||
738 | lshift | ||
739 | #ifdef KR_headers | ||
740 | (b, k) Bigint *b; int k; | ||
741 | #else | ||
742 | (Bigint *b, int k) | ||
743 | #endif | ||
744 | { | ||
745 | int i, k1, n, n1; | ||
746 | Bigint *b1; | ||
747 | ULong *x, *x1, *xe, z; | ||
748 | |||
749 | #ifdef Pack_32 | ||
750 | n = k >> 5; | ||
751 | #else | ||
752 | n = k >> 4; | ||
753 | #endif | ||
754 | k1 = b->k; | ||
755 | n1 = n + b->wds + 1; | ||
756 | for(i = b->maxwds; n1 > i; i <<= 1) | ||
757 | k1++; | ||
758 | b1 = Balloc(k1); | ||
759 | x1 = b1->x; | ||
760 | for(i = 0; i < n; i++) | ||
761 | *x1++ = 0; | ||
762 | x = b->x; | ||
763 | xe = x + b->wds; | ||
764 | #ifdef Pack_32 | ||
765 | if (k &= 0x1f) { | ||
766 | k1 = 32 - k; | ||
767 | z = 0; | ||
768 | do { | ||
769 | *x1++ = *x << k | z; | ||
770 | z = *x++ >> k1; | ||
771 | } | ||
772 | while(x < xe); | ||
773 | if ((*x1 = z)) | ||
774 | ++n1; | ||
775 | } | ||
776 | #else | ||
777 | if (k &= 0xf) { | ||
778 | k1 = 16 - k; | ||
779 | z = 0; | ||
780 | do { | ||
781 | *x1++ = *x << k & 0xffff | z; | ||
782 | z = *x++ >> k1; | ||
783 | } | ||
784 | while(x < xe); | ||
785 | if (*x1 = z) | ||
786 | ++n1; | ||
787 | } | ||
788 | #endif | ||
789 | else do | ||
790 | *x1++ = *x++; | ||
791 | while(x < xe); | ||
792 | b1->wds = n1 - 1; | ||
793 | Bfree(b); | ||
794 | return b1; | ||
795 | } | ||
796 | |||
797 | static int | ||
798 | cmp | ||
799 | #ifdef KR_headers | ||
800 | (a, b) Bigint *a, *b; | ||
801 | #else | ||
802 | (Bigint *a, Bigint *b) | ||
803 | #endif | ||
804 | { | ||
805 | ULong *xa, *xa0, *xb, *xb0; | ||
806 | int i, j; | ||
807 | |||
808 | i = a->wds; | ||
809 | j = b->wds; | ||
810 | #ifdef DEBUG | ||
811 | if (i > 1 && !a->x[i-1]) | ||
812 | Bug("cmp called with a->x[a->wds-1] == 0"); | ||
813 | if (j > 1 && !b->x[j-1]) | ||
814 | Bug("cmp called with b->x[b->wds-1] == 0"); | ||
815 | #endif | ||
816 | if (i -= j) | ||
817 | return i; | ||
818 | xa0 = a->x; | ||
819 | xa = xa0 + j; | ||
820 | xb0 = b->x; | ||
821 | xb = xb0 + j; | ||
822 | for(;;) { | ||
823 | if (*--xa != *--xb) | ||
824 | return *xa < *xb ? -1 : 1; | ||
825 | if (xa <= xa0) | ||
826 | break; | ||
827 | } | ||
828 | return 0; | ||
829 | } | ||
830 | |||
831 | static Bigint * | ||
832 | diff | ||
833 | #ifdef KR_headers | ||
834 | (a, b) Bigint *a, *b; | ||
835 | #else | ||
836 | (Bigint *a, Bigint *b) | ||
837 | #endif | ||
838 | { | ||
839 | Bigint *c; | ||
840 | int i, wa, wb; | ||
841 | Long borrow, y; /* We need signed shifts here. */ | ||
842 | ULong *xa, *xae, *xb, *xbe, *xc; | ||
843 | #ifdef Pack_32 | ||
844 | Long z; | ||
845 | #endif | ||
846 | |||
847 | i = cmp(a,b); | ||
848 | if (!i) { | ||
849 | c = Balloc(0); | ||
850 | c->wds = 1; | ||
851 | c->x[0] = 0; | ||
852 | return c; | ||
853 | } | ||
854 | if (i < 0) { | ||
855 | c = a; | ||
856 | a = b; | ||
857 | b = c; | ||
858 | i = 1; | ||
859 | } | ||
860 | else | ||
861 | i = 0; | ||
862 | c = Balloc(a->k); | ||
863 | c->sign = i; | ||
864 | wa = a->wds; | ||
865 | xa = a->x; | ||
866 | xae = xa + wa; | ||
867 | wb = b->wds; | ||
868 | xb = b->x; | ||
869 | xbe = xb + wb; | ||
870 | xc = c->x; | ||
871 | borrow = 0; | ||
872 | #ifdef Pack_32 | ||
873 | do { | ||
874 | y = (*xa & 0xffff) - (*xb & 0xffff) + borrow; | ||
875 | borrow = y >> 16; | ||
876 | Sign_Extend(borrow, y); | ||
877 | z = (*xa++ >> 16) - (*xb++ >> 16) + borrow; | ||
878 | borrow = z >> 16; | ||
879 | Sign_Extend(borrow, z); | ||
880 | Storeinc(xc, z, y); | ||
881 | } | ||
882 | while(xb < xbe); | ||
883 | while(xa < xae) { | ||
884 | y = (*xa & 0xffff) + borrow; | ||
885 | borrow = y >> 16; | ||
886 | Sign_Extend(borrow, y); | ||
887 | z = (*xa++ >> 16) + borrow; | ||
888 | borrow = z >> 16; | ||
889 | Sign_Extend(borrow, z); | ||
890 | Storeinc(xc, z, y); | ||
891 | } | ||
892 | #else | ||
893 | do { | ||
894 | y = *xa++ - *xb++ + borrow; | ||
895 | borrow = y >> 16; | ||
896 | Sign_Extend(borrow, y); | ||
897 | *xc++ = y & 0xffff; | ||
898 | } | ||
899 | while(xb < xbe); | ||
900 | while(xa < xae) { | ||
901 | y = *xa++ + borrow; | ||
902 | borrow = y >> 16; | ||
903 | Sign_Extend(borrow, y); | ||
904 | *xc++ = y & 0xffff; | ||
905 | } | ||
906 | #endif | ||
907 | while(!*--xc) | ||
908 | wa--; | ||
909 | c->wds = wa; | ||
910 | return c; | ||
911 | } | ||
912 | |||
913 | static double | ||
914 | ulp | ||
915 | #ifdef KR_headers | ||
916 | (_x) double _x; | ||
917 | #else | ||
918 | (double _x) | ||
919 | #endif | ||
920 | { | ||
921 | _double x; | ||
922 | register Long L; | ||
923 | _double a; | ||
924 | |||
925 | value(x) = _x; | ||
926 | L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; | ||
927 | #ifndef Sudden_Underflow | ||
928 | if (L > 0) { | ||
929 | #endif | ||
930 | #ifdef IBM | ||
931 | L |= Exp_msk1 >> 4; | ||
932 | #endif | ||
933 | word0(a) = L; | ||
934 | word1(a) = 0; | ||
935 | #ifndef Sudden_Underflow | ||
936 | } | ||
937 | else { | ||
938 | L = -L >> Exp_shift; | ||
939 | if (L < Exp_shift) { | ||
940 | word0(a) = 0x80000 >> L; | ||
941 | word1(a) = 0; | ||
942 | } | ||
943 | else { | ||
944 | word0(a) = 0; | ||
945 | L -= Exp_shift; | ||
946 | word1(a) = L >= 31 ? 1 : 1 << 31 - L; | ||
947 | } | ||
948 | } | ||
949 | #endif | ||
950 | return value(a); | ||
951 | } | ||
952 | |||
953 | static double | ||
954 | b2d | ||
955 | #ifdef KR_headers | ||
956 | (a, e) Bigint *a; int *e; | ||
957 | #else | ||
958 | (Bigint *a, int *e) | ||
959 | #endif | ||
960 | { | ||
961 | ULong *xa, *xa0, w, y, z; | ||
962 | int k; | ||
963 | _double d; | ||
964 | #ifdef VAX | ||
965 | ULong d0, d1; | ||
966 | #else | ||
967 | #define d0 word0(d) | ||
968 | #define d1 word1(d) | ||
969 | #endif | ||
970 | |||
971 | xa0 = a->x; | ||
972 | xa = xa0 + a->wds; | ||
973 | y = *--xa; | ||
974 | #ifdef DEBUG | ||
975 | if (!y) Bug("zero y in b2d"); | ||
976 | #endif | ||
977 | k = hi0bits(y); | ||
978 | *e = 32 - k; | ||
979 | #ifdef Pack_32 | ||
980 | if (k < Ebits) { | ||
981 | d0 = Exp_1 | y >> Ebits - k; | ||
982 | w = xa > xa0 ? *--xa : 0; | ||
983 | d1 = y << (32-Ebits) + k | w >> Ebits - k; | ||
984 | goto ret_d; | ||
985 | } | ||
986 | z = xa > xa0 ? *--xa : 0; | ||
987 | if (k -= Ebits) { | ||
988 | d0 = Exp_1 | y << k | z >> 32 - k; | ||
989 | y = xa > xa0 ? *--xa : 0; | ||
990 | d1 = z << k | y >> 32 - k; | ||
991 | } | ||
992 | else { | ||
993 | d0 = Exp_1 | y; | ||
994 | d1 = z; | ||
995 | } | ||
996 | #else | ||
997 | if (k < Ebits + 16) { | ||
998 | z = xa > xa0 ? *--xa : 0; | ||
999 | d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; | ||
1000 | w = xa > xa0 ? *--xa : 0; | ||
1001 | y = xa > xa0 ? *--xa : 0; | ||
1002 | d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; | ||
1003 | goto ret_d; | ||
1004 | } | ||
1005 | z = xa > xa0 ? *--xa : 0; | ||
1006 | w = xa > xa0 ? *--xa : 0; | ||
1007 | k -= Ebits + 16; | ||
1008 | d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; | ||
1009 | y = xa > xa0 ? *--xa : 0; | ||
1010 | d1 = w << k + 16 | y << k; | ||
1011 | #endif | ||
1012 | ret_d: | ||
1013 | #ifdef VAX | ||
1014 | word0(d) = d0 >> 16 | d0 << 16; | ||
1015 | word1(d) = d1 >> 16 | d1 << 16; | ||
1016 | #else | ||
1017 | #undef d0 | ||
1018 | #undef d1 | ||
1019 | #endif | ||
1020 | return value(d); | ||
1021 | } | ||
1022 | |||
1023 | static Bigint * | ||
1024 | d2b | ||
1025 | #ifdef KR_headers | ||
1026 | (_d, e, bits) double d; int *e, *bits; | ||
1027 | #else | ||
1028 | (double _d, int *e, int *bits) | ||
1029 | #endif | ||
1030 | { | ||
1031 | Bigint *b; | ||
1032 | int de, i, k; | ||
1033 | ULong *x, y, z; | ||
1034 | _double d; | ||
1035 | #ifdef VAX | ||
1036 | ULong d0, d1; | ||
1037 | #endif | ||
1038 | |||
1039 | value(d) = _d; | ||
1040 | #ifdef VAX | ||
1041 | d0 = word0(d) >> 16 | word0(d) << 16; | ||
1042 | d1 = word1(d) >> 16 | word1(d) << 16; | ||
1043 | #else | ||
1044 | #define d0 word0(d) | ||
1045 | #define d1 word1(d) | ||
1046 | #endif | ||
1047 | |||
1048 | #ifdef Pack_32 | ||
1049 | b = Balloc(1); | ||
1050 | #else | ||
1051 | b = Balloc(2); | ||
1052 | #endif | ||
1053 | x = b->x; | ||
1054 | |||
1055 | z = d0 & Frac_mask; | ||
1056 | d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ | ||
1057 | #ifdef Sudden_Underflow | ||
1058 | de = (int)(d0 >> Exp_shift); | ||
1059 | #ifndef IBM | ||
1060 | z |= Exp_msk11; | ||
1061 | #endif | ||
1062 | #else | ||
1063 | if (de = (int)(d0 >> Exp_shift)) | ||
1064 | z |= Exp_msk1; | ||
1065 | #endif | ||
1066 | #ifdef Pack_32 | ||
1067 | if (y = d1) { | ||
1068 | if (k = lo0bits(&y)) { | ||
1069 | x[0] = y | z << 32 - k; | ||
1070 | z >>= k; | ||
1071 | } | ||
1072 | else | ||
1073 | x[0] = y; | ||
1074 | i = b->wds = (x[1] = z) ? 2 : 1; | ||
1075 | } | ||
1076 | else { | ||
1077 | #ifdef DEBUG | ||
1078 | if (!z) | ||
1079 | Bug("Zero passed to d2b"); | ||
1080 | #endif | ||
1081 | k = lo0bits(&z); | ||
1082 | x[0] = z; | ||
1083 | i = b->wds = 1; | ||
1084 | k += 32; | ||
1085 | } | ||
1086 | #else | ||
1087 | if (y = d1) { | ||
1088 | if (k = lo0bits(&y)) | ||
1089 | if (k >= 16) { | ||
1090 | x[0] = y | z << 32 - k & 0xffff; | ||
1091 | x[1] = z >> k - 16 & 0xffff; | ||
1092 | x[2] = z >> k; | ||
1093 | i = 2; | ||
1094 | } | ||
1095 | else { | ||
1096 | x[0] = y & 0xffff; | ||
1097 | x[1] = y >> 16 | z << 16 - k & 0xffff; | ||
1098 | x[2] = z >> k & 0xffff; | ||
1099 | x[3] = z >> k+16; | ||
1100 | i = 3; | ||
1101 | } | ||
1102 | else { | ||
1103 | x[0] = y & 0xffff; | ||
1104 | x[1] = y >> 16; | ||
1105 | x[2] = z & 0xffff; | ||
1106 | x[3] = z >> 16; | ||
1107 | i = 3; | ||
1108 | } | ||
1109 | } | ||
1110 | else { | ||
1111 | #ifdef DEBUG | ||
1112 | if (!z) | ||
1113 | Bug("Zero passed to d2b"); | ||
1114 | #endif | ||
1115 | k = lo0bits(&z); | ||
1116 | if (k >= 16) { | ||
1117 | x[0] = z; | ||
1118 | i = 0; | ||
1119 | } | ||
1120 | else { | ||
1121 | x[0] = z & 0xffff; | ||
1122 | x[1] = z >> 16; | ||
1123 | i = 1; | ||
1124 | } | ||
1125 | k += 32; | ||
1126 | } | ||
1127 | while(!x[i]) | ||
1128 | --i; | ||
1129 | b->wds = i + 1; | ||
1130 | #endif | ||
1131 | #ifndef Sudden_Underflow | ||
1132 | if (de) { | ||
1133 | #endif | ||
1134 | #ifdef IBM | ||
1135 | *e = (de - Bias - (P-1) << 2) + k; | ||
1136 | *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); | ||
1137 | #else | ||
1138 | *e = de - Bias - (P-1) + k; | ||
1139 | *bits = P - k; | ||
1140 | #endif | ||
1141 | #ifndef Sudden_Underflow | ||
1142 | } | ||
1143 | else { | ||
1144 | *e = de - Bias - (P-1) + 1 + k; | ||
1145 | #ifdef Pack_32 | ||
1146 | *bits = 32*i - hi0bits(x[i-1]); | ||
1147 | #else | ||
1148 | *bits = (i+2)*16 - hi0bits(x[i]); | ||
1149 | #endif | ||
1150 | } | ||
1151 | #endif | ||
1152 | return b; | ||
1153 | } | ||
1154 | #undef d0 | ||
1155 | #undef d1 | ||
1156 | |||
1157 | static double | ||
1158 | ratio | ||
1159 | #ifdef KR_headers | ||
1160 | (a, b) Bigint *a, *b; | ||
1161 | #else | ||
1162 | (Bigint *a, Bigint *b) | ||
1163 | #endif | ||
1164 | { | ||
1165 | _double da, db; | ||
1166 | int k, ka, kb; | ||
1167 | |||
1168 | value(da) = b2d(a, &ka); | ||
1169 | value(db) = b2d(b, &kb); | ||
1170 | #ifdef Pack_32 | ||
1171 | k = ka - kb + 32*(a->wds - b->wds); | ||
1172 | #else | ||
1173 | k = ka - kb + 16*(a->wds - b->wds); | ||
1174 | #endif | ||
1175 | #ifdef IBM | ||
1176 | if (k > 0) { | ||
1177 | word0(da) += (k >> 2)*Exp_msk1; | ||
1178 | if (k &= 3) | ||
1179 | da *= 1 << k; | ||
1180 | } | ||
1181 | else { | ||
1182 | k = -k; | ||
1183 | word0(db) += (k >> 2)*Exp_msk1; | ||
1184 | if (k &= 3) | ||
1185 | db *= 1 << k; | ||
1186 | } | ||
1187 | #else | ||
1188 | if (k > 0) | ||
1189 | word0(da) += k*Exp_msk1; | ||
1190 | else { | ||
1191 | k = -k; | ||
1192 | word0(db) += k*Exp_msk1; | ||
1193 | } | ||
1194 | #endif | ||
1195 | return value(da) / value(db); | ||
1196 | } | ||
1197 | |||
1198 | static CONST double | ||
1199 | tens[] = { | ||
1200 | 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, | ||
1201 | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, | ||
1202 | 1e20, 1e21, 1e22 | ||
1203 | #ifdef VAX | ||
1204 | , 1e23, 1e24 | ||
1205 | #endif | ||
1206 | }; | ||
1207 | |||
1208 | #ifdef IEEE_Arith | ||
1209 | static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; | ||
1210 | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 }; | ||
1211 | #define n_bigtens 5 | ||
1212 | #else | ||
1213 | #ifdef IBM | ||
1214 | static CONST double bigtens[] = { 1e16, 1e32, 1e64 }; | ||
1215 | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; | ||
1216 | #define n_bigtens 3 | ||
1217 | #else | ||
1218 | static CONST double bigtens[] = { 1e16, 1e32 }; | ||
1219 | static CONST double tinytens[] = { 1e-16, 1e-32 }; | ||
1220 | #define n_bigtens 2 | ||
1221 | #endif | ||
1222 | #endif | ||
1223 | |||
1224 | double | ||
1225 | strtod | ||
1226 | #ifdef KR_headers | ||
1227 | (s00, se) CONST char *s00; char **se; | ||
1228 | #else | ||
1229 | (CONST char *s00, char **se) | ||
1230 | #endif | ||
1231 | { | ||
1232 | int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, | ||
1233 | e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; | ||
1234 | CONST char *s, *s0, *s1; | ||
1235 | double aadj, aadj1, adj; | ||
1236 | _double rv, rv0; | ||
1237 | Long L; | ||
1238 | ULong y, z; | ||
1239 | Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; | ||
1240 | |||
1241 | #ifndef KR_headers | ||
1242 | CONST char decimal_point = localeconv()->decimal_point[0]; | ||
1243 | #else | ||
1244 | CONST char decimal_point = '.'; | ||
1245 | #endif | ||
1246 | |||
1247 | sign = nz0 = nz = 0; | ||
1248 | value(rv) = 0.; | ||
1249 | |||
1250 | |||
1251 | for(s = s00; isspace((unsigned char) *s); s++) | ||
1252 | ; | ||
1253 | |||
1254 | if (*s == '-') { | ||
1255 | sign = 1; | ||
1256 | s++; | ||
1257 | } else if (*s == '+') { | ||
1258 | s++; | ||
1259 | } | ||
1260 | |||
1261 | if (*s == '\0') { | ||
1262 | s = s00; | ||
1263 | goto ret; | ||
1264 | } | ||
1265 | |||
1266 | if (*s == '0') { | ||
1267 | nz0 = 1; | ||
1268 | while(*++s == '0') ; | ||
1269 | if (!*s) | ||
1270 | goto ret; | ||
1271 | } | ||
1272 | s0 = s; | ||
1273 | y = z = 0; | ||
1274 | for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) | ||
1275 | if (nd < 9) | ||
1276 | y = 10*y + c - '0'; | ||
1277 | else if (nd < 16) | ||
1278 | z = 10*z + c - '0'; | ||
1279 | nd0 = nd; | ||
1280 | if (c == decimal_point) { | ||
1281 | c = *++s; | ||
1282 | if (!nd) { | ||
1283 | for(; c == '0'; c = *++s) | ||
1284 | nz++; | ||
1285 | if (c > '0' && c <= '9') { | ||
1286 | s0 = s; | ||
1287 | nf += nz; | ||
1288 | nz = 0; | ||
1289 | goto have_dig; | ||
1290 | } | ||
1291 | goto dig_done; | ||
1292 | } | ||
1293 | for(; c >= '0' && c <= '9'; c = *++s) { | ||
1294 | have_dig: | ||
1295 | nz++; | ||
1296 | if (c -= '0') { | ||
1297 | nf += nz; | ||
1298 | for(i = 1; i < nz; i++) | ||
1299 | if (nd++ < 9) | ||
1300 | y *= 10; | ||
1301 | else if (nd <= DBL_DIG + 1) | ||
1302 | z *= 10; | ||
1303 | if (nd++ < 9) | ||
1304 | y = 10*y + c; | ||
1305 | else if (nd <= DBL_DIG + 1) | ||
1306 | z = 10*z + c; | ||
1307 | nz = 0; | ||
1308 | } | ||
1309 | } | ||
1310 | } | ||
1311 | dig_done: | ||
1312 | e = 0; | ||
1313 | if (c == 'e' || c == 'E') { | ||
1314 | if (!nd && !nz && !nz0) { | ||
1315 | s = s00; | ||
1316 | goto ret; | ||
1317 | } | ||
1318 | s00 = s; | ||
1319 | esign = 0; | ||
1320 | switch(c = *++s) { | ||
1321 | case '-': | ||
1322 | esign = 1; | ||
1323 | case '+': | ||
1324 | c = *++s; | ||
1325 | } | ||
1326 | if (c >= '0' && c <= '9') { | ||
1327 | while(c == '0') | ||
1328 | c = *++s; | ||
1329 | if (c > '0' && c <= '9') { | ||
1330 | L = c - '0'; | ||
1331 | s1 = s; | ||
1332 | while((c = *++s) >= '0' && c <= '9') | ||
1333 | L = 10*L + c - '0'; | ||
1334 | if (s - s1 > 8 || L > 19999) | ||
1335 | /* Avoid confusion from exponents | ||
1336 | * so large that e might overflow. | ||
1337 | */ | ||
1338 | e = 19999; /* safe for 16 bit ints */ | ||
1339 | else | ||
1340 | e = (int)L; | ||
1341 | if (esign) | ||
1342 | e = -e; | ||
1343 | } | ||
1344 | else | ||
1345 | e = 0; | ||
1346 | } | ||
1347 | else | ||
1348 | s = s00; | ||
1349 | } | ||
1350 | if (!nd) { | ||
1351 | if (!nz && !nz0) | ||
1352 | s = s00; | ||
1353 | goto ret; | ||
1354 | } | ||
1355 | e1 = e -= nf; | ||
1356 | |||
1357 | /* Now we have nd0 digits, starting at s0, followed by a | ||
1358 | * decimal point, followed by nd-nd0 digits. The number we're | ||
1359 | * after is the integer represented by those digits times | ||
1360 | * 10**e */ | ||
1361 | |||
1362 | if (!nd0) | ||
1363 | nd0 = nd; | ||
1364 | k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; | ||
1365 | value(rv) = y; | ||
1366 | if (k > 9) | ||
1367 | value(rv) = tens[k - 9] * value(rv) + z; | ||
1368 | bd0 = 0; | ||
1369 | if (nd <= DBL_DIG | ||
1370 | #ifndef RND_PRODQUOT | ||
1371 | && FLT_ROUNDS == 1 | ||
1372 | #endif | ||
1373 | ) { | ||
1374 | if (!e) | ||
1375 | goto ret; | ||
1376 | if (e > 0) { | ||
1377 | if (e <= Ten_pmax) { | ||
1378 | #ifdef VAX | ||
1379 | goto vax_ovfl_check; | ||
1380 | #else | ||
1381 | /* value(rv) = */ rounded_product(value(rv), | ||
1382 | tens[e]); | ||
1383 | goto ret; | ||
1384 | #endif | ||
1385 | } | ||
1386 | i = DBL_DIG - nd; | ||
1387 | if (e <= Ten_pmax + i) { | ||
1388 | /* A fancier test would sometimes let us do | ||
1389 | * this for larger i values. | ||
1390 | */ | ||
1391 | e -= i; | ||
1392 | value(rv) *= tens[i]; | ||
1393 | #ifdef VAX | ||
1394 | /* VAX exponent range is so narrow we must | ||
1395 | * worry about overflow here... | ||
1396 | */ | ||
1397 | vax_ovfl_check: | ||
1398 | word0(rv) -= P*Exp_msk1; | ||
1399 | /* value(rv) = */ rounded_product(value(rv), | ||
1400 | tens[e]); | ||
1401 | if ((word0(rv) & Exp_mask) | ||
1402 | > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) | ||
1403 | goto ovfl; | ||
1404 | word0(rv) += P*Exp_msk1; | ||
1405 | #else | ||
1406 | /* value(rv) = */ rounded_product(value(rv), | ||
1407 | tens[e]); | ||
1408 | #endif | ||
1409 | goto ret; | ||
1410 | } | ||
1411 | } | ||
1412 | #ifndef Inaccurate_Divide | ||
1413 | else if (e >= -Ten_pmax) { | ||
1414 | /* value(rv) = */ rounded_quotient(value(rv), | ||
1415 | tens[-e]); | ||
1416 | goto ret; | ||
1417 | } | ||
1418 | #endif | ||
1419 | } | ||
1420 | e1 += nd - k; | ||
1421 | |||
1422 | /* Get starting approximation = rv * 10**e1 */ | ||
1423 | |||
1424 | if (e1 > 0) { | ||
1425 | if (i = e1 & 15) | ||
1426 | value(rv) *= tens[i]; | ||
1427 | if (e1 &= ~15) { | ||
1428 | if (e1 > DBL_MAX_10_EXP) { | ||
1429 | ovfl: | ||
1430 | errno = ERANGE; | ||
1431 | #ifndef Bad_float_h | ||
1432 | value(rv) = HUGE_VAL; | ||
1433 | #else | ||
1434 | /* Can't trust HUGE_VAL */ | ||
1435 | #ifdef IEEE_Arith | ||
1436 | word0(rv) = Exp_mask; | ||
1437 | word1(rv) = 0; | ||
1438 | #else | ||
1439 | word0(rv) = Big0; | ||
1440 | word1(rv) = Big1; | ||
1441 | #endif | ||
1442 | #endif | ||
1443 | if (bd0) | ||
1444 | goto retfree; | ||
1445 | goto ret; | ||
1446 | } | ||
1447 | if (e1 >>= 4) { | ||
1448 | for(j = 0; e1 > 1; j++, e1 >>= 1) | ||
1449 | if (e1 & 1) | ||
1450 | value(rv) *= bigtens[j]; | ||
1451 | /* The last multiplication could overflow. */ | ||
1452 | word0(rv) -= P*Exp_msk1; | ||
1453 | value(rv) *= bigtens[j]; | ||
1454 | if ((z = word0(rv) & Exp_mask) | ||
1455 | > Exp_msk1*(DBL_MAX_EXP+Bias-P)) | ||
1456 | goto ovfl; | ||
1457 | if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { | ||
1458 | /* set to largest number */ | ||
1459 | /* (Can't trust DBL_MAX) */ | ||
1460 | word0(rv) = Big0; | ||
1461 | word1(rv) = Big1; | ||
1462 | } | ||
1463 | else | ||
1464 | word0(rv) += P*Exp_msk1; | ||
1465 | } | ||
1466 | |||
1467 | } | ||
1468 | } | ||
1469 | else if (e1 < 0) { | ||
1470 | e1 = -e1; | ||
1471 | if (i = e1 & 15) | ||
1472 | value(rv) /= tens[i]; | ||
1473 | if (e1 &= ~15) { | ||
1474 | e1 >>= 4; | ||
1475 | if (e1 >= 1 << n_bigtens) | ||
1476 | goto undfl; | ||
1477 | for(j = 0; e1 > 1; j++, e1 >>= 1) | ||
1478 | if (e1 & 1) | ||
1479 | value(rv) *= tinytens[j]; | ||
1480 | /* The last multiplication could underflow. */ | ||
1481 | value(rv0) = value(rv); | ||
1482 | value(rv) *= tinytens[j]; | ||
1483 | if (!value(rv)) { | ||
1484 | value(rv) = 2.*value(rv0); | ||
1485 | value(rv) *= tinytens[j]; | ||
1486 | if (!value(rv)) { | ||
1487 | undfl: | ||
1488 | value(rv) = 0.; | ||
1489 | errno = ERANGE; | ||
1490 | if (bd0) | ||
1491 | goto retfree; | ||
1492 | goto ret; | ||
1493 | } | ||
1494 | word0(rv) = Tiny0; | ||
1495 | word1(rv) = Tiny1; | ||
1496 | /* The refinement below will clean | ||
1497 | * this approximation up. | ||
1498 | */ | ||
1499 | } | ||
1500 | } | ||
1501 | } | ||
1502 | |||
1503 | /* Now the hard part -- adjusting rv to the correct value.*/ | ||
1504 | |||
1505 | /* Put digits into bd: true value = bd * 10^e */ | ||
1506 | |||
1507 | bd0 = s2b(s0, nd0, nd, y); | ||
1508 | |||
1509 | for(;;) { | ||
1510 | bd = Balloc(bd0->k); | ||
1511 | Bcopy(bd, bd0); | ||
1512 | bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */ | ||
1513 | bs = i2b(1); | ||
1514 | |||
1515 | if (e >= 0) { | ||
1516 | bb2 = bb5 = 0; | ||
1517 | bd2 = bd5 = e; | ||
1518 | } | ||
1519 | else { | ||
1520 | bb2 = bb5 = -e; | ||
1521 | bd2 = bd5 = 0; | ||
1522 | } | ||
1523 | if (bbe >= 0) | ||
1524 | bb2 += bbe; | ||
1525 | else | ||
1526 | bd2 -= bbe; | ||
1527 | bs2 = bb2; | ||
1528 | #ifdef Sudden_Underflow | ||
1529 | #ifdef IBM | ||
1530 | j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); | ||
1531 | #else | ||
1532 | j = P + 1 - bbbits; | ||
1533 | #endif | ||
1534 | #else | ||
1535 | i = bbe + bbbits - 1; /* logb(rv) */ | ||
1536 | if (i < Emin) /* denormal */ | ||
1537 | j = bbe + (P-Emin); | ||
1538 | else | ||
1539 | j = P + 1 - bbbits; | ||
1540 | #endif | ||
1541 | bb2 += j; | ||
1542 | bd2 += j; | ||
1543 | i = bb2 < bd2 ? bb2 : bd2; | ||
1544 | if (i > bs2) | ||
1545 | i = bs2; | ||
1546 | if (i > 0) { | ||
1547 | bb2 -= i; | ||
1548 | bd2 -= i; | ||
1549 | bs2 -= i; | ||
1550 | } | ||
1551 | if (bb5 > 0) { | ||
1552 | bs = pow5mult(bs, bb5); | ||
1553 | bb1 = mult(bs, bb); | ||
1554 | Bfree(bb); | ||
1555 | bb = bb1; | ||
1556 | } | ||
1557 | if (bb2 > 0) | ||
1558 | bb = lshift(bb, bb2); | ||
1559 | if (bd5 > 0) | ||
1560 | bd = pow5mult(bd, bd5); | ||
1561 | if (bd2 > 0) | ||
1562 | bd = lshift(bd, bd2); | ||
1563 | if (bs2 > 0) | ||
1564 | bs = lshift(bs, bs2); | ||
1565 | delta = diff(bb, bd); | ||
1566 | dsign = delta->sign; | ||
1567 | delta->sign = 0; | ||
1568 | i = cmp(delta, bs); | ||
1569 | if (i < 0) { | ||
1570 | /* Error is less than half an ulp -- check for | ||
1571 | * special case of mantissa a power of two. | ||
1572 | */ | ||
1573 | if (dsign || word1(rv) || word0(rv) & Bndry_mask) | ||
1574 | break; | ||
1575 | delta = lshift(delta,Log2P); | ||
1576 | if (cmp(delta, bs) > 0) | ||
1577 | goto drop_down; | ||
1578 | break; | ||
1579 | } | ||
1580 | if (i == 0) { | ||
1581 | /* exactly half-way between */ | ||
1582 | if (dsign) { | ||
1583 | if ((word0(rv) & Bndry_mask1) == Bndry_mask1 | ||
1584 | && word1(rv) == 0xffffffff) { | ||
1585 | /*boundary case -- increment exponent*/ | ||
1586 | word0(rv) = (word0(rv) & Exp_mask) | ||
1587 | + Exp_msk1 | ||
1588 | #ifdef IBM | ||
1589 | | Exp_msk1 >> 4 | ||
1590 | #endif | ||
1591 | ; | ||
1592 | word1(rv) = 0; | ||
1593 | break; | ||
1594 | } | ||
1595 | } | ||
1596 | else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { | ||
1597 | drop_down: | ||
1598 | /* boundary case -- decrement exponent */ | ||
1599 | #ifdef Sudden_Underflow | ||
1600 | L = word0(rv) & Exp_mask; | ||
1601 | #ifdef IBM | ||
1602 | if (L < Exp_msk1) | ||
1603 | #else | ||
1604 | if (L <= Exp_msk1) | ||
1605 | #endif | ||
1606 | goto undfl; | ||
1607 | L -= Exp_msk1; | ||
1608 | #else | ||
1609 | L = (word0(rv) & Exp_mask) - Exp_msk1; | ||
1610 | #endif | ||
1611 | word0(rv) = L | Bndry_mask1; | ||
1612 | word1(rv) = 0xffffffff; | ||
1613 | #ifdef IBM | ||
1614 | goto cont; | ||
1615 | #else | ||
1616 | break; | ||
1617 | #endif | ||
1618 | } | ||
1619 | #ifndef ROUND_BIASED | ||
1620 | if (!(word1(rv) & LSB)) | ||
1621 | break; | ||
1622 | #endif | ||
1623 | if (dsign) | ||
1624 | value(rv) += ulp(value(rv)); | ||
1625 | #ifndef ROUND_BIASED | ||
1626 | else { | ||
1627 | value(rv) -= ulp(value(rv)); | ||
1628 | #ifndef Sudden_Underflow | ||
1629 | if (!value(rv)) | ||
1630 | goto undfl; | ||
1631 | #endif | ||
1632 | } | ||
1633 | #endif | ||
1634 | break; | ||
1635 | } | ||
1636 | if ((aadj = ratio(delta, bs)) <= 2.) { | ||
1637 | if (dsign) | ||
1638 | aadj = aadj1 = 1.; | ||
1639 | else if (word1(rv) || word0(rv) & Bndry_mask) { | ||
1640 | #ifndef Sudden_Underflow | ||
1641 | if (word1(rv) == Tiny1 && !word0(rv)) | ||
1642 | goto undfl; | ||
1643 | #endif | ||
1644 | aadj = 1.; | ||
1645 | aadj1 = -1.; | ||
1646 | } | ||
1647 | else { | ||
1648 | /* special case -- power of FLT_RADIX to be */ | ||
1649 | /* rounded down... */ | ||
1650 | |||
1651 | if (aadj < 2./FLT_RADIX) | ||
1652 | aadj = 1./FLT_RADIX; | ||
1653 | else | ||
1654 | aadj *= 0.5; | ||
1655 | aadj1 = -aadj; | ||
1656 | } | ||
1657 | } | ||
1658 | else { | ||
1659 | aadj *= 0.5; | ||
1660 | aadj1 = dsign ? aadj : -aadj; | ||
1661 | #ifdef Check_FLT_ROUNDS | ||
1662 | switch(FLT_ROUNDS) { | ||
1663 | case 2: /* towards +infinity */ | ||
1664 | aadj1 -= 0.5; | ||
1665 | break; | ||
1666 | case 0: /* towards 0 */ | ||
1667 | case 3: /* towards -infinity */ | ||
1668 | aadj1 += 0.5; | ||
1669 | } | ||
1670 | #else | ||
1671 | if (FLT_ROUNDS == 0) | ||
1672 | aadj1 += 0.5; | ||
1673 | #endif | ||
1674 | } | ||
1675 | y = word0(rv) & Exp_mask; | ||
1676 | |||
1677 | /* Check for overflow */ | ||
1678 | |||
1679 | if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { | ||
1680 | value(rv0) = value(rv); | ||
1681 | word0(rv) -= P*Exp_msk1; | ||
1682 | adj = aadj1 * ulp(value(rv)); | ||
1683 | value(rv) += adj; | ||
1684 | if ((word0(rv) & Exp_mask) >= | ||
1685 | Exp_msk1*(DBL_MAX_EXP+Bias-P)) { | ||
1686 | if (word0(rv0) == Big0 && word1(rv0) == Big1) | ||
1687 | goto ovfl; | ||
1688 | word0(rv) = Big0; | ||
1689 | word1(rv) = Big1; | ||
1690 | goto cont; | ||
1691 | } | ||
1692 | else | ||
1693 | word0(rv) += P*Exp_msk1; | ||
1694 | } | ||
1695 | else { | ||
1696 | #ifdef Sudden_Underflow | ||
1697 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { | ||
1698 | value(rv0) = value(rv); | ||
1699 | word0(rv) += P*Exp_msk1; | ||
1700 | adj = aadj1 * ulp(value(rv)); | ||
1701 | value(rv) += adj; | ||
1702 | #ifdef IBM | ||
1703 | if ((word0(rv) & Exp_mask) < P*Exp_msk1) | ||
1704 | #else | ||
1705 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) | ||
1706 | #endif | ||
1707 | { | ||
1708 | if (word0(rv0) == Tiny0 | ||
1709 | && word1(rv0) == Tiny1) | ||
1710 | goto undfl; | ||
1711 | word0(rv) = Tiny0; | ||
1712 | word1(rv) = Tiny1; | ||
1713 | goto cont; | ||
1714 | } | ||
1715 | else | ||
1716 | word0(rv) -= P*Exp_msk1; | ||
1717 | } | ||
1718 | else { | ||
1719 | adj = aadj1 * ulp(value(rv)); | ||
1720 | value(rv) += adj; | ||
1721 | } | ||
1722 | #else | ||
1723 | /* Compute adj so that the IEEE rounding rules will | ||
1724 | * correctly round rv + adj in some half-way cases. | ||
1725 | * If rv * ulp(rv) is denormalized (i.e., | ||
1726 | * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid | ||
1727 | * trouble from bits lost to denormalization; | ||
1728 | * example: 1.2e-307 . | ||
1729 | */ | ||
1730 | if (y <= (P-1)*Exp_msk1 && aadj >= 1.) { | ||
1731 | aadj1 = (double)(int)(aadj + 0.5); | ||
1732 | if (!dsign) | ||
1733 | aadj1 = -aadj1; | ||
1734 | } | ||
1735 | adj = aadj1 * ulp(value(rv)); | ||
1736 | value(rv) += adj; | ||
1737 | #endif | ||
1738 | } | ||
1739 | z = word0(rv) & Exp_mask; | ||
1740 | if (y == z) { | ||
1741 | /* Can we stop now? */ | ||
1742 | L = aadj; | ||
1743 | aadj -= L; | ||
1744 | /* The tolerances below are conservative. */ | ||
1745 | if (dsign || word1(rv) || word0(rv) & Bndry_mask) { | ||
1746 | if (aadj < .4999999 || aadj > .5000001) | ||
1747 | break; | ||
1748 | } | ||
1749 | else if (aadj < .4999999/FLT_RADIX) | ||
1750 | break; | ||
1751 | } | ||
1752 | cont: | ||
1753 | Bfree(bb); | ||
1754 | Bfree(bd); | ||
1755 | Bfree(bs); | ||
1756 | Bfree(delta); | ||
1757 | } | ||
1758 | retfree: | ||
1759 | Bfree(bb); | ||
1760 | Bfree(bd); | ||
1761 | Bfree(bs); | ||
1762 | Bfree(bd0); | ||
1763 | Bfree(delta); | ||
1764 | ret: | ||
1765 | if (se) | ||
1766 | *se = (char *)s; | ||
1767 | return sign ? -value(rv) : value(rv); | ||
1768 | } | ||
1769 | |||
1770 | static int | ||
1771 | quorem | ||
1772 | #ifdef KR_headers | ||
1773 | (b, S) Bigint *b, *S; | ||
1774 | #else | ||
1775 | (Bigint *b, Bigint *S) | ||
1776 | #endif | ||
1777 | { | ||
1778 | int n; | ||
1779 | Long borrow, y; | ||
1780 | ULong carry, q, ys; | ||
1781 | ULong *bx, *bxe, *sx, *sxe; | ||
1782 | #ifdef Pack_32 | ||
1783 | Long z; | ||
1784 | ULong si, zs; | ||
1785 | #endif | ||
1786 | |||
1787 | n = S->wds; | ||
1788 | #ifdef DEBUG | ||
1789 | /*debug*/ if (b->wds > n) | ||
1790 | /*debug*/ Bug("oversize b in quorem"); | ||
1791 | #endif | ||
1792 | if (b->wds < n) | ||
1793 | return 0; | ||
1794 | sx = S->x; | ||
1795 | sxe = sx + --n; | ||
1796 | bx = b->x; | ||
1797 | bxe = bx + n; | ||
1798 | q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ | ||
1799 | #ifdef DEBUG | ||
1800 | /*debug*/ if (q > 9) | ||
1801 | /*debug*/ Bug("oversized quotient in quorem"); | ||
1802 | #endif | ||
1803 | if (q) { | ||
1804 | borrow = 0; | ||
1805 | carry = 0; | ||
1806 | do { | ||
1807 | #ifdef Pack_32 | ||
1808 | si = *sx++; | ||
1809 | ys = (si & 0xffff) * q + carry; | ||
1810 | zs = (si >> 16) * q + (ys >> 16); | ||
1811 | carry = zs >> 16; | ||
1812 | y = (*bx & 0xffff) - (ys & 0xffff) + borrow; | ||
1813 | borrow = y >> 16; | ||
1814 | Sign_Extend(borrow, y); | ||
1815 | z = (*bx >> 16) - (zs & 0xffff) + borrow; | ||
1816 | borrow = z >> 16; | ||
1817 | Sign_Extend(borrow, z); | ||
1818 | Storeinc(bx, z, y); | ||
1819 | #else | ||
1820 | ys = *sx++ * q + carry; | ||
1821 | carry = ys >> 16; | ||
1822 | y = *bx - (ys & 0xffff) + borrow; | ||
1823 | borrow = y >> 16; | ||
1824 | Sign_Extend(borrow, y); | ||
1825 | *bx++ = y & 0xffff; | ||
1826 | #endif | ||
1827 | } | ||
1828 | while(sx <= sxe); | ||
1829 | if (!*bxe) { | ||
1830 | bx = b->x; | ||
1831 | while(--bxe > bx && !*bxe) | ||
1832 | --n; | ||
1833 | b->wds = n; | ||
1834 | } | ||
1835 | } | ||
1836 | if (cmp(b, S) >= 0) { | ||
1837 | q++; | ||
1838 | borrow = 0; | ||
1839 | carry = 0; | ||
1840 | bx = b->x; | ||
1841 | sx = S->x; | ||
1842 | do { | ||
1843 | #ifdef Pack_32 | ||
1844 | si = *sx++; | ||
1845 | ys = (si & 0xffff) + carry; | ||
1846 | zs = (si >> 16) + (ys >> 16); | ||
1847 | carry = zs >> 16; | ||
1848 | y = (*bx & 0xffff) - (ys & 0xffff) + borrow; | ||
1849 | borrow = y >> 16; | ||
1850 | Sign_Extend(borrow, y); | ||
1851 | z = (*bx >> 16) - (zs & 0xffff) + borrow; | ||
1852 | borrow = z >> 16; | ||
1853 | Sign_Extend(borrow, z); | ||
1854 | Storeinc(bx, z, y); | ||
1855 | #else | ||
1856 | ys = *sx++ + carry; | ||
1857 | carry = ys >> 16; | ||
1858 | y = *bx - (ys & 0xffff) + borrow; | ||
1859 | borrow = y >> 16; | ||
1860 | Sign_Extend(borrow, y); | ||
1861 | *bx++ = y & 0xffff; | ||
1862 | #endif | ||
1863 | } | ||
1864 | while(sx <= sxe); | ||
1865 | bx = b->x; | ||
1866 | bxe = bx + n; | ||
1867 | if (!*bxe) { | ||
1868 | while(--bxe > bx && !*bxe) | ||
1869 | --n; | ||
1870 | b->wds = n; | ||
1871 | } | ||
1872 | } | ||
1873 | return q; | ||
1874 | } | ||
1875 | |||
1876 | /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. | ||
1877 | * | ||
1878 | * Inspired by "How to Print Floating-Point Numbers Accurately" by | ||
1879 | * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101]. | ||
1880 | * | ||
1881 | * Modifications: | ||
1882 | * 1. Rather than iterating, we use a simple numeric overestimate | ||
1883 | * to determine k = floor(log10(d)). We scale relevant | ||
1884 | * quantities using O(log2(k)) rather than O(k) multiplications. | ||
1885 | * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't | ||
1886 | * try to generate digits strictly left to right. Instead, we | ||
1887 | * compute with fewer bits and propagate the carry if necessary | ||
1888 | * when rounding the final digit up. This is often faster. | ||
1889 | * 3. Under the assumption that input will be rounded nearest, | ||
1890 | * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. | ||
1891 | * That is, we allow equality in stopping tests when the | ||
1892 | * round-nearest rule will give the same floating-point value | ||
1893 | * as would satisfaction of the stopping test with strict | ||
1894 | * inequality. | ||
1895 | * 4. We remove common factors of powers of 2 from relevant | ||
1896 | * quantities. | ||
1897 | * 5. When converting floating-point integers less than 1e16, | ||
1898 | * we use floating-point arithmetic rather than resorting | ||
1899 | * to multiple-precision integers. | ||
1900 | * 6. When asked to produce fewer than 15 digits, we first try | ||
1901 | * to get by with floating-point arithmetic; we resort to | ||
1902 | * multiple-precision integer arithmetic only if we cannot | ||
1903 | * guarantee that the floating-point calculation has given | ||
1904 | * the correctly rounded result. For k requested digits and | ||
1905 | * "uniformly" distributed input, the probability is | ||
1906 | * something like 10^(k-15) that we must resort to the Long | ||
1907 | * calculation. | ||
1908 | */ | ||
1909 | |||
1910 | char * | ||
1911 | __dtoa | ||
1912 | #ifdef KR_headers | ||
1913 | (_d, mode, ndigits, decpt, sign, rve) | ||
1914 | double _d; int mode, ndigits, *decpt, *sign; char **rve; | ||
1915 | #else | ||
1916 | (double _d, int mode, int ndigits, int *decpt, int *sign, char **rve) | ||
1917 | #endif | ||
1918 | { | ||
1919 | /* Arguments ndigits, decpt, sign are similar to those | ||
1920 | of ecvt and fcvt; trailing zeros are suppressed from | ||
1921 | the returned string. If not null, *rve is set to point | ||
1922 | to the end of the return value. If d is +-Infinity or NaN, | ||
1923 | then *decpt is set to 9999. | ||
1924 | |||
1925 | mode: | ||
1926 | 0 ==> shortest string that yields d when read in | ||
1927 | and rounded to nearest. | ||
1928 | 1 ==> like 0, but with Steele & White stopping rule; | ||
1929 | e.g. with IEEE P754 arithmetic , mode 0 gives | ||
1930 | 1e23 whereas mode 1 gives 9.999999999999999e22. | ||
1931 | 2 ==> max(1,ndigits) significant digits. This gives a | ||
1932 | return value similar to that of ecvt, except | ||
1933 | that trailing zeros are suppressed. | ||
1934 | 3 ==> through ndigits past the decimal point. This | ||
1935 | gives a return value similar to that from fcvt, | ||
1936 | except that trailing zeros are suppressed, and | ||
1937 | ndigits can be negative. | ||
1938 | 4-9 should give the same return values as 2-3, i.e., | ||
1939 | 4 <= mode <= 9 ==> same return as mode | ||
1940 | 2 + (mode & 1). These modes are mainly for | ||
1941 | debugging; often they run slower but sometimes | ||
1942 | faster than modes 2-3. | ||
1943 | 4,5,8,9 ==> left-to-right digit generation. | ||
1944 | 6-9 ==> don't try fast floating-point estimate | ||
1945 | (if applicable). | ||
1946 | |||
1947 | Values of mode other than 0-9 are treated as mode 0. | ||
1948 | |||
1949 | Sufficient space is allocated to the return value | ||
1950 | to hold the suppressed trailing zeros. | ||
1951 | */ | ||
1952 | |||
1953 | int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, | ||
1954 | j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, | ||
1955 | spec_case, try_quick; | ||
1956 | Long L; | ||
1957 | #ifndef Sudden_Underflow | ||
1958 | int denorm; | ||
1959 | ULong x; | ||
1960 | #endif | ||
1961 | Bigint *b, *b1, *delta, *mlo, *mhi, *S; | ||
1962 | double ds; | ||
1963 | char *s, *s0; | ||
1964 | static Bigint *result; | ||
1965 | static int result_k; | ||
1966 | _double d, d2, eps; | ||
1967 | |||
1968 | value(d) = _d; | ||
1969 | if (result) { | ||
1970 | result->k = result_k; | ||
1971 | result->maxwds = 1 << result_k; | ||
1972 | Bfree(result); | ||
1973 | result = 0; | ||
1974 | } | ||
1975 | |||
1976 | if (word0(d) & Sign_bit) { | ||
1977 | /* set sign for everything, including 0's and NaNs */ | ||
1978 | *sign = 1; | ||
1979 | word0(d) &= ~Sign_bit; /* clear sign bit */ | ||
1980 | } | ||
1981 | else | ||
1982 | *sign = 0; | ||
1983 | |||
1984 | #if defined(IEEE_Arith) + defined(VAX) | ||
1985 | #ifdef IEEE_Arith | ||
1986 | if ((word0(d) & Exp_mask) == Exp_mask) | ||
1987 | #else | ||
1988 | if (word0(d) == 0x8000) | ||
1989 | #endif | ||
1990 | { | ||
1991 | /* Infinity or NaN */ | ||
1992 | *decpt = 9999; | ||
1993 | s = | ||
1994 | #ifdef IEEE_Arith | ||
1995 | !word1(d) && !(word0(d) & 0xfffff) ? ndigits < 8 ? "Inf" : "Infinity" : | ||
1996 | #endif | ||
1997 | "NaN"; | ||
1998 | if (rve) | ||
1999 | *rve = | ||
2000 | #ifdef IEEE_Arith | ||
2001 | s[3] ? s + 8 : | ||
2002 | #endif | ||
2003 | s + 3; | ||
2004 | return s; | ||
2005 | } | ||
2006 | #endif | ||
2007 | #ifdef IBM | ||
2008 | value(d) += 0; /* normalize */ | ||
2009 | #endif | ||
2010 | if (!value(d)) { | ||
2011 | *decpt = 1; | ||
2012 | s = "0"; | ||
2013 | if (rve) | ||
2014 | *rve = s + 1; | ||
2015 | return s; | ||
2016 | } | ||
2017 | |||
2018 | b = d2b(value(d), &be, &bbits); | ||
2019 | #ifdef Sudden_Underflow | ||
2020 | i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); | ||
2021 | #else | ||
2022 | if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { | ||
2023 | #endif | ||
2024 | value(d2) = value(d); | ||
2025 | word0(d2) &= Frac_mask1; | ||
2026 | word0(d2) |= Exp_11; | ||
2027 | #ifdef IBM | ||
2028 | if (j = 11 - hi0bits(word0(d2) & Frac_mask)) | ||
2029 | value(d2) /= 1 << j; | ||
2030 | #endif | ||
2031 | |||
2032 | /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 | ||
2033 | * log10(x) = log(x) / log(10) | ||
2034 | * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) | ||
2035 | * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) | ||
2036 | * | ||
2037 | * This suggests computing an approximation k to log10(d) by | ||
2038 | * | ||
2039 | * k = (i - Bias)*0.301029995663981 | ||
2040 | * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); | ||
2041 | * | ||
2042 | * We want k to be too large rather than too small. | ||
2043 | * The error in the first-order Taylor series approximation | ||
2044 | * is in our favor, so we just round up the constant enough | ||
2045 | * to compensate for any error in the multiplication of | ||
2046 | * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, | ||
2047 | * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, | ||
2048 | * adding 1e-13 to the constant term more than suffices. | ||
2049 | * Hence we adjust the constant term to 0.1760912590558. | ||
2050 | * (We could get a more accurate k by invoking log10, | ||
2051 | * but this is probably not worthwhile.) | ||
2052 | */ | ||
2053 | |||
2054 | i -= Bias; | ||
2055 | #ifdef IBM | ||
2056 | i <<= 2; | ||
2057 | i += j; | ||
2058 | #endif | ||
2059 | #ifndef Sudden_Underflow | ||
2060 | denorm = 0; | ||
2061 | } | ||
2062 | else { | ||
2063 | /* d is denormalized */ | ||
2064 | |||
2065 | i = bbits + be + (Bias + (P-1) - 1); | ||
2066 | x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 | ||
2067 | : word1(d) << 32 - i; | ||
2068 | value(d2) = x; | ||
2069 | word0(d2) -= 31*Exp_msk1; /* adjust exponent */ | ||
2070 | i -= (Bias + (P-1) - 1) + 1; | ||
2071 | denorm = 1; | ||
2072 | } | ||
2073 | #endif | ||
2074 | ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + | ||
2075 | i*0.301029995663981; | ||
2076 | k = (int)ds; | ||
2077 | if (ds < 0. && ds != k) | ||
2078 | k--; /* want k = floor(ds) */ | ||
2079 | k_check = 1; | ||
2080 | if (k >= 0 && k <= Ten_pmax) { | ||
2081 | if (value(d) < tens[k]) | ||
2082 | k--; | ||
2083 | k_check = 0; | ||
2084 | } | ||
2085 | j = bbits - i - 1; | ||
2086 | if (j >= 0) { | ||
2087 | b2 = 0; | ||
2088 | s2 = j; | ||
2089 | } | ||
2090 | else { | ||
2091 | b2 = -j; | ||
2092 | s2 = 0; | ||
2093 | } | ||
2094 | if (k >= 0) { | ||
2095 | b5 = 0; | ||
2096 | s5 = k; | ||
2097 | s2 += k; | ||
2098 | } | ||
2099 | else { | ||
2100 | b2 -= k; | ||
2101 | b5 = -k; | ||
2102 | s5 = 0; | ||
2103 | } | ||
2104 | if (mode < 0 || mode > 9) | ||
2105 | mode = 0; | ||
2106 | try_quick = 1; | ||
2107 | if (mode > 5) { | ||
2108 | mode -= 4; | ||
2109 | try_quick = 0; | ||
2110 | } | ||
2111 | leftright = 1; | ||
2112 | switch(mode) { | ||
2113 | case 0: | ||
2114 | case 1: | ||
2115 | ilim = ilim1 = -1; | ||
2116 | i = 18; | ||
2117 | ndigits = 0; | ||
2118 | break; | ||
2119 | case 2: | ||
2120 | leftright = 0; | ||
2121 | /* no break */ | ||
2122 | case 4: | ||
2123 | if (ndigits <= 0) | ||
2124 | ndigits = 1; | ||
2125 | ilim = ilim1 = i = ndigits; | ||
2126 | break; | ||
2127 | case 3: | ||
2128 | leftright = 0; | ||
2129 | /* no break */ | ||
2130 | case 5: | ||
2131 | i = ndigits + k + 1; | ||
2132 | ilim = i; | ||
2133 | ilim1 = i - 1; | ||
2134 | if (i <= 0) | ||
2135 | i = 1; | ||
2136 | } | ||
2137 | j = sizeof(ULong); | ||
2138 | for(result_k = 0; sizeof(Bigint) - sizeof(ULong) + j <= i; | ||
2139 | j <<= 1) result_k++; | ||
2140 | result = Balloc(result_k); | ||
2141 | s = s0 = (char *)result; | ||
2142 | |||
2143 | if (ilim >= 0 && ilim <= Quick_max && try_quick) { | ||
2144 | |||
2145 | /* Try to get by with floating-point arithmetic. */ | ||
2146 | |||
2147 | i = 0; | ||
2148 | value(d2) = value(d); | ||
2149 | k0 = k; | ||
2150 | ilim0 = ilim; | ||
2151 | ieps = 2; /* conservative */ | ||
2152 | if (k > 0) { | ||
2153 | ds = tens[k&0xf]; | ||
2154 | j = k >> 4; | ||
2155 | if (j & Bletch) { | ||
2156 | /* prevent overflows */ | ||
2157 | j &= Bletch - 1; | ||
2158 | value(d) /= bigtens[n_bigtens-1]; | ||
2159 | ieps++; | ||
2160 | } | ||
2161 | for(; j; j >>= 1, i++) | ||
2162 | if (j & 1) { | ||
2163 | ieps++; | ||
2164 | ds *= bigtens[i]; | ||
2165 | } | ||
2166 | value(d) /= ds; | ||
2167 | } | ||
2168 | else if (j1 = -k) { | ||
2169 | value(d) *= tens[j1 & 0xf]; | ||
2170 | for(j = j1 >> 4; j; j >>= 1, i++) | ||
2171 | if (j & 1) { | ||
2172 | ieps++; | ||
2173 | value(d) *= bigtens[i]; | ||
2174 | } | ||
2175 | } | ||
2176 | if (k_check && value(d) < 1. && ilim > 0) { | ||
2177 | if (ilim1 <= 0) | ||
2178 | goto fast_failed; | ||
2179 | ilim = ilim1; | ||
2180 | k--; | ||
2181 | value(d) *= 10.; | ||
2182 | ieps++; | ||
2183 | } | ||
2184 | value(eps) = ieps*value(d) + 7.; | ||
2185 | word0(eps) -= (P-1)*Exp_msk1; | ||
2186 | if (ilim == 0) { | ||
2187 | S = mhi = 0; | ||
2188 | value(d) -= 5.; | ||
2189 | if (value(d) > value(eps)) | ||
2190 | goto one_digit; | ||
2191 | if (value(d) < -value(eps)) | ||
2192 | goto no_digits; | ||
2193 | goto fast_failed; | ||
2194 | } | ||
2195 | #ifndef No_leftright | ||
2196 | if (leftright) { | ||
2197 | /* Use Steele & White method of only | ||
2198 | * generating digits needed. | ||
2199 | */ | ||
2200 | value(eps) = 0.5/tens[ilim-1] - value(eps); | ||
2201 | for(i = 0;;) { | ||
2202 | L = value(d); | ||
2203 | value(d) -= L; | ||
2204 | *s++ = '0' + (int)L; | ||
2205 | if (value(d) < value(eps)) | ||
2206 | goto ret1; | ||
2207 | if (1. - value(d) < value(eps)) | ||
2208 | goto bump_up; | ||
2209 | if (++i >= ilim) | ||
2210 | break; | ||
2211 | value(eps) *= 10.; | ||
2212 | value(d) *= 10.; | ||
2213 | } | ||
2214 | } | ||
2215 | else { | ||
2216 | #endif | ||
2217 | /* Generate ilim digits, then fix them up. */ | ||
2218 | value(eps) *= tens[ilim-1]; | ||
2219 | for(i = 1;; i++, value(d) *= 10.) { | ||
2220 | L = value(d); | ||
2221 | value(d) -= L; | ||
2222 | *s++ = '0' + (int)L; | ||
2223 | if (i == ilim) { | ||
2224 | if (value(d) > 0.5 + value(eps)) | ||
2225 | goto bump_up; | ||
2226 | else if (value(d) < 0.5 - value(eps)) { | ||
2227 | while(*--s == '0'); | ||
2228 | s++; | ||
2229 | goto ret1; | ||
2230 | } | ||
2231 | break; | ||
2232 | } | ||
2233 | } | ||
2234 | #ifndef No_leftright | ||
2235 | } | ||
2236 | #endif | ||
2237 | fast_failed: | ||
2238 | s = s0; | ||
2239 | value(d) = value(d2); | ||
2240 | k = k0; | ||
2241 | ilim = ilim0; | ||
2242 | } | ||
2243 | |||
2244 | /* Do we have a "small" integer? */ | ||
2245 | |||
2246 | if (be >= 0 && k <= Int_max) { | ||
2247 | /* Yes. */ | ||
2248 | ds = tens[k]; | ||
2249 | if (ndigits < 0 && ilim <= 0) { | ||
2250 | S = mhi = 0; | ||
2251 | if (ilim < 0 || value(d) <= 5*ds) | ||
2252 | goto no_digits; | ||
2253 | goto one_digit; | ||
2254 | } | ||
2255 | for(i = 1;; i++) { | ||
2256 | L = value(d) / ds; | ||
2257 | value(d) -= L*ds; | ||
2258 | #ifdef Check_FLT_ROUNDS | ||
2259 | /* If FLT_ROUNDS == 2, L will usually be high by 1 */ | ||
2260 | if (value(d) < 0) { | ||
2261 | L--; | ||
2262 | value(d) += ds; | ||
2263 | } | ||
2264 | #endif | ||
2265 | *s++ = '0' + (int)L; | ||
2266 | if (i == ilim) { | ||
2267 | value(d) += value(d); | ||
2268 | if (value(d) > ds || value(d) == ds && L & 1) { | ||
2269 | bump_up: | ||
2270 | while(*--s == '9') | ||
2271 | if (s == s0) { | ||
2272 | k++; | ||
2273 | *s = '0'; | ||
2274 | break; | ||
2275 | } | ||
2276 | ++*s++; | ||
2277 | } | ||
2278 | break; | ||
2279 | } | ||
2280 | if (!(value(d) *= 10.)) | ||
2281 | break; | ||
2282 | } | ||
2283 | goto ret1; | ||
2284 | } | ||
2285 | |||
2286 | m2 = b2; | ||
2287 | m5 = b5; | ||
2288 | mhi = mlo = 0; | ||
2289 | if (leftright) { | ||
2290 | if (mode < 2) { | ||
2291 | i = | ||
2292 | #ifndef Sudden_Underflow | ||
2293 | denorm ? be + (Bias + (P-1) - 1 + 1) : | ||
2294 | #endif | ||
2295 | #ifdef IBM | ||
2296 | 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); | ||
2297 | #else | ||
2298 | 1 + P - bbits; | ||
2299 | #endif | ||
2300 | } | ||
2301 | else { | ||
2302 | j = ilim - 1; | ||
2303 | if (m5 >= j) | ||
2304 | m5 -= j; | ||
2305 | else { | ||
2306 | s5 += j -= m5; | ||
2307 | b5 += j; | ||
2308 | m5 = 0; | ||
2309 | } | ||
2310 | if ((i = ilim) < 0) { | ||
2311 | m2 -= i; | ||
2312 | i = 0; | ||
2313 | } | ||
2314 | } | ||
2315 | b2 += i; | ||
2316 | s2 += i; | ||
2317 | mhi = i2b(1); | ||
2318 | } | ||
2319 | if (m2 > 0 && s2 > 0) { | ||
2320 | i = m2 < s2 ? m2 : s2; | ||
2321 | b2 -= i; | ||
2322 | m2 -= i; | ||
2323 | s2 -= i; | ||
2324 | } | ||
2325 | if (b5 > 0) { | ||
2326 | if (leftright) { | ||
2327 | if (m5 > 0) { | ||
2328 | mhi = pow5mult(mhi, m5); | ||
2329 | b1 = mult(mhi, b); | ||
2330 | Bfree(b); | ||
2331 | b = b1; | ||
2332 | } | ||
2333 | if (j = b5 - m5) | ||
2334 | b = pow5mult(b, j); | ||
2335 | } | ||
2336 | else | ||
2337 | b = pow5mult(b, b5); | ||
2338 | } | ||
2339 | S = i2b(1); | ||
2340 | if (s5 > 0) | ||
2341 | S = pow5mult(S, s5); | ||
2342 | |||
2343 | /* Check for special case that d is a normalized power of 2. */ | ||
2344 | |||
2345 | if (mode < 2) { | ||
2346 | if (!word1(d) && !(word0(d) & Bndry_mask) | ||
2347 | #ifndef Sudden_Underflow | ||
2348 | && word0(d) & Exp_mask | ||
2349 | #endif | ||
2350 | ) { | ||
2351 | /* The special case */ | ||
2352 | b2 += Log2P; | ||
2353 | s2 += Log2P; | ||
2354 | spec_case = 1; | ||
2355 | } | ||
2356 | else | ||
2357 | spec_case = 0; | ||
2358 | } | ||
2359 | |||
2360 | /* Arrange for convenient computation of quotients: | ||
2361 | * shift left if necessary so divisor has 4 leading 0 bits. | ||
2362 | * | ||
2363 | * Perhaps we should just compute leading 28 bits of S once | ||
2364 | * and for all and pass them and a shift to quorem, so it | ||
2365 | * can do shifts and ors to compute the numerator for q. | ||
2366 | */ | ||
2367 | #ifdef Pack_32 | ||
2368 | if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) | ||
2369 | i = 32 - i; | ||
2370 | #else | ||
2371 | if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) | ||
2372 | i = 16 - i; | ||
2373 | #endif | ||
2374 | if (i > 4) { | ||
2375 | i -= 4; | ||
2376 | b2 += i; | ||
2377 | m2 += i; | ||
2378 | s2 += i; | ||
2379 | } | ||
2380 | else if (i < 4) { | ||
2381 | i += 28; | ||
2382 | b2 += i; | ||
2383 | m2 += i; | ||
2384 | s2 += i; | ||
2385 | } | ||
2386 | if (b2 > 0) | ||
2387 | b = lshift(b, b2); | ||
2388 | if (s2 > 0) | ||
2389 | S = lshift(S, s2); | ||
2390 | if (k_check) { | ||
2391 | if (cmp(b,S) < 0) { | ||
2392 | k--; | ||
2393 | b = multadd(b, 10, 0); /* we botched the k estimate */ | ||
2394 | if (leftright) | ||
2395 | mhi = multadd(mhi, 10, 0); | ||
2396 | ilim = ilim1; | ||
2397 | } | ||
2398 | } | ||
2399 | if (ilim <= 0 && mode > 2) { | ||
2400 | if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { | ||
2401 | /* no digits, fcvt style */ | ||
2402 | no_digits: | ||
2403 | k = -1 - ndigits; | ||
2404 | goto ret; | ||
2405 | } | ||
2406 | one_digit: | ||
2407 | *s++ = '1'; | ||
2408 | k++; | ||
2409 | goto ret; | ||
2410 | } | ||
2411 | if (leftright) { | ||
2412 | if (m2 > 0) | ||
2413 | mhi = lshift(mhi, m2); | ||
2414 | |||
2415 | /* Compute mlo -- check for special case | ||
2416 | * that d is a normalized power of 2. | ||
2417 | */ | ||
2418 | |||
2419 | mlo = mhi; | ||
2420 | if (spec_case) { | ||
2421 | mhi = Balloc(mhi->k); | ||
2422 | Bcopy(mhi, mlo); | ||
2423 | mhi = lshift(mhi, Log2P); | ||
2424 | } | ||
2425 | |||
2426 | for(i = 1;;i++) { | ||
2427 | dig = quorem(b,S) + '0'; | ||
2428 | /* Do we yet have the shortest decimal string | ||
2429 | * that will round to d? | ||
2430 | */ | ||
2431 | j = cmp(b, mlo); | ||
2432 | delta = diff(S, mhi); | ||
2433 | j1 = delta->sign ? 1 : cmp(b, delta); | ||
2434 | Bfree(delta); | ||
2435 | #ifndef ROUND_BIASED | ||
2436 | if (j1 == 0 && !mode && !(word1(d) & 1)) { | ||
2437 | if (dig == '9') | ||
2438 | goto round_9_up; | ||
2439 | if (j > 0) | ||
2440 | dig++; | ||
2441 | *s++ = dig; | ||
2442 | goto ret; | ||
2443 | } | ||
2444 | #endif | ||
2445 | if (j < 0 || j == 0 && !mode | ||
2446 | #ifndef ROUND_BIASED | ||
2447 | && !(word1(d) & 1) | ||
2448 | #endif | ||
2449 | ) { | ||
2450 | if (j1 > 0) { | ||
2451 | b = lshift(b, 1); | ||
2452 | j1 = cmp(b, S); | ||
2453 | if ((j1 > 0 || j1 == 0 && dig & 1) | ||
2454 | && dig++ == '9') | ||
2455 | goto round_9_up; | ||
2456 | } | ||
2457 | *s++ = dig; | ||
2458 | goto ret; | ||
2459 | } | ||
2460 | if (j1 > 0) { | ||
2461 | if (dig == '9') { /* possible if i == 1 */ | ||
2462 | round_9_up: | ||
2463 | *s++ = '9'; | ||
2464 | goto roundoff; | ||
2465 | } | ||
2466 | *s++ = dig + 1; | ||
2467 | goto ret; | ||
2468 | } | ||
2469 | *s++ = dig; | ||
2470 | if (i == ilim) | ||
2471 | break; | ||
2472 | b = multadd(b, 10, 0); | ||
2473 | if (mlo == mhi) | ||
2474 | mlo = mhi = multadd(mhi, 10, 0); | ||
2475 | else { | ||
2476 | mlo = multadd(mlo, 10, 0); | ||
2477 | mhi = multadd(mhi, 10, 0); | ||
2478 | } | ||
2479 | } | ||
2480 | } | ||
2481 | else | ||
2482 | for(i = 1;; i++) { | ||
2483 | *s++ = dig = quorem(b,S) + '0'; | ||
2484 | if (i >= ilim) | ||
2485 | break; | ||
2486 | b = multadd(b, 10, 0); | ||
2487 | } | ||
2488 | |||
2489 | /* Round off last digit */ | ||
2490 | |||
2491 | b = lshift(b, 1); | ||
2492 | j = cmp(b, S); | ||
2493 | if (j > 0 || j == 0 && dig & 1) { | ||
2494 | roundoff: | ||
2495 | while(*--s == '9') | ||
2496 | if (s == s0) { | ||
2497 | k++; | ||
2498 | *s++ = '1'; | ||
2499 | goto ret; | ||
2500 | } | ||
2501 | ++*s++; | ||
2502 | } | ||
2503 | else { | ||
2504 | while(*--s == '0'); | ||
2505 | s++; | ||
2506 | } | ||
2507 | ret: | ||
2508 | Bfree(S); | ||
2509 | if (mhi) { | ||
2510 | if (mlo && mlo != mhi) | ||
2511 | Bfree(mlo); | ||
2512 | Bfree(mhi); | ||
2513 | } | ||
2514 | ret1: | ||
2515 | Bfree(b); | ||
2516 | if (s == s0) { /* don't return empty string */ | ||
2517 | *s++ = '0'; | ||
2518 | k = 0; | ||
2519 | } | ||
2520 | *s = 0; | ||
2521 | *decpt = k + 1; | ||
2522 | if (rve) | ||
2523 | *rve = s; | ||
2524 | return s0; | ||
2525 | } | ||
2526 | #ifdef __cplusplus | ||
2527 | } | ||
2528 | #endif | ||
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 new file mode 100644 index 0000000000..3f5375c5e3 --- /dev/null +++ b/src/lib/libc/stdlib/strtol.3 | |||
@@ -0,0 +1,247 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strtol.3,v 1.13 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 25, 1992 | ||
35 | .Dt STRTOL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtol , | ||
39 | .Nm strtoll , | ||
40 | .Nm strtoq | ||
41 | .Nd "convert string value to a long or long long integer" | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Fd #include <limits.h> | ||
45 | .Ft long | ||
46 | .Fn strtol "const char *nptr" "char **endptr" "int base" | ||
47 | .Pp | ||
48 | .Fd #include <stdlib.h> | ||
49 | .Fd #include <limits.h> | ||
50 | .Ft long long | ||
51 | .Fn strtoll "const char *nptr" "char **endptr" "int base" | ||
52 | .Pp | ||
53 | .Fd #include <sys/types.h> | ||
54 | .Fd #include <stdlib.h> | ||
55 | .Fd #include <limits.h> | ||
56 | .Ft quad_t | ||
57 | .Fn strtoq "const char *nptr" "char **endptr" "int base" | ||
58 | .Sh DESCRIPTION | ||
59 | The | ||
60 | .Fn strtol | ||
61 | function converts the string in | ||
62 | .Fa nptr | ||
63 | to a | ||
64 | .Li long | ||
65 | value. | ||
66 | The | ||
67 | .Fn strtoll | ||
68 | function converts the string in | ||
69 | .Fa nptr | ||
70 | to a | ||
71 | .Li long long | ||
72 | value. | ||
73 | The | ||
74 | .Fn strtoq | ||
75 | function is a deprecated equivalent of | ||
76 | .Fn strtoll | ||
77 | and is provided for backwards compatibility with legacy programs. | ||
78 | The conversion is done according to the given | ||
79 | .Fa base , | ||
80 | which must be a number between 2 and 36 inclusive or the special value 0. | ||
81 | .Pp | ||
82 | The string may begin with an arbitrary amount of whitespace | ||
83 | (as determined by | ||
84 | .Xr isspace 3 ) | ||
85 | followed by a single optional | ||
86 | .Ql + | ||
87 | or | ||
88 | .Ql - | ||
89 | sign. | ||
90 | If | ||
91 | .Fa base | ||
92 | is zero or 16, the string may then include a | ||
93 | .Ql 0x | ||
94 | prefix, and the number will be read in base 16; otherwise, a zero | ||
95 | .Fa base | ||
96 | is taken as 10 (decimal) unless the next character is | ||
97 | .Ql 0 , | ||
98 | in which case it is taken as 8 (octal). | ||
99 | .Pp | ||
100 | The remainder of the string is converted to a | ||
101 | .Li long | ||
102 | value in the obvious manner, | ||
103 | stopping at the first character which is not a valid digit | ||
104 | in the given base. | ||
105 | (In bases above 10, the letter | ||
106 | .Ql A | ||
107 | in either upper or lower case represents 10, | ||
108 | .Ql B | ||
109 | represents 11, and so forth, with | ||
110 | .Ql Z | ||
111 | representing 35.) | ||
112 | .Pp | ||
113 | If | ||
114 | .Fa endptr | ||
115 | is non-null, | ||
116 | .Fn strtol | ||
117 | stores the address of the first invalid character in | ||
118 | .Fa *endptr . | ||
119 | If there were no digits at all, however, | ||
120 | .Fn strtol | ||
121 | stores the original value of | ||
122 | .Fa nptr | ||
123 | in | ||
124 | .Fa *endptr . | ||
125 | (Thus, if | ||
126 | .Fa *nptr | ||
127 | is not | ||
128 | .Ql \e0 | ||
129 | but | ||
130 | .Fa **endptr | ||
131 | is | ||
132 | .Ql \e0 | ||
133 | on return, the entire string was valid.) | ||
134 | .Sh RETURN VALUES | ||
135 | The | ||
136 | .Fn strtol | ||
137 | function returns the result of the conversion, | ||
138 | unless the value would underflow or overflow. | ||
139 | If an underflow occurs, | ||
140 | .Fn strtol | ||
141 | returns | ||
142 | .Dv LONG_MIN . | ||
143 | If an overflow occurs, | ||
144 | .Fn strtol | ||
145 | returns | ||
146 | .Dv LONG_MAX . | ||
147 | In both cases, | ||
148 | .Va errno | ||
149 | is set to | ||
150 | .Er ERANGE . | ||
151 | .Pp | ||
152 | The | ||
153 | .Fn strtoll | ||
154 | function has identical return values except that | ||
155 | .Dv LLONG_MIN | ||
156 | and | ||
157 | .Dv LLONG_MAX | ||
158 | are used to indicate underflow and overflow respectively. | ||
159 | .Sh EXAMPLES | ||
160 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
161 | trailing characters) requires clearing | ||
162 | .Va errno | ||
163 | beforehand explicitly since | ||
164 | .Va errno | ||
165 | is not changed on a successful call to | ||
166 | .Fn strtol , | ||
167 | and the return value of | ||
168 | .Fn strtol | ||
169 | cannot be used unambiguously to signal an error: | ||
170 | .Bd -literal -offset indent | ||
171 | char *ep; | ||
172 | long lval; | ||
173 | |||
174 | \&... | ||
175 | |||
176 | errno = 0; | ||
177 | lval = strtol(buf, &ep, 10); | ||
178 | if (buf[0] == '\e0' || *ep != '\e0') | ||
179 | goto not_a_number; | ||
180 | if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) | ||
181 | goto out_of_range; | ||
182 | .Ed | ||
183 | .Pp | ||
184 | This example will accept | ||
185 | .Dq 12 | ||
186 | but not | ||
187 | .Dq 12foo | ||
188 | or | ||
189 | .Dq 12\en . | ||
190 | If trailing whitespace is acceptable, further checks must be done on | ||
191 | .Va *ep ; | ||
192 | alternately, use | ||
193 | .Xr sscanf 3 . | ||
194 | .Pp | ||
195 | If | ||
196 | .Fn strtol | ||
197 | is being used instead of | ||
198 | .Xr atoi 3 , | ||
199 | error checking is further complicated because the desired return value is an | ||
200 | .Li int | ||
201 | rather than a | ||
202 | .Li long ; | ||
203 | however, on some architectures integers and long integers are the same size. | ||
204 | Thus the following is necessary: | ||
205 | .Bd -literal -offset indent | ||
206 | char *ep; | ||
207 | int ival; | ||
208 | long lval; | ||
209 | |||
210 | \&... | ||
211 | |||
212 | errno = 0; | ||
213 | lval = strtol(buf, &ep, 10); | ||
214 | if (buf[0] == '\e0' || *ep != '\e0') | ||
215 | goto not_a_number; | ||
216 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | ||
217 | (lval > INT_MAX || lval < INT_MIN)) | ||
218 | goto out_of_range; | ||
219 | ival = lval; | ||
220 | .Ed | ||
221 | .Sh ERRORS | ||
222 | .Bl -tag -width Er | ||
223 | .It Bq Er ERANGE | ||
224 | The given string was out of range; the value converted has been clamped. | ||
225 | .El | ||
226 | .Sh SEE ALSO | ||
227 | .Xr atof 3 , | ||
228 | .Xr atoi 3 , | ||
229 | .Xr atol 3 , | ||
230 | .Xr atoll 3 , | ||
231 | .Xr sscanf 3 , | ||
232 | .Xr strtod 3 , | ||
233 | .Xr strtoul 3 | ||
234 | .Sh STANDARDS | ||
235 | The | ||
236 | .Fn strtol | ||
237 | and | ||
238 | .Fn strtoll | ||
239 | functions conform to | ||
240 | .St -ansiC-99 . | ||
241 | The | ||
242 | .Fn strtoq | ||
243 | function is a | ||
244 | .Bx | ||
245 | extension and is provided for backwards compatibility with legacy programs. | ||
246 | .Sh BUGS | ||
247 | Ignores the current locale. | ||
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c new file mode 100644 index 0000000000..1c39720794 --- /dev/null +++ b/src/lib/libc/stdlib/strtol.c | |||
@@ -0,0 +1,146 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strtol.c,v 1.5 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <ctype.h> | ||
35 | #include <errno.h> | ||
36 | #include <limits.h> | ||
37 | #include <stdlib.h> | ||
38 | |||
39 | |||
40 | /* | ||
41 | * Convert a string to a long integer. | ||
42 | * | ||
43 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
44 | * alphabets and digits are each contiguous. | ||
45 | */ | ||
46 | long | ||
47 | strtol(nptr, endptr, base) | ||
48 | const char *nptr; | ||
49 | char **endptr; | ||
50 | register int base; | ||
51 | { | ||
52 | register const char *s; | ||
53 | register long acc, cutoff; | ||
54 | register int c; | ||
55 | register int neg, any, cutlim; | ||
56 | |||
57 | /* | ||
58 | * Skip white space and pick up leading +/- sign if any. | ||
59 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
60 | * assume decimal; if base is already 16, allow 0x. | ||
61 | */ | ||
62 | s = nptr; | ||
63 | do { | ||
64 | c = (unsigned char) *s++; | ||
65 | } while (isspace(c)); | ||
66 | if (c == '-') { | ||
67 | neg = 1; | ||
68 | c = *s++; | ||
69 | } else { | ||
70 | neg = 0; | ||
71 | if (c == '+') | ||
72 | c = *s++; | ||
73 | } | ||
74 | if ((base == 0 || base == 16) && | ||
75 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
76 | c = s[1]; | ||
77 | s += 2; | ||
78 | base = 16; | ||
79 | } | ||
80 | if (base == 0) | ||
81 | base = c == '0' ? 8 : 10; | ||
82 | |||
83 | /* | ||
84 | * Compute the cutoff value between legal numbers and illegal | ||
85 | * numbers. That is the largest legal value, divided by the | ||
86 | * base. An input number that is greater than this value, if | ||
87 | * followed by a legal input character, is too big. One that | ||
88 | * is equal to this value may be valid or not; the limit | ||
89 | * between valid and invalid numbers is then based on the last | ||
90 | * digit. For instance, if the range for longs is | ||
91 | * [-2147483648..2147483647] and the input base is 10, | ||
92 | * cutoff will be set to 214748364 and cutlim to either | ||
93 | * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated | ||
94 | * a value > 214748364, or equal but the next digit is > 7 (or 8), | ||
95 | * the number is too big, and we will return a range error. | ||
96 | * | ||
97 | * Set any if any `digits' consumed; make it negative to indicate | ||
98 | * overflow. | ||
99 | */ | ||
100 | cutoff = neg ? LONG_MIN : LONG_MAX; | ||
101 | cutlim = cutoff % base; | ||
102 | cutoff /= base; | ||
103 | if (neg) { | ||
104 | if (cutlim > 0) { | ||
105 | cutlim -= base; | ||
106 | cutoff += 1; | ||
107 | } | ||
108 | cutlim = -cutlim; | ||
109 | } | ||
110 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
111 | if (isdigit(c)) | ||
112 | c -= '0'; | ||
113 | else if (isalpha(c)) | ||
114 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
115 | else | ||
116 | break; | ||
117 | if (c >= base) | ||
118 | break; | ||
119 | if (any < 0) | ||
120 | continue; | ||
121 | if (neg) { | ||
122 | if (acc < cutoff || acc == cutoff && c > cutlim) { | ||
123 | any = -1; | ||
124 | acc = LONG_MIN; | ||
125 | errno = ERANGE; | ||
126 | } else { | ||
127 | any = 1; | ||
128 | acc *= base; | ||
129 | acc -= c; | ||
130 | } | ||
131 | } else { | ||
132 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
133 | any = -1; | ||
134 | acc = LONG_MAX; | ||
135 | errno = ERANGE; | ||
136 | } else { | ||
137 | any = 1; | ||
138 | acc *= base; | ||
139 | acc += c; | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | if (endptr != 0) | ||
144 | *endptr = (char *) (any ? s - 1 : nptr); | ||
145 | return (acc); | ||
146 | } | ||
diff --git a/src/lib/libc/stdlib/strtoll.c b/src/lib/libc/stdlib/strtoll.c new file mode 100644 index 0000000000..ac9353890f --- /dev/null +++ b/src/lib/libc/stdlib/strtoll.c | |||
@@ -0,0 +1,162 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1992 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static const char rcsid[] = "$OpenBSD: strtoll.c,v 1.2 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | |||
36 | #include <ctype.h> | ||
37 | #include <errno.h> | ||
38 | #include <limits.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | /* | ||
42 | * Convert a string to a long long. | ||
43 | * | ||
44 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
45 | * alphabets and digits are each contiguous. | ||
46 | */ | ||
47 | long long | ||
48 | strtoll(nptr, endptr, base) | ||
49 | const char *nptr; | ||
50 | char **endptr; | ||
51 | int base; | ||
52 | { | ||
53 | const char *s; | ||
54 | long long acc, cutoff; | ||
55 | int c; | ||
56 | int neg, any, cutlim; | ||
57 | |||
58 | /* | ||
59 | * Skip white space and pick up leading +/- sign if any. | ||
60 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
61 | * assume decimal; if base is already 16, allow 0x. | ||
62 | */ | ||
63 | s = nptr; | ||
64 | do { | ||
65 | c = (unsigned char) *s++; | ||
66 | } while (isspace(c)); | ||
67 | if (c == '-') { | ||
68 | neg = 1; | ||
69 | c = *s++; | ||
70 | } else { | ||
71 | neg = 0; | ||
72 | if (c == '+') | ||
73 | c = *s++; | ||
74 | } | ||
75 | if ((base == 0 || base == 16) && | ||
76 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
77 | c = s[1]; | ||
78 | s += 2; | ||
79 | base = 16; | ||
80 | } | ||
81 | if (base == 0) | ||
82 | base = c == '0' ? 8 : 10; | ||
83 | |||
84 | /* | ||
85 | * Compute the cutoff value between legal numbers and illegal | ||
86 | * numbers. That is the largest legal value, divided by the | ||
87 | * base. An input number that is greater than this value, if | ||
88 | * followed by a legal input character, is too big. One that | ||
89 | * is equal to this value may be valid or not; the limit | ||
90 | * between valid and invalid numbers is then based on the last | ||
91 | * digit. For instance, if the range for long longs is | ||
92 | * [-9223372036854775808..9223372036854775807] and the input base | ||
93 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | ||
94 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | ||
95 | * accumulated a value > 922337203685477580, or equal but the | ||
96 | * next digit is > 7 (or 8), the number is too big, and we will | ||
97 | * return a range error. | ||
98 | * | ||
99 | * Set any if any `digits' consumed; make it negative to indicate | ||
100 | * overflow. | ||
101 | */ | ||
102 | cutoff = neg ? LLONG_MIN : LLONG_MAX; | ||
103 | cutlim = cutoff % base; | ||
104 | cutoff /= base; | ||
105 | if (neg) { | ||
106 | if (cutlim > 0) { | ||
107 | cutlim -= base; | ||
108 | cutoff += 1; | ||
109 | } | ||
110 | cutlim = -cutlim; | ||
111 | } | ||
112 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
113 | if (isdigit(c)) | ||
114 | c -= '0'; | ||
115 | else if (isalpha(c)) | ||
116 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
117 | else | ||
118 | break; | ||
119 | if (c >= base) | ||
120 | break; | ||
121 | if (any < 0) | ||
122 | continue; | ||
123 | if (neg) { | ||
124 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { | ||
125 | any = -1; | ||
126 | acc = LLONG_MIN; | ||
127 | errno = ERANGE; | ||
128 | } else { | ||
129 | any = 1; | ||
130 | acc *= base; | ||
131 | acc -= c; | ||
132 | } | ||
133 | } else { | ||
134 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
135 | any = -1; | ||
136 | acc = LLONG_MAX; | ||
137 | errno = ERANGE; | ||
138 | } else { | ||
139 | any = 1; | ||
140 | acc *= base; | ||
141 | acc += c; | ||
142 | } | ||
143 | } | ||
144 | } | ||
145 | if (endptr != 0) | ||
146 | *endptr = (char *) (any ? s - 1 : nptr); | ||
147 | return (acc); | ||
148 | } | ||
149 | |||
150 | #ifdef __weak_alias | ||
151 | __weak_alias(strtoq, strtoll); | ||
152 | #else | ||
153 | quad_t | ||
154 | strtoq(nptr, endptr, base) | ||
155 | const char *nptr; | ||
156 | char **endptr; | ||
157 | int base; | ||
158 | { | ||
159 | |||
160 | return ((quad_t)strtoll(nptr, endptr, base); | ||
161 | } | ||
162 | #endif | ||
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 new file mode 100644 index 0000000000..4f6d6a51f9 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.3 | |||
@@ -0,0 +1,231 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strtoul.3,v 1.15 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 25, 1992 | ||
35 | .Dt STRTOUL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtoul , | ||
39 | .Nm strtoull , | ||
40 | .Nm strtouq | ||
41 | .Nd "convert a string to an unsigned long or unsigned long long integer" | ||
42 | .Sh SYNOPSIS | ||
43 | .Fd #include <stdlib.h> | ||
44 | .Fd #include <limits.h> | ||
45 | .Ft unsigned long | ||
46 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | ||
47 | .Pp | ||
48 | .Fd #include <stdlib.h> | ||
49 | .Fd #include <limits.h> | ||
50 | .Ft unsigned long long | ||
51 | .Fn strtoull "const char *nptr" "char **endptr" "int base" | ||
52 | .Pp | ||
53 | .Fd #include <sys/types.h> | ||
54 | .Fd #include <stdlib.h> | ||
55 | .Fd #include <limits.h> | ||
56 | .Ft u_quad_t | ||
57 | .Fn strtouq "const char *nptr" "char **endptr" "int base" | ||
58 | .Sh DESCRIPTION | ||
59 | The | ||
60 | .Fn strtoul | ||
61 | function converts the string in | ||
62 | .Fa nptr | ||
63 | to an | ||
64 | .Li unsigned long | ||
65 | value. | ||
66 | The | ||
67 | .Fn strtoull | ||
68 | function converts the string in | ||
69 | .Fa nptr | ||
70 | to an | ||
71 | .Li unsigned long long | ||
72 | value. | ||
73 | The | ||
74 | .Fn strtouq | ||
75 | function is a deprecated equivalent of | ||
76 | .Fn strtoull | ||
77 | and is provided for backwards compatibility with legacy programs. | ||
78 | The conversion is done according to the given | ||
79 | .Fa base , | ||
80 | which must be a number between 2 and 36 inclusive | ||
81 | or the special value 0. | ||
82 | If the string in | ||
83 | .Fa nptr | ||
84 | represents a negative number, it will be converted to its unsigned equivalent. | ||
85 | This behavior is consistent with what happens when a signed integer type is | ||
86 | cast to its unsigned counterpart. | ||
87 | .Pp | ||
88 | The string may begin with an arbitrary amount of whitespace | ||
89 | (as determined by | ||
90 | .Xr isspace 3 ) | ||
91 | followed by a single optional | ||
92 | .Ql + | ||
93 | or | ||
94 | .Ql - | ||
95 | sign. | ||
96 | If | ||
97 | .Fa base | ||
98 | is zero or 16, the string may then include a | ||
99 | .Ql 0x | ||
100 | prefix, and the number will be read in base 16; otherwise, a zero | ||
101 | .Fa base | ||
102 | is taken as 10 (decimal) unless the next character is | ||
103 | .Ql 0 , | ||
104 | in which case it is taken as 8 (octal). | ||
105 | .Pp | ||
106 | The remainder of the string is converted to an | ||
107 | .Li unsigned long | ||
108 | value in the obvious manner, stopping at the end of the string | ||
109 | or at the first character that does not produce a valid digit | ||
110 | in the given base. | ||
111 | (In bases above 10, the letter | ||
112 | .Ql A | ||
113 | in either upper or lower case represents 10, | ||
114 | .Ql B | ||
115 | represents 11, and so forth, with | ||
116 | .Ql Z | ||
117 | representing 35.) | ||
118 | .Pp | ||
119 | If | ||
120 | .Fa endptr | ||
121 | is non-null, | ||
122 | .Fn strtoul | ||
123 | stores the address of the first invalid character in | ||
124 | .Fa *endptr . | ||
125 | If there were no digits at all, however, | ||
126 | .Fn strtoul | ||
127 | stores the original value of | ||
128 | .Fa nptr | ||
129 | in | ||
130 | .Fa *endptr . | ||
131 | (Thus, if | ||
132 | .Fa *nptr | ||
133 | is not | ||
134 | .Ql \e0 | ||
135 | but | ||
136 | .Fa **endptr | ||
137 | is | ||
138 | .Ql \e0 | ||
139 | on return, the entire string was valid.) | ||
140 | .Sh RETURN VALUES | ||
141 | The | ||
142 | .Fn strtoul | ||
143 | function returns the result of the conversion, | ||
144 | unless the value would overflow, in which case | ||
145 | .Dv ULONG_MAX | ||
146 | is returned and | ||
147 | .Va errno | ||
148 | is set to | ||
149 | .Er ERANGE . | ||
150 | If there was a leading minus sign, | ||
151 | .Fn strtoul | ||
152 | returns the (unsigned) negation of the absolute value of the number, unless | ||
153 | the absolute value would overflow. | ||
154 | In this case, | ||
155 | .Fn strtoul | ||
156 | returns | ||
157 | .Dv ULONG_MAX | ||
158 | and sets the global variable | ||
159 | .Va errno | ||
160 | to | ||
161 | .Er ERANGE . | ||
162 | .Pp | ||
163 | The | ||
164 | .Fn strtoull | ||
165 | function has identical return values except that | ||
166 | .Dv ULLONG_MAX | ||
167 | is used to indicate overflow. | ||
168 | .Pp | ||
169 | There is no way to determine if | ||
170 | .Fn strtoul | ||
171 | has processed a negative number (and returned an unsigned value) short of | ||
172 | examining the string in | ||
173 | .Fa nptr | ||
174 | directly. | ||
175 | .Sh EXAMPLES | ||
176 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
177 | trailing characters) requires clearing | ||
178 | .Va errno | ||
179 | beforehand explicitly since | ||
180 | .Va errno | ||
181 | is not changed on a successful call to | ||
182 | .Fn strtoul , | ||
183 | and the return value of | ||
184 | .Fn strtoul | ||
185 | cannot be used unambiguously to signal an error: | ||
186 | .Bd -literal -offset indent | ||
187 | char *ep; | ||
188 | unsigned long ulval; | ||
189 | |||
190 | \&... | ||
191 | |||
192 | errno = 0; | ||
193 | ulval = strtoul(buf, &ep, 10); | ||
194 | if (buf[0] == '\e0' || *ep != '\e0') | ||
195 | goto not_a_number; | ||
196 | if (errno == ERANGE && ulval == ULONG_MAX) | ||
197 | goto out_of_range; | ||
198 | .Ed | ||
199 | .Pp | ||
200 | This example will accept | ||
201 | .Dq 12 | ||
202 | but not | ||
203 | .Dq 12foo | ||
204 | or | ||
205 | .Dq 12\en . | ||
206 | If trailing whitespace is acceptable, further checks must be done on | ||
207 | .Va *ep ; | ||
208 | alternately, use | ||
209 | .Xr sscanf 3 . | ||
210 | .Sh ERRORS | ||
211 | .Bl -tag -width Er | ||
212 | .It Bq Er ERANGE | ||
213 | The given string was out of range; the value converted has been clamped. | ||
214 | .El | ||
215 | .Sh SEE ALSO | ||
216 | .Xr sscanf 3 , | ||
217 | .Xr strtol 3 | ||
218 | .Sh STANDARDS | ||
219 | The | ||
220 | .Fn strtoul | ||
221 | and | ||
222 | .Fn strtoull | ||
223 | functions conform to | ||
224 | .St -ansiC-99 . | ||
225 | The | ||
226 | .Fn strtouq | ||
227 | function is a | ||
228 | .Bx | ||
229 | extension and is provided for backwards compatibility with legacy programs. | ||
230 | .Sh BUGS | ||
231 | Ignores the current locale. | ||
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c new file mode 100644 index 0000000000..7d31ce79fb --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.c | |||
@@ -0,0 +1,108 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1990 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strtoul.c,v 1.5 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <ctype.h> | ||
35 | #include <errno.h> | ||
36 | #include <limits.h> | ||
37 | #include <stdlib.h> | ||
38 | |||
39 | /* | ||
40 | * Convert a string to an unsigned long integer. | ||
41 | * | ||
42 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
43 | * alphabets and digits are each contiguous. | ||
44 | */ | ||
45 | unsigned long | ||
46 | strtoul(nptr, endptr, base) | ||
47 | const char *nptr; | ||
48 | char **endptr; | ||
49 | register int base; | ||
50 | { | ||
51 | register const char *s; | ||
52 | register unsigned long acc, cutoff; | ||
53 | register int c; | ||
54 | register int neg, any, cutlim; | ||
55 | |||
56 | /* | ||
57 | * See strtol for comments as to the logic used. | ||
58 | */ | ||
59 | s = nptr; | ||
60 | do { | ||
61 | c = (unsigned char) *s++; | ||
62 | } while (isspace(c)); | ||
63 | if (c == '-') { | ||
64 | neg = 1; | ||
65 | c = *s++; | ||
66 | } else { | ||
67 | neg = 0; | ||
68 | if (c == '+') | ||
69 | c = *s++; | ||
70 | } | ||
71 | if ((base == 0 || base == 16) && | ||
72 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
73 | c = s[1]; | ||
74 | s += 2; | ||
75 | base = 16; | ||
76 | } | ||
77 | if (base == 0) | ||
78 | base = c == '0' ? 8 : 10; | ||
79 | |||
80 | cutoff = ULONG_MAX / (unsigned long)base; | ||
81 | cutlim = ULONG_MAX % (unsigned long)base; | ||
82 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
83 | if (isdigit(c)) | ||
84 | c -= '0'; | ||
85 | else if (isalpha(c)) | ||
86 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
87 | else | ||
88 | break; | ||
89 | if (c >= base) | ||
90 | break; | ||
91 | if (any < 0) | ||
92 | continue; | ||
93 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
94 | any = -1; | ||
95 | acc = ULONG_MAX; | ||
96 | errno = ERANGE; | ||
97 | } else { | ||
98 | any = 1; | ||
99 | acc *= (unsigned long)base; | ||
100 | acc += c; | ||
101 | } | ||
102 | } | ||
103 | if (neg && any > 0) | ||
104 | acc = -acc; | ||
105 | if (endptr != 0) | ||
106 | *endptr = (char *) (any ? s - 1 : nptr); | ||
107 | return (acc); | ||
108 | } | ||
diff --git a/src/lib/libc/stdlib/strtoull.c b/src/lib/libc/stdlib/strtoull.c new file mode 100644 index 0000000000..a0ac9b381f --- /dev/null +++ b/src/lib/libc/stdlib/strtoull.c | |||
@@ -0,0 +1,124 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1992 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static const char rcsid[] = "$OpenBSD: strtoull.c,v 1.2 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | |||
36 | #include <ctype.h> | ||
37 | #include <errno.h> | ||
38 | #include <limits.h> | ||
39 | #include <stdlib.h> | ||
40 | |||
41 | /* | ||
42 | * Convert a string to an unsigned long long. | ||
43 | * | ||
44 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
45 | * alphabets and digits are each contiguous. | ||
46 | */ | ||
47 | unsigned long long | ||
48 | strtoull(nptr, endptr, base) | ||
49 | const char *nptr; | ||
50 | char **endptr; | ||
51 | int base; | ||
52 | { | ||
53 | const char *s; | ||
54 | unsigned long long acc, cutoff; | ||
55 | int c; | ||
56 | int neg, any, cutlim; | ||
57 | |||
58 | /* | ||
59 | * See strtoq for comments as to the logic used. | ||
60 | */ | ||
61 | s = nptr; | ||
62 | do { | ||
63 | c = (unsigned char) *s++; | ||
64 | } while (isspace(c)); | ||
65 | if (c == '-') { | ||
66 | neg = 1; | ||
67 | c = *s++; | ||
68 | } else { | ||
69 | neg = 0; | ||
70 | if (c == '+') | ||
71 | c = *s++; | ||
72 | } | ||
73 | if ((base == 0 || base == 16) && | ||
74 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
75 | c = s[1]; | ||
76 | s += 2; | ||
77 | base = 16; | ||
78 | } | ||
79 | if (base == 0) | ||
80 | base = c == '0' ? 8 : 10; | ||
81 | |||
82 | cutoff = ULLONG_MAX / (unsigned long long)base; | ||
83 | cutlim = ULLONG_MAX % (unsigned long long)base; | ||
84 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
85 | if (isdigit(c)) | ||
86 | c -= '0'; | ||
87 | else if (isalpha(c)) | ||
88 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
89 | else | ||
90 | break; | ||
91 | if (c >= base) | ||
92 | break; | ||
93 | if (any < 0) | ||
94 | continue; | ||
95 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
96 | any = -1; | ||
97 | acc = ULLONG_MAX; | ||
98 | errno = ERANGE; | ||
99 | } else { | ||
100 | any = 1; | ||
101 | acc *= (unsigned long long)base; | ||
102 | acc += c; | ||
103 | } | ||
104 | } | ||
105 | if (neg && any > 0) | ||
106 | acc = -acc; | ||
107 | if (endptr != 0) | ||
108 | *endptr = (char *) (any ? s - 1 : nptr); | ||
109 | return (acc); | ||
110 | } | ||
111 | |||
112 | #ifdef __weak_alias | ||
113 | __weak_alias(strtouq, strtoull); | ||
114 | #else | ||
115 | u_quad_t | ||
116 | strtouq(nptr, endptr, base) | ||
117 | const char *nptr; | ||
118 | char **endptr; | ||
119 | int base; | ||
120 | { | ||
121 | |||
122 | return ((u_quad_t)strtoull(nptr, endptr, base); | ||
123 | } | ||
124 | #endif | ||
diff --git a/src/lib/libc/stdlib/system.3 b/src/lib/libc/stdlib/system.3 new file mode 100644 index 0000000000..1034b01066 --- /dev/null +++ b/src/lib/libc/stdlib/system.3 | |||
@@ -0,0 +1,99 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: system.3,v 1.9 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt SYSTEM 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm system | ||
39 | .Nd pass a command to the shell | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft int | ||
43 | .Fn system "const char *string" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn system | ||
47 | function hands the argument | ||
48 | .Fa string | ||
49 | to the command interpreter | ||
50 | .Xr sh 1 . | ||
51 | The calling process waits for the shell to finish executing the command, | ||
52 | ignoring | ||
53 | .Dv SIGINT | ||
54 | and | ||
55 | .Dv SIGQUIT , | ||
56 | and blocking | ||
57 | .Dv SIGCHLD . | ||
58 | .Pp | ||
59 | If | ||
60 | .Fa string | ||
61 | is | ||
62 | .Dv NULL , | ||
63 | .Fn system | ||
64 | will return non-zero. | ||
65 | Otherwise, | ||
66 | .Fn system | ||
67 | returns the termination status of the shell in the format specified by | ||
68 | .Xr waitpid 2 . | ||
69 | .Sh RETURN VALUES | ||
70 | If a child process cannot be created, or the termination status of | ||
71 | the shell cannot be obtained, | ||
72 | .Fn system | ||
73 | returns \-1 and sets | ||
74 | .Va errno | ||
75 | to indicate the error. | ||
76 | If execution of the shell fails, | ||
77 | .Fn system | ||
78 | returns the termination status for a program that terminates with a call of | ||
79 | .Fn exit 127 . | ||
80 | .Sh SEE ALSO | ||
81 | .Xr sh 1 , | ||
82 | .Xr execve 2 , | ||
83 | .Xr waitpid 2 , | ||
84 | .Xr popen 3 | ||
85 | .Sh STANDARDS | ||
86 | The | ||
87 | .Fn system | ||
88 | function conforms to | ||
89 | .St -ansiC | ||
90 | and | ||
91 | .St -p1003.2-92 . | ||
92 | .Sh CAVEATS | ||
93 | Never supply the | ||
94 | .Fn system | ||
95 | function with a command containing any part of an unsanitized user-supplied | ||
96 | string. | ||
97 | Shell meta-characters present will be honored by the | ||
98 | .Xr sh 1 | ||
99 | command interpreter. | ||
diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c new file mode 100644 index 0000000000..06b439230b --- /dev/null +++ b/src/lib/libc/stdlib/system.c | |||
@@ -0,0 +1,78 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: system.c,v 1.6 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/wait.h> | ||
36 | #include <signal.h> | ||
37 | #include <stdlib.h> | ||
38 | #include <unistd.h> | ||
39 | #include <paths.h> | ||
40 | |||
41 | extern char **environ; | ||
42 | |||
43 | int | ||
44 | system(command) | ||
45 | const char *command; | ||
46 | { | ||
47 | pid_t pid; | ||
48 | sig_t intsave, quitsave; | ||
49 | sigset_t mask, omask; | ||
50 | int pstat; | ||
51 | char *argp[] = {"sh", "-c", NULL, NULL}; | ||
52 | |||
53 | if (!command) /* just checking... */ | ||
54 | return(1); | ||
55 | |||
56 | argp[2] = (char *)command; | ||
57 | |||
58 | sigemptyset(&mask); | ||
59 | sigaddset(&mask, SIGCHLD); | ||
60 | sigprocmask(SIG_BLOCK, &mask, &omask); | ||
61 | switch (pid = vfork()) { | ||
62 | case -1: /* error */ | ||
63 | sigprocmask(SIG_SETMASK, &omask, NULL); | ||
64 | return(-1); | ||
65 | case 0: /* child */ | ||
66 | sigprocmask(SIG_SETMASK, &omask, NULL); | ||
67 | execve(_PATH_BSHELL, argp, environ); | ||
68 | _exit(127); | ||
69 | } | ||
70 | |||
71 | intsave = signal(SIGINT, SIG_IGN); | ||
72 | quitsave = signal(SIGQUIT, SIG_IGN); | ||
73 | pid = waitpid(pid, (int *)&pstat, 0); | ||
74 | sigprocmask(SIG_SETMASK, &omask, NULL); | ||
75 | (void)signal(SIGINT, intsave); | ||
76 | (void)signal(SIGQUIT, quitsave); | ||
77 | return (pid == -1 ? -1 : pstat); | ||
78 | } | ||
diff --git a/src/lib/libc/stdlib/tfind.c b/src/lib/libc/stdlib/tfind.c new file mode 100644 index 0000000000..5c3b8c17f5 --- /dev/null +++ b/src/lib/libc/stdlib/tfind.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /* | ||
2 | * Tree search generalized from Knuth (6.2.2) Algorithm T just like | ||
3 | * the AT&T man page says. | ||
4 | * | ||
5 | * The node_t structure is for internal use only, lint doesn't grok it. | ||
6 | * | ||
7 | * Written by reading the System V Interface Definition, not the code. | ||
8 | * | ||
9 | * Totally public domain. | ||
10 | */ | ||
11 | /*LINTLIBRARY*/ | ||
12 | #include <search.h> | ||
13 | |||
14 | typedef struct node_t | ||
15 | { | ||
16 | char *key; | ||
17 | struct node_t *llink, *rlink; | ||
18 | } node; | ||
19 | |||
20 | /* find a node, or return 0 */ | ||
21 | void * | ||
22 | tfind(vkey, vrootp, compar) | ||
23 | const void *vkey; /* key to be found */ | ||
24 | void *const *vrootp; /* address of the tree root */ | ||
25 | int (*compar)(const void *, const void *); | ||
26 | { | ||
27 | char *key = (char *)vkey; | ||
28 | node **rootp = (node **)vrootp; | ||
29 | |||
30 | if (rootp == (struct node_t **)0) | ||
31 | return ((struct node_t *)0); | ||
32 | while (*rootp != (struct node_t *)0) { /* T1: */ | ||
33 | int r; | ||
34 | if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */ | ||
35 | return (*rootp); /* key found */ | ||
36 | rootp = (r < 0) ? | ||
37 | &(*rootp)->llink : /* T3: follow left branch */ | ||
38 | &(*rootp)->rlink; /* T4: follow right branch */ | ||
39 | } | ||
40 | return (node *)0; | ||
41 | } | ||
diff --git a/src/lib/libc/stdlib/tsearch.3 b/src/lib/libc/stdlib/tsearch.3 new file mode 100644 index 0000000000..15d7b84a3a --- /dev/null +++ b/src/lib/libc/stdlib/tsearch.3 | |||
@@ -0,0 +1,116 @@ | |||
1 | .\" $OpenBSD: tsearch.3,v 1.12 2003/06/17 21:56:24 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | .\" | ||
17 | .Dd June 15, 1997 | ||
18 | .Dt TSEARCH 3 | ||
19 | .Os | ||
20 | .Sh NAME | ||
21 | .Nm tsearch , | ||
22 | .Nm tfind , | ||
23 | .Nm tdelete , | ||
24 | .Nm twalk | ||
25 | .Nd manipulate binary search trees | ||
26 | .Sh SYNOPSIS | ||
27 | .Fd #include <search.h> | ||
28 | .Ft void * | ||
29 | .Fn tdelete "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" | ||
30 | .Ft void * | ||
31 | .Fn tfind "const void *key" "void * const *rootp" "int (*compar) (const void *, const void *)" | ||
32 | .Ft void * | ||
33 | .Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" | ||
34 | .Ft void | ||
35 | .Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)" | ||
36 | .Sh DESCRIPTION | ||
37 | The | ||
38 | .Fn tdelete , | ||
39 | .Fn tfind , | ||
40 | .Fn tsearch , | ||
41 | and | ||
42 | .Fn twalk | ||
43 | functions manage binary search trees based on algorithms T and D | ||
44 | from Knuth (6.2.2). | ||
45 | The comparison function passed in by | ||
46 | the user has the same style of return values as | ||
47 | .Xr strcmp 3 . | ||
48 | .Pp | ||
49 | .Fn tfind | ||
50 | searches for the datum matched by the argument | ||
51 | .Fa key | ||
52 | in the binary tree rooted at | ||
53 | .Fa rootp , | ||
54 | returning a pointer to the datum if it is found and | ||
55 | .Dv NULL | ||
56 | if it is not. | ||
57 | .Pp | ||
58 | .Fn tsearch | ||
59 | is identical to | ||
60 | .Fn tfind | ||
61 | except that if no match is found, | ||
62 | .Fa key | ||
63 | is inserted into the tree and a pointer to it is returned. | ||
64 | If | ||
65 | .Fa rootp | ||
66 | points to a null value a new binary search tree is created. | ||
67 | .Pp | ||
68 | .Fn tdelete | ||
69 | deletes a node from the specified binary search tree and returns | ||
70 | a pointer to the parent of the node to be deleted. | ||
71 | It takes the same arguments as | ||
72 | .Fn tfind | ||
73 | and | ||
74 | .Fn tsearch . | ||
75 | If the node to be deleted is the root of the binary search tree, | ||
76 | .Fa rootp | ||
77 | will be adjusted. | ||
78 | .Pp | ||
79 | .Fn twalk | ||
80 | walks the binary search tree rooted in | ||
81 | .Fa root | ||
82 | and calls the function | ||
83 | .Fa action | ||
84 | on each node. | ||
85 | .Fa action | ||
86 | is called with three arguments: a pointer to the current node, | ||
87 | a value from the enum | ||
88 | .Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;" | ||
89 | specifying the traversal type, and a node level (where level | ||
90 | zero is the root of the tree). | ||
91 | .Sh RETURN VALUES | ||
92 | The | ||
93 | .Fn tsearch | ||
94 | function returns | ||
95 | .Dv NULL | ||
96 | if allocation of a new node fails (usually | ||
97 | due to a lack of free memory). | ||
98 | .Pp | ||
99 | .Fn tfind , | ||
100 | .Fn tsearch , | ||
101 | and | ||
102 | .Fn tdelete | ||
103 | return | ||
104 | .Dv NULL | ||
105 | if | ||
106 | .Fa rootp | ||
107 | is | ||
108 | .Dv NULL | ||
109 | or the datum cannot be found. | ||
110 | .Pp | ||
111 | The | ||
112 | .Fn twalk | ||
113 | function returns no value. | ||
114 | .Sh SEE ALSO | ||
115 | .Xr bsearch 3 , | ||
116 | .Xr lsearch 3 | ||
diff --git a/src/lib/libc/stdlib/tsearch.c b/src/lib/libc/stdlib/tsearch.c new file mode 100644 index 0000000000..0ad5866172 --- /dev/null +++ b/src/lib/libc/stdlib/tsearch.c | |||
@@ -0,0 +1,126 @@ | |||
1 | /* | ||
2 | * Tree search generalized from Knuth (6.2.2) Algorithm T just like | ||
3 | * the AT&T man page says. | ||
4 | * | ||
5 | * The node_t structure is for internal use only, lint doesn't grok it. | ||
6 | * | ||
7 | * Written by reading the System V Interface Definition, not the code. | ||
8 | * | ||
9 | * Totally public domain. | ||
10 | */ | ||
11 | /*LINTLIBRARY*/ | ||
12 | |||
13 | #include <search.h> | ||
14 | #include <stdlib.h> | ||
15 | |||
16 | typedef struct node_t { | ||
17 | char *key; | ||
18 | struct node_t *left, *right; | ||
19 | } node; | ||
20 | |||
21 | /* find or insert datum into search tree */ | ||
22 | void * | ||
23 | tsearch(vkey, vrootp, compar) | ||
24 | const void *vkey; /* key to be located */ | ||
25 | void **vrootp; /* address of tree root */ | ||
26 | int (*compar)(const void *, const void *); | ||
27 | { | ||
28 | register node *q; | ||
29 | char *key = (char *)vkey; | ||
30 | node **rootp = (node **)vrootp; | ||
31 | |||
32 | if (rootp == (struct node_t **)0) | ||
33 | return ((void *)0); | ||
34 | while (*rootp != (struct node_t *)0) { /* Knuth's T1: */ | ||
35 | int r; | ||
36 | |||
37 | if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */ | ||
38 | return ((void *)*rootp); /* we found it! */ | ||
39 | rootp = (r < 0) ? | ||
40 | &(*rootp)->left : /* T3: follow left branch */ | ||
41 | &(*rootp)->right; /* T4: follow right branch */ | ||
42 | } | ||
43 | q = (node *) malloc(sizeof(node)); /* T5: key not found */ | ||
44 | if (q != (struct node_t *)0) { /* make new node */ | ||
45 | *rootp = q; /* link new node to old */ | ||
46 | q->key = key; /* initialize new node */ | ||
47 | q->left = q->right = (struct node_t *)0; | ||
48 | } | ||
49 | return ((void *)q); | ||
50 | } | ||
51 | |||
52 | /* delete node with given key */ | ||
53 | void * | ||
54 | tdelete(vkey, vrootp, compar) | ||
55 | const void *vkey; /* key to be deleted */ | ||
56 | void **vrootp; /* address of the root of tree */ | ||
57 | int (*compar)(const void *, const void *); | ||
58 | { | ||
59 | node **rootp = (node **)vrootp; | ||
60 | char *key = (char *)vkey; | ||
61 | node *p; | ||
62 | register node *q; | ||
63 | register node *r; | ||
64 | int cmp; | ||
65 | |||
66 | if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0) | ||
67 | return ((struct node_t *)0); | ||
68 | while ((cmp = (*compar)(key, (*rootp)->key)) != 0) { | ||
69 | p = *rootp; | ||
70 | rootp = (cmp < 0) ? | ||
71 | &(*rootp)->left : /* follow left branch */ | ||
72 | &(*rootp)->right; /* follow right branch */ | ||
73 | if (*rootp == (struct node_t *)0) | ||
74 | return ((void *)0); /* key not found */ | ||
75 | } | ||
76 | r = (*rootp)->right; /* D1: */ | ||
77 | if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */ | ||
78 | q = r; | ||
79 | else if (r != (struct node_t *)0) { /* Right link is null? */ | ||
80 | if (r->left == (struct node_t *)0) { /* D2: Find successor */ | ||
81 | r->left = q; | ||
82 | q = r; | ||
83 | } else { /* D3: Find (struct node_t *)0 link */ | ||
84 | for (q = r->left; q->left != (struct node_t *)0; q = r->left) | ||
85 | r = q; | ||
86 | r->left = q->right; | ||
87 | q->left = (*rootp)->left; | ||
88 | q->right = (*rootp)->right; | ||
89 | } | ||
90 | } | ||
91 | free((struct node_t *) *rootp); /* D4: Free node */ | ||
92 | *rootp = q; /* link parent to new node */ | ||
93 | return(p); | ||
94 | } | ||
95 | |||
96 | /* Walk the nodes of a tree */ | ||
97 | static void | ||
98 | trecurse(root, action, level) | ||
99 | register node *root; /* Root of the tree to be walked */ | ||
100 | register void (*action)(); /* Function to be called at each node */ | ||
101 | register int level; | ||
102 | { | ||
103 | if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0) | ||
104 | (*action)(root, leaf, level); | ||
105 | else { | ||
106 | (*action)(root, preorder, level); | ||
107 | if (root->left != (struct node_t *)0) | ||
108 | trecurse(root->left, action, level + 1); | ||
109 | (*action)(root, postorder, level); | ||
110 | if (root->right != (struct node_t *)0) | ||
111 | trecurse(root->right, action, level + 1); | ||
112 | (*action)(root, endorder, level); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | /* Walk the nodes of a tree */ | ||
117 | void | ||
118 | twalk(vroot, action) | ||
119 | const void *vroot; /* Root of the tree to be walked */ | ||
120 | void (*action)(const void *, VISIT, int); | ||
121 | { | ||
122 | node *root = (node *)vroot; | ||
123 | |||
124 | if (root != (node *)0 && action != (void(*)())0) | ||
125 | trecurse(root, action, 0); | ||
126 | } | ||
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc new file mode 100644 index 0000000000..26e2bc10f2 --- /dev/null +++ b/src/lib/libc/string/Makefile.inc | |||
@@ -0,0 +1,152 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.12 2003/02/20 03:20:09 deraadt Exp $ | ||
2 | |||
3 | # string sources | ||
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string ${LIBCSRCDIR}/string | ||
5 | |||
6 | SRCS+= bm.c memccpy.c strcasecmp.c strcoll.c strdup.c strerror.c \ | ||
7 | strerror_r.c strlcat.c strmode.c strsignal.c strtok.c strxfrm.c \ | ||
8 | __strerror.c __strsignal.c | ||
9 | |||
10 | # machine-dependent net sources | ||
11 | # m-d Makefile.inc must include sources for: | ||
12 | # bcmp() bcopy() bzero() ffs() index() memchr() memcmp() memset() | ||
13 | # rindex() strcat() strcmp() strcpy() strcspn() strlen() strlcpy() | ||
14 | # strncat() strncmp() strncpy() strpbrk() strsep() | ||
15 | # strspn() strstr() swav() | ||
16 | # m-d Makefile.inc may include sources for: | ||
17 | # memcpy() memmove() strchr() strrchr() | ||
18 | |||
19 | .include "${LIBCSRCDIR}/arch/${MACHINE_ARCH}/string/Makefile.inc" | ||
20 | |||
21 | # if no machine specific memmove(3), build one out of bcopy(3). | ||
22 | .if empty(SRCS:Mmemmove.S) | ||
23 | OBJS+= memmove.o | ||
24 | memmove.o: bcopy.c | ||
25 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
26 | @${LD} -x -r ${.TARGET} | ||
27 | @mv a.out ${.TARGET} | ||
28 | |||
29 | memmove.go: bcopy.c | ||
30 | ${CC} -g -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
31 | @${LD} -x -r ${.TARGET} | ||
32 | @mv a.out ${.TARGET} | ||
33 | |||
34 | memmove.po: bcopy.c | ||
35 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
36 | @${LD} -X -r ${.TARGET} | ||
37 | @mv a.out ${.TARGET} | ||
38 | |||
39 | memmove.so: bcopy.c | ||
40 | ${CC} ${PICFLAG} -DPIC -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
41 | -o ${.TARGET} | ||
42 | .endif | ||
43 | |||
44 | # if no machine specific memcpy(3), build one out of bcopy(3). | ||
45 | # if there is a machine specific memmove(3), we'll assume it aliases | ||
46 | # memcpy(3). | ||
47 | .if empty(SRCS:Mmemcpy.S) | ||
48 | .if empty(SRCS:Mmemmove.S) | ||
49 | OBJS+= memcpy.o | ||
50 | memcpy.o: bcopy.c | ||
51 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
52 | @${LD} -x -r ${.TARGET} | ||
53 | @mv a.out ${.TARGET} | ||
54 | |||
55 | memcpy.go: bcopy.c | ||
56 | ${CC} -g -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
57 | @${LD} -x -r ${.TARGET} | ||
58 | @mv a.out ${.TARGET} | ||
59 | |||
60 | memcpy.po: bcopy.c | ||
61 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
62 | @${LD} -X -r ${.TARGET} | ||
63 | @mv a.out ${.TARGET} | ||
64 | |||
65 | memcpy.so: bcopy.c | ||
66 | ${CC} ${PICFLAG} -DPIC -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
67 | -o ${.TARGET} | ||
68 | .endif | ||
69 | .endif | ||
70 | |||
71 | # if no machine specific strchr(3), build one out of index(3). | ||
72 | .if empty(SRCS:Mstrchr.S) | ||
73 | OBJS+= strchr.o | ||
74 | strchr.o: index.c | ||
75 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
76 | @${LD} -x -r ${.TARGET} | ||
77 | @mv a.out ${.TARGET} | ||
78 | |||
79 | strchr.go: index.c | ||
80 | ${CC} -g -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
81 | @${LD} -x -r ${.TARGET} | ||
82 | @mv a.out ${.TARGET} | ||
83 | |||
84 | strchr.po: index.c | ||
85 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
86 | @${LD} -X -r ${.TARGET} | ||
87 | @mv a.out ${.TARGET} | ||
88 | |||
89 | strchr.so: index.c | ||
90 | ${CC} ${PICFLAG} -DPIC -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
91 | -o ${.TARGET} | ||
92 | .endif | ||
93 | |||
94 | # if no machine specific strrchr(3), build one out of rindex(3). | ||
95 | .if empty(SRCS:Mstrrchr.S) | ||
96 | OBJS+= strrchr.o | ||
97 | strrchr.o: rindex.c | ||
98 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
99 | @${LD} -x -r ${.TARGET} | ||
100 | @mv a.out ${.TARGET} | ||
101 | |||
102 | strrchr.go: rindex.c | ||
103 | ${CC} -g -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
104 | @${LD} -x -r ${.TARGET} | ||
105 | @mv a.out ${.TARGET} | ||
106 | |||
107 | strrchr.po: rindex.c | ||
108 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
109 | @${LD} -X -r ${.TARGET} | ||
110 | @mv a.out ${.TARGET} | ||
111 | |||
112 | strrchr.so: rindex.c | ||
113 | ${CC} ${PICFLAG} -DPIC -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
114 | -o ${.TARGET} | ||
115 | .endif | ||
116 | |||
117 | # build .ln files for memmove, memcpy, strchr and strrchr always from | ||
118 | # bcopy, index, and rindex | ||
119 | LOBJS+= memmove.ln memcpy.ln strchr.ln strrchr.ln | ||
120 | |||
121 | memmove.ln: bcopy.c | ||
122 | lint ${LINTFLAGS} -DMEMMOVE ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
123 | ${LIBCSRCDIR}/string/bcopy.c | ||
124 | |||
125 | memcpy.ln: bcopy.c | ||
126 | lint ${LINTFLAGS} -DMEMCOPY ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
127 | ${LIBCSRCDIR}/string/bcopy.c | ||
128 | |||
129 | strchr.ln: index.c | ||
130 | lint ${LINTFLAGS} -DSTRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
131 | ${LIBCSRCDIR}/string/index.c | ||
132 | |||
133 | strrchr.ln: rindex.c | ||
134 | lint ${LINTFLAGS} -DSTRRCHR ${CFLAGS:M-[IDU]*} -i -o ${.TARGET} \ | ||
135 | ${LIBCSRCDIR}/string/rindex.c | ||
136 | |||
137 | MAN+= bm.3 bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ | ||
138 | memcmp.3 memcpy.3 memmove.3 memset.3 strcasecmp.3 strcat.3 \ | ||
139 | strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strerror.3 \ | ||
140 | string.3 strlen.3 strmode.3 strdup.3 strpbrk.3 strrchr.3 strsep.3 \ | ||
141 | strsignal.3 strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 strlcpy.3 | ||
142 | |||
143 | MLINKS+=bm.3 bm_comp.3 bm.3 bm_exec.3 bm.3 bm_free.3 | ||
144 | MLINKS+=strchr.3 index.3 | ||
145 | MLINKS+=strrchr.3 rindex.3 | ||
146 | MLINKS+=strcasecmp.3 strncasecmp.3 | ||
147 | MLINKS+=strcat.3 strncat.3 | ||
148 | MLINKS+=strcmp.3 strncmp.3 | ||
149 | MLINKS+=strcpy.3 strncpy.3 | ||
150 | MLINKS+=strlcpy.3 strlcat.3 | ||
151 | MLINKS+=strtok.3 strtok_r.3 | ||
152 | MLINKS+=strerror.3 strerror_r.3 | ||
diff --git a/src/lib/libc/string/__strerror.c b/src/lib/libc/string/__strerror.c new file mode 100644 index 0000000000..15436eaab2 --- /dev/null +++ b/src/lib/libc/string/__strerror.c | |||
@@ -0,0 +1,110 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: __strerror.c,v 1.10 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #ifdef NLS | ||
35 | #define catclose _catclose | ||
36 | #define catgets _catgets | ||
37 | #define catopen _catopen | ||
38 | #include <nl_types.h> | ||
39 | #endif | ||
40 | |||
41 | #define sys_errlist _sys_errlist | ||
42 | #define sys_nerr _sys_nerr | ||
43 | |||
44 | #include <errno.h> | ||
45 | #include <limits.h> | ||
46 | #include <stdio.h> | ||
47 | #include <string.h> | ||
48 | |||
49 | static char * | ||
50 | itoa(num) | ||
51 | int num; | ||
52 | { | ||
53 | static char buffer[11]; | ||
54 | char *p; | ||
55 | |||
56 | p = buffer + 4; | ||
57 | while (num >= 10) { | ||
58 | *--p = (num % 10) + '0'; | ||
59 | num /= 10; | ||
60 | } | ||
61 | *p = (num % 10) + '0'; | ||
62 | return p; | ||
63 | } | ||
64 | |||
65 | /* | ||
66 | * Since perror() is not allowed to change the contents of strerror()'s | ||
67 | * static buffer, both functions supply their own buffers to the | ||
68 | * internal function __strerror(). | ||
69 | */ | ||
70 | |||
71 | char * | ||
72 | __strerror(num, buf) | ||
73 | int num; | ||
74 | char *buf; | ||
75 | { | ||
76 | #define UPREFIX "Unknown error: " | ||
77 | register unsigned int errnum; | ||
78 | #ifdef NLS | ||
79 | int save_errno; | ||
80 | nl_catd catd; | ||
81 | |||
82 | catd = catopen("libc", 0); | ||
83 | #endif | ||
84 | |||
85 | errnum = num; /* convert to unsigned */ | ||
86 | if (errnum < sys_nerr) { | ||
87 | #ifdef NLS | ||
88 | strlcpy(buf, catgets(catd, 1, errnum, | ||
89 | (char *)sys_errlist[errnum]), NL_TEXTMAX); | ||
90 | #else | ||
91 | return(sys_errlist[errnum]); | ||
92 | #endif | ||
93 | } else { | ||
94 | #ifdef NLS | ||
95 | strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX); | ||
96 | #else | ||
97 | strlcpy(buf, UPREFIX, NL_TEXTMAX); | ||
98 | #endif | ||
99 | strlcat(buf, itoa(errnum), NL_TEXTMAX); | ||
100 | errno = EINVAL; | ||
101 | } | ||
102 | |||
103 | #ifdef NLS | ||
104 | save_errno = errno; | ||
105 | catclose(catd); | ||
106 | errno = save_errno; | ||
107 | #endif | ||
108 | |||
109 | return buf; | ||
110 | } | ||
diff --git a/src/lib/libc/string/__strsignal.c b/src/lib/libc/string/__strsignal.c new file mode 100644 index 0000000000..5bab3cd0af --- /dev/null +++ b/src/lib/libc/string/__strsignal.c | |||
@@ -0,0 +1,98 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: __strsignal.c,v 1.8 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #ifdef NLS | ||
35 | #define catclose _catclose | ||
36 | #define catgets _catgets | ||
37 | #define catopen _catopen | ||
38 | #include <nl_types.h> | ||
39 | #endif | ||
40 | |||
41 | #define sys_siglist _sys_siglist | ||
42 | |||
43 | #include <stdio.h> | ||
44 | #include <limits.h> | ||
45 | #include <signal.h> | ||
46 | #include <string.h> | ||
47 | |||
48 | static char *itoa(num) | ||
49 | int num; | ||
50 | { | ||
51 | static char buffer[11]; | ||
52 | char *p; | ||
53 | |||
54 | p = buffer + 4; | ||
55 | while (num >= 10) { | ||
56 | *--p = (num % 10) + '0'; | ||
57 | num /= 10; | ||
58 | } | ||
59 | *p = (num % 10) + '0'; | ||
60 | return p; | ||
61 | } | ||
62 | |||
63 | char * | ||
64 | __strsignal(num, buf) | ||
65 | int num; | ||
66 | char *buf; | ||
67 | { | ||
68 | #define UPREFIX "Unknown signal: " | ||
69 | register unsigned int signum; | ||
70 | |||
71 | #ifdef NLS | ||
72 | nl_catd catd ; | ||
73 | catd = catopen("libc", 0); | ||
74 | #endif | ||
75 | |||
76 | signum = num; /* convert to unsigned */ | ||
77 | if (signum < NSIG) { | ||
78 | #ifdef NLS | ||
79 | strlcpy(buf, catgets(catd, 2, signum, | ||
80 | (char *)sys_siglist[signum]), NL_TEXTMAX); | ||
81 | #else | ||
82 | return((char *)sys_siglist[signum]); | ||
83 | #endif | ||
84 | } else { | ||
85 | #ifdef NLS | ||
86 | strlcpy(buf, catgets(catd, 1, 0xffff, UPREFIX), NL_TEXTMAX); | ||
87 | #else | ||
88 | strlcpy(buf, UPREFIX, NL_TEXTMAX); | ||
89 | #endif | ||
90 | strlcat(buf, itoa(signum), NL_TEXTMAX); | ||
91 | } | ||
92 | |||
93 | #ifdef NLS | ||
94 | catclose(catd); | ||
95 | #endif | ||
96 | |||
97 | return buf; | ||
98 | } | ||
diff --git a/src/lib/libc/string/bcmp.3 b/src/lib/libc/string/bcmp.3 new file mode 100644 index 0000000000..953476caa7 --- /dev/null +++ b/src/lib/libc/string/bcmp.3 | |||
@@ -0,0 +1,66 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" $OpenBSD: bcmp.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
31 | .\" | ||
32 | .Dd April 19, 1991 | ||
33 | .Dt BCMP 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm bcmp | ||
37 | .Nd compare byte string | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft int | ||
41 | .Fn bcmp "const void *b1" "const void *b2" "size_t len" | ||
42 | .Sh DESCRIPTION | ||
43 | The | ||
44 | .Fn bcmp | ||
45 | function compares byte string | ||
46 | .Fa b1 | ||
47 | against byte string | ||
48 | .Fa b2 , | ||
49 | returning zero if they are identical, non-zero otherwise. | ||
50 | Both strings are assumed to be | ||
51 | .Fa len | ||
52 | bytes long. | ||
53 | Zero-length strings are always identical. | ||
54 | .Pp | ||
55 | The strings may overlap. | ||
56 | .Sh SEE ALSO | ||
57 | .Xr memcmp 3 , | ||
58 | .Xr strcasecmp 3 , | ||
59 | .Xr strcmp 3 , | ||
60 | .Xr strcoll 3 , | ||
61 | .Xr strxfrm 3 | ||
62 | .Sh HISTORY | ||
63 | A | ||
64 | .Fn bcmp | ||
65 | function first appeared in | ||
66 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/string/bcmp.c b/src/lib/libc/string/bcmp.c new file mode 100644 index 0000000000..2beeef8298 --- /dev/null +++ b/src/lib/libc/string/bcmp.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1987 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: bcmp.c,v 1.5 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #ifndef _KERNEL | ||
35 | #include <string.h> | ||
36 | #else | ||
37 | #include <lib/libkern/libkern.h> | ||
38 | #endif | ||
39 | |||
40 | /* | ||
41 | * bcmp -- vax cmpc3 instruction | ||
42 | */ | ||
43 | int | ||
44 | bcmp(b1, b2, length) | ||
45 | const void *b1, *b2; | ||
46 | register size_t length; | ||
47 | { | ||
48 | register char *p1, *p2; | ||
49 | |||
50 | if (length == 0) | ||
51 | return(0); | ||
52 | p1 = (char *)b1; | ||
53 | p2 = (char *)b2; | ||
54 | do | ||
55 | if (*p1++ != *p2++) | ||
56 | break; | ||
57 | while (--length); | ||
58 | return(length); | ||
59 | } | ||
diff --git a/src/lib/libc/string/bcopy.3 b/src/lib/libc/string/bcopy.3 new file mode 100644 index 0000000000..7b55bcbf43 --- /dev/null +++ b/src/lib/libc/string/bcopy.3 | |||
@@ -0,0 +1,67 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" $OpenBSD: bcopy.3,v 1.8 2003/06/21 12:04:01 avsm Exp $ | ||
32 | .\" | ||
33 | .Dd April 19, 1991 | ||
34 | .Dt BCOPY 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm bcopy | ||
38 | .Nd copy bytes | ||
39 | .Sh SYNOPSIS | ||
40 | .Fd #include <string.h> | ||
41 | .Ft void | ||
42 | .Fn bcopy "const void *src" "void *dst" "size_t len" | ||
43 | .Sh DESCRIPTION | ||
44 | The | ||
45 | .Fn bcopy | ||
46 | function copies | ||
47 | .Fa len | ||
48 | bytes from buffer | ||
49 | .Fa src | ||
50 | to buffer | ||
51 | .Fa dst . | ||
52 | The two buffers may overlap. | ||
53 | If | ||
54 | .Fa len | ||
55 | is zero, no bytes are copied. | ||
56 | .Sh SEE ALSO | ||
57 | .Xr memccpy 3 , | ||
58 | .Xr memcpy 3 , | ||
59 | .Xr memmove 3 , | ||
60 | .Xr strcpy 3 , | ||
61 | .Xr strlcpy 3 , | ||
62 | .Xr strncpy 3 | ||
63 | .Sh HISTORY | ||
64 | A | ||
65 | .Fn bcopy | ||
66 | function appeared in | ||
67 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/string/bcopy.c b/src/lib/libc/string/bcopy.c new file mode 100644 index 0000000000..c48faa145e --- /dev/null +++ b/src/lib/libc/string/bcopy.c | |||
@@ -0,0 +1,134 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: bcopy.c,v 1.3 2003/06/02 20:18:38 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * sizeof(word) MUST BE A POWER OF TWO | ||
41 | * SO THAT wmask BELOW IS ALL ONES | ||
42 | */ | ||
43 | typedef long word; /* "word" used for optimal copy speed */ | ||
44 | |||
45 | #define wsize sizeof(word) | ||
46 | #define wmask (wsize - 1) | ||
47 | |||
48 | /* | ||
49 | * Copy a block of memory, handling overlap. | ||
50 | * This is the routine that actually implements | ||
51 | * (the portable versions of) bcopy, memcpy, and memmove. | ||
52 | */ | ||
53 | #ifdef MEMCOPY | ||
54 | void * | ||
55 | memcpy(dst0, src0, length) | ||
56 | #else | ||
57 | #ifdef MEMMOVE | ||
58 | void * | ||
59 | memmove(dst0, src0, length) | ||
60 | #else | ||
61 | void | ||
62 | bcopy(src0, dst0, length) | ||
63 | #endif | ||
64 | #endif | ||
65 | void *dst0; | ||
66 | const void *src0; | ||
67 | register size_t length; | ||
68 | { | ||
69 | register char *dst = dst0; | ||
70 | register const char *src = src0; | ||
71 | register size_t t; | ||
72 | |||
73 | if (length == 0 || dst == src) /* nothing to do */ | ||
74 | goto done; | ||
75 | |||
76 | /* | ||
77 | * Macros: loop-t-times; and loop-t-times, t>0 | ||
78 | */ | ||
79 | #define TLOOP(s) if (t) TLOOP1(s) | ||
80 | #define TLOOP1(s) do { s; } while (--t) | ||
81 | |||
82 | if ((unsigned long)dst < (unsigned long)src) { | ||
83 | /* | ||
84 | * Copy forward. | ||
85 | */ | ||
86 | t = (long)src; /* only need low bits */ | ||
87 | if ((t | (long)dst) & wmask) { | ||
88 | /* | ||
89 | * Try to align operands. This cannot be done | ||
90 | * unless the low bits match. | ||
91 | */ | ||
92 | if ((t ^ (long)dst) & wmask || length < wsize) | ||
93 | t = length; | ||
94 | else | ||
95 | t = wsize - (t & wmask); | ||
96 | length -= t; | ||
97 | TLOOP1(*dst++ = *src++); | ||
98 | } | ||
99 | /* | ||
100 | * Copy whole words, then mop up any trailing bytes. | ||
101 | */ | ||
102 | t = length / wsize; | ||
103 | TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); | ||
104 | t = length & wmask; | ||
105 | TLOOP(*dst++ = *src++); | ||
106 | } else { | ||
107 | /* | ||
108 | * Copy backwards. Otherwise essentially the same. | ||
109 | * Alignment works as before, except that it takes | ||
110 | * (t&wmask) bytes to align, not wsize-(t&wmask). | ||
111 | */ | ||
112 | src += length; | ||
113 | dst += length; | ||
114 | t = (long)src; | ||
115 | if ((t | (long)dst) & wmask) { | ||
116 | if ((t ^ (long)dst) & wmask || length <= wsize) | ||
117 | t = length; | ||
118 | else | ||
119 | t &= wmask; | ||
120 | length -= t; | ||
121 | TLOOP1(*--dst = *--src); | ||
122 | } | ||
123 | t = length / wsize; | ||
124 | TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); | ||
125 | t = length & wmask; | ||
126 | TLOOP(*--dst = *--src); | ||
127 | } | ||
128 | done: | ||
129 | #if defined(MEMCOPY) || defined(MEMMOVE) | ||
130 | return (dst0); | ||
131 | #else | ||
132 | return; | ||
133 | #endif | ||
134 | } | ||
diff --git a/src/lib/libc/string/bm.3 b/src/lib/libc/string/bm.3 new file mode 100644 index 0000000000..772b0d372e --- /dev/null +++ b/src/lib/libc/string/bm.3 | |||
@@ -0,0 +1,111 @@ | |||
1 | .\" Copyright (c) 1994 | ||
2 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Andrew Hume of AT&T Bell Laboratories. | ||
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. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" $OpenBSD: bm.3,v 1.7 2003/06/02 20:18:38 millert Exp $ | ||
32 | .\" | ||
33 | .Dd June 29, 1999 | ||
34 | .Dt BM 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm bm_comp , | ||
38 | .Nm bm_exec , | ||
39 | .Nm bm_free | ||
40 | .Nd Boyer-Moore string search | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <sys/types.h> | ||
43 | .Fd #include <bm.h> | ||
44 | .Ft bm_pat * | ||
45 | .Fn bm_comp "u_char *pattern" "size_t patlen" "u_char freq[256]" | ||
46 | .Ft u_char * | ||
47 | .Fn bm_exec "bm_pat *pdesc" "u_char *text" "size_t len" | ||
48 | .Ft void | ||
49 | .Fn bm_free "bm_pat *pdesc" | ||
50 | .Sh DESCRIPTION | ||
51 | These routines implement an efficient mechanism to find an | ||
52 | occurrence of a byte string within another byte string. | ||
53 | .Pp | ||
54 | .Fn bm_comp | ||
55 | evaluates | ||
56 | .Fa patlen | ||
57 | bytes starting at | ||
58 | .Fa pattern | ||
59 | and returns a pointer to a structure describing them. | ||
60 | The bytes referenced by | ||
61 | .Fa pattern | ||
62 | may be of any value. | ||
63 | .Pp | ||
64 | The search takes advantage of the frequency distribution of the | ||
65 | bytes in the text to be searched. | ||
66 | If specified, | ||
67 | .Ar freq | ||
68 | should be an array of 256 values, | ||
69 | with higher values indicating that the corresponding character occurs | ||
70 | more frequently. | ||
71 | (A less than optimal frequency distribution can only result in less | ||
72 | than optimal performance, not incorrect results.) | ||
73 | If | ||
74 | .Ar freq | ||
75 | is | ||
76 | .Dv NULL , | ||
77 | a system default table is used. | ||
78 | .Pp | ||
79 | .Fn bm_exec | ||
80 | returns a pointer to the leftmost occurrence of the string given to | ||
81 | .Fn bm_comp | ||
82 | within | ||
83 | .Ar text , | ||
84 | or | ||
85 | .Dv NULL | ||
86 | if none occurs. | ||
87 | The number of bytes in | ||
88 | .Ar text | ||
89 | must be specified by | ||
90 | .Ar len . | ||
91 | .Pp | ||
92 | Space allocated for the returned description is discarded | ||
93 | by calling | ||
94 | .Fn bm_free | ||
95 | with the returned description as an argument. | ||
96 | .Pp | ||
97 | The asymptotic speed of | ||
98 | .Fn bm_exec | ||
99 | is | ||
100 | .Pf O Ns Pq len / patlen . | ||
101 | .Sh SEE ALSO | ||
102 | .Xr regexp 3 , | ||
103 | .Xr strstr 3 | ||
104 | .Rs | ||
105 | .%R "Fast String Searching" | ||
106 | .%A Andrew Hume | ||
107 | .%A Daniel Sunday | ||
108 | .%J "Software Practice and Experience" | ||
109 | .%V Volume 21, 11 (November 1991) | ||
110 | .%P 1221-48 | ||
111 | .Re | ||
diff --git a/src/lib/libc/string/bm.c b/src/lib/libc/string/bm.c new file mode 100644 index 0000000000..2c7f8fac32 --- /dev/null +++ b/src/lib/libc/string/bm.c | |||
@@ -0,0 +1,215 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1994 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Andrew Hume of AT&T Bell Laboratories. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: bm.c,v 1.4 2003/06/02 20:18:38 millert Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <sys/types.h> | ||
38 | |||
39 | #include <bm.h> | ||
40 | #include <errno.h> | ||
41 | #include <stdlib.h> | ||
42 | #include <string.h> | ||
43 | |||
44 | /* | ||
45 | * XXX | ||
46 | * The default frequency table starts at 99 and counts down. The default | ||
47 | * table should probably be oriented toward text, and will necessarily be | ||
48 | * locale specific. This one is for English. It was derived from the | ||
49 | * OSF/1 and 4.4BSD formatted and unformatted manual pages, and about 100Mb | ||
50 | * of email and random text. Change it if you can find something better. | ||
51 | */ | ||
52 | static u_char const freq_def[256] = { | ||
53 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
54 | 0, 77, 90, 0, 0, 0, 0, 0, | ||
55 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
56 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
57 | 99, 28, 42, 27, 16, 14, 20, 51, | ||
58 | 66, 65, 59, 24, 75, 76, 84, 56, | ||
59 | 72, 74, 64, 55, 54, 47, 41, 37, | ||
60 | 44, 61, 70, 43, 23, 53, 49, 22, | ||
61 | 33, 58, 40, 46, 45, 57, 60, 26, | ||
62 | 30, 63, 21, 12, 32, 50, 38, 39, | ||
63 | 34, 11, 48, 67, 62, 35, 15, 29, | ||
64 | 71, 18, 9, 17, 25, 13, 10, 52, | ||
65 | 36, 95, 78, 86, 87, 98, 82, 80, | ||
66 | 88, 94, 19, 68, 89, 83, 93, 96, | ||
67 | 81, 7, 91, 92, 97, 85, 69, 73, | ||
68 | 31, 79, 8, 5, 4, 6, 3, 0, | ||
69 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
70 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
71 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
72 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
73 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
74 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
75 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
76 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
77 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
78 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
79 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
80 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
81 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
82 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
83 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
84 | 0, 0, 0, 0, 0, 0, 0, 0, | ||
85 | }; | ||
86 | |||
87 | bm_pat * | ||
88 | bm_comp(pb, len, freq) | ||
89 | u_char const *pb; | ||
90 | size_t len; | ||
91 | u_char const *freq; | ||
92 | { | ||
93 | register u_char const *pe, *p; | ||
94 | register size_t *d, r; | ||
95 | register int j; | ||
96 | int sv_errno; | ||
97 | bm_pat *pat; | ||
98 | |||
99 | if (len == 0) { | ||
100 | errno = EINVAL; | ||
101 | return (NULL); | ||
102 | } | ||
103 | if ((pat = malloc(sizeof(*pat))) == NULL) | ||
104 | return (NULL); | ||
105 | pat->pat = NULL; | ||
106 | pat->delta = NULL; | ||
107 | |||
108 | pat->patlen = len; /* copy pattern */ | ||
109 | if ((pat->pat = malloc(pat->patlen)) == NULL) | ||
110 | goto mem; | ||
111 | memcpy(pat->pat, pb, pat->patlen); | ||
112 | /* get skip delta */ | ||
113 | if ((pat->delta = malloc(256 * sizeof(*d))) == NULL) | ||
114 | goto mem; | ||
115 | for (j = 0, d = pat->delta; j < 256; j++) | ||
116 | d[j] = pat->patlen; | ||
117 | for (pe = pb + pat->patlen - 1; pb <= pe; pb++) | ||
118 | d[*pb] = pe - pb; | ||
119 | |||
120 | if (freq == NULL) /* default freq table */ | ||
121 | freq = freq_def; | ||
122 | r = 0; /* get guard */ | ||
123 | for (pb = pat->pat, pe = pb + pat->patlen - 1; pb < pe; pb++) | ||
124 | if (freq[*pb] < freq[pat->pat[r]]) | ||
125 | r = pb - pat->pat; | ||
126 | pat->rarec = pat->pat[r]; | ||
127 | pat->rareoff = r - (pat->patlen - 1); | ||
128 | |||
129 | /* get md2 shift */ | ||
130 | for (pe = pat->pat + pat->patlen - 1, p = pe - 1; p >= pat->pat; p--) | ||
131 | if (*p == *pe) | ||
132 | break; | ||
133 | |||
134 | /* *p is first leftward reoccurrence of *pe */ | ||
135 | pat->md2 = pe - p; | ||
136 | return (pat); | ||
137 | |||
138 | mem: sv_errno = errno; | ||
139 | bm_free(pat); | ||
140 | errno = sv_errno; | ||
141 | return (NULL); | ||
142 | } | ||
143 | |||
144 | void | ||
145 | bm_free(pat) | ||
146 | bm_pat *pat; | ||
147 | { | ||
148 | if (pat->pat != NULL) | ||
149 | free(pat->pat); | ||
150 | if (pat->delta != NULL) | ||
151 | free(pat->delta); | ||
152 | free(pat); | ||
153 | } | ||
154 | |||
155 | u_char * | ||
156 | bm_exec(pat, base, n) | ||
157 | bm_pat *pat; | ||
158 | u_char *base; | ||
159 | size_t n; | ||
160 | { | ||
161 | register u_char *e, *ep, *p, *q, *s; | ||
162 | register size_t *d0, k, md2, n1, ro; | ||
163 | register int rc; | ||
164 | |||
165 | if (n == 0) | ||
166 | return (NULL); | ||
167 | |||
168 | d0 = pat->delta; | ||
169 | n1 = pat->patlen - 1; | ||
170 | md2 = pat->md2; | ||
171 | ro = pat->rareoff; | ||
172 | rc = pat->rarec; | ||
173 | ep = pat->pat + pat->patlen - 1; | ||
174 | s = base + (pat->patlen - 1); | ||
175 | |||
176 | /* fast loop up to n - 3 * patlen */ | ||
177 | e = base + n - 3 * pat->patlen; | ||
178 | while (s < e) { | ||
179 | k = d0[*s]; /* ufast skip loop */ | ||
180 | while (k) { | ||
181 | k = d0[*(s += k)]; | ||
182 | k = d0[*(s += k)]; | ||
183 | } | ||
184 | if (s >= e) | ||
185 | break; | ||
186 | if (s[ro] != rc) /* guard test */ | ||
187 | goto mismatch1; | ||
188 | /* fwd match */ | ||
189 | for (p = pat->pat, q = s - n1; p < ep;) | ||
190 | if (*q++ != *p++) | ||
191 | goto mismatch1; | ||
192 | return (s - n1); | ||
193 | |||
194 | mismatch1: s += md2; /* md2 shift */ | ||
195 | } | ||
196 | |||
197 | /* slow loop up to end */ | ||
198 | e = base + n; | ||
199 | while (s < e) { | ||
200 | s += d0[*s]; /* step */ | ||
201 | if (s >= e) | ||
202 | break; | ||
203 | if (s[ro] != rc) /* guard test */ | ||
204 | goto mismatch2; | ||
205 | /* fwd match */ | ||
206 | for (p = pat->pat, q = s - n1; p <= ep;) | ||
207 | if (*q++ != *p++) | ||
208 | goto mismatch2; | ||
209 | return (s - n1); | ||
210 | |||
211 | mismatch2: s += md2; /* md2 shift */ | ||
212 | } | ||
213 | |||
214 | return (NULL); | ||
215 | } | ||
diff --git a/src/lib/libc/string/bstring.3 b/src/lib/libc/string/bstring.3 new file mode 100644 index 0000000000..54d8302ff2 --- /dev/null +++ b/src/lib/libc/string/bstring.3 | |||
@@ -0,0 +1,106 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" $OpenBSD: bstring.3,v 1.5 2003/06/02 20:18:38 millert Exp $ | ||
31 | .\" | ||
32 | .Dd April 19, 1991 | ||
33 | .Dt BSTRING 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm bcmp , | ||
37 | .Nm bcopy , | ||
38 | .Nm bzero , | ||
39 | .Nm memccpy , | ||
40 | .Nm memchr , | ||
41 | .Nm memcmp , | ||
42 | .Nm memcpy , | ||
43 | .Nm memmove , | ||
44 | .Nm memset | ||
45 | .Nd byte string operations | ||
46 | .Sh SYNOPSIS | ||
47 | .Fd #include <string.h> | ||
48 | .Ft int | ||
49 | .Fn bcmp "const void *b1" "const void *b2" "size_t len" | ||
50 | .Ft void | ||
51 | .Fn bcopy "const void *src" "void *dst" "size_t len" | ||
52 | .Ft void | ||
53 | .Fn bzero "void *b" "size_t len" | ||
54 | .Ft void * | ||
55 | .Fn memchr "const void *b" "int c" "size_t len" | ||
56 | .Ft int | ||
57 | .Fn memcmp "const void *b1" "const void *b2" "size_t len" | ||
58 | .Ft void * | ||
59 | .Fn memccpy "void *dst" "const void *src" "int c" "size_t len" | ||
60 | .Ft void * | ||
61 | .Fn memcpy "void *dst" "const void *src" "size_t len" | ||
62 | .Ft void * | ||
63 | .Fn memmove "void *dst" "const void *src" "size_t len" | ||
64 | .Ft void * | ||
65 | .Fn memset "void *b" "int c" "size_t len" | ||
66 | .Sh DESCRIPTION | ||
67 | These functions operate on variable length strings of bytes. | ||
68 | They do not check for terminating null bytes as the routines | ||
69 | listed in | ||
70 | .Xr string 3 | ||
71 | do. | ||
72 | .Pp | ||
73 | See the specific manual pages for more information. | ||
74 | .Sh SEE ALSO | ||
75 | .Xr bcmp 3 , | ||
76 | .Xr bcopy 3 , | ||
77 | .Xr bzero 3 , | ||
78 | .Xr memccpy 3 , | ||
79 | .Xr memchr 3 , | ||
80 | .Xr memcmp 3 , | ||
81 | .Xr memcpy 3 , | ||
82 | .Xr memmove 3 , | ||
83 | .Xr memset 3 | ||
84 | .Sh STANDARDS | ||
85 | The functions | ||
86 | .Fn memchr , | ||
87 | .Fn memcmp , | ||
88 | .Fn memcpy , | ||
89 | .Fn memmove , | ||
90 | and | ||
91 | .Fn memset | ||
92 | conform to | ||
93 | .St -ansiC . | ||
94 | .Sh HISTORY | ||
95 | The | ||
96 | .Fn bzero | ||
97 | and | ||
98 | .Fn memccpy | ||
99 | functions appeared in | ||
100 | .Bx 4.3 . | ||
101 | The | ||
102 | .Fn bcmp | ||
103 | and | ||
104 | .Fn bcopy | ||
105 | functions appeared in | ||
106 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/string/bzero.3 b/src/lib/libc/string/bzero.3 new file mode 100644 index 0000000000..592d9f2f08 --- /dev/null +++ b/src/lib/libc/string/bzero.3 | |||
@@ -0,0 +1,61 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" $OpenBSD: bzero.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
31 | .\" | ||
32 | .Dd April 19, 1991 | ||
33 | .Dt BZERO 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm bzero | ||
37 | .Nd write zeroes to a byte string | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft void | ||
41 | .Fn bzero "void *b" "size_t len" | ||
42 | .Sh DESCRIPTION | ||
43 | The | ||
44 | .Fn bzero | ||
45 | function writes | ||
46 | .Fa len | ||
47 | zero bytes to the string | ||
48 | .Fa b . | ||
49 | If | ||
50 | .Fa len | ||
51 | is zero, | ||
52 | .Fn bzero | ||
53 | does nothing. | ||
54 | .Sh SEE ALSO | ||
55 | .Xr memset 3 , | ||
56 | .Xr swab 3 | ||
57 | .Sh HISTORY | ||
58 | A | ||
59 | .Fn bzero | ||
60 | function appeared in | ||
61 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/string/bzero.c b/src/lib/libc/string/bzero.c new file mode 100644 index 0000000000..7e557ecd4a --- /dev/null +++ b/src/lib/libc/string/bzero.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1987 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: bzero.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #ifndef _KERNEL | ||
35 | #include <string.h> | ||
36 | #else | ||
37 | #include <lib/libkern/libkern.h> | ||
38 | #endif | ||
39 | |||
40 | /* | ||
41 | * bzero -- vax movc5 instruction | ||
42 | */ | ||
43 | void | ||
44 | bzero(void *b, size_t length) | ||
45 | { | ||
46 | char *p; | ||
47 | |||
48 | for (p = b; length--;) | ||
49 | *p++ = '\0'; | ||
50 | } | ||
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3 new file mode 100644 index 0000000000..a2f0cd336d --- /dev/null +++ b/src/lib/libc/string/ffs.3 | |||
@@ -0,0 +1,56 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" $OpenBSD: ffs.3,v 1.4 2003/06/02 20:18:38 millert Exp $ | ||
31 | .\" | ||
32 | .Dd April 19, 1991 | ||
33 | .Dt FFS 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm ffs | ||
37 | .Nd find first bit set in a bit string | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft int | ||
41 | .Fn ffs "int value" | ||
42 | .Sh DESCRIPTION | ||
43 | The | ||
44 | .Fn ffs | ||
45 | function finds the first bit set in | ||
46 | .Fa value | ||
47 | and returns the index of that bit. | ||
48 | Bits are numbered starting from 1, starting at the rightmost bit. | ||
49 | A return value of 0 means that the argument was zero. | ||
50 | .Sh SEE ALSO | ||
51 | .Xr bitstring 3 | ||
52 | .Sh HISTORY | ||
53 | The | ||
54 | .Fn ffs | ||
55 | function appeared in | ||
56 | .Bx 4.3 . | ||
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c new file mode 100644 index 0000000000..f1d14096a2 --- /dev/null +++ b/src/lib/libc/string/ffs.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: ffs.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Public domain. | ||
5 | * Written by Dale Rahn. | ||
6 | */ | ||
7 | |||
8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
9 | static char *rcsid = "$OpenBSD: ffs.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $"; | ||
10 | #endif /* LIBC_SCCS and not lint */ | ||
11 | |||
12 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
13 | #include <string.h> | ||
14 | #else | ||
15 | #include <lib/libkern/libkern.h> | ||
16 | #endif | ||
17 | |||
18 | /* | ||
19 | * ffs -- vax ffs instruction | ||
20 | */ | ||
21 | int | ||
22 | ffs(int mask) | ||
23 | { | ||
24 | int bit; | ||
25 | unsigned int r = mask; | ||
26 | static const signed char t[16] = { | ||
27 | -28, 1, 2, 1, | ||
28 | 3, 1, 2, 1, | ||
29 | 4, 1, 2, 1, | ||
30 | 3, 1, 2, 1 | ||
31 | }; | ||
32 | |||
33 | bit = 0; | ||
34 | if (!(r & 0xffff)) { | ||
35 | bit += 16; | ||
36 | r >>= 16; | ||
37 | } | ||
38 | if (!(r & 0xff)) { | ||
39 | bit += 8; | ||
40 | r >>= 8; | ||
41 | } | ||
42 | if (!(r & 0xf)) { | ||
43 | bit += 4; | ||
44 | r >>= 4; | ||
45 | } | ||
46 | |||
47 | return (bit + t[ r & 0xf ]); | ||
48 | } | ||
diff --git a/src/lib/libc/string/index.c b/src/lib/libc/string/index.c new file mode 100644 index 0000000000..52d0fd46fa --- /dev/null +++ b/src/lib/libc/string/index.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: index.c,v 1.4 2003/06/02 20:18:38 millert Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <string.h> | ||
35 | |||
36 | char * | ||
37 | #ifdef STRCHR | ||
38 | strchr(const char *p, int ch) | ||
39 | #else | ||
40 | index(const char *p, int ch) | ||
41 | #endif | ||
42 | { | ||
43 | for (;; ++p) { | ||
44 | if (*p == ch) | ||
45 | return((char *)p); | ||
46 | if (!*p) | ||
47 | return((char *)NULL); | ||
48 | } | ||
49 | /* NOTREACHED */ | ||
50 | } | ||
diff --git a/src/lib/libc/string/memccpy.3 b/src/lib/libc/string/memccpy.3 new file mode 100644 index 0000000000..8c159cb8de --- /dev/null +++ b/src/lib/libc/string/memccpy.3 | |||
@@ -0,0 +1,73 @@ | |||
1 | .\" $OpenBSD: memccpy.3,v 1.8 2003/06/02 20:18:38 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" @(#)memccpy.3 8.1 (Berkeley) 6/9/93 | ||
31 | .\" | ||
32 | .Dd June 9, 1993 | ||
33 | .Dt MEMCCPY 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm memccpy | ||
37 | .Nd copy string until character found | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft void * | ||
41 | .Fn memccpy "void *dst" "const void *src" "int c" "size_t len" | ||
42 | .Sh DESCRIPTION | ||
43 | The | ||
44 | .Fn memccpy | ||
45 | function copies bytes from string | ||
46 | .Fa src | ||
47 | to string | ||
48 | .Fa dst . | ||
49 | If the character | ||
50 | .Fa c | ||
51 | (as converted to an | ||
52 | .Li unsigned char ) | ||
53 | occurs in the string | ||
54 | .Fa src , | ||
55 | the copy stops and a pointer to the byte after the copy of | ||
56 | .Fa c | ||
57 | in the string | ||
58 | .Fa dst | ||
59 | is returned. | ||
60 | Otherwise, | ||
61 | .Fa len | ||
62 | bytes are copied, and a null pointer is returned. | ||
63 | .Sh SEE ALSO | ||
64 | .Xr bcopy 3 , | ||
65 | .Xr memcpy 3 , | ||
66 | .Xr memmove 3 , | ||
67 | .Xr strcpy 3 , | ||
68 | .Xr strlcpy 3 | ||
69 | .Sh HISTORY | ||
70 | The | ||
71 | .Fn memccpy | ||
72 | function first appeared in | ||
73 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/string/memccpy.c b/src/lib/libc/string/memccpy.c new file mode 100644 index 0000000000..37496d1c26 --- /dev/null +++ b/src/lib/libc/string/memccpy.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /* $OpenBSD: memccpy.c,v 1.4 2003/06/02 20:18:38 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. Neither the name of the University nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #if defined(LIBC_SCCS) && !defined(lint) | ||
33 | #if 0 | ||
34 | static char sccsid[] = "@(#)memccpy.c 8.1 (Berkeley) 6/4/93"; | ||
35 | #else | ||
36 | static char *rcsid = "$OpenBSD: memccpy.c,v 1.4 2003/06/02 20:18:38 millert Exp $"; | ||
37 | #endif | ||
38 | #endif /* LIBC_SCCS and not lint */ | ||
39 | |||
40 | #include <string.h> | ||
41 | |||
42 | void * | ||
43 | memccpy(t, f, c, n) | ||
44 | void *t; | ||
45 | const void *f; | ||
46 | int c; | ||
47 | register size_t n; | ||
48 | { | ||
49 | |||
50 | if (n) { | ||
51 | register unsigned char *tp = t; | ||
52 | register const unsigned char *fp = f; | ||
53 | register unsigned char uc = c; | ||
54 | do { | ||
55 | if ((*tp++ = *fp++) == uc) | ||
56 | return (tp); | ||
57 | } while (--n != 0); | ||
58 | } | ||
59 | return (0); | ||
60 | } | ||
diff --git a/src/lib/libc/string/memchr.3 b/src/lib/libc/string/memchr.3 new file mode 100644 index 0000000000..ddca58bc5e --- /dev/null +++ b/src/lib/libc/string/memchr.3 | |||
@@ -0,0 +1,74 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: memchr.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt MEMCHR 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm memchr | ||
39 | .Nd locate byte in byte string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft void * | ||
43 | .Fn memchr "const void *b" "int c" "size_t len" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn memchr | ||
47 | function locates the first occurrence of | ||
48 | .Fa c | ||
49 | (converted to an | ||
50 | .Li unsigned char ) | ||
51 | in string | ||
52 | .Fa b . | ||
53 | .Sh RETURN VALUES | ||
54 | The | ||
55 | .Fn memchr | ||
56 | function returns a pointer to the byte located, or | ||
57 | .Dv NULL | ||
58 | if no such byte exists within | ||
59 | .Fa len | ||
60 | bytes. | ||
61 | .Sh SEE ALSO | ||
62 | .Xr strchr 3 , | ||
63 | .Xr strcspn 3 , | ||
64 | .Xr strpbrk 3 , | ||
65 | .Xr strrchr 3 , | ||
66 | .Xr strsep 3 , | ||
67 | .Xr strspn 3 , | ||
68 | .Xr strstr 3 , | ||
69 | .Xr strtok 3 | ||
70 | .Sh STANDARDS | ||
71 | The | ||
72 | .Fn memchr | ||
73 | function conforms to | ||
74 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memchr.c b/src/lib/libc/string/memchr.c new file mode 100644 index 0000000000..454abcbc17 --- /dev/null +++ b/src/lib/libc/string/memchr.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: memchr.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | void * | ||
40 | memchr(const void *s, int c, size_t n) | ||
41 | { | ||
42 | if (n != 0) { | ||
43 | const unsigned char *p = s; | ||
44 | |||
45 | do { | ||
46 | if (*p++ == (unsigned char)c) | ||
47 | return ((void *)(p - 1)); | ||
48 | } while (--n != 0); | ||
49 | } | ||
50 | return (NULL); | ||
51 | } | ||
diff --git a/src/lib/libc/string/memcmp.3 b/src/lib/libc/string/memcmp.3 new file mode 100644 index 0000000000..bd46e6898e --- /dev/null +++ b/src/lib/libc/string/memcmp.3 | |||
@@ -0,0 +1,77 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: memcmp.3,v 1.5 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt MEMCMP 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm memcmp | ||
39 | .Nd compare byte string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft int | ||
43 | .Fn memcmp "const void *b1" "const void *b2" "size_t len" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn memcmp | ||
47 | function compares byte string | ||
48 | .Fa b1 | ||
49 | against byte string | ||
50 | .Fa b2 . | ||
51 | Both strings are assumed to be | ||
52 | .Fa len | ||
53 | bytes long. | ||
54 | .Sh RETURN VALUES | ||
55 | The | ||
56 | .Fn memcmp | ||
57 | function returns zero if the two strings are identical, | ||
58 | otherwise returns the difference between the first two differing bytes | ||
59 | (treated as | ||
60 | .Li unsigned char | ||
61 | values, so that | ||
62 | .Sq Li \e200 | ||
63 | is greater than | ||
64 | .Sq Li \&\e0 , | ||
65 | for example). | ||
66 | Zero-length strings are always identical. | ||
67 | .Sh SEE ALSO | ||
68 | .Xr bcmp 3 , | ||
69 | .Xr strcasecmp 3 , | ||
70 | .Xr strcmp 3 , | ||
71 | .Xr strcoll 3 , | ||
72 | .Xr strxfrm 3 | ||
73 | .Sh STANDARDS | ||
74 | The | ||
75 | .Fn memcmp | ||
76 | function conforms to | ||
77 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memcmp.c b/src/lib/libc/string/memcmp.c new file mode 100644 index 0000000000..6c83208a7b --- /dev/null +++ b/src/lib/libc/string/memcmp.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: memcmp.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Compare memory regions. | ||
41 | */ | ||
42 | int | ||
43 | memcmp(const void *s1, const void *s2, size_t n) | ||
44 | { | ||
45 | if (n != 0) { | ||
46 | const unsigned char *p1 = s1, *p2 = s2; | ||
47 | |||
48 | do { | ||
49 | if (*p1++ != *p2++) | ||
50 | return (*--p1 - *--p2); | ||
51 | } while (--n != 0); | ||
52 | } | ||
53 | return (0); | ||
54 | } | ||
diff --git a/src/lib/libc/string/memcpy.3 b/src/lib/libc/string/memcpy.3 new file mode 100644 index 0000000000..c8265d94a6 --- /dev/null +++ b/src/lib/libc/string/memcpy.3 | |||
@@ -0,0 +1,77 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: memcpy.3,v 1.6 2003/06/21 12:04:01 avsm Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt MEMCPY 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm memcpy | ||
39 | .Nd copy bytes | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft void * | ||
43 | .Fn memcpy "void *dst" "const void *src" "size_t len" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn memcpy | ||
47 | function copies | ||
48 | .Fa len | ||
49 | bytes from buffer | ||
50 | .Fa src | ||
51 | to buffer | ||
52 | .Fa dst . | ||
53 | .Sh RETURN VALUES | ||
54 | The | ||
55 | .Fn memcpy | ||
56 | function returns the original value of | ||
57 | .Fa dst . | ||
58 | .Sh SEE ALSO | ||
59 | .Xr bcopy 3 , | ||
60 | .Xr memccpy 3 , | ||
61 | .Xr memmove 3 , | ||
62 | .Xr strcpy 3 , | ||
63 | .Xr strlcpy 3 | ||
64 | .Sh STANDARDS | ||
65 | The | ||
66 | .Fn memcpy | ||
67 | function conforms to | ||
68 | .St -ansiC . | ||
69 | .Sh BUGS | ||
70 | In this implementation | ||
71 | .Fn memcpy | ||
72 | is implemented using | ||
73 | .Xr bcopy 3 , | ||
74 | and therefore the buffers may overlap. | ||
75 | On other systems, copying overlapping buffers may produce surprises. | ||
76 | A simpler solution is to not use | ||
77 | .Fn memcpy . | ||
diff --git a/src/lib/libc/string/memmove.3 b/src/lib/libc/string/memmove.3 new file mode 100644 index 0000000000..d4a364eb36 --- /dev/null +++ b/src/lib/libc/string/memmove.3 | |||
@@ -0,0 +1,70 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: memmove.3,v 1.6 2003/06/21 12:04:01 avsm Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt MEMMOVE 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm memmove | ||
39 | .Nd copy bytes | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft void * | ||
43 | .Fn memmove "void *dst" "const void *src" "size_t len" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn memmove | ||
47 | function copies | ||
48 | .Fa len | ||
49 | bytes from buffer | ||
50 | .Fa src | ||
51 | to buffer | ||
52 | .Fa dst . | ||
53 | The two buffers may overlap; | ||
54 | the copy is always done in a non-destructive manner. | ||
55 | .Sh RETURN VALUES | ||
56 | The | ||
57 | .Fn memmove | ||
58 | function returns the original value of | ||
59 | .Fa dst . | ||
60 | .Sh SEE ALSO | ||
61 | .Xr bcopy 3 , | ||
62 | .Xr memccpy 3 , | ||
63 | .Xr memcpy 3 , | ||
64 | .Xr strcpy 3 , | ||
65 | .Xr strlcpy 3 | ||
66 | .Sh STANDARDS | ||
67 | The | ||
68 | .Fn memmove | ||
69 | function conforms to | ||
70 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memset.3 b/src/lib/libc/string/memset.3 new file mode 100644 index 0000000000..291b1427dc --- /dev/null +++ b/src/lib/libc/string/memset.3 | |||
@@ -0,0 +1,67 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: memset.3,v 1.5 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt MEMSET 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm memset | ||
39 | .Nd write a byte to byte string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft void * | ||
43 | .Fn memset "void *b" "int c" "size_t len" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn memset | ||
47 | function writes | ||
48 | .Fa len | ||
49 | bytes of value | ||
50 | .Fa c | ||
51 | (converted to an | ||
52 | .Li unsigned char ) | ||
53 | to the string | ||
54 | .Fa b . | ||
55 | .Sh RETURN VALUES | ||
56 | The | ||
57 | .Fn memset | ||
58 | function returns the original value of | ||
59 | .Fa b . | ||
60 | .Sh SEE ALSO | ||
61 | .Xr bzero 3 , | ||
62 | .Xr swab 3 | ||
63 | .Sh STANDARDS | ||
64 | The | ||
65 | .Fn memset | ||
66 | function conforms to | ||
67 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/memset.c b/src/lib/libc/string/memset.c new file mode 100644 index 0000000000..9bc0556faa --- /dev/null +++ b/src/lib/libc/string/memset.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: memset.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | void * | ||
40 | memset(void *dst, int c, size_t n) | ||
41 | { | ||
42 | |||
43 | if (n != 0) { | ||
44 | char *d = dst; | ||
45 | |||
46 | do | ||
47 | *d++ = c; | ||
48 | while (--n != 0); | ||
49 | } | ||
50 | return (dst); | ||
51 | } | ||
diff --git a/src/lib/libc/string/rindex.c b/src/lib/libc/string/rindex.c new file mode 100644 index 0000000000..b880d0eaeb --- /dev/null +++ b/src/lib/libc/string/rindex.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: rindex.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <string.h> | ||
35 | |||
36 | char * | ||
37 | #ifdef STRRCHR | ||
38 | strrchr(const char *p, int ch) | ||
39 | #else | ||
40 | rindex(const char *p, int ch) | ||
41 | #endif | ||
42 | { | ||
43 | char *save; | ||
44 | |||
45 | for (save = NULL;; ++p) { | ||
46 | if (*p == ch) | ||
47 | save = (char *)p; | ||
48 | if (!*p) | ||
49 | return(save); | ||
50 | } | ||
51 | /* NOTREACHED */ | ||
52 | } | ||
diff --git a/src/lib/libc/string/strcasecmp.3 b/src/lib/libc/string/strcasecmp.3 new file mode 100644 index 0000000000..e1f5d416d1 --- /dev/null +++ b/src/lib/libc/string/strcasecmp.3 | |||
@@ -0,0 +1,84 @@ | |||
1 | .\" $OpenBSD: strcasecmp.3,v 1.8 2003/06/02 20:18:38 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1990, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" This code is derived from software contributed to Berkeley by | ||
7 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" @(#)strcasecmp.3 8.1 (Berkeley) 6/9/93 | ||
33 | .\" | ||
34 | .Dd June 9, 1993 | ||
35 | .Dt STRCASECMP 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strcasecmp , | ||
39 | .Nm strncasecmp | ||
40 | .Nd compare strings, ignoring case | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft int | ||
44 | .Fn strcasecmp "const char *s1" "const char *s2" | ||
45 | .Ft int | ||
46 | .Fn strncasecmp "const char *s1" "const char *s2" "size_t len" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn strcasecmp | ||
50 | and | ||
51 | .Fn strncasecmp | ||
52 | functions compare the null-terminated strings | ||
53 | .Fa s1 | ||
54 | and | ||
55 | .Fa s2 | ||
56 | and return an integer greater than, equal to, or less than 0, | ||
57 | according to whether | ||
58 | .Fa s1 | ||
59 | is lexicographically greater than, equal to, or less than | ||
60 | .Fa s2 | ||
61 | after translation of each corresponding character to lower-case. | ||
62 | The strings themselves are not modified. | ||
63 | The comparison is done using unsigned characters, so that | ||
64 | .Sq Li \e200 | ||
65 | is greater than | ||
66 | .Ql \e0 . | ||
67 | .Pp | ||
68 | .Fn strncasecmp | ||
69 | compares at most | ||
70 | .Fa len | ||
71 | characters. | ||
72 | .Sh SEE ALSO | ||
73 | .Xr bcmp 3 , | ||
74 | .Xr memcmp 3 , | ||
75 | .Xr strcmp 3 , | ||
76 | .Xr strcoll 3 , | ||
77 | .Xr strxfrm 3 | ||
78 | .Sh HISTORY | ||
79 | The | ||
80 | .Fn strcasecmp | ||
81 | and | ||
82 | .Fn strncasecmp | ||
83 | functions first appeared in | ||
84 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/string/strcasecmp.c b/src/lib/libc/string/strcasecmp.c new file mode 100644 index 0000000000..62da063444 --- /dev/null +++ b/src/lib/libc/string/strcasecmp.c | |||
@@ -0,0 +1,113 @@ | |||
1 | /* $OpenBSD: strcasecmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1987, 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. Neither the name of the University nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <string.h> | ||
33 | |||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | #if 0 | ||
36 | static char sccsid[] = "@(#)strcasecmp.c 8.1 (Berkeley) 6/4/93"; | ||
37 | #else | ||
38 | static char *rcsid = "$OpenBSD: strcasecmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
39 | #endif | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | typedef unsigned char u_char; | ||
43 | |||
44 | /* | ||
45 | * This array is designed for mapping upper and lower case letter | ||
46 | * together for a case independent comparison. The mappings are | ||
47 | * based upon ascii character sequences. | ||
48 | */ | ||
49 | static const u_char charmap[] = { | ||
50 | '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007', | ||
51 | '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017', | ||
52 | '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027', | ||
53 | '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037', | ||
54 | '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047', | ||
55 | '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057', | ||
56 | '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067', | ||
57 | '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077', | ||
58 | '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147', | ||
59 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', | ||
60 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', | ||
61 | '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137', | ||
62 | '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147', | ||
63 | '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157', | ||
64 | '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167', | ||
65 | '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177', | ||
66 | '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207', | ||
67 | '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217', | ||
68 | '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227', | ||
69 | '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237', | ||
70 | '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247', | ||
71 | '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257', | ||
72 | '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267', | ||
73 | '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277', | ||
74 | '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307', | ||
75 | '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317', | ||
76 | '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327', | ||
77 | '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337', | ||
78 | '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347', | ||
79 | '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357', | ||
80 | '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367', | ||
81 | '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377', | ||
82 | }; | ||
83 | |||
84 | int | ||
85 | strcasecmp(const char *s1, const char *s2) | ||
86 | { | ||
87 | const u_char *cm = charmap; | ||
88 | const u_char *us1 = (const u_char *)s1; | ||
89 | const u_char *us2 = (const u_char *)s2; | ||
90 | |||
91 | while (cm[*us1] == cm[*us2++]) | ||
92 | if (*us1++ == '\0') | ||
93 | return (0); | ||
94 | return (cm[*us1] - cm[*--us2]); | ||
95 | } | ||
96 | |||
97 | int | ||
98 | strncasecmp(const char *s1, const char *s2, size_t n) | ||
99 | { | ||
100 | if (n != 0) { | ||
101 | const u_char *cm = charmap; | ||
102 | const u_char *us1 = (const u_char *)s1; | ||
103 | const u_char *us2 = (const u_char *)s2; | ||
104 | |||
105 | do { | ||
106 | if (cm[*us1] != cm[*us2++]) | ||
107 | return (cm[*us1] - cm[*--us2]); | ||
108 | if (*us1++ == '\0') | ||
109 | break; | ||
110 | } while (--n != 0); | ||
111 | } | ||
112 | return (0); | ||
113 | } | ||
diff --git a/src/lib/libc/string/strcat.3 b/src/lib/libc/string/strcat.3 new file mode 100644 index 0000000000..5b878c3e8c --- /dev/null +++ b/src/lib/libc/string/strcat.3 | |||
@@ -0,0 +1,131 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strcat.3,v 1.9 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd July 8, 1997 | ||
35 | .Dt STRCAT 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strcat , | ||
39 | .Nm strncat | ||
40 | .Nd concatenate strings | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft char * | ||
44 | .Fn strcat "char *s" "const char *append" | ||
45 | .Ft char * | ||
46 | .Fn strncat "char *s" "const char *append" "size_t count" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn strcat | ||
50 | and | ||
51 | .Fn strncat | ||
52 | functions append a copy of the null-terminated string | ||
53 | .Fa append | ||
54 | to the end of the null-terminated string | ||
55 | .Fa s , | ||
56 | then add a terminating | ||
57 | .Ql \e0 . | ||
58 | The string | ||
59 | .Fa s | ||
60 | must have sufficient space to hold the result. | ||
61 | .Pp | ||
62 | The | ||
63 | .Fn strncat | ||
64 | function appends not more than | ||
65 | .Fa count | ||
66 | characters where space for the terminating | ||
67 | .Ql \e0 | ||
68 | should not be included in | ||
69 | .Fa count . | ||
70 | .Sh RETURN VALUES | ||
71 | The | ||
72 | .Fn strcat | ||
73 | and | ||
74 | .Fn strncat | ||
75 | functions return the pointer | ||
76 | .Fa s . | ||
77 | .Sh EXAMPLES | ||
78 | The following appends | ||
79 | .Dq Li abc | ||
80 | to | ||
81 | .Dq Li chararray : | ||
82 | .Bd -literal -offset indent | ||
83 | char *letters = "abcdefghi"; | ||
84 | |||
85 | (void)strncat(chararray, letters, 3); | ||
86 | .Ed | ||
87 | .Pp | ||
88 | The following example shows how to use | ||
89 | .Fn strncat | ||
90 | safely in conjunction with | ||
91 | .Xr strncpy 3 . | ||
92 | .Bd -literal -offset indent | ||
93 | char buf[BUFSIZ]; | ||
94 | char *input, *suffix; | ||
95 | |||
96 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
97 | buf[sizeof(buf) - 1] = '\e0'; | ||
98 | (void)strncat(buf, suffix, sizeof(buf) - 1 - strlen(buf)); | ||
99 | .Ed | ||
100 | .Pp | ||
101 | The above will copy as many characters from | ||
102 | .Dq Li input | ||
103 | to | ||
104 | .Dq Li buf | ||
105 | as will fit. | ||
106 | It then appends as many characters from suffix as will fit (or none | ||
107 | if there is no space). | ||
108 | For operations like this, the | ||
109 | .Xr strlcpy 3 | ||
110 | and | ||
111 | .Xr strlcat 3 | ||
112 | functions are a better choice, as shown below. | ||
113 | .Bd -literal -offset indent | ||
114 | (void)strlcpy(buf, input, sizeof(buf)); | ||
115 | (void)strlcat(buf, suffix, sizeof(buf)); | ||
116 | .Ed | ||
117 | .Sh SEE ALSO | ||
118 | .Xr bcopy 3 , | ||
119 | .Xr memccpy 3 , | ||
120 | .Xr memcpy 3 , | ||
121 | .Xr memmove 3 , | ||
122 | .Xr strcpy 3 , | ||
123 | .Xr strlcat 3 , | ||
124 | .Xr strlcpy 3 | ||
125 | .Sh STANDARDS | ||
126 | The | ||
127 | .Fn strcat | ||
128 | and | ||
129 | .Fn strncat | ||
130 | functions conform to | ||
131 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcat.c b/src/lib/libc/string/strcat.c new file mode 100644 index 0000000000..885822bae3 --- /dev/null +++ b/src/lib/libc/string/strcat.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strcat.c,v 1.6 2003/07/24 01:15:42 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #ifndef _KERNEL | ||
35 | #include <string.h> | ||
36 | #else | ||
37 | #include <lib/libkern/libkern.h> | ||
38 | #endif | ||
39 | |||
40 | #if defined(APIWARN) | ||
41 | __warn_references(strcat, | ||
42 | "warning: strcat() is almost always misused, please use strlcat()"); | ||
43 | #endif | ||
44 | |||
45 | char * | ||
46 | strcat(char *s, const char *append) | ||
47 | { | ||
48 | char *save = s; | ||
49 | |||
50 | for (; *s; ++s); | ||
51 | while ((*s++ = *append++) != '\0'); | ||
52 | return(save); | ||
53 | } | ||
diff --git a/src/lib/libc/string/strchr.3 b/src/lib/libc/string/strchr.3 new file mode 100644 index 0000000000..86841bac25 --- /dev/null +++ b/src/lib/libc/string/strchr.3 | |||
@@ -0,0 +1,102 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strchr.3,v 1.8 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRCHR 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strchr , | ||
39 | .Nm index | ||
40 | .Nd locate first occurrence of a character in a string | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft char * | ||
44 | .Fn strchr "const char *s" "int c" | ||
45 | .Ft char * | ||
46 | .Fn index "const char *s" "int c" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn strchr | ||
50 | function locates the first occurrence of the character | ||
51 | .Fa c | ||
52 | in the string | ||
53 | .Fa s . | ||
54 | The terminating NUL character is considered part of the string. | ||
55 | If | ||
56 | .Fa c | ||
57 | is | ||
58 | .Ql \e0 , | ||
59 | .Fn strchr | ||
60 | locates the terminating | ||
61 | .Ql \e0 . | ||
62 | .Pp | ||
63 | The | ||
64 | .Fn index | ||
65 | function is an old synonym for | ||
66 | .Fn strchr . | ||
67 | .Sh RETURN VALUES | ||
68 | The | ||
69 | .Fn strchr | ||
70 | function returns a pointer to the located character or | ||
71 | .Dv NULL | ||
72 | if the character does not appear in the string. | ||
73 | .Sh EXAMPLES | ||
74 | After the following call to | ||
75 | .Fn strchr , | ||
76 | .Va p | ||
77 | will point to the string | ||
78 | .Qq oobar : | ||
79 | .Bd -literal -offset indent | ||
80 | char *p; | ||
81 | char *s = "foobar"; | ||
82 | |||
83 | p = strchr(s, 'o'); | ||
84 | .Ed | ||
85 | .Sh SEE ALSO | ||
86 | .Xr memchr 3 , | ||
87 | .Xr strcspn 3 , | ||
88 | .Xr strpbrk 3 , | ||
89 | .Xr strrchr 3 , | ||
90 | .Xr strsep 3 , | ||
91 | .Xr strspn 3 , | ||
92 | .Xr strstr 3 , | ||
93 | .Xr strtok 3 | ||
94 | .Sh STANDARDS | ||
95 | The | ||
96 | .Fn strchr | ||
97 | function conforms to | ||
98 | .St -ansiC . | ||
99 | .Pp | ||
100 | The | ||
101 | .Fn index | ||
102 | function is deprecated and shouldn't be used in new code. | ||
diff --git a/src/lib/libc/string/strcmp.3 b/src/lib/libc/string/strcmp.3 new file mode 100644 index 0000000000..d025b9cf7c --- /dev/null +++ b/src/lib/libc/string/strcmp.3 | |||
@@ -0,0 +1,87 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strcmp.3,v 1.7 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRCMP 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strcmp , | ||
39 | .Nm strncmp | ||
40 | .Nd compare strings | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft int | ||
44 | .Fn strcmp "const char *s1" "const char *s2" | ||
45 | .Ft int | ||
46 | .Fn strncmp "const char *s1" "const char *s2" "size_t len" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn strcmp | ||
50 | and | ||
51 | .Fn strncmp | ||
52 | functions lexicographically compare the null-terminated strings | ||
53 | .Fa s1 | ||
54 | and | ||
55 | .Fa s2 . | ||
56 | .Sh RETURN VALUES | ||
57 | The | ||
58 | .Fn strcmp | ||
59 | and | ||
60 | .Fn strncmp | ||
61 | return an integer greater than, equal to, or less than 0, according | ||
62 | to whether the string | ||
63 | .Fa s1 | ||
64 | is greater than, equal to, or less than the string | ||
65 | .Fa s2 . | ||
66 | The comparison is done using unsigned characters, so that | ||
67 | .Ql \e200 | ||
68 | is greater than | ||
69 | .Ql \e0 . | ||
70 | .Pp | ||
71 | .Fn strncmp | ||
72 | compares at most | ||
73 | .Fa len | ||
74 | characters. | ||
75 | .Sh SEE ALSO | ||
76 | .Xr bcmp 3 , | ||
77 | .Xr memcmp 3 , | ||
78 | .Xr strcasecmp 3 , | ||
79 | .Xr strcoll 3 , | ||
80 | .Xr strxfrm 3 | ||
81 | .Sh STANDARDS | ||
82 | The | ||
83 | .Fn strcmp | ||
84 | and | ||
85 | .Fn strncmp | ||
86 | functions conform to | ||
87 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcmp.c b/src/lib/libc/string/strcmp.c new file mode 100644 index 0000000000..9bda0a8d75 --- /dev/null +++ b/src/lib/libc/string/strcmp.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strcmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #ifndef _KERNEL | ||
38 | #include <string.h> | ||
39 | #else | ||
40 | #include <lib/libkern/libkern.h> | ||
41 | #endif | ||
42 | |||
43 | /* | ||
44 | * Compare strings. | ||
45 | */ | ||
46 | int | ||
47 | strcmp(const char *s1, const char *s2) | ||
48 | { | ||
49 | while (*s1 == *s2++) | ||
50 | if (*s1++ == 0) | ||
51 | return (0); | ||
52 | return (*(unsigned char *)s1 - *(unsigned char *)--s2); | ||
53 | } | ||
diff --git a/src/lib/libc/string/strcoll.3 b/src/lib/libc/string/strcoll.3 new file mode 100644 index 0000000000..fe0ff35510 --- /dev/null +++ b/src/lib/libc/string/strcoll.3 | |||
@@ -0,0 +1,68 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strcoll.3,v 1.5 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRCOLL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strcoll | ||
39 | .Nd compare strings according to current collation | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft int | ||
43 | .Fn strcoll "const char *s1" "const char *s2" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strcoll | ||
47 | function lexicographically compares the null-terminated strings | ||
48 | .Fa s1 | ||
49 | and | ||
50 | .Fa s2 | ||
51 | according to the current locale collation | ||
52 | and returns an integer greater than, equal to, or less than 0, | ||
53 | according to whether | ||
54 | .Fa s1 | ||
55 | is greater than, equal to, or less than | ||
56 | .Fa s2 . | ||
57 | .Sh SEE ALSO | ||
58 | .Xr bcmp 3 , | ||
59 | .Xr memcmp 3 , | ||
60 | .Xr setlocale 3 , | ||
61 | .Xr strcasecmp 3 , | ||
62 | .Xr strcmp 3 , | ||
63 | .Xr strxfrm 3 | ||
64 | .Sh STANDARDS | ||
65 | The | ||
66 | .Fn strcoll | ||
67 | function conforms to | ||
68 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcoll.c b/src/lib/libc/string/strcoll.c new file mode 100644 index 0000000000..9622b67281 --- /dev/null +++ b/src/lib/libc/string/strcoll.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strcoll.c,v 1.4 2003/06/11 21:08:29 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Compare strings according to LC_COLLATE category of current locale. | ||
41 | */ | ||
42 | int | ||
43 | strcoll(const char *s1, const char *s2) | ||
44 | { | ||
45 | /* LC_COLLATE is unimplemented, hence always "C" */ | ||
46 | return (strcmp(s1, s2)); | ||
47 | } | ||
diff --git a/src/lib/libc/string/strcpy.3 b/src/lib/libc/string/strcpy.3 new file mode 100644 index 0000000000..8304508df6 --- /dev/null +++ b/src/lib/libc/string/strcpy.3 | |||
@@ -0,0 +1,150 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strcpy.3,v 1.12 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRCPY 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strcpy , | ||
39 | .Nm strncpy | ||
40 | .Nd copy strings | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft char * | ||
44 | .Fn strcpy "char *dst" "const char *src" | ||
45 | .Ft char * | ||
46 | .Fn strncpy "char *dst" "const char *src" "size_t len" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn strcpy | ||
50 | and | ||
51 | .Fn strncpy | ||
52 | functions copy the string | ||
53 | .Fa src | ||
54 | to | ||
55 | .Fa dst | ||
56 | (including the terminating | ||
57 | .Ql \e0 | ||
58 | character). | ||
59 | .Pp | ||
60 | .Fn strncpy | ||
61 | copies not more than | ||
62 | .Fa len | ||
63 | characters into | ||
64 | .Fa dst , | ||
65 | appending | ||
66 | .Ql \e0 | ||
67 | characters if | ||
68 | .Fa src | ||
69 | is less than | ||
70 | .Fa len | ||
71 | characters long, and | ||
72 | .Em not | ||
73 | terminating | ||
74 | .Fa dst | ||
75 | if the length of | ||
76 | .Fa src | ||
77 | is greater than or equal to | ||
78 | .Fa len . | ||
79 | .Sh RETURN VALUES | ||
80 | The | ||
81 | .Fn strcpy | ||
82 | and | ||
83 | .Fn strncpy | ||
84 | functions return | ||
85 | .Fa dst . | ||
86 | .Sh EXAMPLES | ||
87 | The following sets | ||
88 | .Va chararray | ||
89 | to | ||
90 | .Dq abc\e0\e0\e0 : | ||
91 | .Bd -literal -offset indent | ||
92 | (void)strncpy(chararray, "abc", 6); | ||
93 | .Ed | ||
94 | .Pp | ||
95 | The following sets | ||
96 | .Va chararray | ||
97 | to | ||
98 | .Dq abcdef | ||
99 | and does | ||
100 | .Em not | ||
101 | null terminate | ||
102 | .Va chararray | ||
103 | because the source string is >= the length parameter. | ||
104 | .Fn strncpy | ||
105 | .Em only | ||
106 | null terminates the destination string when the length of the source | ||
107 | string is less than the length parameter. | ||
108 | .Bd -literal -offset indent | ||
109 | (void)strncpy(chararray, "abcdefgh", 6); | ||
110 | .Ed | ||
111 | .Pp | ||
112 | The following copies as many characters from | ||
113 | .Va input | ||
114 | to | ||
115 | .Va buf | ||
116 | as will fit and null terminates the result. | ||
117 | Because | ||
118 | .Fn strncpy | ||
119 | does | ||
120 | .Em not | ||
121 | guarantee to null terminate the string itself, we must do this by hand. | ||
122 | .Bd -literal -offset indent | ||
123 | char buf[BUFSIZ]; | ||
124 | |||
125 | (void)strncpy(buf, input, sizeof(buf) - 1); | ||
126 | buf[sizeof(buf) - 1] = '\e0'; | ||
127 | .Ed | ||
128 | .Pp | ||
129 | Note that | ||
130 | .Xr strlcpy 3 | ||
131 | is a better choice for this kind of operation. | ||
132 | The equivalent using | ||
133 | .Xr strlcpy 3 | ||
134 | is simply: | ||
135 | .Bd -literal -offset indent | ||
136 | (void)strlcpy(buf, input, sizeof(buf)); | ||
137 | .Ed | ||
138 | .Sh SEE ALSO | ||
139 | .Xr bcopy 3 , | ||
140 | .Xr memccpy 3 , | ||
141 | .Xr memcpy 3 , | ||
142 | .Xr memmove 3 , | ||
143 | .Xr strlcpy 3 | ||
144 | .Sh STANDARDS | ||
145 | The | ||
146 | .Fn strcpy | ||
147 | and | ||
148 | .Fn strncpy | ||
149 | functions conform to | ||
150 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcpy.c b/src/lib/libc/string/strcpy.c new file mode 100644 index 0000000000..e5c3d7dcbe --- /dev/null +++ b/src/lib/libc/string/strcpy.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strcpy.c,v 1.6 2003/07/24 01:15:42 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #ifndef _KERNEL | ||
35 | #include <string.h> | ||
36 | #else | ||
37 | #include <lib/libkern/libkern.h> | ||
38 | #endif | ||
39 | |||
40 | #if defined(APIWARN) | ||
41 | __warn_references(strcpy, | ||
42 | "warning: strcpy() is almost always misused, please use strlcpy()"); | ||
43 | #endif | ||
44 | |||
45 | char * | ||
46 | strcpy(char *to, const char *from) | ||
47 | { | ||
48 | char *save = to; | ||
49 | |||
50 | for (; (*to = *from) != '\0'; ++from, ++to); | ||
51 | return(save); | ||
52 | } | ||
diff --git a/src/lib/libc/string/strcspn.3 b/src/lib/libc/string/strcspn.3 new file mode 100644 index 0000000000..6dfe1b276e --- /dev/null +++ b/src/lib/libc/string/strcspn.3 | |||
@@ -0,0 +1,88 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strcspn.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRCSPN 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strcspn | ||
39 | .Nd span the complement of a string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft size_t | ||
43 | .Fn strcspn "const char *s" "const char *charset" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strcspn | ||
47 | function spans the initial part of the null-terminated string | ||
48 | .Fa s | ||
49 | as long as the characters from | ||
50 | .Fa s | ||
51 | do not occur in string | ||
52 | .Fa charset | ||
53 | (it spans the | ||
54 | .Em complement | ||
55 | of | ||
56 | .Fa charset ) . | ||
57 | .Sh RETURN VALUES | ||
58 | The | ||
59 | .Fn strcspn | ||
60 | function returns the number of characters spanned. | ||
61 | .Sh EXAMPLES | ||
62 | The following call to | ||
63 | .Fn strcspn | ||
64 | will return 3, since the first three characters of string | ||
65 | .Fa s | ||
66 | do not occur in string | ||
67 | .Fa charset : | ||
68 | .Bd -literal -offset indent | ||
69 | char *s = "foobar"; | ||
70 | char *charset = "bar"; | ||
71 | size_t span; | ||
72 | |||
73 | span = strcspn(s, charset); | ||
74 | .Ed | ||
75 | .Sh SEE ALSO | ||
76 | .Xr memchr 3 , | ||
77 | .Xr strchr 3 , | ||
78 | .Xr strpbrk 3 , | ||
79 | .Xr strrchr 3 , | ||
80 | .Xr strsep 3 , | ||
81 | .Xr strspn 3 , | ||
82 | .Xr strstr 3 , | ||
83 | .Xr strtok 3 | ||
84 | .Sh STANDARDS | ||
85 | The | ||
86 | .Fn strcspn | ||
87 | function conforms to | ||
88 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strcspn.c b/src/lib/libc/string/strcspn.c new file mode 100644 index 0000000000..f96f903809 --- /dev/null +++ b/src/lib/libc/string/strcspn.c | |||
@@ -0,0 +1,61 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strcspn.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Span the complement of string s2. | ||
41 | */ | ||
42 | size_t | ||
43 | strcspn(const char *s1, const char *s2) | ||
44 | { | ||
45 | const char *p, *spanp; | ||
46 | char c, sc; | ||
47 | |||
48 | /* | ||
49 | * Stop as soon as we find any character from s2. Note that there | ||
50 | * must be a NUL in s2; it suffices to stop when we find that, too. | ||
51 | */ | ||
52 | for (p = s1;;) { | ||
53 | c = *p++; | ||
54 | spanp = s2; | ||
55 | do { | ||
56 | if ((sc = *spanp++) == c) | ||
57 | return (p - 1 - s1); | ||
58 | } while (sc != 0); | ||
59 | } | ||
60 | /* NOTREACHED */ | ||
61 | } | ||
diff --git a/src/lib/libc/string/strdup.3 b/src/lib/libc/string/strdup.3 new file mode 100644 index 0000000000..59b77f7a16 --- /dev/null +++ b/src/lib/libc/string/strdup.3 | |||
@@ -0,0 +1,84 @@ | |||
1 | .\" $OpenBSD: strdup.3,v 1.12 2003/06/02 20:18:38 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" @(#)strdup.3 8.1 (Berkeley) 6/9/93 | ||
31 | .\" | ||
32 | .Dd June 9, 1993 | ||
33 | .Dt STRDUP 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm strdup | ||
37 | .Nd save a copy of a string | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft char * | ||
41 | .Fn strdup "const char *s" | ||
42 | .Sh DESCRIPTION | ||
43 | The | ||
44 | .Fn strdup | ||
45 | function allocates sufficient memory for a copy of the string | ||
46 | .Fa s , | ||
47 | does the copy, and returns a pointer to it. | ||
48 | The pointer may subsequently be used as an argument to the function | ||
49 | .Xr free 3 . | ||
50 | .Pp | ||
51 | If insufficient memory is available, | ||
52 | .Dv NULL | ||
53 | is returned. | ||
54 | .Sh EXAMPLES | ||
55 | The following will point | ||
56 | .Va p | ||
57 | to an allocated area of memory containing the null-terminated string | ||
58 | .Qq foobar : | ||
59 | .Bd -literal -offset indent | ||
60 | char *p; | ||
61 | |||
62 | if ((p = strdup("foobar")) == NULL) { | ||
63 | fprintf(stderr, "Out of memory.\en"); | ||
64 | exit(1); | ||
65 | } | ||
66 | .Ed | ||
67 | .Sh ERRORS | ||
68 | The | ||
69 | .Fn strdup | ||
70 | function may fail and set the external variable | ||
71 | .Va errno | ||
72 | for any of the errors specified for the library function | ||
73 | .Xr malloc 3 . | ||
74 | .Sh SEE ALSO | ||
75 | .Xr free 3 , | ||
76 | .Xr malloc 3 , | ||
77 | .Xr strcpy 3 , | ||
78 | .Xr strlcpy 3 , | ||
79 | .Xr strlen 3 | ||
80 | .Sh HISTORY | ||
81 | The | ||
82 | .Fn strdup | ||
83 | function first appeared in | ||
84 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/string/strdup.c b/src/lib/libc/string/strdup.c new file mode 100644 index 0000000000..bbea59888c --- /dev/null +++ b/src/lib/libc/string/strdup.c | |||
@@ -0,0 +1,57 @@ | |||
1 | /* $OpenBSD: strdup.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1988, 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. Neither the name of the University nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #if defined(LIBC_SCCS) && !defined(lint) | ||
33 | #if 0 | ||
34 | static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; | ||
35 | #else | ||
36 | static char *rcsid = "$OpenBSD: strdup.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
37 | #endif | ||
38 | #endif /* LIBC_SCCS and not lint */ | ||
39 | |||
40 | #include <sys/types.h> | ||
41 | |||
42 | #include <stddef.h> | ||
43 | #include <stdlib.h> | ||
44 | #include <string.h> | ||
45 | |||
46 | char * | ||
47 | strdup(const char *str) | ||
48 | { | ||
49 | size_t siz; | ||
50 | char *copy; | ||
51 | |||
52 | siz = strlen(str) + 1; | ||
53 | if ((copy = malloc(siz)) == NULL) | ||
54 | return(NULL); | ||
55 | (void)memcpy(copy, str, siz); | ||
56 | return(copy); | ||
57 | } | ||
diff --git a/src/lib/libc/string/strerror.3 b/src/lib/libc/string/strerror.3 new file mode 100644 index 0000000000..fa573bc72a --- /dev/null +++ b/src/lib/libc/string/strerror.3 | |||
@@ -0,0 +1,85 @@ | |||
1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strerror.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRERROR 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strerror | ||
39 | .Nd get error message string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft char * | ||
43 | .Fn strerror "int errnum" | ||
44 | .Ft int | ||
45 | .Fn strerror_r "int errnum" "char *strerrbuf" "size_t buflen" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn strerror | ||
49 | function returns a pointer to the language-dependent error message | ||
50 | string affiliated with the specified error number | ||
51 | .Fa errnum . | ||
52 | The returned string contains a maximum of | ||
53 | .Dv NL_TEXTMAX | ||
54 | characters, including the trailing NUL. | ||
55 | .Pp | ||
56 | The array pointed to is not to be modified by the program, but may be | ||
57 | overwritten by subsequent calls to | ||
58 | .Fn strerror . | ||
59 | .Pp | ||
60 | .Fn strerror_r | ||
61 | is a thread safe version of | ||
62 | .Fn strerror | ||
63 | that places the error message in the given buffer | ||
64 | .Fa strerrbuf . | ||
65 | If the error message is larger then | ||
66 | .Fa buflen | ||
67 | the message will be truncated to fit within buflen and | ||
68 | .Er ERANGE | ||
69 | is returned. | ||
70 | .Fn strerror_r | ||
71 | returns zero upon successful completion. | ||
72 | An error number is returned, otherwise. | ||
73 | .Sh SEE ALSO | ||
74 | .Xr intro 2 , | ||
75 | .Xr perror 3 , | ||
76 | .Xr setlocale 3 | ||
77 | .Sh STANDARDS | ||
78 | The | ||
79 | .Fn strerror | ||
80 | function conforms to | ||
81 | .St -ansiC . | ||
82 | The | ||
83 | .Fn strerror_r | ||
84 | function conforms to | ||
85 | .St -p1003.1 . | ||
diff --git a/src/lib/libc/string/strerror.c b/src/lib/libc/string/strerror.c new file mode 100644 index 0000000000..6496f50afd --- /dev/null +++ b/src/lib/libc/string/strerror.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strerror.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <string.h> | ||
35 | #include <limits.h> | ||
36 | |||
37 | /* | ||
38 | * Since perror() is not allowed to change the contents of strerror()'s | ||
39 | * static buffer, both functions supply their own buffers to the | ||
40 | * internal function __strerror(). | ||
41 | */ | ||
42 | |||
43 | extern char *__strerror(int, char *); | ||
44 | |||
45 | char * | ||
46 | strerror(int num) | ||
47 | { | ||
48 | static char buf[NL_TEXTMAX]; | ||
49 | return __strerror(num, buf); | ||
50 | } | ||
diff --git a/src/lib/libc/string/strerror_r.c b/src/lib/libc/string/strerror_r.c new file mode 100644 index 0000000000..aab6db5303 --- /dev/null +++ b/src/lib/libc/string/strerror_r.c | |||
@@ -0,0 +1,30 @@ | |||
1 | /* $OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $ */ | ||
2 | /* Public Domain <marc@snafu.org> */ | ||
3 | |||
4 | #if defined(LIBC_SCCS) && !defined(lint) | ||
5 | static char *rcsid = "$OpenBSD: strerror_r.c,v 1.1 2002/11/21 20:45:05 marc Exp $"; | ||
6 | #endif /* LIBC_SCCS and not lint */ | ||
7 | |||
8 | #include <errno.h> | ||
9 | #include <limits.h> | ||
10 | #include <string.h> | ||
11 | |||
12 | extern char *__strerror(int, char *); | ||
13 | |||
14 | int | ||
15 | strerror_r(int errnum, char *strerrbuf, size_t buflen) | ||
16 | { | ||
17 | int save_errno; | ||
18 | int ret_errno; | ||
19 | char buf[NL_TEXTMAX]; | ||
20 | |||
21 | save_errno = errno; | ||
22 | errno = 0; | ||
23 | __strerror(errnum, buf); | ||
24 | if (strlcpy(strerrbuf, buf, buflen) >= buflen) | ||
25 | errno = ERANGE; | ||
26 | ret_errno = errno; | ||
27 | errno = save_errno; | ||
28 | |||
29 | return (ret_errno); | ||
30 | } | ||
diff --git a/src/lib/libc/string/string.3 b/src/lib/libc/string/string.3 new file mode 100644 index 0000000000..e29aa6c02c --- /dev/null +++ b/src/lib/libc/string/string.3 | |||
@@ -0,0 +1,157 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" $OpenBSD: string.3,v 1.9 2003/06/02 20:18:38 millert Exp $ | ||
31 | .\" | ||
32 | .Dd April 19, 1991 | ||
33 | .Dt STRING 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm strcat , | ||
37 | .Nm strlcat , | ||
38 | .Nm strncat , | ||
39 | .Nm strchr , | ||
40 | .Nm strrchr , | ||
41 | .Nm strcmp , | ||
42 | .Nm strncmp , | ||
43 | .Nm strcasecmp , | ||
44 | .Nm strncasecmp , | ||
45 | .Nm strcpy , | ||
46 | .Nm strlcpy , | ||
47 | .Nm strncpy , | ||
48 | .Nm strerror , | ||
49 | .Nm strlen , | ||
50 | .Nm strpbrk , | ||
51 | .Nm strsep , | ||
52 | .Nm strspn , | ||
53 | .Nm strcspn , | ||
54 | .Nm strstr , | ||
55 | .Nm strtok , | ||
56 | .Nm index , | ||
57 | .Nm rindex | ||
58 | .Nd string specific functions | ||
59 | .Sh SYNOPSIS | ||
60 | .Fd #include <string.h> | ||
61 | .Ft char * | ||
62 | .Fn strcat "char *s" "const char *append" | ||
63 | .Ft char * | ||
64 | .Fn strlcat "char *s" "const char *append" "size_t size" | ||
65 | .Ft char * | ||
66 | .Fn strncat "char *s" "const char *append" "size_t count" | ||
67 | .Ft char * | ||
68 | .Fn strchr "const char *s" "int c" | ||
69 | .Ft char * | ||
70 | .Fn strrchr "const char *s" "int c" | ||
71 | .Ft int | ||
72 | .Fn strcmp "const char *s1" "const char *s2" | ||
73 | .Ft int | ||
74 | .Fn strncmp "const char *s1" "const char *s2" "size_t count" | ||
75 | .Ft int | ||
76 | .Fn strcasecmp "const char *s1" "const char *s2" | ||
77 | .Ft int | ||
78 | .Fn strncasecmp "const char *s1" "const char *s2" "size_t count" | ||
79 | .Ft char * | ||
80 | .Fn strcpy "char *dst" "const char *src" | ||
81 | .Ft char * | ||
82 | .Fn strlcpy "char *dst" "const char *src" "size_t size" | ||
83 | .Ft char * | ||
84 | .Fn strncpy "char *dst" "const char *src" "size_t count" | ||
85 | .Ft char * | ||
86 | .Fn strerror "int errno" | ||
87 | .Ft size_t | ||
88 | .Fn strlen "const char *s" | ||
89 | .Ft char * | ||
90 | .Fn strpbrk "const char *s" "const char *charset" | ||
91 | .Ft char * | ||
92 | .Fn strsep "char **stringp" "const char *delim" | ||
93 | .Ft size_t | ||
94 | .Fn strspn "const char *s" "const char *charset" | ||
95 | .Ft size_t | ||
96 | .Fn strcspn "const char *s" "const char *charset" | ||
97 | .Ft char * | ||
98 | .Fn strstr "const char *big" "const char *little" | ||
99 | .Ft char * | ||
100 | .Fn strtok "char *s" "const char *delim" | ||
101 | .Ft char * | ||
102 | .Fn index "const char *s" "int c" | ||
103 | .Ft char * | ||
104 | .Fn rindex "const char *s" "int c" | ||
105 | .Sh DESCRIPTION | ||
106 | The string functions | ||
107 | manipulate strings terminated by a | ||
108 | null byte. | ||
109 | .Pp | ||
110 | See the specific manual pages for more information. | ||
111 | For manipulating variable length generic objects as byte | ||
112 | strings (without the null byte check), see | ||
113 | .Xr bstring 3 . | ||
114 | .Pp | ||
115 | Except as noted in their specific manual pages, | ||
116 | the string functions do not test the destination | ||
117 | for size limitations. | ||
118 | .Sh SEE ALSO | ||
119 | .Xr bstring 3 , | ||
120 | .Xr index 3 , | ||
121 | .Xr rindex 3 , | ||
122 | .Xr strcasecmp 3 , | ||
123 | .Xr strcat 3 , | ||
124 | .Xr strchr 3 , | ||
125 | .Xr strcmp 3 , | ||
126 | .Xr strcpy 3 , | ||
127 | .Xr strcspn 3 , | ||
128 | .Xr strerror 3 , | ||
129 | .Xr strlcat 3 , | ||
130 | .Xr strlcpy 3 , | ||
131 | .Xr strlen 3 , | ||
132 | .Xr strpbrk 3 , | ||
133 | .Xr strrchr 3 , | ||
134 | .Xr strsep 3 , | ||
135 | .Xr strspn 3 , | ||
136 | .Xr strstr 3 , | ||
137 | .Xr strtok 3 | ||
138 | .Sh STANDARDS | ||
139 | The | ||
140 | .Fn strcat , | ||
141 | .Fn strncat , | ||
142 | .Fn strchr , | ||
143 | .Fn strrchr , | ||
144 | .Fn strcmp , | ||
145 | .Fn strncmp , | ||
146 | .Fn strcpy , | ||
147 | .Fn strncpy , | ||
148 | .Fn strerror , | ||
149 | .Fn strlen , | ||
150 | .Fn strpbrk , | ||
151 | .Fn strspn , | ||
152 | .Fn strcspn , | ||
153 | .Fn strstr , | ||
154 | and | ||
155 | .Fn strtok | ||
156 | functions conform to | ||
157 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strlcat.c b/src/lib/libc/string/strlcat.c new file mode 100644 index 0000000000..2b6f1adbb4 --- /dev/null +++ b/src/lib/libc/string/strlcat.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #if defined(LIBC_SCCS) && !defined(lint) | ||
20 | static char *rcsid = "$OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $"; | ||
21 | #endif /* LIBC_SCCS and not lint */ | ||
22 | |||
23 | #include <sys/types.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | /* | ||
27 | * Appends src to string dst of size siz (unlike strncat, siz is the | ||
28 | * full size of dst, not space left). At most siz-1 characters | ||
29 | * will be copied. Always NUL terminates (unless siz <= strlen(dst)). | ||
30 | * Returns strlen(src) + MIN(siz, strlen(initial dst)). | ||
31 | * If retval >= siz, truncation occurred. | ||
32 | */ | ||
33 | size_t | ||
34 | strlcat(char *dst, const char *src, size_t siz) | ||
35 | { | ||
36 | register char *d = dst; | ||
37 | register const char *s = src; | ||
38 | register size_t n = siz; | ||
39 | size_t dlen; | ||
40 | |||
41 | /* Find the end of dst and adjust bytes left but don't go past end */ | ||
42 | while (n-- != 0 && *d != '\0') | ||
43 | d++; | ||
44 | dlen = d - dst; | ||
45 | n = siz - dlen; | ||
46 | |||
47 | if (n == 0) | ||
48 | return(dlen + strlen(s)); | ||
49 | while (*s != '\0') { | ||
50 | if (n != 1) { | ||
51 | *d++ = *s; | ||
52 | n--; | ||
53 | } | ||
54 | s++; | ||
55 | } | ||
56 | *d = '\0'; | ||
57 | |||
58 | return(dlen + (s - src)); /* count does not include NUL */ | ||
59 | } | ||
diff --git a/src/lib/libc/string/strlcpy.3 b/src/lib/libc/string/strlcpy.3 new file mode 100644 index 0000000000..b392588879 --- /dev/null +++ b/src/lib/libc/string/strlcpy.3 | |||
@@ -0,0 +1,179 @@ | |||
1 | .\" $OpenBSD: strlcpy.3,v 1.16 2003/06/17 21:56:24 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1998, 2000 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | .\" | ||
17 | .Dd June 22, 1998 | ||
18 | .Dt STRLCPY 3 | ||
19 | .Os | ||
20 | .Sh NAME | ||
21 | .Nm strlcpy , | ||
22 | .Nm strlcat | ||
23 | .Nd size-bounded string copying and concatenation | ||
24 | .Sh SYNOPSIS | ||
25 | .Fd #include <string.h> | ||
26 | .Ft size_t | ||
27 | .Fn strlcpy "char *dst" "const char *src" "size_t size" | ||
28 | .Ft size_t | ||
29 | .Fn strlcat "char *dst" "const char *src" "size_t size" | ||
30 | .Sh DESCRIPTION | ||
31 | The | ||
32 | .Fn strlcpy | ||
33 | and | ||
34 | .Fn strlcat | ||
35 | functions copy and concatenate strings respectively. | ||
36 | They are designed | ||
37 | to be safer, more consistent, and less error prone replacements for | ||
38 | .Xr strncpy 3 | ||
39 | and | ||
40 | .Xr strncat 3 . | ||
41 | Unlike those functions, | ||
42 | .Fn strlcpy | ||
43 | and | ||
44 | .Fn strlcat | ||
45 | take the full size of the buffer (not just the length) and guarantee to | ||
46 | NUL-terminate the result (as long as | ||
47 | .Fa size | ||
48 | is larger than 0 or, in the case of | ||
49 | .Fn strlcat , | ||
50 | as long as there is at least one byte free in | ||
51 | .Fa dst ) . | ||
52 | Note that you should include a byte for the NUL in | ||
53 | .Fa size . | ||
54 | Also note that | ||
55 | .Fn strlcpy | ||
56 | and | ||
57 | .Fn strlcat | ||
58 | only operate on true | ||
59 | .Dq C | ||
60 | strings. | ||
61 | This means that for | ||
62 | .Fn strlcpy | ||
63 | .Fa src | ||
64 | must be NUL-terminated and for | ||
65 | .Fn strlcat | ||
66 | both | ||
67 | .Fa src | ||
68 | and | ||
69 | .Fa dst | ||
70 | must be NUL-terminated. | ||
71 | .Pp | ||
72 | The | ||
73 | .Fn strlcpy | ||
74 | function copies up to | ||
75 | .Fa size | ||
76 | - 1 characters from the NUL-terminated string | ||
77 | .Fa src | ||
78 | to | ||
79 | .Fa dst , | ||
80 | NUL-terminating the result. | ||
81 | .Pp | ||
82 | The | ||
83 | .Fn strlcat | ||
84 | function appends the NUL-terminated string | ||
85 | .Fa src | ||
86 | to the end of | ||
87 | .Fa dst . | ||
88 | It will append at most | ||
89 | .Fa size | ||
90 | - strlen(dst) - 1 bytes, NUL-terminating the result. | ||
91 | .Sh RETURN VALUES | ||
92 | The | ||
93 | .Fn strlcpy | ||
94 | and | ||
95 | .Fn strlcat | ||
96 | functions return the total length of the string they tried to create. | ||
97 | For | ||
98 | .Fn strlcpy | ||
99 | that means the length of | ||
100 | .Fa src . | ||
101 | For | ||
102 | .Fn strlcat | ||
103 | that means the initial length of | ||
104 | .Fa dst | ||
105 | plus | ||
106 | the length of | ||
107 | .Fa src . | ||
108 | While this may seem somewhat confusing it was done to make | ||
109 | truncation detection simple. | ||
110 | .Pp | ||
111 | Note however, that if | ||
112 | .Fn strlcat | ||
113 | traverses | ||
114 | .Fa size | ||
115 | characters without finding a NUL, the length of the string is considered | ||
116 | to be | ||
117 | .Fa size | ||
118 | and the destination string will not be NUL-terminated (since there was | ||
119 | no space for the NUL). | ||
120 | This keeps | ||
121 | .Fn strlcat | ||
122 | from running off the end of a string. | ||
123 | In practice this should not happen (as it means that either | ||
124 | .Fa size | ||
125 | is incorrect or that | ||
126 | .Fa dst | ||
127 | is not a proper | ||
128 | .Dq C | ||
129 | string). | ||
130 | The check exists to prevent potential security problems in incorrect code. | ||
131 | .Sh EXAMPLES | ||
132 | The following code fragment illustrates the simple case: | ||
133 | .Bd -literal -offset indent | ||
134 | char *s, *p, buf[BUFSIZ]; | ||
135 | |||
136 | \&... | ||
137 | |||
138 | (void)strlcpy(buf, s, sizeof(buf)); | ||
139 | (void)strlcat(buf, p, sizeof(buf)); | ||
140 | .Ed | ||
141 | .Pp | ||
142 | To detect truncation, perhaps while building a pathname, something | ||
143 | like the following might be used: | ||
144 | .Bd -literal -offset indent | ||
145 | char *dir, *file, pname[MAXPATHLEN]; | ||
146 | |||
147 | \&... | ||
148 | |||
149 | if (strlcpy(pname, dir, sizeof(pname)) >= sizeof(pname)) | ||
150 | goto toolong; | ||
151 | if (strlcat(pname, file, sizeof(pname)) >= sizeof(pname)) | ||
152 | goto toolong; | ||
153 | .Ed | ||
154 | .Pp | ||
155 | Since we know how many characters we copied the first time, we can | ||
156 | speed things up a bit by using a copy instead of an append: | ||
157 | .Bd -literal -offset indent | ||
158 | char *dir, *file, pname[MAXPATHLEN]; | ||
159 | size_t n; | ||
160 | |||
161 | \&... | ||
162 | |||
163 | n = strlcpy(pname, dir, sizeof(pname)); | ||
164 | if (n >= sizeof(pname)) | ||
165 | goto toolong; | ||
166 | if (strlcpy(pname + n, file, sizeof(pname) - n) >= sizeof(pname) - n) | ||
167 | goto toolong; | ||
168 | .Ed | ||
169 | .Pp | ||
170 | However, one may question the validity of such optimizations, as they | ||
171 | defeat the whole purpose of | ||
172 | .Fn strlcpy | ||
173 | and | ||
174 | .Fn strlcat . | ||
175 | As a matter of fact, the first version of this manual page got it wrong. | ||
176 | .Sh SEE ALSO | ||
177 | .Xr snprintf 3 , | ||
178 | .Xr strncat 3 , | ||
179 | .Xr strncpy 3 | ||
diff --git a/src/lib/libc/string/strlcpy.c b/src/lib/libc/string/strlcpy.c new file mode 100644 index 0000000000..82cbfac016 --- /dev/null +++ b/src/lib/libc/string/strlcpy.c | |||
@@ -0,0 +1,55 @@ | |||
1 | /* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | ||
5 | * | ||
6 | * Permission to use, copy, modify, and distribute this software for any | ||
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | ||
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
17 | */ | ||
18 | |||
19 | #if defined(LIBC_SCCS) && !defined(lint) | ||
20 | static char *rcsid = "$OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $"; | ||
21 | #endif /* LIBC_SCCS and not lint */ | ||
22 | |||
23 | #include <sys/types.h> | ||
24 | #include <string.h> | ||
25 | |||
26 | /* | ||
27 | * Copy src to string dst of size siz. At most siz-1 characters | ||
28 | * will be copied. Always NUL terminates (unless siz == 0). | ||
29 | * Returns strlen(src); if retval >= siz, truncation occurred. | ||
30 | */ | ||
31 | size_t | ||
32 | strlcpy(char *dst, const char *src, size_t siz) | ||
33 | { | ||
34 | register char *d = dst; | ||
35 | register const char *s = src; | ||
36 | register size_t n = siz; | ||
37 | |||
38 | /* Copy as many bytes as will fit */ | ||
39 | if (n != 0 && --n != 0) { | ||
40 | do { | ||
41 | if ((*d++ = *s++) == 0) | ||
42 | break; | ||
43 | } while (--n != 0); | ||
44 | } | ||
45 | |||
46 | /* Not enough room in dst, add NUL and traverse rest of src */ | ||
47 | if (n == 0) { | ||
48 | if (siz != 0) | ||
49 | *d = '\0'; /* NUL-terminate dst */ | ||
50 | while (*s++) | ||
51 | ; | ||
52 | } | ||
53 | |||
54 | return(s - src - 1); /* count does not include NUL */ | ||
55 | } | ||
diff --git a/src/lib/libc/string/strlen.3 b/src/lib/libc/string/strlen.3 new file mode 100644 index 0000000000..32bbc88542 --- /dev/null +++ b/src/lib/libc/string/strlen.3 | |||
@@ -0,0 +1,61 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strlen.3,v 1.5 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRLEN 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strlen | ||
39 | .Nd find length of a string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft size_t | ||
43 | .Fn strlen "const char *s" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strlen | ||
47 | function computes the length of the string | ||
48 | .Fa s . | ||
49 | .Sh RETURN VALUES | ||
50 | The | ||
51 | .Fn strlen | ||
52 | function returns the number of characters that precede the terminating | ||
53 | .Tn NUL | ||
54 | character. | ||
55 | .Sh SEE ALSO | ||
56 | .Xr string 3 | ||
57 | .Sh STANDARDS | ||
58 | The | ||
59 | .Fn strlen | ||
60 | function conforms to | ||
61 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strlen.c b/src/lib/libc/string/strlen.c new file mode 100644 index 0000000000..298a9966a3 --- /dev/null +++ b/src/lib/libc/string/strlen.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: strlen.c,v 1.6 2003/06/11 21:08:16 deraadt 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. Neither the name of the University nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #if defined(LIBC_SCCS) && !defined(lint) | ||
33 | static char *rcsid = "$OpenBSD: strlen.c,v 1.6 2003/06/11 21:08:16 deraadt Exp $"; | ||
34 | #endif /* LIBC_SCCS and not lint */ | ||
35 | |||
36 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
37 | #include <string.h> | ||
38 | #else | ||
39 | #include <lib/libkern/libkern.h> | ||
40 | #endif | ||
41 | |||
42 | size_t | ||
43 | strlen(const char *str) | ||
44 | { | ||
45 | const char *s; | ||
46 | |||
47 | for (s = str; *s; ++s) | ||
48 | ; | ||
49 | return (s - str); | ||
50 | } | ||
51 | |||
diff --git a/src/lib/libc/string/strmode.3 b/src/lib/libc/string/strmode.3 new file mode 100644 index 0000000000..bc45c4ce1f --- /dev/null +++ b/src/lib/libc/string/strmode.3 | |||
@@ -0,0 +1,159 @@ | |||
1 | .\" $OpenBSD: strmode.3,v 1.11 2003/09/02 18:24:21 jmc 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. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" @(#)strmode.3 8.3 (Berkeley) 7/28/94 | ||
31 | .\" | ||
32 | .Dd July 28, 1994 | ||
33 | .Dt STRMODE 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm strmode | ||
37 | .Nd convert inode status information into a symbolic string | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <string.h> | ||
40 | .Ft void | ||
41 | .Fn strmode "mode_t mode" "char *bp" | ||
42 | .Sh DESCRIPTION | ||
43 | The | ||
44 | .Fn strmode | ||
45 | function converts a file | ||
46 | .Fa mode | ||
47 | (the type and permission information associated with an inode, see | ||
48 | .Xr stat 2 ) | ||
49 | into a symbolic string which is stored in the location referenced by | ||
50 | .Fa bp . | ||
51 | This stored string is eleven characters in length plus a trailing null byte. | ||
52 | .Pp | ||
53 | The first character is the inode type, and will be one of the following: | ||
54 | .Pp | ||
55 | .Bl -tag -width flag -offset indent -compact | ||
56 | .It \- | ||
57 | regular file | ||
58 | .It b | ||
59 | block special | ||
60 | .It c | ||
61 | character special | ||
62 | .It d | ||
63 | directory | ||
64 | .It l | ||
65 | symbolic link | ||
66 | .It p | ||
67 | FIFO | ||
68 | .It s | ||
69 | socket | ||
70 | .It w | ||
71 | whiteout | ||
72 | .It \&? | ||
73 | unknown inode type | ||
74 | .El | ||
75 | .Pp | ||
76 | The next nine characters encode three sets of permissions, in three | ||
77 | characters each. | ||
78 | The first three characters are the permissions for the owner of the | ||
79 | file, the second three for the group the file belongs to, and the | ||
80 | third for the | ||
81 | .Dq other , | ||
82 | or default, set of users. | ||
83 | .Pp | ||
84 | Permission checking is done as specifically as possible. | ||
85 | If read permission is denied to the owner of a file in the first set | ||
86 | of permissions, the owner of the file will not be able to read the file. | ||
87 | This is true even if the owner is in the file's group and the group | ||
88 | permissions allow reading or the | ||
89 | .Dq other | ||
90 | permissions allow reading. | ||
91 | .Pp | ||
92 | If the first character of the three character set is an | ||
93 | .Sq r , | ||
94 | the file is readable for that set of users; if a dash | ||
95 | .Pq Ql - , | ||
96 | it is not readable. | ||
97 | .Pp | ||
98 | If the second character of the three character set is a | ||
99 | .Sq w , | ||
100 | the file is writable for that set of users; if a dash | ||
101 | .Pq Ql - , | ||
102 | it is not writable. | ||
103 | .Pp | ||
104 | The third character is the first of the following characters that apply: | ||
105 | .Bl -tag -width xxxx | ||
106 | .It S | ||
107 | If the character is part of the owner permissions and the file is not | ||
108 | executable or the directory is not searchable by the owner, and the | ||
109 | set-user-ID bit is set. | ||
110 | .It S | ||
111 | If the character is part of the group permissions and the file is not | ||
112 | executable or the directory is not searchable by the group, and the | ||
113 | set-group-ID bit is set. | ||
114 | .It T | ||
115 | If the character is part of the other permissions and the file is not | ||
116 | executable or the directory is not searchable by others, and the | ||
117 | .Dq sticky | ||
118 | .Pq Dv S_ISVTX | ||
119 | bit is set. | ||
120 | .It s | ||
121 | If the character is part of the owner permissions and the file is | ||
122 | executable or the directory searchable by the owner, and the set-user-ID | ||
123 | bit is set. | ||
124 | .It s | ||
125 | If the character is part of the group permissions and the file is | ||
126 | executable or the directory searchable by the group, and the set-group-ID | ||
127 | bit is set. | ||
128 | .It t | ||
129 | If the character is part of the other permissions and the file is | ||
130 | executable or the directory searchable by others, and the | ||
131 | .Dq sticky | ||
132 | .Pq Dv S_ISVTX | ||
133 | bit is set. | ||
134 | .It x | ||
135 | The file is executable or the directory is searchable. | ||
136 | .It \- | ||
137 | None of the above apply. | ||
138 | .El | ||
139 | .Pp | ||
140 | The last character is a plus sign | ||
141 | .Pq Ql + | ||
142 | if there are any alternate | ||
143 | or additional access control methods associated with the inode, otherwise | ||
144 | it will be a space. | ||
145 | .Sh RETURN VALUES | ||
146 | The | ||
147 | .Fn strmode | ||
148 | function always returns 0. | ||
149 | .Sh SEE ALSO | ||
150 | .Xr chmod 1 , | ||
151 | .Xr find 1 , | ||
152 | .Xr stat 2 , | ||
153 | .Xr getmode 3 , | ||
154 | .Xr setmode 3 | ||
155 | .Sh HISTORY | ||
156 | The | ||
157 | .Fn strmode | ||
158 | function first appeared in | ||
159 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/string/strmode.c b/src/lib/libc/string/strmode.c new file mode 100644 index 0000000000..bd78403cb2 --- /dev/null +++ b/src/lib/libc/string/strmode.c | |||
@@ -0,0 +1,148 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strmode.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <sys/types.h> | ||
35 | #include <sys/stat.h> | ||
36 | #include <string.h> | ||
37 | |||
38 | /* XXX mode should be mode_t */ | ||
39 | |||
40 | void | ||
41 | strmode(int mode, char *p) | ||
42 | { | ||
43 | /* print type */ | ||
44 | switch (mode & S_IFMT) { | ||
45 | case S_IFDIR: /* directory */ | ||
46 | *p++ = 'd'; | ||
47 | break; | ||
48 | case S_IFCHR: /* character special */ | ||
49 | *p++ = 'c'; | ||
50 | break; | ||
51 | case S_IFBLK: /* block special */ | ||
52 | *p++ = 'b'; | ||
53 | break; | ||
54 | case S_IFREG: /* regular */ | ||
55 | *p++ = '-'; | ||
56 | break; | ||
57 | case S_IFLNK: /* symbolic link */ | ||
58 | *p++ = 'l'; | ||
59 | break; | ||
60 | case S_IFSOCK: /* socket */ | ||
61 | *p++ = 's'; | ||
62 | break; | ||
63 | #ifdef S_IFIFO | ||
64 | case S_IFIFO: /* fifo */ | ||
65 | *p++ = 'p'; | ||
66 | break; | ||
67 | #endif | ||
68 | #ifdef S_IFWHT | ||
69 | case S_IFWHT: /* whiteout */ | ||
70 | *p++ = 'w'; | ||
71 | break; | ||
72 | #endif | ||
73 | default: /* unknown */ | ||
74 | *p++ = '?'; | ||
75 | break; | ||
76 | } | ||
77 | /* usr */ | ||
78 | if (mode & S_IRUSR) | ||
79 | *p++ = 'r'; | ||
80 | else | ||
81 | *p++ = '-'; | ||
82 | if (mode & S_IWUSR) | ||
83 | *p++ = 'w'; | ||
84 | else | ||
85 | *p++ = '-'; | ||
86 | switch (mode & (S_IXUSR | S_ISUID)) { | ||
87 | case 0: | ||
88 | *p++ = '-'; | ||
89 | break; | ||
90 | case S_IXUSR: | ||
91 | *p++ = 'x'; | ||
92 | break; | ||
93 | case S_ISUID: | ||
94 | *p++ = 'S'; | ||
95 | break; | ||
96 | case S_IXUSR | S_ISUID: | ||
97 | *p++ = 's'; | ||
98 | break; | ||
99 | } | ||
100 | /* group */ | ||
101 | if (mode & S_IRGRP) | ||
102 | *p++ = 'r'; | ||
103 | else | ||
104 | *p++ = '-'; | ||
105 | if (mode & S_IWGRP) | ||
106 | *p++ = 'w'; | ||
107 | else | ||
108 | *p++ = '-'; | ||
109 | switch (mode & (S_IXGRP | S_ISGID)) { | ||
110 | case 0: | ||
111 | *p++ = '-'; | ||
112 | break; | ||
113 | case S_IXGRP: | ||
114 | *p++ = 'x'; | ||
115 | break; | ||
116 | case S_ISGID: | ||
117 | *p++ = 'S'; | ||
118 | break; | ||
119 | case S_IXGRP | S_ISGID: | ||
120 | *p++ = 's'; | ||
121 | break; | ||
122 | } | ||
123 | /* other */ | ||
124 | if (mode & S_IROTH) | ||
125 | *p++ = 'r'; | ||
126 | else | ||
127 | *p++ = '-'; | ||
128 | if (mode & S_IWOTH) | ||
129 | *p++ = 'w'; | ||
130 | else | ||
131 | *p++ = '-'; | ||
132 | switch (mode & (S_IXOTH | S_ISVTX)) { | ||
133 | case 0: | ||
134 | *p++ = '-'; | ||
135 | break; | ||
136 | case S_IXOTH: | ||
137 | *p++ = 'x'; | ||
138 | break; | ||
139 | case S_ISVTX: | ||
140 | *p++ = 'T'; | ||
141 | break; | ||
142 | case S_IXOTH | S_ISVTX: | ||
143 | *p++ = 't'; | ||
144 | break; | ||
145 | } | ||
146 | *p++ = ' '; /* will be a '+' if ACL's implemented */ | ||
147 | *p = '\0'; | ||
148 | } | ||
diff --git a/src/lib/libc/string/strncat.c b/src/lib/libc/string/strncat.c new file mode 100644 index 0000000000..05d35e0900 --- /dev/null +++ b/src/lib/libc/string/strncat.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strncat.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Concatenate src on the end of dst. At most strlen(dst)+n+1 bytes | ||
41 | * are written at dst (at most n+1 bytes being appended). Return dst. | ||
42 | */ | ||
43 | char * | ||
44 | strncat(char *dst, const char *src, size_t n) | ||
45 | { | ||
46 | if (n != 0) { | ||
47 | char *d = dst; | ||
48 | const char *s = src; | ||
49 | |||
50 | while (*d != 0) | ||
51 | d++; | ||
52 | do { | ||
53 | if ((*d = *s++) == 0) | ||
54 | break; | ||
55 | d++; | ||
56 | } while (--n != 0); | ||
57 | *d = 0; | ||
58 | } | ||
59 | return (dst); | ||
60 | } | ||
diff --git a/src/lib/libc/string/strncmp.c b/src/lib/libc/string/strncmp.c new file mode 100644 index 0000000000..82420e215e --- /dev/null +++ b/src/lib/libc/string/strncmp.c | |||
@@ -0,0 +1,53 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1989 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strncmp.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #ifndef _KERNEL | ||
35 | #include <string.h> | ||
36 | #else | ||
37 | #include <lib/libkern/libkern.h> | ||
38 | #endif | ||
39 | |||
40 | int | ||
41 | strncmp(const char *s1, const char *s2, size_t n) | ||
42 | { | ||
43 | |||
44 | if (n == 0) | ||
45 | return (0); | ||
46 | do { | ||
47 | if (*s1 != *s2++) | ||
48 | return (*(unsigned char *)s1 - *(unsigned char *)--s2); | ||
49 | if (*s1++ == 0) | ||
50 | break; | ||
51 | } while (--n != 0); | ||
52 | return (0); | ||
53 | } | ||
diff --git a/src/lib/libc/string/strncpy.c b/src/lib/libc/string/strncpy.c new file mode 100644 index 0000000000..00493f4f7f --- /dev/null +++ b/src/lib/libc/string/strncpy.c | |||
@@ -0,0 +1,60 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strncpy.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Copy src to dst, truncating or null-padding to always copy n bytes. | ||
41 | * Return dst. | ||
42 | */ | ||
43 | char * | ||
44 | strncpy(char *dst, const char *src, size_t n) | ||
45 | { | ||
46 | if (n != 0) { | ||
47 | char *d = dst; | ||
48 | const char *s = src; | ||
49 | |||
50 | do { | ||
51 | if ((*d++ = *s++) == 0) { | ||
52 | /* NUL pad the remaining n-1 bytes */ | ||
53 | while (--n != 0) | ||
54 | *d++ = 0; | ||
55 | break; | ||
56 | } | ||
57 | } while (--n != 0); | ||
58 | } | ||
59 | return (dst); | ||
60 | } | ||
diff --git a/src/lib/libc/string/strpbrk.3 b/src/lib/libc/string/strpbrk.3 new file mode 100644 index 0000000000..7e54f9de54 --- /dev/null +++ b/src/lib/libc/string/strpbrk.3 | |||
@@ -0,0 +1,72 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strpbrk.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRPBRK 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strpbrk | ||
39 | .Nd locate multiple characters in string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft char * | ||
43 | .Fn strpbrk "const char *s" "const char *charset" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strpbrk | ||
47 | function locates in the null-terminated string | ||
48 | .Fa s | ||
49 | the first occurrence of any character in the string | ||
50 | .Fa charset | ||
51 | and returns a pointer to this character. | ||
52 | If no characters from | ||
53 | .Fa charset | ||
54 | occur anywhere in | ||
55 | .Fa s , | ||
56 | .Fn strpbrk | ||
57 | returns | ||
58 | .Dv NULL . | ||
59 | .Sh SEE ALSO | ||
60 | .Xr memchr 3 , | ||
61 | .Xr strchr 3 , | ||
62 | .Xr strcspn 3 , | ||
63 | .Xr strrchr 3 , | ||
64 | .Xr strsep 3 , | ||
65 | .Xr strspn 3 , | ||
66 | .Xr strstr 3 , | ||
67 | .Xr strtok 3 | ||
68 | .Sh STANDARDS | ||
69 | The | ||
70 | .Fn strpbrk | ||
71 | function conforms to | ||
72 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strpbrk.c b/src/lib/libc/string/strpbrk.c new file mode 100644 index 0000000000..024c42bdc3 --- /dev/null +++ b/src/lib/libc/string/strpbrk.c | |||
@@ -0,0 +1,51 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1985 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strpbrk.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Find the first occurrence in s1 of a character in s2 (excluding NUL). | ||
38 | */ | ||
39 | char * | ||
40 | strpbrk(const char *s1, const char *s2) | ||
41 | { | ||
42 | const char *scanp; | ||
43 | int c, sc; | ||
44 | |||
45 | while ((c = *s1++) != 0) { | ||
46 | for (scanp = s2; (sc = *scanp++) != 0;) | ||
47 | if (sc == c) | ||
48 | return ((char *)(s1 - 1)); | ||
49 | } | ||
50 | return (NULL); | ||
51 | } | ||
diff --git a/src/lib/libc/string/strrchr.3 b/src/lib/libc/string/strrchr.3 new file mode 100644 index 0000000000..d7e0eeff85 --- /dev/null +++ b/src/lib/libc/string/strrchr.3 | |||
@@ -0,0 +1,104 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strrchr.3,v 1.7 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRRCHR 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strrchr , | ||
39 | .Nm rindex | ||
40 | .Nd locate last occurrence of a character in a string | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft char * | ||
44 | .Fn strrchr "const char *s" "int c" | ||
45 | .Ft char * | ||
46 | .Fn rindex "const char *s" "int c" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn strrchr | ||
50 | function locates the last occurrence of the character | ||
51 | .Fa c | ||
52 | in the string | ||
53 | .Fa s . | ||
54 | The terminating | ||
55 | .Tn NUL | ||
56 | character is considered part of the string. | ||
57 | If | ||
58 | .Fa c | ||
59 | is | ||
60 | .Ql \e0 , | ||
61 | .Fn strrchr | ||
62 | locates the terminating | ||
63 | .Ql \e0 . | ||
64 | .Pp | ||
65 | The | ||
66 | .Fn rindex | ||
67 | function is an old synonym for | ||
68 | .Fn strrchr . | ||
69 | .Sh RETURN VALUES | ||
70 | The | ||
71 | .Fn strrchr | ||
72 | function returns a pointer to the located character or | ||
73 | .Dv NULL | ||
74 | if the character does not appear in the string. | ||
75 | .Sh EXAMPLES | ||
76 | After the following call to | ||
77 | .Fn strrchr , | ||
78 | .Va p | ||
79 | will point to the string | ||
80 | .Qq obar : | ||
81 | .Bd -literal -offset indent | ||
82 | char *p; | ||
83 | char *s = "foobar"; | ||
84 | |||
85 | p = strrchr(s, 'o'); | ||
86 | .Ed | ||
87 | .Sh SEE ALSO | ||
88 | .Xr memchr 3 , | ||
89 | .Xr strchr 3 , | ||
90 | .Xr strcspn 3 , | ||
91 | .Xr strpbrk 3 , | ||
92 | .Xr strsep 3 , | ||
93 | .Xr strspn 3 , | ||
94 | .Xr strstr 3 , | ||
95 | .Xr strtok 3 | ||
96 | .Sh STANDARDS | ||
97 | The | ||
98 | .Fn strrchr | ||
99 | function conforms to | ||
100 | .St -ansiC . | ||
101 | .Pp | ||
102 | The | ||
103 | .Fn rindex | ||
104 | function is deprecated and shouldn't be used in new code. | ||
diff --git a/src/lib/libc/string/strsep.3 b/src/lib/libc/string/strsep.3 new file mode 100644 index 0000000000..ddcc22916a --- /dev/null +++ b/src/lib/libc/string/strsep.3 | |||
@@ -0,0 +1,112 @@ | |||
1 | .\" $OpenBSD: strsep.3,v 1.11 2003/06/02 20:18:38 millert Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1990, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" This code is derived from software contributed to Berkeley by | ||
7 | .\" Chris Torek. | ||
8 | .\" | ||
9 | .\" Redistribution and use in source and binary forms, with or without | ||
10 | .\" modification, are permitted provided that the following conditions | ||
11 | .\" are met: | ||
12 | .\" 1. Redistributions of source code must retain the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer. | ||
14 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
15 | .\" notice, this list of conditions and the following disclaimer in the | ||
16 | .\" documentation and/or other materials provided with the distribution. | ||
17 | .\" 3. Neither the name of the University nor the names of its contributors | ||
18 | .\" may be used to endorse or promote products derived from this software | ||
19 | .\" without specific prior written permission. | ||
20 | .\" | ||
21 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | .\" SUCH DAMAGE. | ||
32 | .\" | ||
33 | .\" @(#)strsep.3 8.1 (Berkeley) 6/9/93 | ||
34 | .\" | ||
35 | .Dd June 9, 1993 | ||
36 | .Dt STRSEP 3 | ||
37 | .Os | ||
38 | .Sh NAME | ||
39 | .Nm strsep | ||
40 | .Nd separate strings | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft char * | ||
44 | .Fn strsep "char **stringp" "const char *delim" | ||
45 | .Sh DESCRIPTION | ||
46 | The | ||
47 | .Fn strsep | ||
48 | function locates, in the string referenced by | ||
49 | .Fa *stringp , | ||
50 | the first occurrence of any character in the string | ||
51 | .Fa delim | ||
52 | (or the terminating | ||
53 | .Ql \e0 | ||
54 | character) and replaces it with a | ||
55 | .Ql \e0 . | ||
56 | The location of the next character after the delimiter character | ||
57 | (or | ||
58 | .Dv NULL , | ||
59 | if the end of the string was reached) is stored in | ||
60 | .Fa *stringp . | ||
61 | The original value of | ||
62 | .Fa *stringp | ||
63 | is returned. | ||
64 | .Pp | ||
65 | An | ||
66 | .Dq empty | ||
67 | field, i.e., one caused by two adjacent delimiter characters, | ||
68 | can be detected by comparing the location referenced by the pointer returned | ||
69 | by | ||
70 | .Fn strsep | ||
71 | to | ||
72 | .Ql \e0 . | ||
73 | .Pp | ||
74 | If | ||
75 | .Fa *stringp | ||
76 | is initially | ||
77 | .Dv NULL , | ||
78 | .Fn strsep | ||
79 | returns | ||
80 | .Dv NULL . | ||
81 | .Sh EXAMPLES | ||
82 | The following uses | ||
83 | .Fn strsep | ||
84 | to parse a string, containing tokens delimited by whitespace, into an | ||
85 | argument vector: | ||
86 | .Bd -literal -offset indent | ||
87 | char **ap, *argv[10], *inputstring; | ||
88 | |||
89 | for (ap = argv; ap < &argv[9] && | ||
90 | (*ap = strsep(&inputstring, " \et")) != NULL;) { | ||
91 | if (**ap != '\e0') | ||
92 | ap++; | ||
93 | } | ||
94 | *ap = NULL; | ||
95 | .Ed | ||
96 | .Sh HISTORY | ||
97 | The | ||
98 | .Fn strsep | ||
99 | function is intended as a replacement for the | ||
100 | .Fn strtok | ||
101 | function. | ||
102 | While the | ||
103 | .Fn strtok | ||
104 | function should be preferred for portability reasons (it conforms to | ||
105 | .St -ansiC ) | ||
106 | it is unable to handle empty fields, i.e., detect fields delimited by | ||
107 | two adjacent delimiter characters, or to be used for more than a single | ||
108 | string at a time. | ||
109 | The | ||
110 | .Fn strsep | ||
111 | function first appeared in | ||
112 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/string/strsep.c b/src/lib/libc/string/strsep.c new file mode 100644 index 0000000000..337d78e466 --- /dev/null +++ b/src/lib/libc/string/strsep.c | |||
@@ -0,0 +1,79 @@ | |||
1 | /* $OpenBSD: strsep.c,v 1.5 2003/06/11 21:08:16 deraadt 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. Neither the name of the University nor the names of its contributors | ||
16 | * may be used to endorse or promote products derived from this software | ||
17 | * without specific prior written permission. | ||
18 | * | ||
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | * SUCH DAMAGE. | ||
30 | */ | ||
31 | |||
32 | #include <string.h> | ||
33 | #include <stdio.h> | ||
34 | |||
35 | #if defined(LIBC_SCCS) && !defined(lint) | ||
36 | #if 0 | ||
37 | static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; | ||
38 | #else | ||
39 | static char *rcsid = "$OpenBSD: strsep.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
40 | #endif | ||
41 | #endif /* LIBC_SCCS and not lint */ | ||
42 | |||
43 | /* | ||
44 | * Get next token from string *stringp, where tokens are possibly-empty | ||
45 | * strings separated by characters from delim. | ||
46 | * | ||
47 | * Writes NULs into the string at *stringp to end tokens. | ||
48 | * delim need not remain constant from call to call. | ||
49 | * On return, *stringp points past the last NUL written (if there might | ||
50 | * be further tokens), or is NULL (if there are definitely no more tokens). | ||
51 | * | ||
52 | * If *stringp is NULL, strsep returns NULL. | ||
53 | */ | ||
54 | char * | ||
55 | strsep(char **stringp, const char *delim) | ||
56 | { | ||
57 | char *s; | ||
58 | const char *spanp; | ||
59 | int c, sc; | ||
60 | char *tok; | ||
61 | |||
62 | if ((s = *stringp) == NULL) | ||
63 | return (NULL); | ||
64 | for (tok = s;;) { | ||
65 | c = *s++; | ||
66 | spanp = delim; | ||
67 | do { | ||
68 | if ((sc = *spanp++) == c) { | ||
69 | if (c == 0) | ||
70 | s = NULL; | ||
71 | else | ||
72 | s[-1] = 0; | ||
73 | *stringp = s; | ||
74 | return (tok); | ||
75 | } | ||
76 | } while (sc != 0); | ||
77 | } | ||
78 | /* NOTREACHED */ | ||
79 | } | ||
diff --git a/src/lib/libc/string/strsignal.3 b/src/lib/libc/string/strsignal.3 new file mode 100644 index 0000000000..b1082c2dd0 --- /dev/null +++ b/src/lib/libc/string/strsignal.3 | |||
@@ -0,0 +1,56 @@ | |||
1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strsignal.3,v 1.4 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRSIGNAL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strsignal | ||
39 | .Nd get signal description string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft char * | ||
43 | .Fn strsignal "int sig" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strsignal | ||
47 | function returns a pointer to the language-dependent string describing | ||
48 | a signal. | ||
49 | .Pp | ||
50 | The array pointed to is not to be modified by the program, but may be | ||
51 | overwritten by subsequent calls to | ||
52 | .Fn strsignal . | ||
53 | .Sh SEE ALSO | ||
54 | .Xr intro 2 , | ||
55 | .Xr psignal 3 , | ||
56 | .Xr setlocale 3 | ||
diff --git a/src/lib/libc/string/strsignal.c b/src/lib/libc/string/strsignal.c new file mode 100644 index 0000000000..afe0deb272 --- /dev/null +++ b/src/lib/libc/string/strsignal.c | |||
@@ -0,0 +1,45 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strsignal.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <string.h> | ||
35 | #include <limits.h> | ||
36 | |||
37 | extern char *__strsignal(int, char *); | ||
38 | |||
39 | char * | ||
40 | strsignal(int sig) | ||
41 | { | ||
42 | static char buf[NL_TEXTMAX]; | ||
43 | |||
44 | return __strsignal(sig, buf); | ||
45 | } | ||
diff --git a/src/lib/libc/string/strspn.3 b/src/lib/libc/string/strspn.3 new file mode 100644 index 0000000000..54077b810c --- /dev/null +++ b/src/lib/libc/string/strspn.3 | |||
@@ -0,0 +1,84 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strspn.3,v 1.7 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRSPN 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strspn | ||
39 | .Nd span a string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft size_t | ||
43 | .Fn strspn "const char *s" "const char *charset" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strspn | ||
47 | function spans the initial part of the null-terminated string | ||
48 | .Fa s | ||
49 | as long as the characters from | ||
50 | .Fa s | ||
51 | occur in string | ||
52 | .Fa charset . | ||
53 | .Sh RETURN VALUES | ||
54 | The | ||
55 | .Fn strspn | ||
56 | function returns the number of characters spanned. | ||
57 | .Sh EXAMPLES | ||
58 | The following call to | ||
59 | .Fn strspn | ||
60 | will return 3, since the first three characters of string | ||
61 | .Fa s | ||
62 | are part of string | ||
63 | .Fa charset : | ||
64 | .Bd -literal -offset indent | ||
65 | char *s = "foobar"; | ||
66 | char *charset = "of"; | ||
67 | size_t span; | ||
68 | |||
69 | span = strspn(s, charset); | ||
70 | .Ed | ||
71 | .Sh SEE ALSO | ||
72 | .Xr memchr 3 , | ||
73 | .Xr strchr 3 , | ||
74 | .Xr strcspn 3 , | ||
75 | .Xr strpbrk 3 , | ||
76 | .Xr strrchr 3 , | ||
77 | .Xr strsep 3 , | ||
78 | .Xr strstr 3 , | ||
79 | .Xr strtok 3 | ||
80 | .Sh STANDARDS | ||
81 | The | ||
82 | .Fn strspn | ||
83 | function conforms to | ||
84 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strspn.c b/src/lib/libc/string/strspn.c new file mode 100644 index 0000000000..d51a8cd3e6 --- /dev/null +++ b/src/lib/libc/string/strspn.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1989 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strspn.c,v 1.4 2003/06/11 21:08:29 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * Span the string s2 (skip characters that are in s2). | ||
38 | */ | ||
39 | size_t | ||
40 | strspn(const char *s1, const char *s2) | ||
41 | { | ||
42 | const char *p = s1, *spanp; | ||
43 | char c, sc; | ||
44 | |||
45 | /* | ||
46 | * Skip any characters in s2, excluding the terminating \0. | ||
47 | */ | ||
48 | cont: | ||
49 | c = *p++; | ||
50 | for (spanp = s2; (sc = *spanp++) != 0;) | ||
51 | if (sc == c) | ||
52 | goto cont; | ||
53 | return (p - 1 - s1); | ||
54 | } | ||
diff --git a/src/lib/libc/string/strstr.3 b/src/lib/libc/string/strstr.3 new file mode 100644 index 0000000000..47777dd89f --- /dev/null +++ b/src/lib/libc/string/strstr.3 | |||
@@ -0,0 +1,81 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strstr.3,v 1.6 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRSTR 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strstr | ||
39 | .Nd locate a substring in a string | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft char * | ||
43 | .Fn strstr "const char *big" "const char *little" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strstr | ||
47 | function locates the first occurrence of the null-terminated string | ||
48 | .Fa little | ||
49 | in the null-terminated string | ||
50 | .Fa big . | ||
51 | If | ||
52 | .Fa little | ||
53 | is the empty string, | ||
54 | .Fn strstr | ||
55 | returns | ||
56 | .Fa big ; | ||
57 | if | ||
58 | .Fa little | ||
59 | occurs nowhere in | ||
60 | .Fa big , | ||
61 | .Fn strstr | ||
62 | returns | ||
63 | .Dv NULL ; | ||
64 | otherwise | ||
65 | .Fn strstr | ||
66 | returns a pointer to the first character of the first occurrence of | ||
67 | .Fa little . | ||
68 | .Sh SEE ALSO | ||
69 | .Xr memchr 3 , | ||
70 | .Xr strchr 3 , | ||
71 | .Xr strcspn 3 , | ||
72 | .Xr strpbrk 3 , | ||
73 | .Xr strrchr 3 , | ||
74 | .Xr strsep 3 , | ||
75 | .Xr strspn 3 , | ||
76 | .Xr strtok 3 | ||
77 | .Sh STANDARDS | ||
78 | The | ||
79 | .Fn strstr | ||
80 | function conforms to | ||
81 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strstr.c b/src/lib/libc/string/strstr.c new file mode 100644 index 0000000000..e6a94c5ce6 --- /dev/null +++ b/src/lib/libc/string/strstr.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strstr.c,v 1.4 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Find the first occurrence of find in s. | ||
41 | */ | ||
42 | char * | ||
43 | strstr(const char *s, const char *find) | ||
44 | { | ||
45 | char c, sc; | ||
46 | size_t len; | ||
47 | |||
48 | if ((c = *find++) != 0) { | ||
49 | len = strlen(find); | ||
50 | do { | ||
51 | do { | ||
52 | if ((sc = *s++) == 0) | ||
53 | return (NULL); | ||
54 | } while (sc != c); | ||
55 | } while (strncmp(s, find, len) != 0); | ||
56 | s--; | ||
57 | } | ||
58 | return ((char *)s); | ||
59 | } | ||
diff --git a/src/lib/libc/string/strtok.3 b/src/lib/libc/string/strtok.3 new file mode 100644 index 0000000000..bdf28f68f6 --- /dev/null +++ b/src/lib/libc/string/strtok.3 | |||
@@ -0,0 +1,152 @@ | |||
1 | .\" Copyright (c) 1988, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" the American National Standards Committee X3, on Information | ||
6 | .\" Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strtok.3,v 1.16 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRTOK 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtok , | ||
39 | .Nm strtok_r | ||
40 | .Nd string token operations | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <string.h> | ||
43 | .Ft char * | ||
44 | .Fn strtok "char *str" "const char *sep" | ||
45 | .Ft char * | ||
46 | .Fn strtok_r "char *str" "const char *sep" "char **last" | ||
47 | .Sh DESCRIPTION | ||
48 | .Bf -symbolic | ||
49 | This interface is obsoleted by | ||
50 | .Xr strsep 3 . | ||
51 | .Ef | ||
52 | .Pp | ||
53 | The | ||
54 | .Fn strtok | ||
55 | function is used to isolate sequential tokens in a null-terminated string, | ||
56 | .Fa str . | ||
57 | These tokens are separated in the string by at least one of the | ||
58 | characters in | ||
59 | .Fa sep . | ||
60 | The first time that | ||
61 | .Fn strtok | ||
62 | is called, | ||
63 | .Fa str | ||
64 | should be specified; subsequent calls, wishing to obtain further tokens | ||
65 | from the same string, should pass a null pointer instead. | ||
66 | The separator string, | ||
67 | .Fa sep , | ||
68 | must be supplied each time, and may change between calls. | ||
69 | .Pp | ||
70 | The | ||
71 | .Fn strtok_r | ||
72 | function is a version of | ||
73 | .Fn strtok | ||
74 | that takes an explicit context argument and is reentrant. | ||
75 | .Pp | ||
76 | The | ||
77 | .Fn strtok | ||
78 | and | ||
79 | .Fn strtok_r | ||
80 | functions return a pointer to the beginning of each subsequent token | ||
81 | in the string, after replacing the separator character itself with an | ||
82 | .Tn ASCII NUL | ||
83 | character. | ||
84 | When no more tokens remain, a null pointer is returned. | ||
85 | .Pp | ||
86 | Since | ||
87 | .Fn strtok | ||
88 | and | ||
89 | .Fn strtok_r | ||
90 | modify the string, | ||
91 | .Fa str | ||
92 | should not point to an area in the initialized data segment. | ||
93 | .Sh EXAMPLES | ||
94 | The following will construct an array of pointers to each individual word in | ||
95 | the string | ||
96 | .Va s : | ||
97 | .Bd -literal -offset indent | ||
98 | #define MAXTOKENS 128 | ||
99 | |||
100 | char s[512], *p, *tokens[MAXTOKENS]; | ||
101 | char *last; | ||
102 | int i = 0; | ||
103 | |||
104 | snprintf(s, sizeof(s), "cat dog horse cow"); | ||
105 | |||
106 | for ((p = strtok_r(s, " ", &last)); p; | ||
107 | (p = strtok_r(NULL, " ", &last)), i++) { | ||
108 | if (i < MAXTOKENS - 1) | ||
109 | tokens[i] = p; | ||
110 | } | ||
111 | tokens[i] = NULL; | ||
112 | .Ed | ||
113 | .Pp | ||
114 | That is, | ||
115 | .Li tokens[0] | ||
116 | will point to | ||
117 | .Qq cat , | ||
118 | .Li tokens[1] | ||
119 | will point to | ||
120 | .Qq dog , | ||
121 | .Li tokens[2] | ||
122 | will point to | ||
123 | .Qq horse , | ||
124 | and | ||
125 | .Li tokens[3] | ||
126 | will point to | ||
127 | .Qq cow . | ||
128 | .Sh SEE ALSO | ||
129 | .Xr memchr 3 , | ||
130 | .Xr strchr 3 , | ||
131 | .Xr strcspn 3 , | ||
132 | .Xr strpbrk 3 , | ||
133 | .Xr strrchr 3 , | ||
134 | .Xr strsep 3 , | ||
135 | .Xr strspn 3 , | ||
136 | .Xr strstr 3 | ||
137 | .Sh STANDARDS | ||
138 | The | ||
139 | .Fn strtok | ||
140 | function conforms to | ||
141 | .St -ansiC . | ||
142 | .Sh BUGS | ||
143 | The System V | ||
144 | .Fn strtok , | ||
145 | if handed a string containing only delimiter characters, | ||
146 | will not alter the next starting point, so that a call to | ||
147 | .Fn strtok | ||
148 | with a different (or empty) delimiter string | ||
149 | may return a non-null value. | ||
150 | Since this implementation always alters the next starting point, | ||
151 | such a sequence of calls would always return | ||
152 | .Dv NULL . | ||
diff --git a/src/lib/libc/string/strtok.c b/src/lib/libc/string/strtok.c new file mode 100644 index 0000000000..6498eac8cc --- /dev/null +++ b/src/lib/libc/string/strtok.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. Neither the name of the University nor the names of its contributors | ||
14 | * may be used to endorse or promote products derived from this software | ||
15 | * without specific prior written permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
27 | * SUCH DAMAGE. | ||
28 | */ | ||
29 | |||
30 | #if defined(LIBC_SCCS) && !defined(lint) | ||
31 | static char *rcsid = "$OpenBSD: strtok.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
32 | #endif /* LIBC_SCCS and not lint */ | ||
33 | |||
34 | #include <string.h> | ||
35 | |||
36 | char * | ||
37 | strtok(char *s, const char *delim) | ||
38 | { | ||
39 | static char *last; | ||
40 | |||
41 | return strtok_r(s, delim, &last); | ||
42 | } | ||
43 | |||
44 | char * | ||
45 | strtok_r(char *s, const char *delim, char **last) | ||
46 | { | ||
47 | char *spanp; | ||
48 | int c, sc; | ||
49 | char *tok; | ||
50 | |||
51 | |||
52 | if (s == NULL && (s = *last) == NULL) | ||
53 | return (NULL); | ||
54 | |||
55 | /* | ||
56 | * Skip (span) leading delimiters (s += strspn(s, delim), sort of). | ||
57 | */ | ||
58 | cont: | ||
59 | c = *s++; | ||
60 | for (spanp = (char *)delim; (sc = *spanp++) != 0;) { | ||
61 | if (c == sc) | ||
62 | goto cont; | ||
63 | } | ||
64 | |||
65 | if (c == 0) { /* no non-delimiter characters */ | ||
66 | *last = NULL; | ||
67 | return (NULL); | ||
68 | } | ||
69 | tok = s - 1; | ||
70 | |||
71 | /* | ||
72 | * Scan token (scan for delimiters: s += strcspn(s, delim), sort of). | ||
73 | * Note that delim must have one NUL; we stop if we see that, too. | ||
74 | */ | ||
75 | for (;;) { | ||
76 | c = *s++; | ||
77 | spanp = (char *)delim; | ||
78 | do { | ||
79 | if ((sc = *spanp++) == c) { | ||
80 | if (c == 0) | ||
81 | s = NULL; | ||
82 | else | ||
83 | s[-1] = 0; | ||
84 | *last = s; | ||
85 | return (tok); | ||
86 | } | ||
87 | } while (sc != 0); | ||
88 | } | ||
89 | /* NOTREACHED */ | ||
90 | } | ||
diff --git a/src/lib/libc/string/strxfrm.3 b/src/lib/libc/string/strxfrm.3 new file mode 100644 index 0000000000..edf2bfb8a0 --- /dev/null +++ b/src/lib/libc/string/strxfrm.3 | |||
@@ -0,0 +1,62 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" This code is derived from software contributed to Berkeley by | ||
5 | .\" Chris Torek and the American National Standards Committee X3, | ||
6 | .\" on Information Processing Systems. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" $OpenBSD: strxfrm.3,v 1.4 2003/06/02 20:18:38 millert Exp $ | ||
33 | .\" | ||
34 | .Dd June 29, 1991 | ||
35 | .Dt STRXFRM 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strxfrm | ||
39 | .Nd transform a string under locale | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <string.h> | ||
42 | .Ft size_t | ||
43 | .Fn strxfrm "char *dst" "const char *src" "size_t n" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn strxfrm | ||
47 | function does something horrible (see | ||
48 | .Tn ANSI | ||
49 | standard). | ||
50 | In this implementation it just copies. | ||
51 | .Sh SEE ALSO | ||
52 | .Xr bcmp 3 , | ||
53 | .Xr memcmp 3 , | ||
54 | .\" .Xr setlocale 3 , | ||
55 | .Xr strcasecmp 3 , | ||
56 | .Xr strcmp 3 , | ||
57 | .Xr strcoll 3 | ||
58 | .Sh STANDARDS | ||
59 | The | ||
60 | .Fn strxfrm | ||
61 | function conforms to | ||
62 | .St -ansiC . | ||
diff --git a/src/lib/libc/string/strxfrm.c b/src/lib/libc/string/strxfrm.c new file mode 100644 index 0000000000..a2e2dbc440 --- /dev/null +++ b/src/lib/libc/string/strxfrm.c | |||
@@ -0,0 +1,54 @@ | |||
1 | /*- | ||
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: strxfrm.c,v 1.5 2003/09/06 22:43:12 tedu Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <string.h> | ||
38 | |||
39 | /* | ||
40 | * Transform src, storing the result in dst, such that | ||
41 | * strcmp() on transformed strings returns what strcoll() | ||
42 | * on the original untransformed strings would return. | ||
43 | */ | ||
44 | size_t | ||
45 | strxfrm(char *dst, const char *src, size_t n) | ||
46 | { | ||
47 | |||
48 | /* | ||
49 | * Since locales are unimplemented, this is just a copy. | ||
50 | */ | ||
51 | if (n == 0) | ||
52 | return (strlen(src)); | ||
53 | return (strlcpy(dst, src, n)); | ||
54 | } | ||
diff --git a/src/lib/libc/string/swab.3 b/src/lib/libc/string/swab.3 new file mode 100644 index 0000000000..abeff903de --- /dev/null +++ b/src/lib/libc/string/swab.3 | |||
@@ -0,0 +1,61 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
2 | .\" All rights reserved. | ||
3 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | ||
5 | .\" modification, are permitted provided that the following conditions | ||
6 | .\" are met: | ||
7 | .\" 1. Redistributions of source code must retain the above copyright | ||
8 | .\" notice, this list of conditions and the following disclaimer. | ||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer in the | ||
11 | .\" documentation and/or other materials provided with the distribution. | ||
12 | .\" 3. Neither the name of the University nor the names of its contributors | ||
13 | .\" may be used to endorse or promote products derived from this software | ||
14 | .\" without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" $OpenBSD: swab.3,v 1.5 2003/06/02 20:18:38 millert Exp $ | ||
29 | .\" | ||
30 | .Dd May 1, 1991 | ||
31 | .Dt SWAB 3 | ||
32 | .Os | ||
33 | .Sh NAME | ||
34 | .Nm swab | ||
35 | .Nd swap adjacent bytes | ||
36 | .Sh SYNOPSIS | ||
37 | .Fd #include <unistd.h> | ||
38 | .Ft void | ||
39 | .Fn swab "const void *src" "void *dst" "size_t len" | ||
40 | .Sh DESCRIPTION | ||
41 | The function | ||
42 | .Fn swab | ||
43 | copies | ||
44 | .Fa len | ||
45 | bytes from the location referenced by | ||
46 | .Fa src | ||
47 | to the location referenced by | ||
48 | .Fa dst , | ||
49 | swapping adjacent bytes. | ||
50 | .Pp | ||
51 | The argument | ||
52 | .Fa len | ||
53 | must be an even number. | ||
54 | .Sh SEE ALSO | ||
55 | .Xr bzero 3 , | ||
56 | .Xr memset 3 | ||
57 | .Sh HISTORY | ||
58 | A | ||
59 | .Fn swab | ||
60 | function appeared in | ||
61 | .At v7 . | ||
diff --git a/src/lib/libc/string/swab.c b/src/lib/libc/string/swab.c new file mode 100644 index 0000000000..b928f21aa7 --- /dev/null +++ b/src/lib/libc/string/swab.c | |||
@@ -0,0 +1,58 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1988 Regents of the University of California. | ||
3 | * All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Jeffrey Mogul. | ||
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. Neither the name of the University nor the names of its contributors | ||
17 | * may be used to endorse or promote products derived from this software | ||
18 | * without specific prior written permission. | ||
19 | * | ||
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | * SUCH DAMAGE. | ||
31 | */ | ||
32 | |||
33 | #if defined(LIBC_SCCS) && !defined(lint) | ||
34 | static char *rcsid = "$OpenBSD: swab.c,v 1.5 2003/06/11 21:08:16 deraadt Exp $"; | ||
35 | #endif /* LIBC_SCCS and not lint */ | ||
36 | |||
37 | #include <unistd.h> | ||
38 | |||
39 | void | ||
40 | swab(const void *from, void *to, size_t len) | ||
41 | { | ||
42 | register unsigned long temp; | ||
43 | register int n; | ||
44 | register char *fp, *tp; | ||
45 | |||
46 | n = (len >> 1) + 1; | ||
47 | fp = (char *)from; | ||
48 | tp = (char *)to; | ||
49 | #define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp | ||
50 | /* round to multiple of 8 */ | ||
51 | while ((--n) & 07) | ||
52 | STEP; | ||
53 | n >>= 3; | ||
54 | while (--n >= 0) { | ||
55 | STEP; STEP; STEP; STEP; | ||
56 | STEP; STEP; STEP; STEP; | ||
57 | } | ||
58 | } | ||