diff options
Diffstat (limited to 'src/lib/libc/stdlib')
104 files changed, 12748 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc new file mode 100644 index 0000000000..c149aeef0a --- /dev/null +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -0,0 +1,73 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.41 2008/09/13 22:48:45 martynas 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 | cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ | ||
8 | getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ | ||
9 | lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \ | ||
10 | random.c realpath.c setenv.c strtoimax.c strtol.c \ | ||
11 | strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \ | ||
12 | tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ | ||
13 | lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c | ||
14 | |||
15 | .if (${MACHINE_ARCH} == "m68k") | ||
16 | SRCS+= abs.S div.c labs.c ldiv.c | ||
17 | LSRCS+= abs.c | ||
18 | .elif (${MACHINE_ARCH} == "i386") | ||
19 | SRCS+= abs.S div.S labs.S ldiv.S | ||
20 | LSRCS+= abs.c div.c labs.c ldiv.c | ||
21 | .elif (${MACHINE_ARCH} == "ns32k") | ||
22 | SRCS+= abs.S div.c labs.c ldiv.c | ||
23 | LSRCS+= abs.c | ||
24 | .elif (${MACHINE_ARCH} == "tahoe") | ||
25 | SRCS+= abs.S div.c labs.c ldiv.c | ||
26 | LSRCS+= abs.c | ||
27 | .elif (${MACHINE_ARCH} == "vax") | ||
28 | SRCS+= abs.c div.c labs.c ldiv.c | ||
29 | .elif (${MACHINE_ARCH} == "alpha") | ||
30 | # XXX should be .S's | ||
31 | SRCS+= abs.c div.c labs.c ldiv.c | ||
32 | .else | ||
33 | SRCS+= abs.c div.c labs.c ldiv.c | ||
34 | .endif | ||
35 | |||
36 | .if (${MACHINE_ARCH} == "vax") || (${MACHINE_ARCH} == "m68k") | ||
37 | SRCS+= insque.S remque.S | ||
38 | .else | ||
39 | SRCS+= insque.c remque.c | ||
40 | .endif | ||
41 | |||
42 | MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ | ||
43 | bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ | ||
44 | getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \ | ||
45 | lldiv.3 lsearch.3 malloc.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ | ||
46 | rand.3 random.3 realpath.3 strtod.3 strtonum.3 strtol.3 strtoul.3 \ | ||
47 | system.3 tsearch.3 | ||
48 | |||
49 | MLINKS+=exit.3 _Exit.3 | ||
50 | MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 | ||
51 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 | ||
52 | MLINKS+=getopt_long.3 getopt_long_only.3 | ||
53 | MLINKS+=hcreate.3 hdestroy.3 hcreate.3 hsearch.3 | ||
54 | MLINKS+=insque.3 remque.3 | ||
55 | MLINKS+=labs.3 llabs.3 | ||
56 | MLINKS+=lsearch.3 lfind.3 | ||
57 | MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 | ||
58 | MLINKS+=malloc.3 cfree.3 malloc.3 malloc.conf.5 | ||
59 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 | ||
60 | MLINKS+=radixsort.3 sradixsort.3 | ||
61 | MLINKS+=rand.3 srand.3 rand.3 rand_r.3 | ||
62 | MLINKS+=random.3 initstate.3 random.3 setstate.3 | ||
63 | MLINKS+=random.3 srandom.3 random.3 srandomdev.3 | ||
64 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 | ||
65 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 | ||
66 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 | ||
67 | MLINKS+=strtod.3 strtof.3 strtod.3 strtold.3 | ||
68 | MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3 | ||
69 | MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3 | ||
70 | MLINKS+=tsearch.3 tfind.3 | ||
71 | MLINKS+=tsearch.3 tdelete.3 | ||
72 | MLINKS+=tsearch.3 twalk.3 | ||
73 | MLINKS+=a64l.3 l64a.3 | ||
diff --git a/src/lib/libc/stdlib/_Exit.c b/src/lib/libc/stdlib/_Exit.c new file mode 100644 index 0000000000..5a2291a931 --- /dev/null +++ b/src/lib/libc/stdlib/_Exit.c | |||
@@ -0,0 +1,22 @@ | |||
1 | /* $OpenBSD: _Exit.c,v 1.2 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Placed in the public domain by Todd C. Miller on January 21, 2004. | ||
5 | */ | ||
6 | |||
7 | #include <stdlib.h> | ||
8 | #include <unistd.h> | ||
9 | |||
10 | /* | ||
11 | * _Exit() is the ISO/ANSI C99 equivalent of the POSIX _exit() function. | ||
12 | * No atexit() handlers are called and no signal handlers are run. | ||
13 | * Whether or not stdio buffers are flushed or temporary files are removed | ||
14 | * is implementation-dependent. As such it is safest to *not* flush | ||
15 | * stdio buffers or remove temporary files. This is also consistent | ||
16 | * with most other implementations. | ||
17 | */ | ||
18 | void | ||
19 | _Exit(int status) | ||
20 | { | ||
21 | _exit(status); | ||
22 | } | ||
diff --git a/src/lib/libc/stdlib/_rand48.c b/src/lib/libc/stdlib/_rand48.c new file mode 100644 index 0000000000..7c950f7cee --- /dev/null +++ b/src/lib/libc/stdlib/_rand48.c | |||
@@ -0,0 +1,47 @@ | |||
1 | /* $OpenBSD: _rand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | unsigned short __rand48_seed[3] = { | ||
18 | RAND48_SEED_0, | ||
19 | RAND48_SEED_1, | ||
20 | RAND48_SEED_2 | ||
21 | }; | ||
22 | unsigned short __rand48_mult[3] = { | ||
23 | RAND48_MULT_0, | ||
24 | RAND48_MULT_1, | ||
25 | RAND48_MULT_2 | ||
26 | }; | ||
27 | unsigned short __rand48_add = RAND48_ADD; | ||
28 | |||
29 | void | ||
30 | __dorand48(unsigned short xseed[3]) | ||
31 | { | ||
32 | unsigned long accu; | ||
33 | unsigned short temp[2]; | ||
34 | |||
35 | accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] + | ||
36 | (unsigned long) __rand48_add; | ||
37 | temp[0] = (unsigned short) accu; /* lower 16 bits */ | ||
38 | accu >>= sizeof(unsigned short) * 8; | ||
39 | accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] + | ||
40 | (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0]; | ||
41 | temp[1] = (unsigned short) accu; /* middle 16 bits */ | ||
42 | accu >>= sizeof(unsigned short) * 8; | ||
43 | accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0]; | ||
44 | xseed[0] = temp[0]; | ||
45 | xseed[1] = temp[1]; | ||
46 | xseed[2] = (unsigned short) accu; | ||
47 | } | ||
diff --git a/src/lib/libc/stdlib/a64l.3 b/src/lib/libc/stdlib/a64l.3 new file mode 100644 index 0000000000..6eec5ce3b3 --- /dev/null +++ b/src/lib/libc/stdlib/a64l.3 | |||
@@ -0,0 +1,133 @@ | |||
1 | .\" $OpenBSD: a64l.3,v 1.10 2007/05/31 19:19:31 jmc 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 $Mdocdate: May 31 2007 $ | ||
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 NUL-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..5312929c6f --- /dev/null +++ b/src/lib/libc/stdlib/a64l.c | |||
@@ -0,0 +1,42 @@ | |||
1 | /* $OpenBSD: a64l.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <errno.h> | ||
8 | #include <stdlib.h> | ||
9 | |||
10 | long | ||
11 | a64l(const char *s) | ||
12 | { | ||
13 | long value, digit, shift; | ||
14 | int i; | ||
15 | |||
16 | if (s == NULL) { | ||
17 | errno = EINVAL; | ||
18 | return(-1L); | ||
19 | } | ||
20 | |||
21 | value = 0; | ||
22 | shift = 0; | ||
23 | for (i = 0; *s && i < 6; i++, s++) { | ||
24 | if (*s >= '.' && *s <= '/') | ||
25 | digit = *s - '.'; | ||
26 | else if (*s >= '0' && *s <= '9') | ||
27 | digit = *s - '0' + 2; | ||
28 | else if (*s >= 'A' && *s <= 'Z') | ||
29 | digit = *s - 'A' + 12; | ||
30 | else if (*s >= 'a' && *s <= 'z') | ||
31 | digit = *s - 'a' + 38; | ||
32 | else { | ||
33 | errno = EINVAL; | ||
34 | return(-1L); | ||
35 | } | ||
36 | |||
37 | value |= digit << shift; | ||
38 | shift += 6; | ||
39 | } | ||
40 | |||
41 | return(value); | ||
42 | } | ||
diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3 new file mode 100644 index 0000000000..4ec85882b2 --- /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.8 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..244e3b28aa --- /dev/null +++ b/src/lib/libc/stdlib/abort.c | |||
@@ -0,0 +1,78 @@ | |||
1 | /* $OpenBSD: abort.c,v 1.15 2007/09/03 14:40:16 millert Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1985 Regents of the University of California. | ||
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. 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 | #include <signal.h> | ||
32 | #include <stdlib.h> | ||
33 | #include <unistd.h> | ||
34 | #include "thread_private.h" | ||
35 | #include "atexit.h" | ||
36 | |||
37 | void | ||
38 | abort(void) | ||
39 | { | ||
40 | struct atexit *p = __atexit; | ||
41 | static int cleanup_called = 0; | ||
42 | sigset_t mask; | ||
43 | |||
44 | |||
45 | sigfillset(&mask); | ||
46 | /* | ||
47 | * don't block SIGABRT to give any handler a chance; we ignore | ||
48 | * any errors -- X311J doesn't allow abort to return anyway. | ||
49 | */ | ||
50 | sigdelset(&mask, SIGABRT); | ||
51 | (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
52 | |||
53 | /* | ||
54 | * POSIX requires we flush stdio buffers on abort | ||
55 | */ | ||
56 | if (cleanup_called == 0) { | ||
57 | /* the cleanup routine lives in fns[0] on the last page */ | ||
58 | while (p != NULL && p->next != NULL) | ||
59 | p = p->next; | ||
60 | /* the check for fn_dso == NULL is mostly paranoia */ | ||
61 | if (p != NULL && p->fns[0].fn_dso == NULL && | ||
62 | p->fns[0].fn_ptr.std_func != NULL) { | ||
63 | cleanup_called = 1; | ||
64 | (*p->fns[0].fn_ptr.std_func)(); | ||
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..c4135f9c84 --- /dev/null +++ b/src/lib/libc/stdlib/abs.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 | .\" 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.8 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 imaxabs 3 , | ||
58 | .Xr labs 3 , | ||
59 | .Xr math 3 | ||
60 | .Sh STANDARDS | ||
61 | The | ||
62 | .Fn abs | ||
63 | function conforms to | ||
64 | .St -ansiC . | ||
65 | .Sh BUGS | ||
66 | 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..5d2fbae69f --- /dev/null +++ b/src/lib/libc/stdlib/abs.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* $OpenBSD: abs.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | |||
33 | int | ||
34 | abs(int j) | ||
35 | { | ||
36 | return(j < 0 ? -j : j); | ||
37 | } | ||
diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3 new file mode 100644 index 0000000000..d7fddb04c2 --- /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.11 2007/05/31 19:19:31 jmc Exp $ | ||
29 | .\" | ||
30 | .Dd $Mdocdate: May 31 2007 $ | ||
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..0615feabb1 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.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: atexit.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 | .Pp | ||
58 | .Fn atexit | ||
59 | is very difficult to use correctly without creating | ||
60 | .Xr exit 3 Ns -time | ||
61 | races. | ||
62 | Unless absolutely necessary, please avoid using it. | ||
63 | .Sh RETURN VALUES | ||
64 | .Rv -std atexit | ||
65 | .Sh ERRORS | ||
66 | .Bl -tag -width Er | ||
67 | .It Bq Er ENOMEM | ||
68 | No memory was available to add the function to the list. | ||
69 | The existing list of functions is unmodified. | ||
70 | .El | ||
71 | .Sh SEE ALSO | ||
72 | .Xr exit 3 | ||
73 | .Sh STANDARDS | ||
74 | The | ||
75 | .Fn atexit | ||
76 | function conforms to | ||
77 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c new file mode 100644 index 0000000000..4d28fb5ef6 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.c | |||
@@ -0,0 +1,209 @@ | |||
1 | /* $OpenBSD: atexit.c,v 1.14 2007/09/05 20:47:47 chl Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2002 Daniel Hartmeier | ||
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 | * | ||
10 | * - Redistributions of source code must retain the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer. | ||
12 | * - Redistributions in binary form must reproduce the above | ||
13 | * copyright notice, this list of conditions and the following | ||
14 | * disclaimer in the documentation and/or other materials provided | ||
15 | * with the distribution. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
20 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
21 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
23 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * 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 | |||
32 | #include <sys/types.h> | ||
33 | #include <sys/mman.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <unistd.h> | ||
37 | #include "atexit.h" | ||
38 | #include "thread_private.h" | ||
39 | |||
40 | int __atexit_invalid = 1; | ||
41 | struct atexit *__atexit; | ||
42 | |||
43 | /* | ||
44 | * Function pointers are stored in a linked list of pages. The list | ||
45 | * is initially empty, and pages are allocated on demand. The first | ||
46 | * function pointer in the first allocated page (the last one in | ||
47 | * the linked list) is reserved for the cleanup function. | ||
48 | * | ||
49 | * Outside the following functions, all pages are mprotect()'ed | ||
50 | * to prevent unintentional/malicious corruption. | ||
51 | */ | ||
52 | |||
53 | /* | ||
54 | * Register a function to be performed at exit or when a shared object | ||
55 | * with the given dso handle is unloaded dynamically. Also used as | ||
56 | * the backend for atexit(). For more info on this API, see: | ||
57 | * | ||
58 | * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor | ||
59 | */ | ||
60 | int | ||
61 | __cxa_atexit(void (*func)(void *), void *arg, void *dso) | ||
62 | { | ||
63 | struct atexit *p = __atexit; | ||
64 | struct atexit_fn *fnp; | ||
65 | int pgsize = getpagesize(); | ||
66 | int ret = -1; | ||
67 | |||
68 | if (pgsize < sizeof(*p)) | ||
69 | return (-1); | ||
70 | _ATEXIT_LOCK(); | ||
71 | p = __atexit; | ||
72 | if (p != NULL) { | ||
73 | if (p->ind + 1 >= p->max) | ||
74 | p = NULL; | ||
75 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
76 | goto unlock; | ||
77 | } | ||
78 | if (p == NULL) { | ||
79 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
80 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
81 | if (p == MAP_FAILED) | ||
82 | goto unlock; | ||
83 | if (__atexit == NULL) { | ||
84 | memset(&p->fns[0], 0, sizeof(p->fns[0])); | ||
85 | p->ind = 1; | ||
86 | } else | ||
87 | p->ind = 0; | ||
88 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
89 | sizeof(p->fns[0]); | ||
90 | p->next = __atexit; | ||
91 | __atexit = p; | ||
92 | if (__atexit_invalid) | ||
93 | __atexit_invalid = 0; | ||
94 | } | ||
95 | fnp = &p->fns[p->ind++]; | ||
96 | fnp->fn_ptr.cxa_func = func; | ||
97 | fnp->fn_arg = arg; | ||
98 | fnp->fn_dso = dso; | ||
99 | if (mprotect(p, pgsize, PROT_READ)) | ||
100 | goto unlock; | ||
101 | ret = 0; | ||
102 | unlock: | ||
103 | _ATEXIT_UNLOCK(); | ||
104 | return (ret); | ||
105 | } | ||
106 | |||
107 | /* | ||
108 | * Register a function to be performed at exit. | ||
109 | */ | ||
110 | int | ||
111 | atexit(void (*func)(void)) | ||
112 | { | ||
113 | return (__cxa_atexit((void (*)(void *))func, NULL, NULL)); | ||
114 | } | ||
115 | |||
116 | /* | ||
117 | * Call all handlers registered with __cxa_atexit() for the shared | ||
118 | * object owning 'dso'. | ||
119 | * Note: if 'dso' is NULL, then all remaining handlers are called. | ||
120 | */ | ||
121 | void | ||
122 | __cxa_finalize(void *dso) | ||
123 | { | ||
124 | struct atexit *p, *q; | ||
125 | struct atexit_fn fn; | ||
126 | int n, pgsize = getpagesize(); | ||
127 | static int call_depth; | ||
128 | |||
129 | if (__atexit_invalid) | ||
130 | return; | ||
131 | |||
132 | call_depth++; | ||
133 | |||
134 | for (p = __atexit; p != NULL; p = p->next) { | ||
135 | for (n = p->ind; --n >= 0;) { | ||
136 | if (p->fns[n].fn_ptr.cxa_func == NULL) | ||
137 | continue; /* already called */ | ||
138 | if (dso != NULL && dso != p->fns[n].fn_dso) | ||
139 | continue; /* wrong DSO */ | ||
140 | |||
141 | /* | ||
142 | * Mark handler as having been already called to avoid | ||
143 | * dupes and loops, then call the appropriate function. | ||
144 | */ | ||
145 | fn = p->fns[n]; | ||
146 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) { | ||
147 | p->fns[n].fn_ptr.cxa_func = NULL; | ||
148 | mprotect(p, pgsize, PROT_READ); | ||
149 | } | ||
150 | if (dso != NULL) | ||
151 | (*fn.fn_ptr.cxa_func)(fn.fn_arg); | ||
152 | else | ||
153 | (*fn.fn_ptr.std_func)(); | ||
154 | } | ||
155 | } | ||
156 | |||
157 | /* | ||
158 | * If called via exit(), unmap the pages since we have now run | ||
159 | * all the handlers. We defer this until calldepth == 0 so that | ||
160 | * we don't unmap things prematurely if called recursively. | ||
161 | */ | ||
162 | if (dso == NULL && --call_depth == 0) { | ||
163 | for (p = __atexit; p != NULL; ) { | ||
164 | q = p; | ||
165 | p = p->next; | ||
166 | munmap(q, pgsize); | ||
167 | } | ||
168 | __atexit = NULL; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | /* | ||
173 | * Register the cleanup function | ||
174 | */ | ||
175 | void | ||
176 | __atexit_register_cleanup(void (*func)(void)) | ||
177 | { | ||
178 | struct atexit *p; | ||
179 | int pgsize = getpagesize(); | ||
180 | |||
181 | if (pgsize < sizeof(*p)) | ||
182 | return; | ||
183 | _ATEXIT_LOCK(); | ||
184 | p = __atexit; | ||
185 | while (p != NULL && p->next != NULL) | ||
186 | p = p->next; | ||
187 | if (p == NULL) { | ||
188 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
189 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
190 | if (p == MAP_FAILED) | ||
191 | goto unlock; | ||
192 | p->ind = 1; | ||
193 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
194 | sizeof(p->fns[0]); | ||
195 | p->next = NULL; | ||
196 | __atexit = p; | ||
197 | if (__atexit_invalid) | ||
198 | __atexit_invalid = 0; | ||
199 | } else { | ||
200 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
201 | goto unlock; | ||
202 | } | ||
203 | p->fns[0].fn_ptr.std_func = func; | ||
204 | p->fns[0].fn_arg = NULL; | ||
205 | p->fns[0].fn_dso = NULL; | ||
206 | mprotect(p, pgsize, PROT_READ); | ||
207 | unlock: | ||
208 | _ATEXIT_UNLOCK(); | ||
209 | } | ||
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h new file mode 100644 index 0000000000..1b23565dd0 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.h | |||
@@ -0,0 +1,51 @@ | |||
1 | /* $OpenBSD: atexit.h,v 1.7 2007/09/03 14:40:16 millert 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 | struct atexit_fn { | ||
38 | union { | ||
39 | void (*std_func)(void); | ||
40 | void (*cxa_func)(void *); | ||
41 | } fn_ptr; | ||
42 | void *fn_arg; /* argument for CXA callback */ | ||
43 | void *fn_dso; /* shared module handle */ | ||
44 | } fns[1]; /* the table itself */ | ||
45 | }; | ||
46 | |||
47 | extern int __atexit_invalid; | ||
48 | extern struct atexit *__atexit; /* points to head of LIFO stack */ | ||
49 | |||
50 | int __cxa_atexit(void (*)(void *), void *, void *); | ||
51 | void __cxa_finalize(void *); | ||
diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3 new file mode 100644 index 0000000000..2538d0c879 --- /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.5 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..d14b58b070 --- /dev/null +++ b/src/lib/libc/stdlib/atof.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* $OpenBSD: atof.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 The Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | |||
33 | double | ||
34 | atof(const char *ascii) | ||
35 | { | ||
36 | return(strtod(ascii, (char **)NULL)); | ||
37 | } | ||
diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3 new file mode 100644 index 0000000000..0c6b62b25f --- /dev/null +++ b/src/lib/libc/stdlib/atoi.3 | |||
@@ -0,0 +1,86 @@ | |||
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.9 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 strtonum 3 , | ||
65 | .Xr strtoul 3 | ||
66 | .Sh STANDARDS | ||
67 | The | ||
68 | .Fn atoi | ||
69 | function conforms to | ||
70 | .St -ansiC . | ||
71 | .Sh CAVEATS | ||
72 | .Nm | ||
73 | does no overflow checking, handles unsigned numbers poorly, | ||
74 | and handles strings containing trailing extra characters | ||
75 | (like | ||
76 | .Dq "123abc" ) | ||
77 | poorly. | ||
78 | Careful use of | ||
79 | .Xr strtol 3 | ||
80 | and | ||
81 | .Xr strtoul 3 | ||
82 | can alleviate these problems, | ||
83 | but | ||
84 | .Xr strtonum 3 | ||
85 | can be used to convert numbers from strings much more safely | ||
86 | and easily. | ||
diff --git a/src/lib/libc/stdlib/atoi.c b/src/lib/libc/stdlib/atoi.c new file mode 100644 index 0000000000..b0842678e2 --- /dev/null +++ b/src/lib/libc/stdlib/atoi.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* $OpenBSD: atoi.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | |||
33 | int | ||
34 | atoi(const char *str) | ||
35 | { | ||
36 | return((int)strtol(str, (char **)NULL, 10)); | ||
37 | } | ||
diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3 new file mode 100644 index 0000000000..3c5551182d --- /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.7 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..1970804401 --- /dev/null +++ b/src/lib/libc/stdlib/atol.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* $OpenBSD: atol.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | |||
33 | long | ||
34 | atol(const char *str) | ||
35 | { | ||
36 | return(strtol(str, (char **)NULL, 10)); | ||
37 | } | ||
diff --git a/src/lib/libc/stdlib/atoll.3 b/src/lib/libc/stdlib/atoll.3 new file mode 100644 index 0000000000..608a61f4ce --- /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.5 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 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..a65e682cfb --- /dev/null +++ b/src/lib/libc/stdlib/atoll.c | |||
@@ -0,0 +1,38 @@ | |||
1 | /* $OpenBSD: atoll.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | |||
33 | long long | ||
34 | atoll(str) | ||
35 | const char *str; | ||
36 | { | ||
37 | return(strtoll(str, (char **)NULL, 10)); | ||
38 | } | ||
diff --git a/src/lib/libc/stdlib/bsearch.3 b/src/lib/libc/stdlib/bsearch.3 new file mode 100644 index 0000000000..4877a1023e --- /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.7 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..8193d27c60 --- /dev/null +++ b/src/lib/libc/stdlib/bsearch.c | |||
@@ -0,0 +1,67 @@ | |||
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 | #include <stdlib.h> | ||
31 | |||
32 | /* | ||
33 | * Perform a binary search. | ||
34 | * | ||
35 | * The code below is a bit sneaky. After a comparison fails, we | ||
36 | * divide the work in half by moving either left or right. If lim | ||
37 | * is odd, moving left simply involves halving lim: e.g., when lim | ||
38 | * is 5 we look at item 2, so we change lim to 2 so that we will | ||
39 | * look at items 0 & 1. If lim is even, the same applies. If lim | ||
40 | * is odd, moving right again involes halving lim, this time moving | ||
41 | * the base up one item past p: e.g., when lim is 5 we change base | ||
42 | * to item 3 and make lim 2 so that we will look at items 3 and 4. | ||
43 | * If lim is even, however, we have to shrink it by one before | ||
44 | * halving: e.g., when lim is 4, we still looked at item 2, so we | ||
45 | * have to make lim 3, then halve, obtaining 1, so that we will only | ||
46 | * look at item 3. | ||
47 | */ | ||
48 | void * | ||
49 | bsearch(const void *key, const void *base0, size_t nmemb, size_t size, | ||
50 | int (*compar)(const void *, const void *)) | ||
51 | { | ||
52 | const char *base = base0; | ||
53 | int lim, cmp; | ||
54 | const void *p; | ||
55 | |||
56 | for (lim = nmemb; lim != 0; lim >>= 1) { | ||
57 | p = base + (lim >> 1) * size; | ||
58 | cmp = (*compar)(key, p); | ||
59 | if (cmp == 0) | ||
60 | return ((void *)p); | ||
61 | if (cmp > 0) { /* key > p: move right */ | ||
62 | base = (char *)p + size; | ||
63 | lim--; | ||
64 | } /* else move left */ | ||
65 | } | ||
66 | return (NULL); | ||
67 | } | ||
diff --git a/src/lib/libc/stdlib/cfree.c b/src/lib/libc/stdlib/cfree.c new file mode 100644 index 0000000000..373c7ff75d --- /dev/null +++ b/src/lib/libc/stdlib/cfree.c | |||
@@ -0,0 +1,40 @@ | |||
1 | /* $OpenBSD: cfree.c,v 1.5 2005/08/08 08:05:36 espie 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 | #include <sys/cdefs.h> | ||
29 | #include <stdlib.h> | ||
30 | |||
31 | #ifdef __indr_reference | ||
32 | __indr_reference(free, cfree); | ||
33 | #else | ||
34 | |||
35 | void | ||
36 | cfree(void *p) | ||
37 | { | ||
38 | free(p); | ||
39 | } | ||
40 | #endif | ||
diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3 new file mode 100644 index 0000000000..4a48ab1ea4 --- /dev/null +++ b/src/lib/libc/stdlib/div.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. | ||
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.9 2007/05/31 19:19:31 jmc Exp $ | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
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 / 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 imaxdiv 3 , | ||
57 | .Xr ldiv 3 , | ||
58 | .Xr lldiv 3 , | ||
59 | .Xr math 3 , | ||
60 | .Xr qdiv 3 | ||
61 | .Sh STANDARDS | ||
62 | The | ||
63 | .Fn div | ||
64 | function conforms to | ||
65 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c new file mode 100644 index 0000000000..f7ac2db4b0 --- /dev/null +++ b/src/lib/libc/stdlib/div.c | |||
@@ -0,0 +1,71 @@ | |||
1 | /* $OpenBSD: div.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1990 Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <stdlib.h> /* div_t */ | ||
35 | |||
36 | div_t | ||
37 | div(int num, int denom) | ||
38 | { | ||
39 | div_t r; | ||
40 | |||
41 | r.quot = num / denom; | ||
42 | r.rem = num % denom; | ||
43 | /* | ||
44 | * The ANSI standard says that |r.quot| <= |n/d|, where | ||
45 | * n/d is to be computed in infinite precision. In other | ||
46 | * words, we should always truncate the quotient towards | ||
47 | * 0, never -infinity. | ||
48 | * | ||
49 | * Machine division and remainer may work either way when | ||
50 | * one or both of n or d is negative. If only one is | ||
51 | * negative and r.quot has been truncated towards -inf, | ||
52 | * r.rem will have the same sign as denom and the opposite | ||
53 | * sign of num; if both are negative and r.quot has been | ||
54 | * truncated towards -inf, r.rem will be positive (will | ||
55 | * have the opposite sign of num). These are considered | ||
56 | * `wrong'. | ||
57 | * | ||
58 | * If both are num and denom are positive, r will always | ||
59 | * be positive. | ||
60 | * | ||
61 | * This all boils down to: | ||
62 | * if num >= 0, but r.rem < 0, we got the wrong answer. | ||
63 | * In that case, to get the right answer, add 1 to r.quot and | ||
64 | * subtract denom from r.rem. | ||
65 | */ | ||
66 | if (num >= 0 && r.rem < 0) { | ||
67 | r.quot++; | ||
68 | r.rem -= denom; | ||
69 | } | ||
70 | return (r); | ||
71 | } | ||
diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c new file mode 100644 index 0000000000..b6c046c831 --- /dev/null +++ b/src/lib/libc/stdlib/drand48.c | |||
@@ -0,0 +1,23 @@ | |||
1 | /* $OpenBSD: drand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | extern unsigned short __rand48_seed[3]; | ||
18 | |||
19 | double | ||
20 | drand48(void) | ||
21 | { | ||
22 | return erand48(__rand48_seed); | ||
23 | } | ||
diff --git a/src/lib/libc/stdlib/ecvt.3 b/src/lib/libc/stdlib/ecvt.3 new file mode 100644 index 0000000000..0717a2c02a --- /dev/null +++ b/src/lib/libc/stdlib/ecvt.3 | |||
@@ -0,0 +1,165 @@ | |||
1 | .\" $OpenBSD: ecvt.3,v 1.8 2007/05/31 19:19:31 jmc 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 $Mdocdate: May 31 2007 $ | ||
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-2001 . | ||
diff --git a/src/lib/libc/stdlib/ecvt.c b/src/lib/libc/stdlib/ecvt.c new file mode 100644 index 0000000000..719370a8f3 --- /dev/null +++ b/src/lib/libc/stdlib/ecvt.c | |||
@@ -0,0 +1,105 @@ | |||
1 | /* $OpenBSD: ecvt.c,v 1.6 2006/10/29 18:45:56 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2002, 2006 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 | #include <stdio.h> | ||
24 | #include <stdlib.h> | ||
25 | #include <string.h> | ||
26 | |||
27 | extern char *__dtoa(double, int, int, int *, int *, char **); | ||
28 | extern void __freedtoa(char *); | ||
29 | static char *__cvt(double, int, int *, int *, int, int); | ||
30 | |||
31 | static char * | ||
32 | __cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad) | ||
33 | { | ||
34 | static char *s; | ||
35 | char *p, *rve, c; | ||
36 | size_t siz; | ||
37 | |||
38 | if (ndigit == 0) { | ||
39 | *sign = value < 0.0; | ||
40 | *decpt = 0; | ||
41 | return (""); | ||
42 | } | ||
43 | |||
44 | if (s) { | ||
45 | free(s); | ||
46 | s = NULL; | ||
47 | } | ||
48 | |||
49 | if (ndigit < 0) | ||
50 | siz = -ndigit + 1; | ||
51 | else | ||
52 | siz = ndigit + 1; | ||
53 | |||
54 | |||
55 | /* __dtoa() doesn't allocate space for 0 so we do it by hand */ | ||
56 | if (value == 0.0) { | ||
57 | *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */ | ||
58 | *sign = 0; | ||
59 | if ((rve = s = (char *)malloc(siz)) == NULL) | ||
60 | return(NULL); | ||
61 | *rve++ = '0'; | ||
62 | *rve = '\0'; | ||
63 | } else { | ||
64 | p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve); | ||
65 | if (*decpt == 9999) { | ||
66 | /* Infinity or Nan, convert to inf or nan like printf */ | ||
67 | *decpt = 0; | ||
68 | c = *p; | ||
69 | __freedtoa(p); | ||
70 | return(c == 'I' ? "inf" : "nan"); | ||
71 | } | ||
72 | /* Make a local copy and adjust rve to be in terms of s */ | ||
73 | if (pad && fmode) | ||
74 | siz += *decpt; | ||
75 | if ((s = (char *)malloc(siz)) == NULL) { | ||
76 | __freedtoa(p); | ||
77 | return(NULL); | ||
78 | } | ||
79 | (void) strlcpy(s, p, siz); | ||
80 | rve = s + (rve - p); | ||
81 | __freedtoa(p); | ||
82 | } | ||
83 | |||
84 | /* Add trailing zeros */ | ||
85 | if (pad) { | ||
86 | siz -= rve - s; | ||
87 | while (--siz) | ||
88 | *rve++ = '0'; | ||
89 | *rve = '\0'; | ||
90 | } | ||
91 | |||
92 | return(s); | ||
93 | } | ||
94 | |||
95 | char * | ||
96 | ecvt(double value, int ndigit, int *decpt, int *sign) | ||
97 | { | ||
98 | return(__cvt(value, ndigit, decpt, sign, 0, 1)); | ||
99 | } | ||
100 | |||
101 | char * | ||
102 | fcvt(double value, int ndigit, int *decpt, int *sign) | ||
103 | { | ||
104 | return(__cvt(value, ndigit, decpt, sign, 1, 1)); | ||
105 | } | ||
diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c new file mode 100644 index 0000000000..2ffeaa6e71 --- /dev/null +++ b/src/lib/libc/stdlib/erand48.c | |||
@@ -0,0 +1,24 @@ | |||
1 | /* $OpenBSD: erand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | double | ||
18 | erand48(unsigned short xseed[3]) | ||
19 | { | ||
20 | __dorand48(xseed); | ||
21 | return ldexp((double) xseed[0], -48) + | ||
22 | ldexp((double) xseed[1], -32) + | ||
23 | ldexp((double) xseed[2], -16); | ||
24 | } | ||
diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3 new file mode 100644 index 0000000000..45eae80a08 --- /dev/null +++ b/src/lib/libc/stdlib/exit.3 | |||
@@ -0,0 +1,114 @@ | |||
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.11 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
35 | .Dt EXIT 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm exit, _Exit | ||
39 | .Nd perform normal program termination | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft void | ||
43 | .Fn exit "int status" | ||
44 | .Ft void | ||
45 | .Fn _Exit "int status" | ||
46 | .Sh DESCRIPTION | ||
47 | The | ||
48 | .Fn exit | ||
49 | and | ||
50 | .Fn _Exit | ||
51 | functions terminate a process. | ||
52 | .Pp | ||
53 | Before termination, | ||
54 | .Fn exit | ||
55 | performs the following operations in the order listed: | ||
56 | .Bl -enum -offset indent | ||
57 | .It | ||
58 | Call the functions registered with the | ||
59 | .Xr atexit 3 | ||
60 | function, in the reverse order of their registration. | ||
61 | .It | ||
62 | Flush all open output streams. | ||
63 | .It | ||
64 | Close all open streams. | ||
65 | .It | ||
66 | Unlink all files created with the | ||
67 | .Xr tmpfile 3 | ||
68 | function. | ||
69 | .El | ||
70 | .Pp | ||
71 | The | ||
72 | .Fn _Exit | ||
73 | function terminates without calling the functions registered with the | ||
74 | .Xr atexit 3 | ||
75 | function. | ||
76 | The | ||
77 | .Ox | ||
78 | implementation of | ||
79 | .Fn _Exit | ||
80 | does not flush open output streams or unlink files created with the | ||
81 | .Xr tmpfile 3 | ||
82 | function. | ||
83 | However, this behavior is implementation-specific. | ||
84 | .Pp | ||
85 | Lastly, | ||
86 | .Fn exit | ||
87 | and | ||
88 | .Fn _Exit | ||
89 | call | ||
90 | .Xr _exit 2 . | ||
91 | Note that typically | ||
92 | .Xr _exit 2 | ||
93 | only passes the lower 8 bits of | ||
94 | .Fa status | ||
95 | on to the parent, thus negative values have less meaning. | ||
96 | .Sh RETURN VALUES | ||
97 | The | ||
98 | .Fn exit | ||
99 | and | ||
100 | .Fn _Exit | ||
101 | functions never return. | ||
102 | .Sh SEE ALSO | ||
103 | .Xr _exit 2 , | ||
104 | .Xr atexit 3 , | ||
105 | .Xr intro 3 , | ||
106 | .Xr sysexits 3 , | ||
107 | .Xr tmpfile 3 | ||
108 | .Sh STANDARDS | ||
109 | The | ||
110 | .Fn exit | ||
111 | and | ||
112 | .Fn _Exit | ||
113 | functions conform to | ||
114 | .St -ansiC-99 . | ||
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c new file mode 100644 index 0000000000..83fe3d2de5 --- /dev/null +++ b/src/lib/libc/stdlib/exit.c | |||
@@ -0,0 +1,59 @@ | |||
1 | /* $OpenBSD: exit.c,v 1.12 2007/09/03 14:40:16 millert Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
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. 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 | #include <sys/types.h> | ||
32 | #include <sys/mman.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <unistd.h> | ||
35 | #include "atexit.h" | ||
36 | #include "thread_private.h" | ||
37 | |||
38 | /* | ||
39 | * This variable is zero until a process has created a thread. | ||
40 | * It is used to avoid calling locking functions in libc when they | ||
41 | * are not required. By default, libc is intended to be(come) | ||
42 | * thread-safe, but without a (significant) penalty to non-threaded | ||
43 | * processes. | ||
44 | */ | ||
45 | int __isthreaded = 0; | ||
46 | |||
47 | /* | ||
48 | * Exit, flushing stdio buffers if necessary. | ||
49 | */ | ||
50 | void | ||
51 | exit(int status) | ||
52 | { | ||
53 | /* | ||
54 | * Call functions registered by atexit() or _cxa_atexit() | ||
55 | * (including the stdio cleanup routine) and then _exit(). | ||
56 | */ | ||
57 | __cxa_finalize(NULL); | ||
58 | _exit(status); | ||
59 | } | ||
diff --git a/src/lib/libc/stdlib/gcvt.c b/src/lib/libc/stdlib/gcvt.c new file mode 100644 index 0000000000..c24157e465 --- /dev/null +++ b/src/lib/libc/stdlib/gcvt.c | |||
@@ -0,0 +1,111 @@ | |||
1 | /* $OpenBSD: gcvt.c,v 1.10 2006/10/29 18:45:56 deraadt Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2002, 2003, 2006 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 | #include <locale.h> | ||
24 | #include <stdio.h> | ||
25 | #include <stdlib.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | extern char *__dtoa(double, int, int, int *, int *, char **); | ||
29 | extern void __freedtoa(char *); | ||
30 | |||
31 | char * | ||
32 | gcvt(double value, int ndigit, char *buf) | ||
33 | { | ||
34 | char *digits, *dst, *src; | ||
35 | int i, decpt, sign; | ||
36 | struct lconv *lconv; | ||
37 | |||
38 | lconv = localeconv(); | ||
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 | /* | ||
47 | * Infinity or NaN, convert to inf or nan with sign. | ||
48 | * We assume the buffer is at least ndigit long. | ||
49 | */ | ||
50 | snprintf(buf, ndigit + 1, "%s%s", sign ? "-" : "", | ||
51 | *digits == 'I' ? "inf" : "nan"); | ||
52 | __freedtoa(digits); | ||
53 | return (buf); | ||
54 | } | ||
55 | |||
56 | dst = buf; | ||
57 | if (sign) | ||
58 | *dst++ = '-'; | ||
59 | |||
60 | if (decpt < 0 || decpt > ndigit) { | ||
61 | /* exponential format (e.g. 1.2345e+13) */ | ||
62 | if (--decpt < 0) { | ||
63 | sign = 1; | ||
64 | decpt = -decpt; | ||
65 | } else | ||
66 | sign = 0; | ||
67 | src = digits; | ||
68 | *dst++ = *src++; | ||
69 | *dst++ = *lconv->decimal_point; | ||
70 | while (*src != '\0') | ||
71 | *dst++ = *src++; | ||
72 | *dst++ = 'e'; | ||
73 | if (sign) | ||
74 | *dst++ = '-'; | ||
75 | else | ||
76 | *dst++ = '+'; | ||
77 | if (decpt < 10) { | ||
78 | *dst++ = '0'; | ||
79 | *dst++ = '0' + decpt; | ||
80 | *dst = '\0'; | ||
81 | } else { | ||
82 | /* XXX - optimize */ | ||
83 | for (sign = decpt, i = 0; (sign /= 10) != 0; i++) | ||
84 | continue; | ||
85 | dst[i + 1] = '\0'; | ||
86 | while (decpt != 0) { | ||
87 | dst[i--] = '0' + decpt % 10; | ||
88 | decpt /= 10; | ||
89 | } | ||
90 | } | ||
91 | } else { | ||
92 | /* standard format */ | ||
93 | for (i = 0, src = digits; i < decpt; i++) { | ||
94 | if (*src != '\0') | ||
95 | *dst++ = *src++; | ||
96 | else | ||
97 | *dst++ = '0'; | ||
98 | } | ||
99 | if (*src != '\0') { | ||
100 | if (src == digits) | ||
101 | *dst++ = '0'; /* zero before decimal point */ | ||
102 | *dst++ = *lconv->decimal_point; | ||
103 | for (i = decpt; digits[i] != '\0'; i++) { | ||
104 | *dst++ = digits[i]; | ||
105 | } | ||
106 | } | ||
107 | *dst = '\0'; | ||
108 | } | ||
109 | __freedtoa(digits); | ||
110 | return (buf); | ||
111 | } | ||
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3 new file mode 100644 index 0000000000..1d8546997f --- /dev/null +++ b/src/lib/libc/stdlib/getenv.3 | |||
@@ -0,0 +1,148 @@ | |||
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.12 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 = 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 Er | ||
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 function | ||
134 | .Fn getenv | ||
135 | appeared in | ||
136 | .At v7 | ||
137 | and | ||
138 | .Bx 3 . | ||
139 | The functions | ||
140 | .Fn setenv | ||
141 | and | ||
142 | .Fn unsetenv | ||
143 | appeared in | ||
144 | .Bx 4.3 Tahoe . | ||
145 | The | ||
146 | .Fn putenv | ||
147 | function appeared in | ||
148 | .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..72367b34e2 --- /dev/null +++ b/src/lib/libc/stdlib/getenv.c | |||
@@ -0,0 +1,80 @@ | |||
1 | /* $OpenBSD: getenv.c,v 1.8 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1987, 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 | |||
31 | #include <stdlib.h> | ||
32 | #include <string.h> | ||
33 | |||
34 | char *__findenv(const char *name, int *offset); | ||
35 | |||
36 | /* | ||
37 | * __findenv -- | ||
38 | * Returns pointer to value associated with name, if any, else NULL. | ||
39 | * Sets offset to be the offset of the name/value combination in the | ||
40 | * environmental array, for use by setenv(3) and unsetenv(3). | ||
41 | * Explicitly removes '=' in argument name. | ||
42 | * | ||
43 | * This routine *should* be a static; don't use it. | ||
44 | */ | ||
45 | char * | ||
46 | __findenv(const char *name, int *offset) | ||
47 | { | ||
48 | extern char **environ; | ||
49 | int len, i; | ||
50 | const char *np; | ||
51 | char **p, *cp; | ||
52 | |||
53 | if (name == NULL || environ == NULL) | ||
54 | return (NULL); | ||
55 | for (np = name; *np && *np != '='; ++np) | ||
56 | ; | ||
57 | len = np - name; | ||
58 | for (p = environ; (cp = *p) != NULL; ++p) { | ||
59 | for (np = name, i = len; i && *cp; i--) | ||
60 | if (*cp++ != *np++) | ||
61 | break; | ||
62 | if (i == 0 && *cp++ == '=') { | ||
63 | *offset = p - environ; | ||
64 | return (cp); | ||
65 | } | ||
66 | } | ||
67 | return (NULL); | ||
68 | } | ||
69 | |||
70 | /* | ||
71 | * getenv -- | ||
72 | * Returns ptr to value associated with name, if any, else NULL. | ||
73 | */ | ||
74 | char * | ||
75 | getenv(const char *name) | ||
76 | { | ||
77 | int offset; | ||
78 | |||
79 | return (__findenv(name, &offset)); | ||
80 | } | ||
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 new file mode 100644 index 0000000000..06167cb7d6 --- /dev/null +++ b/src/lib/libc/stdlib/getopt.3 | |||
@@ -0,0 +1,371 @@ | |||
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.40 2007/05/31 19:19:31 jmc Exp $ | ||
29 | .\" | ||
30 | .Dd $Mdocdate: May 31 2007 $ | ||
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 opterr; | ||
40 | .Vt extern int optind; | ||
41 | .Vt extern int optopt; | ||
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 POSIX. | ||
66 | For example, an option string | ||
67 | .Qq x | ||
68 | recognizes an option | ||
69 | .Fl x , | ||
70 | and an option string | ||
71 | .Qq Li x: | ||
72 | recognizes an option and argument | ||
73 | .Fl x Ar argument . | ||
74 | It does not matter to | ||
75 | .Fn getopt | ||
76 | if a following argument has leading whitespace. | ||
77 | .Pp | ||
78 | On return from | ||
79 | .Fn getopt , | ||
80 | .Va optarg | ||
81 | points to an option argument, if it is anticipated, | ||
82 | and the variable | ||
83 | .Va optind | ||
84 | contains the index to the next | ||
85 | .Fa argv | ||
86 | argument for a subsequent call | ||
87 | to | ||
88 | .Fn getopt . | ||
89 | .Pp | ||
90 | The variables | ||
91 | .Va opterr | ||
92 | and | ||
93 | .Va optind | ||
94 | are both initialized to 1. | ||
95 | The | ||
96 | .Va optind | ||
97 | variable may be set to another value larger than 0 before a set of calls to | ||
98 | .Fn getopt | ||
99 | in order to skip over more or less | ||
100 | .Fa argv | ||
101 | entries. | ||
102 | An | ||
103 | .Va optind | ||
104 | value of 0 is reserved for compatibility with GNU | ||
105 | .Fn getopt . | ||
106 | .Pp | ||
107 | In order to use | ||
108 | .Fn getopt | ||
109 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
110 | arguments multiple times, | ||
111 | the variable | ||
112 | .Va optreset | ||
113 | must be set to 1 before the second and each additional set of calls to | ||
114 | .Fn getopt , | ||
115 | and the variable | ||
116 | .Va optind | ||
117 | must be reinitialized. | ||
118 | .Pp | ||
119 | The | ||
120 | .Fn getopt | ||
121 | function returns \-1 when the argument list is exhausted. | ||
122 | The interpretation of options in the argument list may be cancelled | ||
123 | by the option | ||
124 | .Ql -- | ||
125 | (double dash) which causes | ||
126 | .Fn getopt | ||
127 | to signal the end of argument processing and return \-1. | ||
128 | When all options have been processed (i.e., up to the first non-option | ||
129 | argument), | ||
130 | .Fn getopt | ||
131 | returns \-1. | ||
132 | .Sh RETURN VALUES | ||
133 | The | ||
134 | .Fn getopt | ||
135 | function returns the next known option character in | ||
136 | .Fa optstring . | ||
137 | If | ||
138 | .Fn getopt | ||
139 | encounters a character not found in | ||
140 | .Fa optstring | ||
141 | or if it detects a missing option argument, | ||
142 | it returns | ||
143 | .Sq \&? | ||
144 | (question mark). | ||
145 | If | ||
146 | .Fa optstring | ||
147 | has a leading | ||
148 | .Sq \&: | ||
149 | then a missing option argument causes | ||
150 | .Sq \&: | ||
151 | to be returned instead of | ||
152 | .Sq \&? . | ||
153 | In either case, the variable | ||
154 | .Va optopt | ||
155 | is set to the character that caused the error. | ||
156 | The | ||
157 | .Fn getopt | ||
158 | function returns \-1 when the argument list is exhausted. | ||
159 | .Sh ENVIRONMENT | ||
160 | .Bl -tag -width POSIXLY_CORRECTXX | ||
161 | .It Ev POSIXLY_CORRECT | ||
162 | If set, a leading | ||
163 | .Sq - | ||
164 | in | ||
165 | .Ar optstring | ||
166 | is ignored. | ||
167 | .El | ||
168 | .Sh EXAMPLES | ||
169 | The following code accepts the options | ||
170 | .Fl b | ||
171 | and | ||
172 | .Fl f Ar argument | ||
173 | and adjusts | ||
174 | .Va argc | ||
175 | and | ||
176 | .Va argv | ||
177 | after option argument processing has completed. | ||
178 | .Bd -literal -offset indent | ||
179 | int bflag, ch, fd; | ||
180 | |||
181 | bflag = 0; | ||
182 | while ((ch = getopt(argc, argv, "bf:")) != -1) { | ||
183 | switch (ch) { | ||
184 | case 'b': | ||
185 | bflag = 1; | ||
186 | break; | ||
187 | case 'f': | ||
188 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
189 | err(1, "%s", optarg); | ||
190 | break; | ||
191 | default: | ||
192 | usage(); | ||
193 | /* NOTREACHED */ | ||
194 | } | ||
195 | } | ||
196 | argc -= optind; | ||
197 | argv += optind; | ||
198 | .Ed | ||
199 | .Sh DIAGNOSTICS | ||
200 | If the | ||
201 | .Fn getopt | ||
202 | function encounters a character not found in the string | ||
203 | .Fa optstring | ||
204 | or detects | ||
205 | a missing option argument, it writes an error message to | ||
206 | .Em stderr | ||
207 | and returns | ||
208 | .Ql \&? . | ||
209 | Setting | ||
210 | .Va opterr | ||
211 | to a zero will disable these error messages. | ||
212 | If | ||
213 | .Fa optstring | ||
214 | has a leading | ||
215 | .Ql \&: | ||
216 | then a missing option argument causes a | ||
217 | .Ql \&: | ||
218 | to be returned in addition to suppressing any error messages. | ||
219 | .Pp | ||
220 | Option arguments are allowed to begin with | ||
221 | .Ql - ; | ||
222 | this is reasonable but reduces the amount of error checking possible. | ||
223 | .Sh SEE ALSO | ||
224 | .Xr getopt 1 , | ||
225 | .Xr getopt_long 3 , | ||
226 | .Xr getsubopt 3 | ||
227 | .Sh STANDARDS | ||
228 | The | ||
229 | .Fn getopt | ||
230 | function implements a superset of the functionality specified by | ||
231 | .St -p1003.1 . | ||
232 | .Pp | ||
233 | The following extensions are supported: | ||
234 | .Bl -tag -width "xxx" | ||
235 | .It Li o | ||
236 | The | ||
237 | .Va optreset | ||
238 | variable was added to make it possible to call the | ||
239 | .Fn getopt | ||
240 | function multiple times. | ||
241 | .It Li o | ||
242 | If the | ||
243 | .Va optind | ||
244 | variable is set to 0, | ||
245 | .Fn getopt | ||
246 | will behave as if the | ||
247 | .Va optreset | ||
248 | variable has been set. | ||
249 | This is for compatibility with | ||
250 | .Tn GNU | ||
251 | .Fn getopt . | ||
252 | New code should use | ||
253 | .Va optreset | ||
254 | instead. | ||
255 | .It Li o | ||
256 | If the first character of | ||
257 | .Fa optstring | ||
258 | is a plus sign | ||
259 | .Pq Ql + , | ||
260 | it will be ignored. | ||
261 | This is for compatibility with | ||
262 | .Tn GNU | ||
263 | .Fn getopt . | ||
264 | .It Li o | ||
265 | If the first character of | ||
266 | .Fa optstring | ||
267 | is a dash | ||
268 | .Pq Ql - , | ||
269 | non-options will be returned as arguments to the option character | ||
270 | .Ql \e1 . | ||
271 | This is for compatibility with | ||
272 | .Tn GNU | ||
273 | .Fn getopt . | ||
274 | .It Li o | ||
275 | A single dash | ||
276 | .Pq Ql - | ||
277 | may be specified as a character in | ||
278 | .Fa optstring , | ||
279 | however it should | ||
280 | .Em never | ||
281 | have an argument associated with it. | ||
282 | This allows | ||
283 | .Fn getopt | ||
284 | to be used with programs that expect | ||
285 | .Ql - | ||
286 | as an option flag. | ||
287 | This practice is wrong, and should not be used in any current development. | ||
288 | It is provided for backward compatibility | ||
289 | .Em only . | ||
290 | Care should be taken not to use | ||
291 | .Ql - | ||
292 | as the first character in | ||
293 | .Fa optstring | ||
294 | to avoid a semantic conflict with | ||
295 | .Tn GNU | ||
296 | .Fn getopt | ||
297 | semantics (see above). | ||
298 | By default, a single dash causes | ||
299 | .Fn getopt | ||
300 | to return \-1. | ||
301 | .El | ||
302 | .Pp | ||
303 | Historic | ||
304 | .Bx | ||
305 | versions of | ||
306 | .Fn getopt | ||
307 | set | ||
308 | .Fa optopt | ||
309 | to the last option character processed. | ||
310 | However, this conflicts with | ||
311 | .St -p1003.1 | ||
312 | which stipulates that | ||
313 | .Fa optopt | ||
314 | be set to the last character that caused an error. | ||
315 | .Sh HISTORY | ||
316 | The | ||
317 | .Fn getopt | ||
318 | function appeared in | ||
319 | .Bx 4.3 . | ||
320 | .Sh BUGS | ||
321 | The | ||
322 | .Fn getopt | ||
323 | function was once specified to return | ||
324 | .Dv EOF | ||
325 | instead of \-1. | ||
326 | This was changed by | ||
327 | .St -p1003.2-92 | ||
328 | to decouple | ||
329 | .Fn getopt | ||
330 | from | ||
331 | .Aq Pa stdio.h . | ||
332 | .Pp | ||
333 | It is possible to handle digits as option letters. | ||
334 | This allows | ||
335 | .Fn getopt | ||
336 | to be used with programs that expect a number | ||
337 | .Pq Dq Li \-3 | ||
338 | as an option. | ||
339 | This practice is wrong, and should not be used in any current development. | ||
340 | It is provided for backward compatibility | ||
341 | .Em only . | ||
342 | The following code fragment works in most cases and can handle mixed | ||
343 | number and letter arguments. | ||
344 | .Bd -literal -offset indent | ||
345 | int aflag = 0, bflag = 0, ch, lastch = '\e0'; | ||
346 | int length = -1, newarg = 1, prevoptind = 1; | ||
347 | |||
348 | while ((ch = getopt(argc, argv, "0123456789ab")) != -1) { | ||
349 | switch (ch) { | ||
350 | case '0': case '1': case '2': case '3': case '4': | ||
351 | case '5': case '6': case '7': case '8': case '9': | ||
352 | if (newarg || !isdigit(lastch)) | ||
353 | length = 0; | ||
354 | else if (length > INT_MAX / 10) | ||
355 | usage(); | ||
356 | length = (length * 10) + (ch - '0'); | ||
357 | break; | ||
358 | case 'a': | ||
359 | aflag = 1; | ||
360 | break; | ||
361 | case 'b': | ||
362 | bflag = 1; | ||
363 | break; | ||
364 | default: | ||
365 | usage(); | ||
366 | } | ||
367 | lastch = ch; | ||
368 | newarg = optind != prevoptind; | ||
369 | prevoptind = optind; | ||
370 | } | ||
371 | .Ed | ||
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3 new file mode 100644 index 0000000000..2bab963734 --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.3 | |||
@@ -0,0 +1,464 @@ | |||
1 | .\" $OpenBSD: getopt_long.3,v 1.15 2007/07/03 12:06:07 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 $Mdocdate: July 3 2007 $ | ||
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 *longindex" | ||
49 | .Ft int | ||
50 | .Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex" | ||
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 -offset indent | ||
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 | Abbreviated long option names are accepted when | ||
97 | .Fn getopt_long | ||
98 | processes long options if the abbreviation is unique. | ||
99 | An exact match is always preferred for a defined long option. | ||
100 | .Pp | ||
101 | The | ||
102 | .Fn getopt_long | ||
103 | call requires a structure to be initialized describing the long | ||
104 | options. | ||
105 | The structure is: | ||
106 | .Bd -literal -offset indent | ||
107 | struct option { | ||
108 | char *name; | ||
109 | int has_arg; | ||
110 | int *flag; | ||
111 | int val; | ||
112 | }; | ||
113 | .Ed | ||
114 | .Pp | ||
115 | The | ||
116 | .Fa name | ||
117 | field should contain the option name without the leading double dash. | ||
118 | .Pp | ||
119 | The | ||
120 | .Fa has_arg | ||
121 | field should be one of: | ||
122 | .Pp | ||
123 | .Bl -tag -width "optional_argument" -compact -offset indent | ||
124 | .It Dv no_argument | ||
125 | no argument to the option is expected. | ||
126 | .It Dv required_argument | ||
127 | an argument to the option is required. | ||
128 | .It Dv optional_argument | ||
129 | an argument to the option may be presented. | ||
130 | .El | ||
131 | .Pp | ||
132 | If | ||
133 | .Fa flag | ||
134 | is not | ||
135 | .Dv NULL , | ||
136 | then the integer pointed to by it will be set to the value in the | ||
137 | .Fa val | ||
138 | field. | ||
139 | If the | ||
140 | .Fa flag | ||
141 | field is | ||
142 | .Dv NULL , | ||
143 | then the | ||
144 | .Fa val | ||
145 | field will be returned. | ||
146 | Setting | ||
147 | .Fa flag | ||
148 | to | ||
149 | .Dv NULL | ||
150 | and setting | ||
151 | .Fa val | ||
152 | to the corresponding short option will make this function act just | ||
153 | like | ||
154 | .Xr getopt 3 . | ||
155 | .Pp | ||
156 | If the | ||
157 | .Fa longindex | ||
158 | field is not | ||
159 | .Dv NULL , | ||
160 | then the integer pointed to by it will be set to the index of the long | ||
161 | option relative to | ||
162 | .Fa longopts . | ||
163 | .Pp | ||
164 | The last element of the | ||
165 | .Fa longopts | ||
166 | array has to be filled with zeroes. | ||
167 | .Pp | ||
168 | The | ||
169 | .Fn getopt_long_only | ||
170 | function behaves identically to | ||
171 | .Fn getopt_long | ||
172 | with the exception that long options may start with | ||
173 | .Sq - | ||
174 | in addition to | ||
175 | .Sq -- . | ||
176 | If an option starting with | ||
177 | .Sq - | ||
178 | does not match a long option but does match a single-character option, | ||
179 | the single-character option is returned. | ||
180 | .Sh RETURN VALUES | ||
181 | If the | ||
182 | .Fa flag | ||
183 | field in | ||
184 | .Li struct option | ||
185 | is | ||
186 | .Dv NULL , | ||
187 | .Fn getopt_long | ||
188 | and | ||
189 | .Fn getopt_long_only | ||
190 | return the value specified in the | ||
191 | .Fa val | ||
192 | field, which is usually just the corresponding short option. | ||
193 | If | ||
194 | .Fa flag | ||
195 | is not | ||
196 | .Dv NULL , | ||
197 | these functions return 0 and store | ||
198 | .Fa val | ||
199 | in the location pointed to by | ||
200 | .Fa flag . | ||
201 | These functions return | ||
202 | .Sq \: | ||
203 | if there was a missing option argument, | ||
204 | .Sq \&? | ||
205 | if the user specified an unknown or ambiguous option, and | ||
206 | \-1 when the argument list has been exhausted. | ||
207 | .Sh EXAMPLES | ||
208 | .Bd -literal | ||
209 | int bflag, ch, fd; | ||
210 | int daggerset; | ||
211 | |||
212 | /* options descriptor */ | ||
213 | static struct option longopts[] = { | ||
214 | { "buffy", no_argument, NULL, 'b' }, | ||
215 | { "fluoride", required_argument, NULL, 'f' }, | ||
216 | { "daggerset", no_argument, &daggerset, 1 }, | ||
217 | { NULL, 0, NULL, 0 } | ||
218 | }; | ||
219 | |||
220 | bflag = 0; | ||
221 | while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) | ||
222 | switch (ch) { | ||
223 | case 'b': | ||
224 | bflag = 1; | ||
225 | break; | ||
226 | case 'f': | ||
227 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
228 | err(1, "unable to open %s", optarg); | ||
229 | break; | ||
230 | case 0: | ||
231 | if (daggerset) | ||
232 | fprintf(stderr, "Buffy will use her dagger to " | ||
233 | "apply fluoride to dracula's teeth\en"); | ||
234 | break; | ||
235 | default: | ||
236 | usage(); | ||
237 | /* NOTREACHED */ | ||
238 | } | ||
239 | argc -= optind; | ||
240 | argv += optind; | ||
241 | .Ed | ||
242 | .Sh IMPLEMENTATION DIFFERENCES | ||
243 | This section describes differences to the GNU implementation | ||
244 | found in glibc-2.1.3: | ||
245 | .Bl -bullet | ||
246 | .It | ||
247 | handling of | ||
248 | .Ql - | ||
249 | as the first character of the option string in the presence of the | ||
250 | environment variable | ||
251 | .Ev POSIXLY_CORRECT : | ||
252 | .Bl -tag -width "OpenBSD" | ||
253 | .It GNU | ||
254 | ignores | ||
255 | .Ev POSIXLY_CORRECT | ||
256 | and returns non-options as arguments to option | ||
257 | .Ql \e1 . | ||
258 | .It OpenBSD | ||
259 | honors | ||
260 | .Ev POSIXLY_CORRECT | ||
261 | and stops at the first non-option. | ||
262 | .El | ||
263 | .It | ||
264 | handling of | ||
265 | .Ql - | ||
266 | within the option string (not the first character): | ||
267 | .Bl -tag -width "OpenBSD" | ||
268 | .It GNU | ||
269 | treats a | ||
270 | .Ql - | ||
271 | on the command line as a non-argument. | ||
272 | .It OpenBSD | ||
273 | a | ||
274 | .Ql - | ||
275 | within the option string matches a | ||
276 | .Ql - | ||
277 | (single dash) on the command line. | ||
278 | This functionality is provided for backward compatibility with | ||
279 | programs, such as | ||
280 | .Xr su 1 , | ||
281 | that use | ||
282 | .Ql - | ||
283 | as an option flag. | ||
284 | This practice is wrong, and should not be used in any current development. | ||
285 | .El | ||
286 | .It | ||
287 | handling of | ||
288 | .Ql :: | ||
289 | in the option string in the presence of | ||
290 | .Ev POSIXLY_CORRECT : | ||
291 | .Bl -tag -width "OpenBSD" | ||
292 | .It Both | ||
293 | GNU and | ||
294 | .Ox | ||
295 | ignore | ||
296 | .Ev POSIXLY_CORRECT | ||
297 | here and take | ||
298 | .Ql :: | ||
299 | to mean the preceding option takes an optional argument. | ||
300 | .El | ||
301 | .It | ||
302 | return value in case of missing argument if first character | ||
303 | (after | ||
304 | .Ql + | ||
305 | or | ||
306 | .Ql - ) | ||
307 | in the option string is not | ||
308 | .Ql \&: : | ||
309 | .Bl -tag -width "OpenBSD" | ||
310 | .It GNU | ||
311 | returns | ||
312 | .Ql \&? | ||
313 | .It OpenBSD | ||
314 | returns | ||
315 | .Ql \&: | ||
316 | (since | ||
317 | .Ox Ns 's | ||
318 | .Xr getopt 3 | ||
319 | does). | ||
320 | .El | ||
321 | .It | ||
322 | handling of | ||
323 | .Ql --a | ||
324 | in | ||
325 | .Xr getopt 3 : | ||
326 | .Bl -tag -width "OpenBSD" | ||
327 | .It GNU | ||
328 | parses this as option | ||
329 | .Ql - , | ||
330 | option | ||
331 | .Ql a . | ||
332 | .It OpenBSD | ||
333 | parses this as | ||
334 | .Ql -- , | ||
335 | and returns \-1 (ignoring the | ||
336 | .Ql a ) | ||
337 | (because the original | ||
338 | .Fn getopt | ||
339 | did.) | ||
340 | .El | ||
341 | .It | ||
342 | setting of | ||
343 | .Va optopt | ||
344 | for long options with | ||
345 | .Va flag | ||
346 | .No non- Ns Dv NULL : | ||
347 | .Bl -tag -width "OpenBSD" | ||
348 | .It GNU | ||
349 | sets | ||
350 | .Va optopt | ||
351 | to | ||
352 | .Va val . | ||
353 | .It OpenBSD | ||
354 | sets | ||
355 | .Va optopt | ||
356 | to 0 (since | ||
357 | .Va val | ||
358 | would never be returned). | ||
359 | .El | ||
360 | .It | ||
361 | handling of | ||
362 | .Ql -W | ||
363 | with | ||
364 | .Ql W; | ||
365 | in the option string in | ||
366 | .Xr getopt 3 | ||
367 | (not | ||
368 | .Fn getopt_long ) : | ||
369 | .Bl -tag -width "OpenBSD" | ||
370 | .It GNU | ||
371 | causes a segmentation fault. | ||
372 | .It OpenBSD | ||
373 | no special handling is done; | ||
374 | .Ql W; | ||
375 | is interpreted as two separate options, neither of which take an argument. | ||
376 | .El | ||
377 | .It | ||
378 | setting of | ||
379 | .Va optarg | ||
380 | for long options without an argument that are invoked via | ||
381 | .Ql -W | ||
382 | (with | ||
383 | .Ql W; | ||
384 | in the option string): | ||
385 | .Bl -tag -width "OpenBSD" | ||
386 | .It GNU | ||
387 | sets | ||
388 | .Va optarg | ||
389 | to the option name (the argument of | ||
390 | .Ql -W ) . | ||
391 | .It OpenBSD | ||
392 | sets | ||
393 | .Va optarg | ||
394 | to | ||
395 | .Dv NULL | ||
396 | (the argument of the long option). | ||
397 | .El | ||
398 | .It | ||
399 | handling of | ||
400 | .Ql -W | ||
401 | with an argument that is not (a prefix to) a known long option | ||
402 | (with | ||
403 | .Ql W; | ||
404 | in the option string): | ||
405 | .Bl -tag -width "OpenBSD" | ||
406 | .It GNU | ||
407 | returns | ||
408 | .Ql -W | ||
409 | with | ||
410 | .Va optarg | ||
411 | set to the unknown option. | ||
412 | .It OpenBSD | ||
413 | treats this as an error (unknown option) and returns | ||
414 | .Ql \&? | ||
415 | with | ||
416 | .Va optopt | ||
417 | set to 0 and | ||
418 | .Va optarg | ||
419 | set to | ||
420 | .Dv NULL | ||
421 | (as GNU's man page documents). | ||
422 | .El | ||
423 | .It | ||
424 | The error messages are different. | ||
425 | .It | ||
426 | .Ox | ||
427 | does not permute the argument vector at the same points in | ||
428 | the calling sequence as GNU does. | ||
429 | The aspects normally used by the caller | ||
430 | (ordering after \-1 is returned, value of | ||
431 | .Va optind | ||
432 | relative to current positions) are the same, though. | ||
433 | (We do fewer variable swaps.) | ||
434 | .El | ||
435 | .Sh ENVIRONMENT | ||
436 | .Bl -tag -width Ev | ||
437 | .It Ev POSIXLY_CORRECT | ||
438 | If set, option processing stops when the first non-option is found and | ||
439 | a leading | ||
440 | .Sq - | ||
441 | or | ||
442 | .Sq + | ||
443 | in the | ||
444 | .Ar optstring | ||
445 | is ignored. | ||
446 | .El | ||
447 | .Sh SEE ALSO | ||
448 | .Xr getopt 3 | ||
449 | .Sh HISTORY | ||
450 | The | ||
451 | .Fn getopt_long | ||
452 | and | ||
453 | .Fn getopt_long_only | ||
454 | functions first appeared in GNU libiberty. | ||
455 | This implementation first appeared in | ||
456 | .Ox 3.3 . | ||
457 | .Sh BUGS | ||
458 | The | ||
459 | .Ar argv | ||
460 | argument is not really | ||
461 | .Dv const | ||
462 | as its elements may be permuted (unless | ||
463 | .Ev POSIXLY_CORRECT | ||
464 | 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..73d97c0ed3 --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.c | |||
@@ -0,0 +1,517 @@ | |||
1 | /* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl 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 | * | ||
39 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
40 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
41 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
43 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
44 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
45 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
46 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
47 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
48 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
49 | * POSSIBILITY OF SUCH DAMAGE. | ||
50 | */ | ||
51 | |||
52 | #include <err.h> | ||
53 | #include <errno.h> | ||
54 | #include <getopt.h> | ||
55 | #include <stdlib.h> | ||
56 | #include <string.h> | ||
57 | |||
58 | #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ | ||
59 | |||
60 | #ifdef REPLACE_GETOPT | ||
61 | int opterr = 1; /* if error message should be printed */ | ||
62 | int optind = 1; /* index into parent argv vector */ | ||
63 | int optopt = '?'; /* character checked for validity */ | ||
64 | int optreset; /* reset getopt */ | ||
65 | char *optarg; /* argument associated with option */ | ||
66 | #endif | ||
67 | |||
68 | #define PRINT_ERROR ((opterr) && (*options != ':')) | ||
69 | |||
70 | #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ | ||
71 | #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ | ||
72 | #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ | ||
73 | |||
74 | /* return values */ | ||
75 | #define BADCH (int)'?' | ||
76 | #define BADARG ((*options == ':') ? (int)':' : (int)'?') | ||
77 | #define INORDER (int)1 | ||
78 | |||
79 | #define EMSG "" | ||
80 | |||
81 | static int getopt_internal(int, char * const *, const char *, | ||
82 | const struct option *, int *, int); | ||
83 | static int parse_long_options(char * const *, const char *, | ||
84 | const struct option *, int *, int); | ||
85 | static int gcd(int, int); | ||
86 | static void permute_args(int, int, int, char * const *); | ||
87 | |||
88 | static char *place = EMSG; /* option letter processing */ | ||
89 | |||
90 | /* XXX: set optreset to 1 rather than these two */ | ||
91 | static int nonopt_start = -1; /* first non option argument (for permute) */ | ||
92 | static int nonopt_end = -1; /* first option after non options (for permute) */ | ||
93 | |||
94 | /* Error messages */ | ||
95 | static const char recargchar[] = "option requires an argument -- %c"; | ||
96 | static const char recargstring[] = "option requires an argument -- %s"; | ||
97 | static const char ambig[] = "ambiguous option -- %.*s"; | ||
98 | static const char noarg[] = "option doesn't take an argument -- %.*s"; | ||
99 | static const char illoptchar[] = "unknown option -- %c"; | ||
100 | static const char illoptstring[] = "unknown option -- %s"; | ||
101 | |||
102 | /* | ||
103 | * Compute the greatest common divisor of a and b. | ||
104 | */ | ||
105 | static int | ||
106 | gcd(int a, int b) | ||
107 | { | ||
108 | int c; | ||
109 | |||
110 | c = a % b; | ||
111 | while (c != 0) { | ||
112 | a = b; | ||
113 | b = c; | ||
114 | c = a % b; | ||
115 | } | ||
116 | |||
117 | return (b); | ||
118 | } | ||
119 | |||
120 | /* | ||
121 | * Exchange the block from nonopt_start to nonopt_end with the block | ||
122 | * from nonopt_end to opt_end (keeping the same order of arguments | ||
123 | * in each block). | ||
124 | */ | ||
125 | static void | ||
126 | permute_args(int panonopt_start, int panonopt_end, int opt_end, | ||
127 | char * const *nargv) | ||
128 | { | ||
129 | int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; | ||
130 | char *swap; | ||
131 | |||
132 | /* | ||
133 | * compute lengths of blocks and number and size of cycles | ||
134 | */ | ||
135 | nnonopts = panonopt_end - panonopt_start; | ||
136 | nopts = opt_end - panonopt_end; | ||
137 | ncycle = gcd(nnonopts, nopts); | ||
138 | cyclelen = (opt_end - panonopt_start) / ncycle; | ||
139 | |||
140 | for (i = 0; i < ncycle; i++) { | ||
141 | cstart = panonopt_end+i; | ||
142 | pos = cstart; | ||
143 | for (j = 0; j < cyclelen; j++) { | ||
144 | if (pos >= panonopt_end) | ||
145 | pos -= nnonopts; | ||
146 | else | ||
147 | pos += nopts; | ||
148 | swap = nargv[pos]; | ||
149 | /* LINTED const cast */ | ||
150 | ((char **) nargv)[pos] = nargv[cstart]; | ||
151 | /* LINTED const cast */ | ||
152 | ((char **)nargv)[cstart] = swap; | ||
153 | } | ||
154 | } | ||
155 | } | ||
156 | |||
157 | /* | ||
158 | * parse_long_options -- | ||
159 | * Parse long options in argc/argv argument vector. | ||
160 | * Returns -1 if short_too is set and the option does not match long_options. | ||
161 | */ | ||
162 | static int | ||
163 | parse_long_options(char * const *nargv, const char *options, | ||
164 | const struct option *long_options, int *idx, int short_too) | ||
165 | { | ||
166 | char *current_argv, *has_equal; | ||
167 | size_t current_argv_len; | ||
168 | int i, match; | ||
169 | |||
170 | current_argv = place; | ||
171 | match = -1; | ||
172 | |||
173 | optind++; | ||
174 | |||
175 | if ((has_equal = strchr(current_argv, '=')) != NULL) { | ||
176 | /* argument found (--option=arg) */ | ||
177 | current_argv_len = has_equal - current_argv; | ||
178 | has_equal++; | ||
179 | } else | ||
180 | current_argv_len = strlen(current_argv); | ||
181 | |||
182 | for (i = 0; long_options[i].name; i++) { | ||
183 | /* find matching long option */ | ||
184 | if (strncmp(current_argv, long_options[i].name, | ||
185 | current_argv_len)) | ||
186 | continue; | ||
187 | |||
188 | if (strlen(long_options[i].name) == current_argv_len) { | ||
189 | /* exact match */ | ||
190 | match = i; | ||
191 | break; | ||
192 | } | ||
193 | /* | ||
194 | * If this is a known short option, don't allow | ||
195 | * a partial match of a single character. | ||
196 | */ | ||
197 | if (short_too && current_argv_len == 1) | ||
198 | continue; | ||
199 | |||
200 | if (match == -1) /* partial match */ | ||
201 | match = i; | ||
202 | else { | ||
203 | /* ambiguous abbreviation */ | ||
204 | if (PRINT_ERROR) | ||
205 | warnx(ambig, (int)current_argv_len, | ||
206 | current_argv); | ||
207 | optopt = 0; | ||
208 | return (BADCH); | ||
209 | } | ||
210 | } | ||
211 | if (match != -1) { /* option found */ | ||
212 | if (long_options[match].has_arg == no_argument | ||
213 | && has_equal) { | ||
214 | if (PRINT_ERROR) | ||
215 | warnx(noarg, (int)current_argv_len, | ||
216 | current_argv); | ||
217 | /* | ||
218 | * XXX: GNU sets optopt to val regardless of flag | ||
219 | */ | ||
220 | if (long_options[match].flag == NULL) | ||
221 | optopt = long_options[match].val; | ||
222 | else | ||
223 | optopt = 0; | ||
224 | return (BADARG); | ||
225 | } | ||
226 | if (long_options[match].has_arg == required_argument || | ||
227 | long_options[match].has_arg == optional_argument) { | ||
228 | if (has_equal) | ||
229 | optarg = has_equal; | ||
230 | else if (long_options[match].has_arg == | ||
231 | required_argument) { | ||
232 | /* | ||
233 | * optional argument doesn't use next nargv | ||
234 | */ | ||
235 | optarg = nargv[optind++]; | ||
236 | } | ||
237 | } | ||
238 | if ((long_options[match].has_arg == required_argument) | ||
239 | && (optarg == NULL)) { | ||
240 | /* | ||
241 | * Missing argument; leading ':' indicates no error | ||
242 | * should be generated. | ||
243 | */ | ||
244 | if (PRINT_ERROR) | ||
245 | warnx(recargstring, | ||
246 | current_argv); | ||
247 | /* | ||
248 | * XXX: GNU sets optopt to val regardless of flag | ||
249 | */ | ||
250 | if (long_options[match].flag == NULL) | ||
251 | optopt = long_options[match].val; | ||
252 | else | ||
253 | optopt = 0; | ||
254 | --optind; | ||
255 | return (BADARG); | ||
256 | } | ||
257 | } else { /* unknown option */ | ||
258 | if (short_too) { | ||
259 | --optind; | ||
260 | return (-1); | ||
261 | } | ||
262 | if (PRINT_ERROR) | ||
263 | warnx(illoptstring, current_argv); | ||
264 | optopt = 0; | ||
265 | return (BADCH); | ||
266 | } | ||
267 | if (idx) | ||
268 | *idx = match; | ||
269 | if (long_options[match].flag) { | ||
270 | *long_options[match].flag = long_options[match].val; | ||
271 | return (0); | ||
272 | } else | ||
273 | return (long_options[match].val); | ||
274 | } | ||
275 | |||
276 | /* | ||
277 | * getopt_internal -- | ||
278 | * Parse argc/argv argument vector. Called by user level routines. | ||
279 | */ | ||
280 | static int | ||
281 | getopt_internal(int nargc, char * const *nargv, const char *options, | ||
282 | const struct option *long_options, int *idx, int flags) | ||
283 | { | ||
284 | char *oli; /* option letter list index */ | ||
285 | int optchar, short_too; | ||
286 | static int posixly_correct = -1; | ||
287 | |||
288 | if (options == NULL) | ||
289 | return (-1); | ||
290 | |||
291 | /* | ||
292 | * Disable GNU extensions if POSIXLY_CORRECT is set or options | ||
293 | * string begins with a '+'. | ||
294 | */ | ||
295 | if (posixly_correct == -1) | ||
296 | posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); | ||
297 | if (posixly_correct || *options == '+') | ||
298 | flags &= ~FLAG_PERMUTE; | ||
299 | else if (*options == '-') | ||
300 | flags |= FLAG_ALLARGS; | ||
301 | if (*options == '+' || *options == '-') | ||
302 | options++; | ||
303 | |||
304 | /* | ||
305 | * XXX Some GNU programs (like cvs) set optind to 0 instead of | ||
306 | * XXX using optreset. Work around this braindamage. | ||
307 | */ | ||
308 | if (optind == 0) | ||
309 | optind = optreset = 1; | ||
310 | |||
311 | optarg = NULL; | ||
312 | if (optreset) | ||
313 | nonopt_start = nonopt_end = -1; | ||
314 | start: | ||
315 | if (optreset || !*place) { /* update scanning pointer */ | ||
316 | optreset = 0; | ||
317 | if (optind >= nargc) { /* end of argument vector */ | ||
318 | place = EMSG; | ||
319 | if (nonopt_end != -1) { | ||
320 | /* do permutation, if we have to */ | ||
321 | permute_args(nonopt_start, nonopt_end, | ||
322 | optind, nargv); | ||
323 | optind -= nonopt_end - nonopt_start; | ||
324 | } | ||
325 | else if (nonopt_start != -1) { | ||
326 | /* | ||
327 | * If we skipped non-options, set optind | ||
328 | * to the first of them. | ||
329 | */ | ||
330 | optind = nonopt_start; | ||
331 | } | ||
332 | nonopt_start = nonopt_end = -1; | ||
333 | return (-1); | ||
334 | } | ||
335 | if (*(place = nargv[optind]) != '-' || | ||
336 | (place[1] == '\0' && strchr(options, '-') == NULL)) { | ||
337 | place = EMSG; /* found non-option */ | ||
338 | if (flags & FLAG_ALLARGS) { | ||
339 | /* | ||
340 | * GNU extension: | ||
341 | * return non-option as argument to option 1 | ||
342 | */ | ||
343 | optarg = nargv[optind++]; | ||
344 | return (INORDER); | ||
345 | } | ||
346 | if (!(flags & FLAG_PERMUTE)) { | ||
347 | /* | ||
348 | * If no permutation wanted, stop parsing | ||
349 | * at first non-option. | ||
350 | */ | ||
351 | return (-1); | ||
352 | } | ||
353 | /* do permutation */ | ||
354 | if (nonopt_start == -1) | ||
355 | nonopt_start = optind; | ||
356 | else if (nonopt_end != -1) { | ||
357 | permute_args(nonopt_start, nonopt_end, | ||
358 | optind, nargv); | ||
359 | nonopt_start = optind - | ||
360 | (nonopt_end - nonopt_start); | ||
361 | nonopt_end = -1; | ||
362 | } | ||
363 | optind++; | ||
364 | /* process next argument */ | ||
365 | goto start; | ||
366 | } | ||
367 | if (nonopt_start != -1 && nonopt_end == -1) | ||
368 | nonopt_end = optind; | ||
369 | |||
370 | /* | ||
371 | * If we have "-" do nothing, if "--" we are done. | ||
372 | */ | ||
373 | if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { | ||
374 | optind++; | ||
375 | place = EMSG; | ||
376 | /* | ||
377 | * We found an option (--), so if we skipped | ||
378 | * non-options, we have to permute. | ||
379 | */ | ||
380 | if (nonopt_end != -1) { | ||
381 | permute_args(nonopt_start, nonopt_end, | ||
382 | optind, nargv); | ||
383 | optind -= nonopt_end - nonopt_start; | ||
384 | } | ||
385 | nonopt_start = nonopt_end = -1; | ||
386 | return (-1); | ||
387 | } | ||
388 | } | ||
389 | |||
390 | /* | ||
391 | * Check long options if: | ||
392 | * 1) we were passed some | ||
393 | * 2) the arg is not just "-" | ||
394 | * 3) either the arg starts with -- we are getopt_long_only() | ||
395 | */ | ||
396 | if (long_options != NULL && place != nargv[optind] && | ||
397 | (*place == '-' || (flags & FLAG_LONGONLY))) { | ||
398 | short_too = 0; | ||
399 | if (*place == '-') | ||
400 | place++; /* --foo long option */ | ||
401 | else if (*place != ':' && strchr(options, *place) != NULL) | ||
402 | short_too = 1; /* could be short option too */ | ||
403 | |||
404 | optchar = parse_long_options(nargv, options, long_options, | ||
405 | idx, short_too); | ||
406 | if (optchar != -1) { | ||
407 | place = EMSG; | ||
408 | return (optchar); | ||
409 | } | ||
410 | } | ||
411 | |||
412 | if ((optchar = (int)*place++) == (int)':' || | ||
413 | (optchar == (int)'-' && *place != '\0') || | ||
414 | (oli = strchr(options, optchar)) == NULL) { | ||
415 | /* | ||
416 | * If the user specified "-" and '-' isn't listed in | ||
417 | * options, return -1 (non-option) as per POSIX. | ||
418 | * Otherwise, it is an unknown option character (or ':'). | ||
419 | */ | ||
420 | if (optchar == (int)'-' && *place == '\0') | ||
421 | return (-1); | ||
422 | if (!*place) | ||
423 | ++optind; | ||
424 | if (PRINT_ERROR) | ||
425 | warnx(illoptchar, optchar); | ||
426 | optopt = optchar; | ||
427 | return (BADCH); | ||
428 | } | ||
429 | if (long_options != NULL && optchar == 'W' && oli[1] == ';') { | ||
430 | /* -W long-option */ | ||
431 | if (*place) /* no space */ | ||
432 | /* NOTHING */; | ||
433 | else if (++optind >= nargc) { /* no arg */ | ||
434 | place = EMSG; | ||
435 | if (PRINT_ERROR) | ||
436 | warnx(recargchar, optchar); | ||
437 | optopt = optchar; | ||
438 | return (BADARG); | ||
439 | } else /* white space */ | ||
440 | place = nargv[optind]; | ||
441 | optchar = parse_long_options(nargv, options, long_options, | ||
442 | idx, 0); | ||
443 | place = EMSG; | ||
444 | return (optchar); | ||
445 | } | ||
446 | if (*++oli != ':') { /* doesn't take argument */ | ||
447 | if (!*place) | ||
448 | ++optind; | ||
449 | } else { /* takes (optional) argument */ | ||
450 | optarg = NULL; | ||
451 | if (*place) /* no white space */ | ||
452 | optarg = place; | ||
453 | else if (oli[1] != ':') { /* arg not optional */ | ||
454 | if (++optind >= nargc) { /* no arg */ | ||
455 | place = EMSG; | ||
456 | if (PRINT_ERROR) | ||
457 | warnx(recargchar, optchar); | ||
458 | optopt = optchar; | ||
459 | return (BADARG); | ||
460 | } else | ||
461 | optarg = nargv[optind]; | ||
462 | } | ||
463 | place = EMSG; | ||
464 | ++optind; | ||
465 | } | ||
466 | /* dump back option letter */ | ||
467 | return (optchar); | ||
468 | } | ||
469 | |||
470 | #ifdef REPLACE_GETOPT | ||
471 | /* | ||
472 | * getopt -- | ||
473 | * Parse argc/argv argument vector. | ||
474 | * | ||
475 | * [eventually this will replace the BSD getopt] | ||
476 | */ | ||
477 | int | ||
478 | getopt(int nargc, char * const *nargv, const char *options) | ||
479 | { | ||
480 | |||
481 | /* | ||
482 | * We don't pass FLAG_PERMUTE to getopt_internal() since | ||
483 | * the BSD getopt(3) (unlike GNU) has never done this. | ||
484 | * | ||
485 | * Furthermore, since many privileged programs call getopt() | ||
486 | * before dropping privileges it makes sense to keep things | ||
487 | * as simple (and bug-free) as possible. | ||
488 | */ | ||
489 | return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); | ||
490 | } | ||
491 | #endif /* REPLACE_GETOPT */ | ||
492 | |||
493 | /* | ||
494 | * getopt_long -- | ||
495 | * Parse argc/argv argument vector. | ||
496 | */ | ||
497 | int | ||
498 | getopt_long(int nargc, char * const *nargv, const char *options, | ||
499 | const struct option *long_options, int *idx) | ||
500 | { | ||
501 | |||
502 | return (getopt_internal(nargc, nargv, options, long_options, idx, | ||
503 | FLAG_PERMUTE)); | ||
504 | } | ||
505 | |||
506 | /* | ||
507 | * getopt_long_only -- | ||
508 | * Parse argc/argv argument vector. | ||
509 | */ | ||
510 | int | ||
511 | getopt_long_only(int nargc, char * const *nargv, const char *options, | ||
512 | const struct option *long_options, int *idx) | ||
513 | { | ||
514 | |||
515 | return (getopt_internal(nargc, nargv, options, long_options, idx, | ||
516 | FLAG_PERMUTE|FLAG_LONGONLY)); | ||
517 | } | ||
diff --git a/src/lib/libc/stdlib/getsubopt.3 b/src/lib/libc/stdlib/getsubopt.3 new file mode 100644 index 0000000000..9f35df703f --- /dev/null +++ b/src/lib/libc/stdlib/getsubopt.3 | |||
@@ -0,0 +1,145 @@ | |||
1 | .\" $OpenBSD: getsubopt.3,v 1.12 2007/05/31 19:19:31 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 | .\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93 | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
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 = 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 NUL 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 | ||
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 | } | ||
134 | break; | ||
135 | } | ||
136 | } | ||
137 | .Ed | ||
138 | .Sh SEE ALSO | ||
139 | .Xr getopt 3 , | ||
140 | .Xr strsep 3 | ||
141 | .Sh HISTORY | ||
142 | The | ||
143 | .Fn getsubopt | ||
144 | function first appeared in | ||
145 | .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..735c85ba8a --- /dev/null +++ b/src/lib/libc/stdlib/getsubopt.c | |||
@@ -0,0 +1,92 @@ | |||
1 | /* $OpenBSD: getsubopt.c,v 1.4 2005/08/08 08:05:36 espie 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 <unistd.h> | ||
33 | #include <stdlib.h> | ||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * The SVID interface to getsubopt provides no way of figuring out which | ||
38 | * part of the suboptions list wasn't matched. This makes error messages | ||
39 | * tricky... The extern variable suboptarg is a pointer to the token | ||
40 | * which didn't match. | ||
41 | */ | ||
42 | char *suboptarg; | ||
43 | |||
44 | int | ||
45 | getsubopt(char **optionp, char * const *tokens, char **valuep) | ||
46 | { | ||
47 | int cnt; | ||
48 | char *p; | ||
49 | |||
50 | suboptarg = *valuep = NULL; | ||
51 | |||
52 | if (!optionp || !*optionp) | ||
53 | return(-1); | ||
54 | |||
55 | /* skip leading white-space, commas */ | ||
56 | for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); | ||
57 | |||
58 | if (!*p) { | ||
59 | *optionp = p; | ||
60 | return(-1); | ||
61 | } | ||
62 | |||
63 | /* save the start of the token, and skip the rest of the token. */ | ||
64 | for (suboptarg = p; | ||
65 | *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';); | ||
66 | |||
67 | if (*p) { | ||
68 | /* | ||
69 | * If there's an equals sign, set the value pointer, and | ||
70 | * skip over the value part of the token. Terminate the | ||
71 | * token. | ||
72 | */ | ||
73 | if (*p == '=') { | ||
74 | *p = '\0'; | ||
75 | for (*valuep = ++p; | ||
76 | *p && *p != ',' && *p != ' ' && *p != '\t'; ++p); | ||
77 | if (*p) | ||
78 | *p++ = '\0'; | ||
79 | } else | ||
80 | *p++ = '\0'; | ||
81 | /* Skip any whitespace or commas after this token. */ | ||
82 | for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); | ||
83 | } | ||
84 | |||
85 | /* set optionp for next round. */ | ||
86 | *optionp = p; | ||
87 | |||
88 | for (cnt = 0; *tokens; ++tokens, ++cnt) | ||
89 | if (!strcmp(suboptarg, *tokens)) | ||
90 | return(cnt); | ||
91 | return(-1); | ||
92 | } | ||
diff --git a/src/lib/libc/stdlib/hcreate.3 b/src/lib/libc/stdlib/hcreate.3 new file mode 100644 index 0000000000..4ff17a087d --- /dev/null +++ b/src/lib/libc/stdlib/hcreate.3 | |||
@@ -0,0 +1,184 @@ | |||
1 | .\" $OpenBSD: hcreate.3,v 1.5 2008/06/26 05:42:05 ray Exp $ | ||
2 | .\" $NetBSD: hcreate.3,v 1.6 2003/04/16 13:34:46 wiz Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1999 The NetBSD Foundation, Inc. | ||
5 | .\" All rights reserved. | ||
6 | .\" | ||
7 | .\" This code is derived from software contributed to The NetBSD Foundation | ||
8 | .\" by Klaus Klein. | ||
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 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
20 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
21 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
22 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
23 | .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
24 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
25 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
26 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
27 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
28 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
29 | .\" POSSIBILITY OF SUCH DAMAGE. | ||
30 | .\" | ||
31 | .Dd $Mdocdate: June 26 2008 $ | ||
32 | .Dt HCREATE 3 | ||
33 | .Os | ||
34 | .Sh NAME | ||
35 | .Nm hcreate , | ||
36 | .Nm hdestroy , | ||
37 | .Nm hsearch | ||
38 | .Nd manage hash search table | ||
39 | .Sh SYNOPSIS | ||
40 | .In search.h | ||
41 | .Ft int | ||
42 | .Fn hcreate "size_t nel" | ||
43 | .Ft void | ||
44 | .Fn hdestroy "void" | ||
45 | .Ft ENTRY * | ||
46 | .Fn hsearch "ENTRY item" "ACTION action" | ||
47 | .Sh DESCRIPTION | ||
48 | The | ||
49 | .Fn hcreate , | ||
50 | .Fn hdestroy , | ||
51 | and | ||
52 | .Fn hsearch | ||
53 | functions manage hash search tables. | ||
54 | .Pp | ||
55 | The | ||
56 | .Fn hcreate | ||
57 | function allocates and initializes the table. | ||
58 | The | ||
59 | .Fa nel | ||
60 | argument specifies an estimate of the maximum number of entries to be held | ||
61 | by the table. | ||
62 | Unless further memory allocation fails, supplying an insufficient | ||
63 | .Fa nel | ||
64 | value will not result in functional harm, although a performance degradation | ||
65 | may occur. | ||
66 | Initialization using the | ||
67 | .Fn hcreate | ||
68 | function is mandatory prior to any access operations using | ||
69 | .Fn hsearch . | ||
70 | .Pp | ||
71 | The | ||
72 | .Fn hdestroy | ||
73 | function destroys a table previously created using | ||
74 | .Fn hcreate . | ||
75 | After a call to | ||
76 | .Fn hdestroy , | ||
77 | the data can no longer be accessed. | ||
78 | .Pp | ||
79 | The | ||
80 | .Fn hsearch | ||
81 | function is used to search to the hash table. | ||
82 | It returns a pointer into the | ||
83 | hash table indicating the address of an item. | ||
84 | The | ||
85 | .Fa item | ||
86 | argument is of type | ||
87 | .Dv ENTRY , | ||
88 | a structural type which contains the following members: | ||
89 | .Pp | ||
90 | .Bl -tag -compact -offset indent -width voidX*dataXX | ||
91 | .It Fa char *key | ||
92 | comparison key. | ||
93 | .It Fa void *data | ||
94 | pointer to data associated with | ||
95 | .Fa key . | ||
96 | .El | ||
97 | .Pp | ||
98 | The key comparison function used by | ||
99 | .Fn hsearch | ||
100 | is | ||
101 | .Xr strcmp 3 . | ||
102 | .Pp | ||
103 | The | ||
104 | .Fa action | ||
105 | argument is of type | ||
106 | .Dv ACTION , | ||
107 | an enumeration type which defines the following values: | ||
108 | .Bl -tag -offset indent -width ENTERXX | ||
109 | .It Dv ENTER | ||
110 | Insert | ||
111 | .Fa item | ||
112 | into the hash table. | ||
113 | If an existing item with the same key is found, it is not replaced. | ||
114 | Note that the | ||
115 | .Fa key | ||
116 | and | ||
117 | .Fa data | ||
118 | elements of | ||
119 | .Fa item | ||
120 | are used directly by the new table entry. | ||
121 | The storage for the | ||
122 | key must not be modified during the lifetime of the hash table. | ||
123 | .It Dv FIND | ||
124 | Search the hash table without inserting | ||
125 | .Fa item . | ||
126 | .El | ||
127 | .Sh RETURN VALUES | ||
128 | If successful, the | ||
129 | .Fn hcreate | ||
130 | function returns a non-zero value. | ||
131 | Otherwise, a value of 0 is returned and | ||
132 | .Va errno | ||
133 | is set to indicate the error. | ||
134 | .Pp | ||
135 | The | ||
136 | .Fn hdestroy | ||
137 | functions | ||
138 | returns no value. | ||
139 | .Pp | ||
140 | If successful, the | ||
141 | .Fn hsearch | ||
142 | function returns a pointer to a hash table entry matching | ||
143 | the provided key. | ||
144 | If the action is | ||
145 | .Dv FIND | ||
146 | and the item was not found, or if the action is | ||
147 | .Dv ENTER | ||
148 | and the insertion failed, | ||
149 | .Dv NULL | ||
150 | is returned and | ||
151 | .Va errno | ||
152 | is set to indicate the error. | ||
153 | If the action is | ||
154 | .Dv ENTER | ||
155 | and an entry already existed in the table matching the given | ||
156 | key, the existing entry is returned and is not replaced. | ||
157 | .Sh ERRORS | ||
158 | The | ||
159 | .Fn hcreate | ||
160 | and | ||
161 | .Fn hsearch | ||
162 | functions will fail if: | ||
163 | .Bl -tag -width Er | ||
164 | .It Bq Er ENOMEM | ||
165 | Insufficient memory is available. | ||
166 | .El | ||
167 | .Sh SEE ALSO | ||
168 | .Xr bsearch 3 , | ||
169 | .Xr lsearch 3 , | ||
170 | .Xr malloc 3 , | ||
171 | .Xr strcmp 3 | ||
172 | .Sh STANDARDS | ||
173 | These functions conform to | ||
174 | .St -p1003.1-2004 . | ||
175 | .Sh HISTORY | ||
176 | The | ||
177 | .Fn hcreate , | ||
178 | .Fn hdestroy | ||
179 | and | ||
180 | .Fn hsearch | ||
181 | functions first appeared in | ||
182 | .At V . | ||
183 | .Sh BUGS | ||
184 | The interface permits the use of only one hash table at a time. | ||
diff --git a/src/lib/libc/stdlib/hcreate.c b/src/lib/libc/stdlib/hcreate.c new file mode 100644 index 0000000000..094f32c173 --- /dev/null +++ b/src/lib/libc/stdlib/hcreate.c | |||
@@ -0,0 +1,191 @@ | |||
1 | /* $OpenBSD: hcreate.c,v 1.4 2007/09/02 15:19:17 deraadt Exp $ */ | ||
2 | /* $NetBSD: hcreate.c,v 1.5 2004/04/23 02:48:12 simonb Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (c) 2001 Christopher G. Demetriou | ||
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. All advertising materials mentioning features or use of this software | ||
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed for the | ||
19 | * NetBSD Project. See http://www.NetBSD.org/ for | ||
20 | * information about NetBSD. | ||
21 | * 4. The name of the author may not be used to endorse or promote products | ||
22 | * derived from this software without specific prior written permission. | ||
23 | * | ||
24 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | ||
25 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | ||
26 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
27 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
28 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
33 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
34 | * | ||
35 | * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>> | ||
36 | */ | ||
37 | |||
38 | /* | ||
39 | * hcreate() / hsearch() / hdestroy() | ||
40 | * | ||
41 | * SysV/XPG4 hash table functions. | ||
42 | * | ||
43 | * Implementation done based on NetBSD manual page and Solaris manual page, | ||
44 | * plus my own personal experience about how they're supposed to work. | ||
45 | * | ||
46 | * I tried to look at Knuth (as cited by the Solaris manual page), but | ||
47 | * nobody had a copy in the office, so... | ||
48 | */ | ||
49 | |||
50 | #include "namespace.h" | ||
51 | #include <assert.h> | ||
52 | #include <errno.h> | ||
53 | #include <inttypes.h> | ||
54 | #include <search.h> | ||
55 | #include <stdlib.h> | ||
56 | #include <string.h> | ||
57 | #include <sys/queue.h> | ||
58 | |||
59 | #ifndef _DIAGASSERT | ||
60 | #define _DIAGASSERT(x) | ||
61 | #endif | ||
62 | |||
63 | /* | ||
64 | * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit | ||
65 | * ptr machine) without adjusting MAX_BUCKETS_LG2 below. | ||
66 | */ | ||
67 | struct internal_entry { | ||
68 | SLIST_ENTRY(internal_entry) link; | ||
69 | ENTRY ent; | ||
70 | }; | ||
71 | SLIST_HEAD(internal_head, internal_entry); | ||
72 | |||
73 | #define MIN_BUCKETS_LG2 4 | ||
74 | #define MIN_BUCKETS (1 << MIN_BUCKETS_LG2) | ||
75 | |||
76 | /* | ||
77 | * max * sizeof internal_entry must fit into size_t. | ||
78 | * assumes internal_entry is <= 32 (2^5) bytes. | ||
79 | */ | ||
80 | #define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5) | ||
81 | #define MAX_BUCKETS ((size_t)1 << MAX_BUCKETS_LG2) | ||
82 | |||
83 | /* Default hash function, from db/hash/hash_func.c */ | ||
84 | extern u_int32_t (*__default_hash)(const void *, size_t); | ||
85 | |||
86 | static struct internal_head *htable; | ||
87 | static size_t htablesize; | ||
88 | |||
89 | int | ||
90 | hcreate(size_t nel) | ||
91 | { | ||
92 | size_t idx; | ||
93 | unsigned int p2; | ||
94 | |||
95 | /* Make sure this isn't called when a table already exists. */ | ||
96 | _DIAGASSERT(htable == NULL); | ||
97 | if (htable != NULL) { | ||
98 | errno = EINVAL; | ||
99 | return 0; | ||
100 | } | ||
101 | |||
102 | /* If nel is too small, make it min sized. */ | ||
103 | if (nel < MIN_BUCKETS) | ||
104 | nel = MIN_BUCKETS; | ||
105 | |||
106 | /* If it's too large, cap it. */ | ||
107 | if (nel > MAX_BUCKETS) | ||
108 | nel = MAX_BUCKETS; | ||
109 | |||
110 | /* If it's is not a power of two in size, round up. */ | ||
111 | if ((nel & (nel - 1)) != 0) { | ||
112 | for (p2 = 0; nel != 0; p2++) | ||
113 | nel >>= 1; | ||
114 | _DIAGASSERT(p2 <= MAX_BUCKETS_LG2); | ||
115 | nel = 1 << p2; | ||
116 | } | ||
117 | |||
118 | /* Allocate the table. */ | ||
119 | htablesize = nel; | ||
120 | htable = calloc(htablesize, sizeof htable[0]); | ||
121 | if (htable == NULL) { | ||
122 | errno = ENOMEM; | ||
123 | return 0; | ||
124 | } | ||
125 | |||
126 | /* Initialize it. */ | ||
127 | for (idx = 0; idx < htablesize; idx++) | ||
128 | SLIST_INIT(&htable[idx]); | ||
129 | |||
130 | return 1; | ||
131 | } | ||
132 | |||
133 | void | ||
134 | hdestroy(void) | ||
135 | { | ||
136 | struct internal_entry *ie; | ||
137 | size_t idx; | ||
138 | |||
139 | _DIAGASSERT(htable != NULL); | ||
140 | if (htable == NULL) | ||
141 | return; | ||
142 | |||
143 | for (idx = 0; idx < htablesize; idx++) { | ||
144 | while (!SLIST_EMPTY(&htable[idx])) { | ||
145 | ie = SLIST_FIRST(&htable[idx]); | ||
146 | SLIST_REMOVE_HEAD(&htable[idx], link); | ||
147 | free(ie->ent.key); | ||
148 | free(ie); | ||
149 | } | ||
150 | } | ||
151 | free(htable); | ||
152 | htable = NULL; | ||
153 | } | ||
154 | |||
155 | ENTRY * | ||
156 | hsearch(ENTRY item, ACTION action) | ||
157 | { | ||
158 | struct internal_head *head; | ||
159 | struct internal_entry *ie; | ||
160 | uint32_t hashval; | ||
161 | size_t len; | ||
162 | |||
163 | _DIAGASSERT(htable != NULL); | ||
164 | _DIAGASSERT(item.key != NULL); | ||
165 | _DIAGASSERT(action == ENTER || action == FIND); | ||
166 | |||
167 | len = strlen(item.key); | ||
168 | hashval = (*__default_hash)(item.key, len); | ||
169 | |||
170 | head = &htable[hashval & (htablesize - 1)]; | ||
171 | ie = SLIST_FIRST(head); | ||
172 | while (ie != NULL) { | ||
173 | if (strcmp(ie->ent.key, item.key) == 0) | ||
174 | break; | ||
175 | ie = SLIST_NEXT(ie, link); | ||
176 | } | ||
177 | |||
178 | if (ie != NULL) | ||
179 | return &ie->ent; | ||
180 | else if (action == FIND) | ||
181 | return NULL; | ||
182 | |||
183 | ie = malloc(sizeof *ie); | ||
184 | if (ie == NULL) | ||
185 | return NULL; | ||
186 | ie->ent.key = item.key; | ||
187 | ie->ent.data = item.data; | ||
188 | |||
189 | SLIST_INSERT_HEAD(head, ie, link); | ||
190 | return &ie->ent; | ||
191 | } | ||
diff --git a/src/lib/libc/stdlib/heapsort.c b/src/lib/libc/stdlib/heapsort.c new file mode 100644 index 0000000000..e6fb61ba2f --- /dev/null +++ b/src/lib/libc/stdlib/heapsort.c | |||
@@ -0,0 +1,173 @@ | |||
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 | #include <sys/types.h> | ||
34 | #include <errno.h> | ||
35 | #include <stdlib.h> | ||
36 | |||
37 | /* | ||
38 | * Swap two areas of size number of bytes. Although qsort(3) permits random | ||
39 | * blocks of memory to be sorted, sorting pointers is almost certainly the | ||
40 | * common case (and, were it not, could easily be made so). Regardless, it | ||
41 | * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer | ||
42 | * arithmetic gets lost in the time required for comparison function calls. | ||
43 | */ | ||
44 | #define SWAP(a, b, count, size, tmp) { \ | ||
45 | count = size; \ | ||
46 | do { \ | ||
47 | tmp = *a; \ | ||
48 | *a++ = *b; \ | ||
49 | *b++ = tmp; \ | ||
50 | } while (--count); \ | ||
51 | } | ||
52 | |||
53 | /* Copy one block of size size to another. */ | ||
54 | #define COPY(a, b, count, size, tmp1, tmp2) { \ | ||
55 | count = size; \ | ||
56 | tmp1 = a; \ | ||
57 | tmp2 = b; \ | ||
58 | do { \ | ||
59 | *tmp1++ = *tmp2++; \ | ||
60 | } while (--count); \ | ||
61 | } | ||
62 | |||
63 | /* | ||
64 | * Build the list into a heap, where a heap is defined such that for | ||
65 | * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N. | ||
66 | * | ||
67 | * There are two cases. If j == nmemb, select largest of Ki and Kj. If | ||
68 | * j < nmemb, select largest of Ki, Kj and Kj+1. | ||
69 | */ | ||
70 | #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ | ||
71 | for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ | ||
72 | par_i = child_i) { \ | ||
73 | child = base + child_i * size; \ | ||
74 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
75 | child += size; \ | ||
76 | ++child_i; \ | ||
77 | } \ | ||
78 | par = base + par_i * size; \ | ||
79 | if (compar(child, par) <= 0) \ | ||
80 | break; \ | ||
81 | SWAP(par, child, count, size, tmp); \ | ||
82 | } \ | ||
83 | } | ||
84 | |||
85 | /* | ||
86 | * Select the top of the heap and 'heapify'. Since by far the most expensive | ||
87 | * action is the call to the compar function, a considerable optimization | ||
88 | * in the average case can be achieved due to the fact that k, the displaced | ||
89 | * element, is usually quite small, so it would be preferable to first | ||
90 | * heapify, always maintaining the invariant that the larger child is copied | ||
91 | * over its parent's record. | ||
92 | * | ||
93 | * Then, starting from the *bottom* of the heap, finding k's correct place, | ||
94 | * again maintaining the invariant. As a result of the invariant no element | ||
95 | * is 'lost' when k is assigned its correct place in the heap. | ||
96 | * | ||
97 | * The time savings from this optimization are on the order of 15-20% for the | ||
98 | * average case. See Knuth, Vol. 3, page 158, problem 18. | ||
99 | * | ||
100 | * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset. | ||
101 | */ | ||
102 | #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \ | ||
103 | for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \ | ||
104 | child = base + child_i * size; \ | ||
105 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
106 | child += size; \ | ||
107 | ++child_i; \ | ||
108 | } \ | ||
109 | par = base + par_i * size; \ | ||
110 | COPY(par, child, count, size, tmp1, tmp2); \ | ||
111 | } \ | ||
112 | for (;;) { \ | ||
113 | child_i = par_i; \ | ||
114 | par_i = child_i / 2; \ | ||
115 | child = base + child_i * size; \ | ||
116 | par = base + par_i * size; \ | ||
117 | if (child_i == 1 || compar(k, par) < 0) { \ | ||
118 | COPY(child, k, count, size, tmp1, tmp2); \ | ||
119 | break; \ | ||
120 | } \ | ||
121 | COPY(child, par, count, size, tmp1, tmp2); \ | ||
122 | } \ | ||
123 | } | ||
124 | |||
125 | /* | ||
126 | * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average | ||
127 | * and worst. While heapsort is faster than the worst case of quicksort, | ||
128 | * the BSD quicksort does median selection so that the chance of finding | ||
129 | * a data set that will trigger the worst case is nonexistent. Heapsort's | ||
130 | * only advantage over quicksort is that it requires little additional memory. | ||
131 | */ | ||
132 | int | ||
133 | heapsort(void *vbase, size_t nmemb, size_t size, | ||
134 | int (*compar)(const void *, const void *)) | ||
135 | { | ||
136 | int cnt, i, j, l; | ||
137 | char tmp, *tmp1, *tmp2; | ||
138 | char *base, *k, *p, *t; | ||
139 | |||
140 | if (nmemb <= 1) | ||
141 | return (0); | ||
142 | |||
143 | if (!size) { | ||
144 | errno = EINVAL; | ||
145 | return (-1); | ||
146 | } | ||
147 | |||
148 | if ((k = malloc(size)) == NULL) | ||
149 | return (-1); | ||
150 | |||
151 | /* | ||
152 | * Items are numbered from 1 to nmemb, so offset from size bytes | ||
153 | * below the starting address. | ||
154 | */ | ||
155 | base = (char *)vbase - size; | ||
156 | |||
157 | for (l = nmemb / 2 + 1; --l;) | ||
158 | CREATE(l, nmemb, i, j, t, p, size, cnt, tmp); | ||
159 | |||
160 | /* | ||
161 | * For each element of the heap, save the largest element into its | ||
162 | * final slot, save the displaced element (k), then recreate the | ||
163 | * heap. | ||
164 | */ | ||
165 | while (nmemb > 1) { | ||
166 | COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2); | ||
167 | COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2); | ||
168 | --nmemb; | ||
169 | SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2); | ||
170 | } | ||
171 | free(k); | ||
172 | return (0); | ||
173 | } | ||
diff --git a/src/lib/libc/stdlib/imaxabs.3 b/src/lib/libc/stdlib/imaxabs.3 new file mode 100644 index 0000000000..10933e0a66 --- /dev/null +++ b/src/lib/libc/stdlib/imaxabs.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 | .\" 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: imaxabs.3,v 1.3 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
35 | .Dt IMAXABS 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm imaxabs | ||
39 | .Nd integer absolute value function | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <inttypes.h> | ||
42 | .Ft intmax_t | ||
43 | .Fn imaxabs "intmax_t j" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn imaxabs | ||
47 | function computes the absolute value of the intmax_t variable | ||
48 | .Fa j . | ||
49 | .Sh RETURN VALUES | ||
50 | The | ||
51 | .Fn imaxabs | ||
52 | function returns the absolute value. | ||
53 | .Sh SEE ALSO | ||
54 | .Xr abs 3 , | ||
55 | .Xr cabs 3 , | ||
56 | .Xr floor 3 , | ||
57 | .Xr hypot 3 , | ||
58 | .Xr labs 3 , | ||
59 | .Xr math 3 | ||
60 | .Sh STANDARDS | ||
61 | The | ||
62 | .Fn abs | ||
63 | function conforms to | ||
64 | .St -ansiC-99 . | ||
65 | .Sh BUGS | ||
66 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/imaxabs.c b/src/lib/libc/stdlib/imaxabs.c new file mode 100644 index 0000000000..b7e910eefd --- /dev/null +++ b/src/lib/libc/stdlib/imaxabs.c | |||
@@ -0,0 +1,38 @@ | |||
1 | /* $OpenBSD: imaxabs.c,v 1.1 2006/01/13 17:58:09 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. 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 <inttypes.h> | ||
33 | |||
34 | intmax_t | ||
35 | imaxabs(intmax_t j) | ||
36 | { | ||
37 | return (j < 0 ? -j : j); | ||
38 | } | ||
diff --git a/src/lib/libc/stdlib/imaxdiv.3 b/src/lib/libc/stdlib/imaxdiv.3 new file mode 100644 index 0000000000..c4dee627aa --- /dev/null +++ b/src/lib/libc/stdlib/imaxdiv.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: imaxdiv.3,v 1.3 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
35 | .Dt IMAXDIV 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm imaxdiv | ||
39 | .Nd return quotient and remainder from division | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <inttypes.h> | ||
42 | .Ft imaxdiv_t | ||
43 | .Fn imaxdiv "intmax_t num" "intmax_t denom" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn imaxdiv | ||
47 | function computes the value | ||
48 | .Fa num Ns / Ns Fa denom | ||
49 | and returns the quotient and remainder in a structure named | ||
50 | .Li imaxdiv_t | ||
51 | that contains two | ||
52 | .Li intmax_t | ||
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 lldiv 3 , | ||
61 | .Xr math 3 , | ||
62 | .Xr qdiv 3 | ||
63 | .Sh STANDARDS | ||
64 | The | ||
65 | .Fn imaxdiv | ||
66 | function conforms to | ||
67 | .St -ansiC-99 . | ||
diff --git a/src/lib/libc/stdlib/imaxdiv.c b/src/lib/libc/stdlib/imaxdiv.c new file mode 100644 index 0000000000..0515a94b96 --- /dev/null +++ b/src/lib/libc/stdlib/imaxdiv.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: imaxdiv.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1990 Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <inttypes.h> /* imaxdiv_t */ | ||
35 | |||
36 | imaxdiv_t | ||
37 | imaxdiv(intmax_t num, intmax_t denom) | ||
38 | { | ||
39 | imaxdiv_t r; | ||
40 | |||
41 | /* see div.c for comments */ | ||
42 | |||
43 | r.quot = num / denom; | ||
44 | r.rem = num % denom; | ||
45 | if (num >= 0 && r.rem < 0) { | ||
46 | r.quot++; | ||
47 | r.rem -= denom; | ||
48 | } | ||
49 | return (r); | ||
50 | } | ||
diff --git a/src/lib/libc/stdlib/insque.3 b/src/lib/libc/stdlib/insque.3 new file mode 100644 index 0000000000..dcb2b0d158 --- /dev/null +++ b/src/lib/libc/stdlib/insque.3 | |||
@@ -0,0 +1,106 @@ | |||
1 | .\" $OpenBSD: insque.3,v 1.7 2007/05/31 19:19:31 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 $Mdocdate: May 31 2007 $ | ||
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 -offset indent | ||
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 | .Pp | ||
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 insque | ||
87 | and | ||
88 | .Fn remque | ||
89 | functions conform to the | ||
90 | .St -p1003.1-2001 | ||
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 | ||
100 | .Li insque | ||
101 | and | ||
102 | .Li remque | ||
103 | instructions on the | ||
104 | .Tn VAX . | ||
105 | They first appeared in | ||
106 | .Bx 4.2 . | ||
diff --git a/src/lib/libc/stdlib/insque.c b/src/lib/libc/stdlib/insque.c new file mode 100644 index 0000000000..8724efec74 --- /dev/null +++ b/src/lib/libc/stdlib/insque.c | |||
@@ -0,0 +1,48 @@ | |||
1 | /* $OpenBSD: insque.c,v 1.2 2005/08/08 08:05:36 espie 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 | #include <search.h> | ||
32 | |||
33 | struct qelem { | ||
34 | struct qelem *q_forw; | ||
35 | struct qelem *q_back; | ||
36 | }; | ||
37 | |||
38 | void | ||
39 | insque(void *entry, void *pred) | ||
40 | { | ||
41 | struct qelem *e = (struct qelem *) entry; | ||
42 | struct qelem *p = (struct qelem *) pred; | ||
43 | |||
44 | e->q_forw = p->q_forw; | ||
45 | e->q_back = p; | ||
46 | p->q_forw->q_back = e; | ||
47 | p->q_forw = e; | ||
48 | } | ||
diff --git a/src/lib/libc/stdlib/jrand48.c b/src/lib/libc/stdlib/jrand48.c new file mode 100644 index 0000000000..cb8c592750 --- /dev/null +++ b/src/lib/libc/stdlib/jrand48.c | |||
@@ -0,0 +1,22 @@ | |||
1 | /* $OpenBSD: jrand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | long | ||
18 | jrand48(unsigned short xseed[3]) | ||
19 | { | ||
20 | __dorand48(xseed); | ||
21 | return ((long) xseed[2] << 16) + (long) xseed[1]; | ||
22 | } | ||
diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c new file mode 100644 index 0000000000..4f33df37b2 --- /dev/null +++ b/src/lib/libc/stdlib/l64a.c | |||
@@ -0,0 +1,42 @@ | |||
1 | /* $OpenBSD: l64a.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include <errno.h> | ||
8 | #include <stdlib.h> | ||
9 | |||
10 | char * | ||
11 | l64a(long value) | ||
12 | { | ||
13 | static char buf[8]; | ||
14 | char *s = buf; | ||
15 | int digit; | ||
16 | int i; | ||
17 | |||
18 | if (value < 0) { | ||
19 | errno = EINVAL; | ||
20 | return(NULL); | ||
21 | } | ||
22 | |||
23 | for (i = 0; value != 0 && i < 6; i++) { | ||
24 | digit = value & 0x3f; | ||
25 | |||
26 | if (digit < 2) | ||
27 | *s = digit + '.'; | ||
28 | else if (digit < 12) | ||
29 | *s = digit + '0' - 2; | ||
30 | else if (digit < 38) | ||
31 | *s = digit + 'A' - 12; | ||
32 | else | ||
33 | *s = digit + 'a' - 38; | ||
34 | |||
35 | value >>= 6; | ||
36 | s++; | ||
37 | } | ||
38 | |||
39 | *s = '\0'; | ||
40 | |||
41 | return(buf); | ||
42 | } | ||
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3 new file mode 100644 index 0000000000..21c134fe5d --- /dev/null +++ b/src/lib/libc/stdlib/labs.3 | |||
@@ -0,0 +1,69 @@ | |||
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.9 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 imaxabs 3 , | ||
60 | .Xr math 3 | ||
61 | .Sh STANDARDS | ||
62 | The | ||
63 | .Fn labs | ||
64 | and | ||
65 | .Fn llabs | ||
66 | functions conform to | ||
67 | .St -ansiC-99 . | ||
68 | .Sh BUGS | ||
69 | 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..ca60b9aba2 --- /dev/null +++ b/src/lib/libc/stdlib/labs.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* $OpenBSD: labs.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | |||
33 | long | ||
34 | labs(long j) | ||
35 | { | ||
36 | return(j < 0 ? -j : j); | ||
37 | } | ||
diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c new file mode 100644 index 0000000000..2cf5c271ba --- /dev/null +++ b/src/lib/libc/stdlib/lcong48.c | |||
@@ -0,0 +1,31 @@ | |||
1 | /* $OpenBSD: lcong48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | extern unsigned short __rand48_seed[3]; | ||
18 | extern unsigned short __rand48_mult[3]; | ||
19 | extern unsigned short __rand48_add; | ||
20 | |||
21 | void | ||
22 | lcong48(unsigned short p[7]) | ||
23 | { | ||
24 | __rand48_seed[0] = p[0]; | ||
25 | __rand48_seed[1] = p[1]; | ||
26 | __rand48_seed[2] = p[2]; | ||
27 | __rand48_mult[0] = p[3]; | ||
28 | __rand48_mult[1] = p[4]; | ||
29 | __rand48_mult[2] = p[5]; | ||
30 | __rand48_add = p[6]; | ||
31 | } | ||
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3 new file mode 100644 index 0000000000..9627776899 --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.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: ldiv.3,v 1.9 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 / 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 imaxdiv 3 , | ||
60 | .Xr lldiv 3 , | ||
61 | .Xr math 3 , | ||
62 | .Xr qdiv 3 | ||
63 | .Sh STANDARDS | ||
64 | The | ||
65 | .Fn ldiv | ||
66 | function conforms to | ||
67 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c new file mode 100644 index 0000000000..775065f525 --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: ldiv.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1990 Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <stdlib.h> /* ldiv_t */ | ||
35 | |||
36 | ldiv_t | ||
37 | ldiv(long num, long denom) | ||
38 | { | ||
39 | ldiv_t r; | ||
40 | |||
41 | /* see div.c for comments */ | ||
42 | |||
43 | r.quot = num / denom; | ||
44 | r.rem = num % denom; | ||
45 | if (num >= 0 && r.rem < 0) { | ||
46 | r.quot++; | ||
47 | r.rem -= denom; | ||
48 | } | ||
49 | return (r); | ||
50 | } | ||
diff --git a/src/lib/libc/stdlib/llabs.c b/src/lib/libc/stdlib/llabs.c new file mode 100644 index 0000000000..fc2cd8261c --- /dev/null +++ b/src/lib/libc/stdlib/llabs.c | |||
@@ -0,0 +1,38 @@ | |||
1 | /* $OpenBSD: llabs.c,v 1.3 2007/01/08 19:39:25 deraadt 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. 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 <stdlib.h> | ||
33 | |||
34 | long long | ||
35 | llabs(long long j) | ||
36 | { | ||
37 | return (j < 0 ? -j : j); | ||
38 | } | ||
diff --git a/src/lib/libc/stdlib/lldiv.3 b/src/lib/libc/stdlib/lldiv.3 new file mode 100644 index 0000000000..a5be188ba2 --- /dev/null +++ b/src/lib/libc/stdlib/lldiv.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: lldiv.3,v 1.2 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
35 | .Dt LLDIV 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm lldiv | ||
39 | .Nd return quotient and remainder from division | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft lldiv_t | ||
43 | .Fn lldiv "long long num" "long long denom" | ||
44 | .Sh DESCRIPTION | ||
45 | The | ||
46 | .Fn lldiv | ||
47 | function computes the value | ||
48 | .Fa num Ns / Ns Fa denom | ||
49 | and returns the quotient and remainder in a structure named | ||
50 | .Li lldiv_t | ||
51 | that contains two | ||
52 | .Li long long integer | ||
53 | members named | ||
54 | .Fa quot | ||
55 | and | ||
56 | .Fa rem . | ||
57 | .Sh SEE ALSO | ||
58 | .Xr div 3 , | ||
59 | .Xr imaxdiv 3 , | ||
60 | .Xr ldiv 3 , | ||
61 | .Xr math 3 , | ||
62 | .Xr qdiv 3 | ||
63 | .Sh STANDARDS | ||
64 | The | ||
65 | .Fn lldiv | ||
66 | function conforms to | ||
67 | .St -ansiC-99 . | ||
diff --git a/src/lib/libc/stdlib/lldiv.c b/src/lib/libc/stdlib/lldiv.c new file mode 100644 index 0000000000..e8d6c978b3 --- /dev/null +++ b/src/lib/libc/stdlib/lldiv.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: lldiv.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1990 Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <stdlib.h> /* lldiv_t */ | ||
35 | |||
36 | lldiv_t | ||
37 | lldiv(long long num, long long denom) | ||
38 | { | ||
39 | lldiv_t r; | ||
40 | |||
41 | /* see div.c for comments */ | ||
42 | |||
43 | r.quot = num / denom; | ||
44 | r.rem = num % denom; | ||
45 | if (num >= 0 && r.rem < 0) { | ||
46 | r.quot++; | ||
47 | r.rem -= denom; | ||
48 | } | ||
49 | return (r); | ||
50 | } | ||
diff --git a/src/lib/libc/stdlib/lrand48.c b/src/lib/libc/stdlib/lrand48.c new file mode 100644 index 0000000000..21beb858ca --- /dev/null +++ b/src/lib/libc/stdlib/lrand48.c | |||
@@ -0,0 +1,24 @@ | |||
1 | /* $OpenBSD: lrand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | extern unsigned short __rand48_seed[3]; | ||
18 | |||
19 | long | ||
20 | lrand48(void) | ||
21 | { | ||
22 | __dorand48(__rand48_seed); | ||
23 | return ((long) __rand48_seed[2] << 15) + ((long) __rand48_seed[1] >> 1); | ||
24 | } | ||
diff --git a/src/lib/libc/stdlib/lsearch.3 b/src/lib/libc/stdlib/lsearch.3 new file mode 100644 index 0000000000..e13ef4ec74 --- /dev/null +++ b/src/lib/libc/stdlib/lsearch.3 | |||
@@ -0,0 +1,108 @@ | |||
1 | .\" $OpenBSD: lsearch.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1989, 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 | .\" @(#)lsearch.3 8.1 (Berkeley) 6/4/93 | ||
31 | .\" | ||
32 | .Dd $Mdocdate: May 31 2007 $ | ||
33 | .Dt LSEARCH 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm lsearch , | ||
37 | .Nm lfind | ||
38 | .Nd linear searching routines | ||
39 | .Sh SYNOPSIS | ||
40 | .Fd #include <search.h> | ||
41 | .Ft char * | ||
42 | .Fn lsearch "const void *key" "const void *base" "size_t *nelp" \ | ||
43 | "size_t width" "int (*compar)(void *, void *)" | ||
44 | .Ft char * | ||
45 | .Fn lfind "const void *key" "const void *base" "size_t *nelp" \ | ||
46 | "size_t width" "int (*compar)(void *, void *)" | ||
47 | .Sh DESCRIPTION | ||
48 | The functions | ||
49 | .Fn lsearch | ||
50 | and | ||
51 | .Fn lfind | ||
52 | provide basic linear searching functionality. | ||
53 | .Pp | ||
54 | .Fa base | ||
55 | is the pointer to the beginning of an array. | ||
56 | The argument | ||
57 | .Fa nelp | ||
58 | is the current number of elements in the array, where each element | ||
59 | is | ||
60 | .Fa width | ||
61 | bytes long. | ||
62 | The | ||
63 | .Fa compar | ||
64 | function | ||
65 | is a comparison routine which is used to compare two elements. | ||
66 | It takes two arguments which point to the | ||
67 | .Fa key | ||
68 | object and to an array member, in that order, and must return an integer | ||
69 | less than, equivalent to, or greater than zero if the | ||
70 | .Fa key | ||
71 | object is considered, respectively, to be less than, equal to, or greater | ||
72 | than the array member. | ||
73 | .Pp | ||
74 | The | ||
75 | .Fn lsearch | ||
76 | and | ||
77 | .Fn lfind | ||
78 | functions | ||
79 | return a pointer into the array referenced by | ||
80 | .Fa base | ||
81 | where | ||
82 | .Fa key | ||
83 | is located. | ||
84 | If | ||
85 | .Fa key | ||
86 | does not exist, | ||
87 | .Fn lfind | ||
88 | will return a null pointer and | ||
89 | .Fn lsearch | ||
90 | will add it to the array. | ||
91 | When an element is added to the array by | ||
92 | .Fn lsearch , | ||
93 | the location referenced by the argument | ||
94 | .Fa nelp | ||
95 | is incremented by one. | ||
96 | .Sh SEE ALSO | ||
97 | .Xr bsearch 3 , | ||
98 | .Xr db 3 | ||
99 | .Sh STANDARDS | ||
100 | The | ||
101 | .Fn lsearch | ||
102 | and | ||
103 | .Fn lfind | ||
104 | functions conform to the | ||
105 | .St -p1003.1-2001 | ||
106 | and | ||
107 | .St -xpg4.3 | ||
108 | specifications. | ||
diff --git a/src/lib/libc/stdlib/lsearch.c b/src/lib/libc/stdlib/lsearch.c new file mode 100644 index 0000000000..0815430199 --- /dev/null +++ b/src/lib/libc/stdlib/lsearch.c | |||
@@ -0,0 +1,88 @@ | |||
1 | /* $OpenBSD: lsearch.c,v 1.3 2004/10/01 04:08:45 jsg Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1989, 1993 | ||
5 | * The Regents of the University of California. All rights reserved. | ||
6 | * | ||
7 | * This code is derived from software contributed to Berkeley by | ||
8 | * Roger L. Snyder. | ||
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. Neither the name of the University nor the names of its contributors | ||
19 | * may be used to endorse or promote products derived from this software | ||
20 | * without specific prior written permission. | ||
21 | * | ||
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
32 | * SUCH DAMAGE. | ||
33 | */ | ||
34 | |||
35 | #if defined(LIBC_SCCS) && !defined(lint) | ||
36 | static char sccsid[] = "@(#)lsearch.c 8.1 (Berkeley) 6/4/93"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <sys/types.h> | ||
40 | #include <string.h> | ||
41 | #include <search.h> | ||
42 | |||
43 | typedef int (*cmp_fn_t)(const void *, const void *); | ||
44 | static void *linear_base(const void *, const void *, size_t *, size_t, | ||
45 | cmp_fn_t, int); | ||
46 | |||
47 | void * | ||
48 | lsearch(const void *key, const void *base, size_t *nelp, size_t width, | ||
49 | cmp_fn_t compar) | ||
50 | { | ||
51 | |||
52 | return(linear_base(key, base, nelp, width, compar, 1)); | ||
53 | } | ||
54 | |||
55 | void * | ||
56 | lfind(const void *key, const void *base, size_t *nelp, size_t width, | ||
57 | cmp_fn_t compar) | ||
58 | { | ||
59 | return(linear_base(key, base, nelp, width, compar, 0)); | ||
60 | } | ||
61 | |||
62 | static void * | ||
63 | linear_base(const void *key, const void *base, size_t *nelp, size_t width, | ||
64 | cmp_fn_t compar, int add_flag) | ||
65 | { | ||
66 | const char *element, *end; | ||
67 | |||
68 | end = (const char *)base + *nelp * width; | ||
69 | for (element = base; element < end; element += width) | ||
70 | if (!compar(key, element)) /* key found */ | ||
71 | return((void *)element); | ||
72 | |||
73 | if (!add_flag) /* key not found */ | ||
74 | return(NULL); | ||
75 | |||
76 | /* | ||
77 | * The UNIX System User's Manual, 1986 edition claims that | ||
78 | * a NULL pointer is returned by lsearch with errno set | ||
79 | * appropriately, if there is not enough room in the table | ||
80 | * to add a new item. This can't be done as none of these | ||
81 | * routines have any method of determining the size of the | ||
82 | * table. This comment isn't in the 1986-87 System V | ||
83 | * manual. | ||
84 | */ | ||
85 | ++*nelp; | ||
86 | memcpy((void *)end, key, width); | ||
87 | return((void *)end); | ||
88 | } | ||
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 new file mode 100644 index 0000000000..c3566e37e8 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -0,0 +1,448 @@ | |||
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.60 2008/12/30 07:44:51 djm Exp $ | ||
34 | .\" | ||
35 | .Dd $Mdocdate: December 30 2008 $ | ||
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 | When using | ||
87 | .Fn malloc | ||
88 | be careful to avoid the following idiom: | ||
89 | .Bd -literal -offset indent | ||
90 | if ((p = malloc(num * size)) == NULL) | ||
91 | err(1, "malloc"); | ||
92 | .Ed | ||
93 | .Pp | ||
94 | The multiplication may lead to an integer overflow. | ||
95 | To avoid this, | ||
96 | .Fn calloc | ||
97 | is recommended. | ||
98 | .Pp | ||
99 | If | ||
100 | .Fn malloc | ||
101 | must be used, be sure to test for overflow: | ||
102 | .Bd -literal -offset indent | ||
103 | if (size && num > SIZE_MAX / size) { | ||
104 | errno = ENOMEM; | ||
105 | err(1, "overflow"); | ||
106 | } | ||
107 | .Ed | ||
108 | .Pp | ||
109 | The | ||
110 | .Fn calloc | ||
111 | function allocates space for an array of | ||
112 | .Fa nmemb | ||
113 | objects, each of whose size is | ||
114 | .Fa size . | ||
115 | The space is initialized to zero. | ||
116 | The use of | ||
117 | .Fn calloc | ||
118 | is strongly encouraged when allocating multiple sized objects | ||
119 | in order to avoid possible integer overflows. | ||
120 | .Pp | ||
121 | The | ||
122 | .Fn free | ||
123 | function causes the space pointed to by | ||
124 | .Fa ptr | ||
125 | to be either placed on a list of free pages to make it available for future | ||
126 | allocation or, if required, to be returned to the kernel using | ||
127 | .Xr munmap 2 . | ||
128 | If | ||
129 | .Fa ptr | ||
130 | is a null pointer, no action occurs. | ||
131 | .Pp | ||
132 | A | ||
133 | .Fn cfree | ||
134 | function is also provided for compatibility with old systems and other | ||
135 | .Nm malloc | ||
136 | libraries; it is simply an alias for | ||
137 | .Fn free . | ||
138 | .Pp | ||
139 | The | ||
140 | .Fn realloc | ||
141 | function changes the size of the object pointed to by | ||
142 | .Fa ptr | ||
143 | to | ||
144 | .Fa size | ||
145 | bytes and returns a pointer to the (possibly moved) object. | ||
146 | The contents of the object are unchanged up to the lesser | ||
147 | of the new and old sizes. | ||
148 | If the new size is larger, the value of the newly allocated portion | ||
149 | of the object is indeterminate and uninitialized. | ||
150 | If | ||
151 | .Fa ptr | ||
152 | is a null pointer, the | ||
153 | .Fn realloc | ||
154 | function behaves like the | ||
155 | .Fn malloc | ||
156 | function for the specified size. | ||
157 | If the space cannot be allocated, the object | ||
158 | pointed to by | ||
159 | .Fa ptr | ||
160 | is unchanged. | ||
161 | If | ||
162 | .Fa size | ||
163 | is zero and | ||
164 | .Fa ptr | ||
165 | is not a null pointer, the object it points to is freed and a new zero size | ||
166 | object is returned. | ||
167 | .Pp | ||
168 | When using | ||
169 | .Fn realloc | ||
170 | be careful to avoid the following idiom: | ||
171 | .Bd -literal -offset indent | ||
172 | size += 50; | ||
173 | if ((p = realloc(p, size)) == NULL) | ||
174 | return (NULL); | ||
175 | .Ed | ||
176 | .Pp | ||
177 | Do not adjust the variable describing how much memory has been allocated | ||
178 | until the allocation has been successful. | ||
179 | This can cause aberrant program behavior if the incorrect size value is used. | ||
180 | In most cases, the above sample will also result in a leak of memory. | ||
181 | As stated earlier, a return value of | ||
182 | .Dv NULL | ||
183 | indicates that the old object still remains allocated. | ||
184 | Better code looks like this: | ||
185 | .Bd -literal -offset indent | ||
186 | newsize = size + 50; | ||
187 | if ((newp = realloc(p, newsize)) == NULL) { | ||
188 | free(p); | ||
189 | p = NULL; | ||
190 | size = 0; | ||
191 | return (NULL); | ||
192 | } | ||
193 | p = newp; | ||
194 | size = newsize; | ||
195 | .Ed | ||
196 | .Pp | ||
197 | As with | ||
198 | .Fn malloc | ||
199 | it is important to ensure the new size value will not overflow; | ||
200 | i.e. avoid allocations like the following: | ||
201 | .Bd -literal -offset indent | ||
202 | if ((newp = realloc(p, num * size)) == NULL) { | ||
203 | ... | ||
204 | .Ed | ||
205 | .Pp | ||
206 | Malloc will first look for a symbolic link called | ||
207 | .Pa /etc/malloc.conf | ||
208 | and next check the environment for a variable called | ||
209 | .Ev MALLOC_OPTIONS | ||
210 | and finally for the global variable | ||
211 | .Va malloc_options | ||
212 | and scan them for flags in that order. | ||
213 | Flags are single letters, uppercase means on, lowercase means off. | ||
214 | .Bl -tag -width indent | ||
215 | .It Cm A | ||
216 | .Dq Abort . | ||
217 | .Fn malloc | ||
218 | will coredump the process, rather than tolerate internal | ||
219 | inconsistencies or incorrect usage. | ||
220 | This is the default and a very handy debugging aid, | ||
221 | since the core file represents the time of failure, | ||
222 | rather than when the bogus pointer was used. | ||
223 | .It Cm D | ||
224 | .Dq Dump . | ||
225 | .Fn malloc | ||
226 | will dump statistics in a file called | ||
227 | .Pa malloc.out | ||
228 | at exit. | ||
229 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
230 | order to have any effect. | ||
231 | .It Cm F | ||
232 | .Dq Freeguard . | ||
233 | Enable use after free protection. | ||
234 | Unused pages on the freelist are read and write protected to | ||
235 | cause a segmentation fault upon access. | ||
236 | .It Cm G | ||
237 | .Dq Guard . | ||
238 | Enable guard pages. | ||
239 | Each page size or larger allocation is followed by a guard page that will | ||
240 | cause a segmentation fault upon any access. | ||
241 | .It Cm H | ||
242 | .Dq Hint . | ||
243 | Pass a hint to the kernel about pages we don't use. | ||
244 | If the machine is paging a lot this may help a bit. | ||
245 | .It Cm J | ||
246 | .Dq Junk . | ||
247 | Fill some junk into the area allocated. | ||
248 | Currently junk is bytes of 0xd0 when allocating; this is pronounced | ||
249 | .Dq Duh . | ||
250 | \&:-) | ||
251 | Freed chunks are filled with 0xdf. | ||
252 | .It Cm P | ||
253 | .Dq Move allocations within a page. | ||
254 | Allocations larger than half a page but smaller than a page | ||
255 | are aligned to the end of a page to catch buffer overruns in more | ||
256 | cases. | ||
257 | This is the default. | ||
258 | .It Cm R | ||
259 | .Dq realloc . | ||
260 | Always reallocate when | ||
261 | .Fn realloc | ||
262 | is called, even if the initial allocation was big enough. | ||
263 | This can substantially aid in compacting memory. | ||
264 | .\".Pp | ||
265 | .\".It Cm U | ||
266 | .\".Dq utrace . | ||
267 | .\"Generate entries for | ||
268 | .\".Xr ktrace 1 | ||
269 | .\"for all operations. | ||
270 | .\"Consult the source for this one. | ||
271 | .It Cm X | ||
272 | .Dq xmalloc . | ||
273 | Rather than return failure, | ||
274 | .Xr abort 3 | ||
275 | the program with a diagnostic message on stderr. | ||
276 | It is the intention that this option be set at compile time by | ||
277 | including in the source: | ||
278 | .Bd -literal -offset indent | ||
279 | extern char *malloc_options; | ||
280 | malloc_options = "X"; | ||
281 | .Ed | ||
282 | .Pp | ||
283 | Note that this will cause code that is supposed to handle | ||
284 | out-of-memory conditions gracefully to abort instead. | ||
285 | .It Cm Z | ||
286 | .Dq Zero . | ||
287 | Fill some junk into the area allocated (see | ||
288 | .Cm J ) , | ||
289 | except for the exact length the user asked for, which is zeroed. | ||
290 | .It Cm < | ||
291 | .Dq Half the cache size . | ||
292 | Decrease the size of the free page cache by a factor of two. | ||
293 | .It Cm > | ||
294 | .Dq Double the cache size . | ||
295 | Increase the size of the free page cache by a factor of two. | ||
296 | .El | ||
297 | .Pp | ||
298 | So to set a systemwide reduction of cache size and use guard pages: | ||
299 | .Dl # ln -s 'G\*(Lt' /etc/malloc.conf | ||
300 | .Pp | ||
301 | The | ||
302 | .Cm J | ||
303 | and | ||
304 | .Cm Z | ||
305 | flags are mostly for testing and debugging. | ||
306 | If a program changes behavior if either of these options are used, | ||
307 | it is buggy. | ||
308 | .Pp | ||
309 | The default number of free pages cached is 64. | ||
310 | .Sh RETURN VALUES | ||
311 | The | ||
312 | .Fn malloc | ||
313 | and | ||
314 | .Fn calloc | ||
315 | functions return a pointer to the allocated space if successful; otherwise, | ||
316 | a null pointer is returned and | ||
317 | .Va errno | ||
318 | is set to | ||
319 | .Er ENOMEM . | ||
320 | .Pp | ||
321 | The | ||
322 | .Fn free | ||
323 | and | ||
324 | .Fn cfree | ||
325 | functions return no value. | ||
326 | .Pp | ||
327 | The | ||
328 | .Fn realloc | ||
329 | function returns a pointer to the (possibly moved) allocated space | ||
330 | if successful; otherwise, a null pointer is returned and | ||
331 | .Va errno | ||
332 | is set to | ||
333 | .Er ENOMEM . | ||
334 | .Sh ENVIRONMENT | ||
335 | .Bl -tag -width Ev | ||
336 | .It Ev MALLOC_OPTIONS | ||
337 | See above. | ||
338 | .El | ||
339 | .Sh FILES | ||
340 | .Bl -tag -width "/etc/malloc.conf" | ||
341 | .It Pa /etc/malloc.conf | ||
342 | symbolic link to filename containing option flags | ||
343 | .El | ||
344 | .Sh DIAGNOSTICS | ||
345 | If | ||
346 | .Fn malloc , | ||
347 | .Fn calloc , | ||
348 | .Fn realloc , | ||
349 | or | ||
350 | .Fn free | ||
351 | detect an error condition, | ||
352 | a message will be printed to file descriptor | ||
353 | 2 (not using stdio). | ||
354 | Errors will result in the process being aborted, | ||
355 | unless the | ||
356 | .Cm a | ||
357 | option has been specified. | ||
358 | .Pp | ||
359 | Here is a brief description of the error messages and what they mean: | ||
360 | .Bl -tag -width Ds | ||
361 | .It Dq out of memory | ||
362 | If the | ||
363 | .Cm X | ||
364 | option is specified it is an error for | ||
365 | .Fn malloc , | ||
366 | .Fn calloc , | ||
367 | or | ||
368 | .Fn realloc | ||
369 | to return | ||
370 | .Dv NULL . | ||
371 | .It Dq malloc init mmap failed | ||
372 | This is a rather weird condition that is most likely to indicate a | ||
373 | seriously overloaded system or a ulimit restriction. | ||
374 | .It Dq bogus pointer (double free?) | ||
375 | An attempt to | ||
376 | .Fn free | ||
377 | or | ||
378 | .Fn realloc | ||
379 | an unallocated pointer was made. | ||
380 | .It Dq chunk is already free | ||
381 | There was an attempt to free a chunk that had already been freed. | ||
382 | .It Dq modified chunk-pointer | ||
383 | The pointer passed to | ||
384 | .Fn free | ||
385 | or | ||
386 | .Fn realloc | ||
387 | has been modified. | ||
388 | .It Dq recursive call | ||
389 | An attempt was made to call recursively into these functions, i.e., from a | ||
390 | signal handler. | ||
391 | This behavior is not supported. | ||
392 | In particular, signal handlers should | ||
393 | .Em not | ||
394 | use any of the | ||
395 | .Fn malloc | ||
396 | functions nor utilize any other functions which may call | ||
397 | .Fn malloc | ||
398 | (e.g., | ||
399 | .Xr stdio 3 | ||
400 | routines). | ||
401 | .It Dq unknown char in MALLOC_OPTIONS | ||
402 | We found something we didn't understand. | ||
403 | .It Dq malloc cache overflow/underflow | ||
404 | The internal malloc page cache has been corrupted. | ||
405 | .It Dq malloc free slot lost | ||
406 | The internal malloc page cache has been corrupted. | ||
407 | .It Dq guard size | ||
408 | An inconsistent guard size was detected. | ||
409 | .It any other error | ||
410 | .Fn malloc | ||
411 | detected an internal error; | ||
412 | consult sources and/or wizards. | ||
413 | .El | ||
414 | .Sh SEE ALSO | ||
415 | .Xr brk 2 , | ||
416 | .Xr mmap 2 , | ||
417 | .Xr munmap 2 , | ||
418 | .Xr alloca 3 , | ||
419 | .Xr getpagesize 3 | ||
420 | .Sh STANDARDS | ||
421 | The | ||
422 | .Fn malloc | ||
423 | function conforms to | ||
424 | .St -ansiC . | ||
425 | .Sh HISTORY | ||
426 | The present implementation of | ||
427 | .Fn malloc | ||
428 | started out as a filesystem on a drum | ||
429 | attached to a 20-bit binary challenged computer built with discrete germanium | ||
430 | transistors, and it has since graduated to handle primary storage rather than | ||
431 | secondary. | ||
432 | .Pp | ||
433 | The main difference from other | ||
434 | .Fn malloc | ||
435 | implementations are believed to be that | ||
436 | the free pages are not accessed until allocated. | ||
437 | Most | ||
438 | .Fn malloc | ||
439 | implementations will store a data structure containing a, | ||
440 | possibly double-, linked list in the free chunks of memory, used to tie | ||
441 | all the free memory together. | ||
442 | That is a quite suboptimal thing to do. | ||
443 | Every time the free-list is traversed, all the otherwise unused, and very | ||
444 | likely paged out, pages get faulted into primary memory, just to see what | ||
445 | lies after them in the list. | ||
446 | .Pp | ||
447 | On systems which are paging, this can increase the page-faults | ||
448 | of a process by a factor of five. | ||
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c new file mode 100644 index 0000000000..2ce545c5fa --- /dev/null +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -0,0 +1,1497 @@ | |||
1 | /* $OpenBSD: malloc.c,v 1.115 2009/01/03 12:58:28 djm Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> | ||
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 | |||
18 | /* | ||
19 | * Parts of this code, mainly the sub page sized chunk management code is | ||
20 | * derived from the malloc implementation with the following license: | ||
21 | */ | ||
22 | /* | ||
23 | * ---------------------------------------------------------------------------- | ||
24 | * "THE BEER-WARE LICENSE" (Revision 42): | ||
25 | * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you | ||
26 | * can do whatever you want with this stuff. If we meet some day, and you think | ||
27 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp | ||
28 | * ---------------------------------------------------------------------------- | ||
29 | */ | ||
30 | |||
31 | /* #define MALLOC_STATS */ | ||
32 | |||
33 | #include <sys/types.h> | ||
34 | #include <sys/param.h> | ||
35 | #include <sys/mman.h> | ||
36 | #include <sys/uio.h> | ||
37 | #include <errno.h> | ||
38 | #include <stdint.h> | ||
39 | #include <stdlib.h> | ||
40 | #include <string.h> | ||
41 | #include <stdio.h> | ||
42 | #include <unistd.h> | ||
43 | |||
44 | #ifdef MALLOC_STATS | ||
45 | #include <fcntl.h> | ||
46 | #endif | ||
47 | |||
48 | #include "thread_private.h" | ||
49 | |||
50 | #define MALLOC_MINSHIFT 4 | ||
51 | #define MALLOC_MAXSHIFT 16 | ||
52 | |||
53 | #if defined(__sparc__) && !defined(__sparcv9__) | ||
54 | #define MALLOC_PAGESHIFT (13U) | ||
55 | #else | ||
56 | #define MALLOC_PAGESHIFT (PGSHIFT) | ||
57 | #endif | ||
58 | |||
59 | #define MALLOC_PAGESIZE (1UL << MALLOC_PAGESHIFT) | ||
60 | #define MALLOC_MINSIZE (1UL << MALLOC_MINSHIFT) | ||
61 | #define MALLOC_PAGEMASK (MALLOC_PAGESIZE - 1) | ||
62 | #define MASK_POINTER(p) ((void *)(((uintptr_t)(p)) & ~MALLOC_PAGEMASK)) | ||
63 | |||
64 | #define MALLOC_MAXCHUNK (1 << (MALLOC_PAGESHIFT-1)) | ||
65 | #define MALLOC_MAXCACHE 256 | ||
66 | #define MALLOC_DELAYED_CHUNKS 16 /* should be power of 2 */ | ||
67 | /* | ||
68 | * When the P option is active, we move allocations between half a page | ||
69 | * and a whole page towards the end, subject to alignment constraints. | ||
70 | * This is the extra headroom we allow. Set to zero to be the most | ||
71 | * strict. | ||
72 | */ | ||
73 | #define MALLOC_LEEWAY 0 | ||
74 | |||
75 | #define PAGEROUND(x) (((x) + (MALLOC_PAGEMASK)) & ~MALLOC_PAGEMASK) | ||
76 | |||
77 | /* | ||
78 | * What to use for Junk. This is the byte value we use to fill with | ||
79 | * when the 'J' option is enabled. Use SOME_JUNK right after alloc, | ||
80 | * and SOME_FREEJUNK right before free. | ||
81 | */ | ||
82 | #define SOME_JUNK 0xd0 /* as in "Duh" :-) */ | ||
83 | #define SOME_FREEJUNK 0xdf | ||
84 | |||
85 | #define MMAP(sz) mmap(NULL, (size_t)(sz), PROT_READ | PROT_WRITE, \ | ||
86 | MAP_ANON | MAP_PRIVATE, -1, (off_t) 0) | ||
87 | |||
88 | #define MMAPA(a,sz) mmap((a), (size_t)(sz), PROT_READ | PROT_WRITE, \ | ||
89 | MAP_ANON | MAP_PRIVATE, -1, (off_t) 0) | ||
90 | |||
91 | struct region_info { | ||
92 | void *p; /* page; low bits used to mark chunks */ | ||
93 | uintptr_t size; /* size for pages, or chunk_info pointer */ | ||
94 | }; | ||
95 | |||
96 | struct dir_info { | ||
97 | u_int32_t canary1; | ||
98 | struct region_info *r; /* region slots */ | ||
99 | size_t regions_total; /* number of region slots */ | ||
100 | size_t regions_bits; /* log2 of total */ | ||
101 | size_t regions_free; /* number of free slots */ | ||
102 | /* list of free chunk info structs */ | ||
103 | struct chunk_info *chunk_info_list; | ||
104 | /* lists of chunks with free slots */ | ||
105 | struct chunk_info *chunk_dir[MALLOC_MAXSHIFT]; | ||
106 | size_t free_regions_size; /* free pages cached */ | ||
107 | /* free pages cache */ | ||
108 | struct region_info free_regions[MALLOC_MAXCACHE]; | ||
109 | /* delayed free chunk slots */ | ||
110 | void *delayed_chunks[MALLOC_DELAYED_CHUNKS]; | ||
111 | #ifdef MALLOC_STATS | ||
112 | size_t inserts; | ||
113 | size_t insert_collisions; | ||
114 | size_t finds; | ||
115 | size_t find_collisions; | ||
116 | size_t deletes; | ||
117 | size_t delete_moves; | ||
118 | size_t cheap_realloc_tries; | ||
119 | size_t cheap_reallocs; | ||
120 | #define STATS_INC(x) ((x)++) | ||
121 | #define STATS_ZERO(x) ((x) = 0) | ||
122 | #else | ||
123 | #define STATS_INC(x) /* nothing */ | ||
124 | #define STATS_ZERO(x) /* nothing */ | ||
125 | #endif /* MALLOC_STATS */ | ||
126 | u_int32_t canary2; | ||
127 | }; | ||
128 | #define DIR_INFO_RSZ ((sizeof(struct dir_info) + MALLOC_PAGEMASK) & \ | ||
129 | ~MALLOC_PAGEMASK) | ||
130 | |||
131 | /* | ||
132 | * This structure describes a page worth of chunks. | ||
133 | * | ||
134 | * How many bits per u_long in the bitmap | ||
135 | */ | ||
136 | #define MALLOC_BITS (NBBY * sizeof(u_long)) | ||
137 | struct chunk_info { | ||
138 | struct chunk_info *next; /* next on the free list */ | ||
139 | void *page; /* pointer to the page */ | ||
140 | u_int32_t canary; | ||
141 | u_short size; /* size of this page's chunks */ | ||
142 | u_short shift; /* how far to shift for this size */ | ||
143 | u_short free; /* how many free chunks */ | ||
144 | u_short total; /* how many chunk */ | ||
145 | /* which chunks are free */ | ||
146 | u_long bits[(MALLOC_PAGESIZE / MALLOC_MINSIZE) / MALLOC_BITS]; | ||
147 | }; | ||
148 | |||
149 | struct malloc_readonly { | ||
150 | struct dir_info *g_pool; /* Main bookkeeping information */ | ||
151 | int malloc_abort; /* abort() on error */ | ||
152 | int malloc_freeprot; /* mprotect free pages PROT_NONE? */ | ||
153 | int malloc_hint; /* call madvice on free pages? */ | ||
154 | int malloc_junk; /* junk fill? */ | ||
155 | int malloc_move; /* move allocations to end of page? */ | ||
156 | int malloc_realloc; /* always realloc? */ | ||
157 | int malloc_xmalloc; /* xmalloc behaviour? */ | ||
158 | int malloc_zero; /* zero fill? */ | ||
159 | size_t malloc_guard; /* use guard pages after allocations? */ | ||
160 | u_int malloc_cache; /* free pages we cache */ | ||
161 | #ifdef MALLOC_STATS | ||
162 | int malloc_stats; /* dump statistics at end */ | ||
163 | #endif | ||
164 | u_int32_t malloc_canary; /* Matched against ones in g_pool */ | ||
165 | }; | ||
166 | |||
167 | /* This object is mapped PROT_READ after initialisation to prevent tampering */ | ||
168 | static union { | ||
169 | struct malloc_readonly mopts; | ||
170 | u_char _pad[MALLOC_PAGESIZE]; | ||
171 | } malloc_readonly __attribute__((aligned(MALLOC_PAGESIZE))); | ||
172 | #define mopts malloc_readonly.mopts | ||
173 | #define g_pool mopts.g_pool | ||
174 | |||
175 | char *malloc_options; /* compile-time options */ | ||
176 | |||
177 | static char *malloc_func; /* current function */ | ||
178 | static int malloc_active; /* status of malloc */ | ||
179 | |||
180 | static size_t malloc_guarded; /* bytes used for guards */ | ||
181 | static size_t malloc_used; /* bytes allocated */ | ||
182 | |||
183 | static size_t rbytesused; /* random bytes used */ | ||
184 | static u_char rbytes[512]; /* random bytes */ | ||
185 | static u_char getrbyte(void); | ||
186 | |||
187 | extern char *__progname; | ||
188 | |||
189 | /* low bits of r->p determine size: 0 means >= page size and p->size holding | ||
190 | * real size, otherwise r->size is a shift count, or 1 for malloc(0) | ||
191 | */ | ||
192 | #define REALSIZE(sz, r) \ | ||
193 | (sz) = (uintptr_t)(r)->p & MALLOC_PAGEMASK, \ | ||
194 | (sz) = ((sz) == 0 ? (r)->size : ((sz) == 1 ? 0 : (1 << ((sz)-1)))) | ||
195 | |||
196 | static inline size_t | ||
197 | hash(void *p) | ||
198 | { | ||
199 | size_t sum; | ||
200 | union { | ||
201 | uintptr_t p; | ||
202 | unsigned short a[sizeof(void *) / sizeof(short)]; | ||
203 | } u; | ||
204 | u.p = (uintptr_t)p >> MALLOC_PAGESHIFT; | ||
205 | sum = u.a[0]; | ||
206 | sum = (sum << 7) - sum + u.a[1]; | ||
207 | #ifdef __LP64__ | ||
208 | sum = (sum << 7) - sum + u.a[2]; | ||
209 | sum = (sum << 7) - sum + u.a[3]; | ||
210 | #endif | ||
211 | return sum; | ||
212 | } | ||
213 | |||
214 | #ifdef MALLOC_STATS | ||
215 | static void | ||
216 | dump_chunk(int fd, struct chunk_info *p, int fromfreelist) | ||
217 | { | ||
218 | char buf[64]; | ||
219 | |||
220 | while (p) { | ||
221 | snprintf(buf, sizeof(buf), "chunk %d %d/%d %p\n", p->size, | ||
222 | p->free, p->total, p->page); | ||
223 | write(fd, buf, strlen(buf)); | ||
224 | if (!fromfreelist) | ||
225 | break; | ||
226 | p = p->next; | ||
227 | if (p != NULL) { | ||
228 | snprintf(buf, sizeof(buf), " "); | ||
229 | write(fd, buf, strlen(buf)); | ||
230 | } | ||
231 | } | ||
232 | } | ||
233 | |||
234 | static void | ||
235 | dump_free_chunk_info(int fd, struct dir_info *d) | ||
236 | { | ||
237 | char buf[64]; | ||
238 | int i; | ||
239 | |||
240 | snprintf(buf, sizeof(buf), "Free chunk structs:\n"); | ||
241 | write(fd, buf, strlen(buf)); | ||
242 | for (i = 0; i < MALLOC_MAXSHIFT; i++) { | ||
243 | struct chunk_info *p = d->chunk_dir[i]; | ||
244 | if (p != NULL) { | ||
245 | snprintf(buf, sizeof(buf), "%2d) ", i); | ||
246 | write(fd, buf, strlen(buf)); | ||
247 | dump_chunk(fd, p, 1); | ||
248 | } | ||
249 | } | ||
250 | |||
251 | } | ||
252 | |||
253 | static void | ||
254 | dump_free_page_info(int fd, struct dir_info *d) | ||
255 | { | ||
256 | char buf[64]; | ||
257 | int i; | ||
258 | |||
259 | snprintf(buf, sizeof(buf), "Free pages cached: %zu\n", | ||
260 | d->free_regions_size); | ||
261 | write(fd, buf, strlen(buf)); | ||
262 | for (i = 0; i < mopts.malloc_cache; i++) { | ||
263 | if (d->free_regions[i].p != NULL) { | ||
264 | snprintf(buf, sizeof(buf), "%2d) ", i); | ||
265 | write(fd, buf, strlen(buf)); | ||
266 | snprintf(buf, sizeof(buf), "free at %p: %zu\n", | ||
267 | d->free_regions[i].p, d->free_regions[i].size); | ||
268 | write(fd, buf, strlen(buf)); | ||
269 | } | ||
270 | } | ||
271 | } | ||
272 | |||
273 | static void | ||
274 | malloc_dump1(int fd, struct dir_info *d) | ||
275 | { | ||
276 | char buf[64]; | ||
277 | size_t i, realsize; | ||
278 | |||
279 | snprintf(buf, sizeof(buf), "Malloc dir of %s at %p\n", __progname, d); | ||
280 | write(fd, buf, strlen(buf)); | ||
281 | if (d == NULL) | ||
282 | return; | ||
283 | snprintf(buf, sizeof(buf), "Regions slots %zu\n", d->regions_total); | ||
284 | write(fd, buf, strlen(buf)); | ||
285 | snprintf(buf, sizeof(buf), "Finds %zu/%zu %f\n", d->finds, | ||
286 | d->find_collisions, | ||
287 | 1.0 + (double)d->find_collisions / d->finds); | ||
288 | write(fd, buf, strlen(buf)); | ||
289 | snprintf(buf, sizeof(buf), "Inserts %zu/%zu %f\n", d->inserts, | ||
290 | d->insert_collisions, | ||
291 | 1.0 + (double)d->insert_collisions / d->inserts); | ||
292 | write(fd, buf, strlen(buf)); | ||
293 | snprintf(buf, sizeof(buf), "Deletes %zu/%zu\n", d->deletes, | ||
294 | d->delete_moves); | ||
295 | write(fd, buf, strlen(buf)); | ||
296 | snprintf(buf, sizeof(buf), "Cheap reallocs %zu/%zu\n", | ||
297 | d->cheap_reallocs, d->cheap_realloc_tries); | ||
298 | write(fd, buf, strlen(buf)); | ||
299 | snprintf(buf, sizeof(buf), "Regions slots free %zu\n", d->regions_free); | ||
300 | write(fd, buf, strlen(buf)); | ||
301 | for (i = 0; i < d->regions_total; i++) { | ||
302 | if (d->r[i].p != NULL) { | ||
303 | size_t h = hash(d->r[i].p) & | ||
304 | (d->regions_total - 1); | ||
305 | snprintf(buf, sizeof(buf), "%4zx) #%zx %zd ", | ||
306 | i, h, h - i); | ||
307 | write(fd, buf, strlen(buf)); | ||
308 | REALSIZE(realsize, &d->r[i]); | ||
309 | if (realsize > MALLOC_MAXCHUNK) { | ||
310 | snprintf(buf, sizeof(buf), | ||
311 | "%p: %zu\n", d->r[i].p, realsize); | ||
312 | write(fd, buf, strlen(buf)); | ||
313 | } else | ||
314 | dump_chunk(fd, | ||
315 | (struct chunk_info *)d->r[i].size, 0); | ||
316 | } | ||
317 | } | ||
318 | dump_free_chunk_info(fd, d); | ||
319 | dump_free_page_info(fd, d); | ||
320 | snprintf(buf, sizeof(buf), "In use %zu\n", malloc_used); | ||
321 | write(fd, buf, strlen(buf)); | ||
322 | snprintf(buf, sizeof(buf), "Guarded %zu\n", malloc_guarded); | ||
323 | write(fd, buf, strlen(buf)); | ||
324 | } | ||
325 | |||
326 | |||
327 | void | ||
328 | malloc_dump(int fd) | ||
329 | { | ||
330 | malloc_dump1(fd, g_pool); | ||
331 | } | ||
332 | |||
333 | static void | ||
334 | malloc_exit(void) | ||
335 | { | ||
336 | static const char q[] = "malloc() warning: Couldn't dump stats\n"; | ||
337 | int save_errno = errno, fd; | ||
338 | |||
339 | fd = open("malloc.out", O_RDWR|O_APPEND); | ||
340 | if (fd != -1) { | ||
341 | malloc_dump(fd); | ||
342 | close(fd); | ||
343 | } else | ||
344 | write(STDERR_FILENO, q, sizeof(q) - 1); | ||
345 | errno = save_errno; | ||
346 | } | ||
347 | #endif /* MALLOC_STATS */ | ||
348 | |||
349 | |||
350 | |||
351 | static void | ||
352 | wrterror(char *p) | ||
353 | { | ||
354 | char *q = " error: "; | ||
355 | struct iovec iov[5]; | ||
356 | |||
357 | iov[0].iov_base = __progname; | ||
358 | iov[0].iov_len = strlen(__progname); | ||
359 | iov[1].iov_base = malloc_func; | ||
360 | iov[1].iov_len = strlen(malloc_func); | ||
361 | iov[2].iov_base = q; | ||
362 | iov[2].iov_len = strlen(q); | ||
363 | iov[3].iov_base = p; | ||
364 | iov[3].iov_len = strlen(p); | ||
365 | iov[4].iov_base = "\n"; | ||
366 | iov[4].iov_len = 1; | ||
367 | writev(STDERR_FILENO, iov, 5); | ||
368 | |||
369 | #ifdef MALLOC_STATS | ||
370 | if (mopts.malloc_stats) | ||
371 | malloc_dump(STDERR_FILENO); | ||
372 | #endif /* MALLOC_STATS */ | ||
373 | //malloc_active--; | ||
374 | if (mopts.malloc_abort) | ||
375 | abort(); | ||
376 | } | ||
377 | |||
378 | /* | ||
379 | * Cache maintenance. We keep at most malloc_cache pages cached. | ||
380 | * If the cache is becoming full, unmap pages in the cache for real, | ||
381 | * and then add the region to the cache | ||
382 | * Opposed to the regular region data structure, the sizes in the | ||
383 | * cache are in MALLOC_PAGESIZE units. | ||
384 | */ | ||
385 | static void | ||
386 | unmap(struct dir_info *d, void *p, size_t sz) | ||
387 | { | ||
388 | size_t psz = sz >> MALLOC_PAGESHIFT; | ||
389 | size_t rsz, tounmap; | ||
390 | struct region_info *r; | ||
391 | u_int i, offset; | ||
392 | |||
393 | if (sz != PAGEROUND(sz)) { | ||
394 | wrterror("munmap round"); | ||
395 | return; | ||
396 | } | ||
397 | |||
398 | if (psz > mopts.malloc_cache) { | ||
399 | if (munmap(p, sz)) | ||
400 | wrterror("munmap"); | ||
401 | malloc_used -= sz; | ||
402 | return; | ||
403 | } | ||
404 | tounmap = 0; | ||
405 | rsz = mopts.malloc_cache - d->free_regions_size; | ||
406 | if (psz > rsz) | ||
407 | tounmap = psz - rsz; | ||
408 | offset = getrbyte(); | ||
409 | for (i = 0; tounmap > 0 && i < mopts.malloc_cache; i++) { | ||
410 | r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; | ||
411 | if (r->p != NULL) { | ||
412 | rsz = r->size << MALLOC_PAGESHIFT; | ||
413 | if (munmap(r->p, rsz)) | ||
414 | wrterror("munmap"); | ||
415 | r->p = NULL; | ||
416 | if (tounmap > r->size) | ||
417 | tounmap -= r->size; | ||
418 | else | ||
419 | tounmap = 0; | ||
420 | d->free_regions_size -= r->size; | ||
421 | r->size = 0; | ||
422 | malloc_used -= rsz; | ||
423 | } | ||
424 | } | ||
425 | if (tounmap > 0) | ||
426 | wrterror("malloc cache underflow"); | ||
427 | for (i = 0; i < mopts.malloc_cache; i++) { | ||
428 | r = &d->free_regions[i]; | ||
429 | if (r->p == NULL) { | ||
430 | if (mopts.malloc_hint) | ||
431 | madvise(p, sz, MADV_FREE); | ||
432 | if (mopts.malloc_freeprot) | ||
433 | mprotect(p, sz, PROT_NONE); | ||
434 | r->p = p; | ||
435 | r->size = psz; | ||
436 | d->free_regions_size += psz; | ||
437 | break; | ||
438 | } | ||
439 | } | ||
440 | if (i == mopts.malloc_cache) | ||
441 | wrterror("malloc free slot lost"); | ||
442 | if (d->free_regions_size > mopts.malloc_cache) | ||
443 | wrterror("malloc cache overflow"); | ||
444 | } | ||
445 | |||
446 | static void | ||
447 | zapcacheregion(struct dir_info *d, void *p) | ||
448 | { | ||
449 | u_int i; | ||
450 | struct region_info *r; | ||
451 | size_t rsz; | ||
452 | |||
453 | for (i = 0; i < mopts.malloc_cache; i++) { | ||
454 | r = &d->free_regions[i]; | ||
455 | if (r->p == p) { | ||
456 | rsz = r->size << MALLOC_PAGESHIFT; | ||
457 | if (munmap(r->p, rsz)) | ||
458 | wrterror("munmap"); | ||
459 | r->p = NULL; | ||
460 | d->free_regions_size -= r->size; | ||
461 | r->size = 0; | ||
462 | malloc_used -= rsz; | ||
463 | } | ||
464 | } | ||
465 | } | ||
466 | |||
467 | static void * | ||
468 | map(struct dir_info *d, size_t sz, int zero_fill) | ||
469 | { | ||
470 | size_t psz = sz >> MALLOC_PAGESHIFT; | ||
471 | struct region_info *r, *big = NULL; | ||
472 | u_int i, offset; | ||
473 | void *p; | ||
474 | |||
475 | if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)d) || | ||
476 | d->canary1 != ~d->canary2) | ||
477 | wrterror("internal struct corrupt"); | ||
478 | if (sz != PAGEROUND(sz)) { | ||
479 | wrterror("map round"); | ||
480 | return NULL; | ||
481 | } | ||
482 | if (psz > d->free_regions_size) { | ||
483 | p = MMAP(sz); | ||
484 | if (p != MAP_FAILED) | ||
485 | malloc_used += sz; | ||
486 | /* zero fill not needed */ | ||
487 | return p; | ||
488 | } | ||
489 | offset = getrbyte(); | ||
490 | for (i = 0; i < mopts.malloc_cache; i++) { | ||
491 | r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; | ||
492 | if (r->p != NULL) { | ||
493 | if (r->size == psz) { | ||
494 | p = r->p; | ||
495 | if (mopts.malloc_freeprot) | ||
496 | mprotect(p, sz, PROT_READ | PROT_WRITE); | ||
497 | if (mopts.malloc_hint) | ||
498 | madvise(p, sz, MADV_NORMAL); | ||
499 | r->p = NULL; | ||
500 | r->size = 0; | ||
501 | d->free_regions_size -= psz; | ||
502 | if (zero_fill) | ||
503 | memset(p, 0, sz); | ||
504 | else if (mopts.malloc_junk && | ||
505 | mopts.malloc_freeprot) | ||
506 | memset(p, SOME_FREEJUNK, sz); | ||
507 | return p; | ||
508 | } else if (r->size > psz) | ||
509 | big = r; | ||
510 | } | ||
511 | } | ||
512 | if (big != NULL) { | ||
513 | r = big; | ||
514 | p = (char *)r->p + ((r->size - psz) << MALLOC_PAGESHIFT); | ||
515 | if (mopts.malloc_freeprot) | ||
516 | mprotect(p, sz, PROT_READ | PROT_WRITE); | ||
517 | if (mopts.malloc_hint) | ||
518 | madvise(p, sz, MADV_NORMAL); | ||
519 | r->size -= psz; | ||
520 | d->free_regions_size -= psz; | ||
521 | if (zero_fill) | ||
522 | memset(p, 0, sz); | ||
523 | return p; | ||
524 | } | ||
525 | p = MMAP(sz); | ||
526 | if (p != MAP_FAILED) | ||
527 | malloc_used += sz; | ||
528 | if (d->free_regions_size > mopts.malloc_cache) | ||
529 | wrterror("malloc cache"); | ||
530 | /* zero fill not needed */ | ||
531 | return p; | ||
532 | } | ||
533 | |||
534 | static void | ||
535 | rbytes_init(void) | ||
536 | { | ||
537 | arc4random_buf(rbytes, sizeof(rbytes)); | ||
538 | rbytesused = 0; | ||
539 | } | ||
540 | |||
541 | static u_char | ||
542 | getrbyte(void) | ||
543 | { | ||
544 | if (rbytesused >= sizeof(rbytes)) | ||
545 | rbytes_init(); | ||
546 | return rbytes[rbytesused++]; | ||
547 | } | ||
548 | |||
549 | /* | ||
550 | * Initialize a dir_info, which should have been cleared by caller | ||
551 | */ | ||
552 | static int | ||
553 | omalloc_init(struct dir_info **dp) | ||
554 | { | ||
555 | char *p, b[64]; | ||
556 | int i, j; | ||
557 | size_t d_avail, regioninfo_size; | ||
558 | struct dir_info *d; | ||
559 | |||
560 | rbytes_init(); | ||
561 | |||
562 | /* | ||
563 | * Default options | ||
564 | */ | ||
565 | mopts.malloc_abort = 1; | ||
566 | mopts.malloc_move = 1; | ||
567 | mopts.malloc_cache = 64; | ||
568 | |||
569 | for (i = 0; i < 3; i++) { | ||
570 | switch (i) { | ||
571 | case 0: | ||
572 | j = readlink("/etc/malloc.conf", b, sizeof b - 1); | ||
573 | if (j <= 0) | ||
574 | continue; | ||
575 | b[j] = '\0'; | ||
576 | p = b; | ||
577 | break; | ||
578 | case 1: | ||
579 | if (issetugid() == 0) | ||
580 | p = getenv("MALLOC_OPTIONS"); | ||
581 | else | ||
582 | continue; | ||
583 | break; | ||
584 | case 2: | ||
585 | p = malloc_options; | ||
586 | break; | ||
587 | default: | ||
588 | p = NULL; | ||
589 | } | ||
590 | |||
591 | for (; p != NULL && *p != '\0'; p++) { | ||
592 | switch (*p) { | ||
593 | case '>': | ||
594 | mopts.malloc_cache <<= 1; | ||
595 | if (mopts.malloc_cache > MALLOC_MAXCACHE) | ||
596 | mopts.malloc_cache = MALLOC_MAXCACHE; | ||
597 | break; | ||
598 | case '<': | ||
599 | mopts.malloc_cache >>= 1; | ||
600 | break; | ||
601 | case 'a': | ||
602 | mopts.malloc_abort = 0; | ||
603 | break; | ||
604 | case 'A': | ||
605 | mopts.malloc_abort = 1; | ||
606 | break; | ||
607 | #ifdef MALLOC_STATS | ||
608 | case 'd': | ||
609 | mopts.malloc_stats = 0; | ||
610 | break; | ||
611 | case 'D': | ||
612 | mopts.malloc_stats = 1; | ||
613 | break; | ||
614 | #endif /* MALLOC_STATS */ | ||
615 | case 'f': | ||
616 | mopts.malloc_freeprot = 0; | ||
617 | break; | ||
618 | case 'F': | ||
619 | mopts.malloc_freeprot = 1; | ||
620 | break; | ||
621 | case 'g': | ||
622 | mopts.malloc_guard = 0; | ||
623 | break; | ||
624 | case 'G': | ||
625 | mopts.malloc_guard = MALLOC_PAGESIZE; | ||
626 | break; | ||
627 | case 'h': | ||
628 | mopts.malloc_hint = 0; | ||
629 | break; | ||
630 | case 'H': | ||
631 | mopts.malloc_hint = 1; | ||
632 | break; | ||
633 | case 'j': | ||
634 | mopts.malloc_junk = 0; | ||
635 | break; | ||
636 | case 'J': | ||
637 | mopts.malloc_junk = 1; | ||
638 | break; | ||
639 | case 'n': | ||
640 | case 'N': | ||
641 | break; | ||
642 | case 'p': | ||
643 | mopts.malloc_move = 0; | ||
644 | break; | ||
645 | case 'P': | ||
646 | mopts.malloc_move = 1; | ||
647 | break; | ||
648 | case 'r': | ||
649 | mopts.malloc_realloc = 0; | ||
650 | break; | ||
651 | case 'R': | ||
652 | mopts.malloc_realloc = 1; | ||
653 | break; | ||
654 | case 'x': | ||
655 | mopts.malloc_xmalloc = 0; | ||
656 | break; | ||
657 | case 'X': | ||
658 | mopts.malloc_xmalloc = 1; | ||
659 | break; | ||
660 | case 'z': | ||
661 | mopts.malloc_zero = 0; | ||
662 | break; | ||
663 | case 'Z': | ||
664 | mopts.malloc_zero = 1; | ||
665 | break; | ||
666 | default: { | ||
667 | static const char q[] = "malloc() warning: " | ||
668 | "unknown char in MALLOC_OPTIONS\n"; | ||
669 | write(STDERR_FILENO, q, sizeof(q) - 1); | ||
670 | break; | ||
671 | } | ||
672 | } | ||
673 | } | ||
674 | } | ||
675 | |||
676 | /* | ||
677 | * We want junk in the entire allocation, and zero only in the part | ||
678 | * the user asked for. | ||
679 | */ | ||
680 | if (mopts.malloc_zero) | ||
681 | mopts.malloc_junk = 1; | ||
682 | |||
683 | #ifdef MALLOC_STATS | ||
684 | if (mopts.malloc_stats && (atexit(malloc_exit) == -1)) { | ||
685 | static const char q[] = "malloc() warning: atexit(2) failed." | ||
686 | " Will not be able to dump stats on exit\n"; | ||
687 | write(STDERR_FILENO, q, sizeof(q) - 1); | ||
688 | } | ||
689 | #endif /* MALLOC_STATS */ | ||
690 | |||
691 | while ((mopts.malloc_canary = arc4random()) == 0) | ||
692 | ; | ||
693 | |||
694 | /* | ||
695 | * Allocate dir_info with a guard page on either side. Also | ||
696 | * randomise offset inside the page at which the dir_info | ||
697 | * lies (subject to alignment by 1 << MALLOC_MINSHIFT) | ||
698 | */ | ||
699 | if ((p = MMAP(DIR_INFO_RSZ + (MALLOC_PAGESIZE * 2))) == NULL) | ||
700 | return -1; | ||
701 | mprotect(p, MALLOC_PAGESIZE, PROT_NONE); | ||
702 | mprotect(p + MALLOC_PAGESIZE + DIR_INFO_RSZ, | ||
703 | MALLOC_PAGESIZE, PROT_NONE); | ||
704 | d_avail = (DIR_INFO_RSZ - sizeof(*d)) >> MALLOC_MINSHIFT; | ||
705 | d = (struct dir_info *)(p + MALLOC_PAGESIZE + | ||
706 | (arc4random_uniform(d_avail) << MALLOC_MINSHIFT)); | ||
707 | |||
708 | d->regions_bits = 9; | ||
709 | d->regions_free = d->regions_total = 1 << d->regions_bits; | ||
710 | regioninfo_size = d->regions_total * sizeof(struct region_info); | ||
711 | d->r = MMAP(regioninfo_size); | ||
712 | if (d->r == MAP_FAILED) { | ||
713 | wrterror("malloc init mmap failed"); | ||
714 | d->regions_total = 0; | ||
715 | return 1; | ||
716 | } | ||
717 | malloc_used += regioninfo_size; | ||
718 | memset(d->r, 0, regioninfo_size); | ||
719 | d->canary1 = mopts.malloc_canary ^ (u_int32_t)d; | ||
720 | d->canary2 = ~d->canary1; | ||
721 | |||
722 | *dp = d; | ||
723 | |||
724 | /* | ||
725 | * Options have been set and will never be reset. | ||
726 | * Prevent further tampering with them. | ||
727 | */ | ||
728 | if (((uintptr_t)&malloc_readonly & MALLOC_PAGEMASK) == 0) | ||
729 | mprotect(&malloc_readonly, sizeof(malloc_readonly), PROT_READ); | ||
730 | |||
731 | return 0; | ||
732 | } | ||
733 | |||
734 | static int | ||
735 | omalloc_grow(struct dir_info *d) | ||
736 | { | ||
737 | size_t newbits; | ||
738 | size_t newtotal; | ||
739 | size_t newsize; | ||
740 | size_t mask; | ||
741 | size_t i; | ||
742 | struct region_info *p; | ||
743 | |||
744 | if (d->regions_total > SIZE_MAX / sizeof(struct region_info) / 2 ) | ||
745 | return 1; | ||
746 | |||
747 | newbits = d->regions_bits + 1; | ||
748 | newtotal = d->regions_total * 2; | ||
749 | newsize = newtotal * sizeof(struct region_info); | ||
750 | mask = newtotal - 1; | ||
751 | |||
752 | p = MMAP(newsize); | ||
753 | if (p == MAP_FAILED) | ||
754 | return 1; | ||
755 | |||
756 | malloc_used += newsize; | ||
757 | memset(p, 0, newsize); | ||
758 | STATS_ZERO(d->inserts); | ||
759 | STATS_ZERO(d->insert_collisions); | ||
760 | for (i = 0; i < d->regions_total; i++) { | ||
761 | void *q = d->r[i].p; | ||
762 | if (q != NULL) { | ||
763 | size_t index = hash(q) & mask; | ||
764 | STATS_INC(d->inserts); | ||
765 | while (p[index].p != NULL) { | ||
766 | index = (index - 1) & mask; | ||
767 | STATS_INC(d->insert_collisions); | ||
768 | } | ||
769 | p[index] = d->r[i]; | ||
770 | } | ||
771 | } | ||
772 | /* avoid pages containing meta info to end up in cache */ | ||
773 | if (munmap(d->r, d->regions_total * sizeof(struct region_info))) | ||
774 | wrterror("munmap"); | ||
775 | else | ||
776 | malloc_used -= d->regions_total * sizeof(struct region_info); | ||
777 | d->regions_free = d->regions_free + d->regions_total; | ||
778 | d->regions_total = newtotal; | ||
779 | d->regions_bits = newbits; | ||
780 | d->r = p; | ||
781 | return 0; | ||
782 | } | ||
783 | |||
784 | static struct chunk_info * | ||
785 | alloc_chunk_info(struct dir_info *d) | ||
786 | { | ||
787 | struct chunk_info *p; | ||
788 | int i; | ||
789 | |||
790 | if (d->chunk_info_list == NULL) { | ||
791 | p = MMAP(MALLOC_PAGESIZE); | ||
792 | if (p == MAP_FAILED) | ||
793 | return NULL; | ||
794 | malloc_used += MALLOC_PAGESIZE; | ||
795 | for (i = 0; i < MALLOC_PAGESIZE / sizeof(*p); i++) { | ||
796 | p[i].next = d->chunk_info_list; | ||
797 | d->chunk_info_list = &p[i]; | ||
798 | } | ||
799 | } | ||
800 | p = d->chunk_info_list; | ||
801 | d->chunk_info_list = p->next; | ||
802 | memset(p, 0, sizeof *p); | ||
803 | p->canary = d->canary1; | ||
804 | return p; | ||
805 | } | ||
806 | |||
807 | |||
808 | static void | ||
809 | put_chunk_info(struct dir_info *d, struct chunk_info *p) | ||
810 | { | ||
811 | p->next = d->chunk_info_list; | ||
812 | d->chunk_info_list = p; | ||
813 | } | ||
814 | |||
815 | static int | ||
816 | insert(struct dir_info *d, void *p, size_t sz) | ||
817 | { | ||
818 | size_t index; | ||
819 | size_t mask; | ||
820 | void *q; | ||
821 | |||
822 | if (d->regions_free * 4 < d->regions_total) { | ||
823 | if (omalloc_grow(d)) | ||
824 | return 1; | ||
825 | } | ||
826 | mask = d->regions_total - 1; | ||
827 | index = hash(p) & mask; | ||
828 | q = d->r[index].p; | ||
829 | STATS_INC(d->inserts); | ||
830 | while (q != NULL) { | ||
831 | index = (index - 1) & mask; | ||
832 | q = d->r[index].p; | ||
833 | STATS_INC(d->insert_collisions); | ||
834 | } | ||
835 | d->r[index].p = p; | ||
836 | d->r[index].size = sz; | ||
837 | d->regions_free--; | ||
838 | return 0; | ||
839 | } | ||
840 | |||
841 | static struct region_info * | ||
842 | find(struct dir_info *d, void *p) | ||
843 | { | ||
844 | size_t index; | ||
845 | size_t mask = d->regions_total - 1; | ||
846 | void *q, *r; | ||
847 | |||
848 | if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)d) || | ||
849 | d->canary1 != ~d->canary2) | ||
850 | wrterror("internal struct corrupt"); | ||
851 | p = MASK_POINTER(p); | ||
852 | index = hash(p) & mask; | ||
853 | r = d->r[index].p; | ||
854 | q = MASK_POINTER(r); | ||
855 | STATS_INC(d->finds); | ||
856 | while (q != p && r != NULL) { | ||
857 | index = (index - 1) & mask; | ||
858 | r = d->r[index].p; | ||
859 | q = MASK_POINTER(r); | ||
860 | STATS_INC(d->find_collisions); | ||
861 | } | ||
862 | return q == p ? &d->r[index] : NULL; | ||
863 | } | ||
864 | |||
865 | static void | ||
866 | delete(struct dir_info *d, struct region_info *ri) | ||
867 | { | ||
868 | /* algorithm R, Knuth Vol III section 6.4 */ | ||
869 | size_t mask = d->regions_total - 1; | ||
870 | size_t i, j, r; | ||
871 | |||
872 | if (d->regions_total & (d->regions_total - 1)) | ||
873 | wrterror("regions_total not 2^x"); | ||
874 | d->regions_free++; | ||
875 | STATS_INC(g_pool->deletes); | ||
876 | |||
877 | i = ri - d->r; | ||
878 | for (;;) { | ||
879 | d->r[i].p = NULL; | ||
880 | d->r[i].size = 0; | ||
881 | j = i; | ||
882 | for (;;) { | ||
883 | i = (i - 1) & mask; | ||
884 | if (d->r[i].p == NULL) | ||
885 | return; | ||
886 | r = hash(d->r[i].p) & mask; | ||
887 | if ((i <= r && r < j) || (r < j && j < i) || | ||
888 | (j < i && i <= r)) | ||
889 | continue; | ||
890 | d->r[j] = d->r[i]; | ||
891 | STATS_INC(g_pool->delete_moves); | ||
892 | break; | ||
893 | } | ||
894 | |||
895 | } | ||
896 | } | ||
897 | |||
898 | /* | ||
899 | * Allocate a page of chunks | ||
900 | */ | ||
901 | static struct chunk_info * | ||
902 | omalloc_make_chunks(struct dir_info *d, int bits) | ||
903 | { | ||
904 | struct chunk_info *bp; | ||
905 | void *pp; | ||
906 | long i, k; | ||
907 | |||
908 | /* Allocate a new bucket */ | ||
909 | pp = map(d, MALLOC_PAGESIZE, 0); | ||
910 | if (pp == MAP_FAILED) | ||
911 | return NULL; | ||
912 | |||
913 | bp = alloc_chunk_info(d); | ||
914 | if (bp == NULL) { | ||
915 | unmap(d, pp, MALLOC_PAGESIZE); | ||
916 | return NULL; | ||
917 | } | ||
918 | |||
919 | /* memory protect the page allocated in the malloc(0) case */ | ||
920 | if (bits == 0) { | ||
921 | bp->size = 0; | ||
922 | bp->shift = 1; | ||
923 | i = MALLOC_MINSIZE - 1; | ||
924 | while (i >>= 1) | ||
925 | bp->shift++; | ||
926 | bp->total = bp->free = MALLOC_PAGESIZE >> bp->shift; | ||
927 | bp->page = pp; | ||
928 | |||
929 | k = mprotect(pp, MALLOC_PAGESIZE, PROT_NONE); | ||
930 | if (k < 0) { | ||
931 | unmap(d, pp, MALLOC_PAGESIZE); | ||
932 | put_chunk_info(d, bp); | ||
933 | return NULL; | ||
934 | } | ||
935 | } else { | ||
936 | bp->size = (1UL << bits); | ||
937 | bp->shift = bits; | ||
938 | bp->total = bp->free = MALLOC_PAGESIZE >> bits; | ||
939 | bp->page = pp; | ||
940 | } | ||
941 | |||
942 | /* set all valid bits in the bitmap */ | ||
943 | k = bp->total; | ||
944 | i = 0; | ||
945 | |||
946 | /* Do a bunch at a time */ | ||
947 | for (; (k - i) >= MALLOC_BITS; i += MALLOC_BITS) | ||
948 | bp->bits[i / MALLOC_BITS] = ~0UL; | ||
949 | |||
950 | for (; i < k; i++) | ||
951 | bp->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); | ||
952 | |||
953 | bp->next = d->chunk_dir[bits]; | ||
954 | d->chunk_dir[bits] = bp; | ||
955 | |||
956 | bits++; | ||
957 | if ((uintptr_t)pp & bits) | ||
958 | wrterror("pp & bits"); | ||
959 | |||
960 | insert(d, (void *)((uintptr_t)pp | bits), (uintptr_t)bp); | ||
961 | return bp; | ||
962 | } | ||
963 | |||
964 | |||
965 | /* | ||
966 | * Allocate a chunk | ||
967 | */ | ||
968 | static void * | ||
969 | malloc_bytes(struct dir_info *d, size_t size) | ||
970 | { | ||
971 | int i, j; | ||
972 | size_t k; | ||
973 | u_long u, *lp; | ||
974 | struct chunk_info *bp; | ||
975 | |||
976 | if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)d) || | ||
977 | d->canary1 != ~d->canary2) | ||
978 | wrterror("internal struct corrupt"); | ||
979 | /* Don't bother with anything less than this */ | ||
980 | /* unless we have a malloc(0) requests */ | ||
981 | if (size != 0 && size < MALLOC_MINSIZE) | ||
982 | size = MALLOC_MINSIZE; | ||
983 | |||
984 | /* Find the right bucket */ | ||
985 | if (size == 0) | ||
986 | j = 0; | ||
987 | else { | ||
988 | j = MALLOC_MINSHIFT; | ||
989 | i = (size - 1) >> (MALLOC_MINSHIFT - 1); | ||
990 | while (i >>= 1) | ||
991 | j++; | ||
992 | } | ||
993 | |||
994 | /* If it's empty, make a page more of that size chunks */ | ||
995 | bp = d->chunk_dir[j]; | ||
996 | if (bp == NULL && (bp = omalloc_make_chunks(d, j)) == NULL) | ||
997 | return NULL; | ||
998 | |||
999 | if (bp->canary != d->canary1) | ||
1000 | wrterror("chunk info corrupted"); | ||
1001 | /* Find first word of bitmap which isn't empty */ | ||
1002 | for (lp = bp->bits; !*lp; lp++) | ||
1003 | /* EMPTY */; | ||
1004 | |||
1005 | /* Find that bit, and tweak it */ | ||
1006 | u = 1; | ||
1007 | k = 0; | ||
1008 | while (!(*lp & u)) { | ||
1009 | u += u; | ||
1010 | k++; | ||
1011 | } | ||
1012 | |||
1013 | /* advance a random # of positions */ | ||
1014 | i = (getrbyte() & (MALLOC_DELAYED_CHUNKS - 1)) % bp->free; | ||
1015 | while (i > 0) { | ||
1016 | u += u; | ||
1017 | k++; | ||
1018 | if (k >= MALLOC_BITS) { | ||
1019 | lp++; | ||
1020 | u = 1; | ||
1021 | k = 0; | ||
1022 | } | ||
1023 | if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) { | ||
1024 | wrterror("chunk overflow"); | ||
1025 | errno = EFAULT; | ||
1026 | return (NULL); | ||
1027 | } | ||
1028 | if (*lp & u) | ||
1029 | i--; | ||
1030 | } | ||
1031 | |||
1032 | *lp ^= u; | ||
1033 | |||
1034 | /* If there are no more free, remove from free-list */ | ||
1035 | if (!--bp->free) { | ||
1036 | d->chunk_dir[j] = bp->next; | ||
1037 | bp->next = NULL; | ||
1038 | } | ||
1039 | /* Adjust to the real offset of that chunk */ | ||
1040 | k += (lp - bp->bits) * MALLOC_BITS; | ||
1041 | k <<= bp->shift; | ||
1042 | |||
1043 | if (mopts.malloc_junk && bp->size > 0) | ||
1044 | memset((char *)bp->page + k, SOME_JUNK, bp->size); | ||
1045 | return ((char *)bp->page + k); | ||
1046 | } | ||
1047 | |||
1048 | |||
1049 | /* | ||
1050 | * Free a chunk, and possibly the page it's on, if the page becomes empty. | ||
1051 | */ | ||
1052 | static void | ||
1053 | free_bytes(struct dir_info *d, struct region_info *r, void *ptr) | ||
1054 | { | ||
1055 | struct chunk_info *info, **mp; | ||
1056 | long i; | ||
1057 | |||
1058 | info = (struct chunk_info *)r->size; | ||
1059 | if (info->canary != d->canary1) | ||
1060 | wrterror("chunk info corrupted"); | ||
1061 | |||
1062 | /* Find the chunk number on the page */ | ||
1063 | i = ((uintptr_t)ptr & MALLOC_PAGEMASK) >> info->shift; | ||
1064 | |||
1065 | if ((uintptr_t)ptr & ((1UL << (info->shift)) - 1)) { | ||
1066 | wrterror("modified chunk-pointer"); | ||
1067 | return; | ||
1068 | } | ||
1069 | if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) { | ||
1070 | wrterror("chunk is already free"); | ||
1071 | return; | ||
1072 | } | ||
1073 | |||
1074 | info->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); | ||
1075 | info->free++; | ||
1076 | |||
1077 | if (info->size != 0) | ||
1078 | mp = d->chunk_dir + info->shift; | ||
1079 | else | ||
1080 | mp = d->chunk_dir; | ||
1081 | |||
1082 | if (info->free == 1) { | ||
1083 | /* Page became non-full */ | ||
1084 | |||
1085 | /* Insert in address order */ | ||
1086 | while (*mp != NULL && (*mp)->next != NULL && | ||
1087 | (*mp)->next->page < info->page) | ||
1088 | mp = &(*mp)->next; | ||
1089 | info->next = *mp; | ||
1090 | *mp = info; | ||
1091 | return; | ||
1092 | } | ||
1093 | if (info->free != info->total) | ||
1094 | return; | ||
1095 | |||
1096 | /* Find & remove this page in the queue */ | ||
1097 | while (*mp != info) { | ||
1098 | mp = &((*mp)->next); | ||
1099 | if (!*mp) { | ||
1100 | wrterror("not on queue"); | ||
1101 | errno = EFAULT; | ||
1102 | return; | ||
1103 | } | ||
1104 | } | ||
1105 | *mp = info->next; | ||
1106 | |||
1107 | if (info->size == 0 && !mopts.malloc_freeprot) | ||
1108 | mprotect(info->page, MALLOC_PAGESIZE, PROT_READ | PROT_WRITE); | ||
1109 | unmap(d, info->page, MALLOC_PAGESIZE); | ||
1110 | |||
1111 | delete(d, r); | ||
1112 | put_chunk_info(d, info); | ||
1113 | } | ||
1114 | |||
1115 | |||
1116 | |||
1117 | static void * | ||
1118 | omalloc(size_t sz, int zero_fill) | ||
1119 | { | ||
1120 | void *p; | ||
1121 | size_t psz; | ||
1122 | |||
1123 | if (sz > MALLOC_MAXCHUNK) { | ||
1124 | if (sz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { | ||
1125 | errno = ENOMEM; | ||
1126 | return NULL; | ||
1127 | } | ||
1128 | sz += mopts.malloc_guard; | ||
1129 | psz = PAGEROUND(sz); | ||
1130 | p = map(g_pool, psz, zero_fill); | ||
1131 | if (p == MAP_FAILED) { | ||
1132 | errno = ENOMEM; | ||
1133 | return NULL; | ||
1134 | } | ||
1135 | if (insert(g_pool, p, sz)) { | ||
1136 | unmap(g_pool, p, psz); | ||
1137 | errno = ENOMEM; | ||
1138 | return NULL; | ||
1139 | } | ||
1140 | if (mopts.malloc_guard) { | ||
1141 | if (mprotect((char *)p + psz - mopts.malloc_guard, | ||
1142 | mopts.malloc_guard, PROT_NONE)) | ||
1143 | wrterror("mprotect"); | ||
1144 | malloc_guarded += mopts.malloc_guard; | ||
1145 | } | ||
1146 | |||
1147 | if (mopts.malloc_move && | ||
1148 | sz - mopts.malloc_guard < MALLOC_PAGESIZE - | ||
1149 | MALLOC_LEEWAY) { | ||
1150 | /* fill whole allocation */ | ||
1151 | if (mopts.malloc_junk) | ||
1152 | memset(p, SOME_JUNK, psz - mopts.malloc_guard); | ||
1153 | /* shift towards the end */ | ||
1154 | p = ((char *)p) + ((MALLOC_PAGESIZE - MALLOC_LEEWAY - | ||
1155 | (sz - mopts.malloc_guard)) & ~(MALLOC_MINSIZE-1)); | ||
1156 | /* fill zeros if needed and overwritten above */ | ||
1157 | if (zero_fill && mopts.malloc_junk) | ||
1158 | memset(p, 0, sz - mopts.malloc_guard); | ||
1159 | } else { | ||
1160 | if (mopts.malloc_junk) { | ||
1161 | if (zero_fill) | ||
1162 | memset(p + sz - mopts.malloc_guard, | ||
1163 | SOME_JUNK, psz - sz); | ||
1164 | else | ||
1165 | memset(p, SOME_JUNK, | ||
1166 | psz - mopts.malloc_guard); | ||
1167 | } | ||
1168 | } | ||
1169 | |||
1170 | } else { | ||
1171 | /* takes care of SOME_JUNK */ | ||
1172 | p = malloc_bytes(g_pool, sz); | ||
1173 | if (zero_fill && p != NULL && sz > 0) | ||
1174 | memset(p, 0, sz); | ||
1175 | } | ||
1176 | |||
1177 | return p; | ||
1178 | } | ||
1179 | |||
1180 | /* | ||
1181 | * Common function for handling recursion. Only | ||
1182 | * print the error message once, to avoid making the problem | ||
1183 | * potentially worse. | ||
1184 | */ | ||
1185 | static void | ||
1186 | malloc_recurse(void) | ||
1187 | { | ||
1188 | static int noprint; | ||
1189 | |||
1190 | if (noprint == 0) { | ||
1191 | noprint = 1; | ||
1192 | wrterror("recursive call"); | ||
1193 | } | ||
1194 | malloc_active--; | ||
1195 | _MALLOC_UNLOCK(); | ||
1196 | errno = EDEADLK; | ||
1197 | } | ||
1198 | |||
1199 | static int | ||
1200 | malloc_init(void) | ||
1201 | { | ||
1202 | if (omalloc_init(&g_pool)) { | ||
1203 | _MALLOC_UNLOCK(); | ||
1204 | if (mopts.malloc_xmalloc) | ||
1205 | wrterror("out of memory"); | ||
1206 | errno = ENOMEM; | ||
1207 | return -1; | ||
1208 | } | ||
1209 | return 0; | ||
1210 | } | ||
1211 | |||
1212 | void * | ||
1213 | malloc(size_t size) | ||
1214 | { | ||
1215 | void *r; | ||
1216 | int saved_errno = errno; | ||
1217 | |||
1218 | _MALLOC_LOCK(); | ||
1219 | malloc_func = " in malloc():"; | ||
1220 | if (g_pool == NULL) { | ||
1221 | if (malloc_init() != 0) | ||
1222 | return NULL; | ||
1223 | } | ||
1224 | if (malloc_active++) { | ||
1225 | malloc_recurse(); | ||
1226 | return NULL; | ||
1227 | } | ||
1228 | r = omalloc(size, mopts.malloc_zero); | ||
1229 | malloc_active--; | ||
1230 | _MALLOC_UNLOCK(); | ||
1231 | if (r == NULL && mopts.malloc_xmalloc) { | ||
1232 | wrterror("out of memory"); | ||
1233 | errno = ENOMEM; | ||
1234 | } | ||
1235 | if (r != NULL) | ||
1236 | errno = saved_errno; | ||
1237 | return r; | ||
1238 | } | ||
1239 | |||
1240 | static void | ||
1241 | ofree(void *p) | ||
1242 | { | ||
1243 | struct region_info *r; | ||
1244 | size_t sz; | ||
1245 | |||
1246 | r = find(g_pool, p); | ||
1247 | if (r == NULL) { | ||
1248 | wrterror("bogus pointer (double free?)"); | ||
1249 | return; | ||
1250 | } | ||
1251 | REALSIZE(sz, r); | ||
1252 | if (sz > MALLOC_MAXCHUNK) { | ||
1253 | if (sz - mopts.malloc_guard >= MALLOC_PAGESIZE - | ||
1254 | MALLOC_LEEWAY) { | ||
1255 | if (r->p != p) { | ||
1256 | wrterror("bogus pointer"); | ||
1257 | return; | ||
1258 | } | ||
1259 | } else { | ||
1260 | #if notyetbecause_of_realloc | ||
1261 | /* shifted towards the end */ | ||
1262 | if (p != ((char *)r->p) + ((MALLOC_PAGESIZE - | ||
1263 | MALLOC_MINSIZE - sz - mopts.malloc_guard) & | ||
1264 | ~(MALLOC_MINSIZE-1))) { | ||
1265 | } | ||
1266 | #endif | ||
1267 | p = r->p; | ||
1268 | } | ||
1269 | if (mopts.malloc_guard) { | ||
1270 | if (sz < mopts.malloc_guard) | ||
1271 | wrterror("guard size"); | ||
1272 | if (!mopts.malloc_freeprot) { | ||
1273 | if (mprotect((char *)p + PAGEROUND(sz) - | ||
1274 | mopts.malloc_guard, mopts.malloc_guard, | ||
1275 | PROT_READ | PROT_WRITE)) | ||
1276 | wrterror("mprotect"); | ||
1277 | } | ||
1278 | malloc_guarded -= mopts.malloc_guard; | ||
1279 | } | ||
1280 | if (mopts.malloc_junk && !mopts.malloc_freeprot) | ||
1281 | memset(p, SOME_FREEJUNK, | ||
1282 | PAGEROUND(sz) - mopts.malloc_guard); | ||
1283 | unmap(g_pool, p, PAGEROUND(sz)); | ||
1284 | delete(g_pool, r); | ||
1285 | } else { | ||
1286 | void *tmp; | ||
1287 | int i; | ||
1288 | |||
1289 | if (mopts.malloc_junk && sz > 0) | ||
1290 | memset(p, SOME_FREEJUNK, sz); | ||
1291 | if (!mopts.malloc_freeprot) { | ||
1292 | i = getrbyte() & (MALLOC_DELAYED_CHUNKS - 1); | ||
1293 | tmp = p; | ||
1294 | p = g_pool->delayed_chunks[i]; | ||
1295 | g_pool->delayed_chunks[i] = tmp; | ||
1296 | } | ||
1297 | if (p != NULL) { | ||
1298 | r = find(g_pool, p); | ||
1299 | if (r == NULL) { | ||
1300 | wrterror("bogus pointer (double free?)"); | ||
1301 | return; | ||
1302 | } | ||
1303 | free_bytes(g_pool, r, p); | ||
1304 | } | ||
1305 | } | ||
1306 | } | ||
1307 | |||
1308 | void | ||
1309 | free(void *ptr) | ||
1310 | { | ||
1311 | int saved_errno = errno; | ||
1312 | |||
1313 | /* This is legal. */ | ||
1314 | if (ptr == NULL) | ||
1315 | return; | ||
1316 | |||
1317 | _MALLOC_LOCK(); | ||
1318 | malloc_func = " in free():"; | ||
1319 | if (g_pool == NULL) { | ||
1320 | _MALLOC_UNLOCK(); | ||
1321 | wrterror("free() called before allocation"); | ||
1322 | return; | ||
1323 | } | ||
1324 | if (malloc_active++) { | ||
1325 | malloc_recurse(); | ||
1326 | return; | ||
1327 | } | ||
1328 | ofree(ptr); | ||
1329 | malloc_active--; | ||
1330 | _MALLOC_UNLOCK(); | ||
1331 | errno = saved_errno; | ||
1332 | } | ||
1333 | |||
1334 | |||
1335 | static void * | ||
1336 | orealloc(void *p, size_t newsz) | ||
1337 | { | ||
1338 | struct region_info *r; | ||
1339 | size_t oldsz, goldsz, gnewsz; | ||
1340 | void *q; | ||
1341 | |||
1342 | if (p == NULL) | ||
1343 | return omalloc(newsz, 0); | ||
1344 | |||
1345 | r = find(g_pool, p); | ||
1346 | if (r == NULL) { | ||
1347 | wrterror("bogus pointer (double free?)"); | ||
1348 | return NULL; | ||
1349 | } | ||
1350 | if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { | ||
1351 | errno = ENOMEM; | ||
1352 | return NULL; | ||
1353 | } | ||
1354 | |||
1355 | REALSIZE(oldsz, r); | ||
1356 | goldsz = oldsz; | ||
1357 | if (oldsz > MALLOC_MAXCHUNK) { | ||
1358 | if (oldsz < mopts.malloc_guard) | ||
1359 | wrterror("guard size"); | ||
1360 | oldsz -= mopts.malloc_guard; | ||
1361 | } | ||
1362 | |||
1363 | gnewsz = newsz; | ||
1364 | if (gnewsz > MALLOC_MAXCHUNK) | ||
1365 | gnewsz += mopts.malloc_guard; | ||
1366 | |||
1367 | if (newsz > MALLOC_MAXCHUNK && oldsz > MALLOC_MAXCHUNK && p == r->p && | ||
1368 | !mopts.malloc_realloc) { | ||
1369 | size_t roldsz = PAGEROUND(goldsz); | ||
1370 | size_t rnewsz = PAGEROUND(gnewsz); | ||
1371 | |||
1372 | if (rnewsz > roldsz) { | ||
1373 | if (!mopts.malloc_guard) { | ||
1374 | STATS_INC(g_pool.cheap_realloc_tries); | ||
1375 | zapcacheregion(g_pool, p + roldsz); | ||
1376 | q = MMAPA(p + roldsz, rnewsz - roldsz); | ||
1377 | if (q == p + roldsz) { | ||
1378 | malloc_used += rnewsz - roldsz; | ||
1379 | if (mopts.malloc_junk) | ||
1380 | memset(q, SOME_JUNK, | ||
1381 | rnewsz - roldsz); | ||
1382 | r->size = newsz; | ||
1383 | STATS_INC(g_pool->cheap_reallocs); | ||
1384 | return p; | ||
1385 | } else if (q != MAP_FAILED) | ||
1386 | munmap(q, rnewsz - roldsz); | ||
1387 | } | ||
1388 | } else if (rnewsz < roldsz) { | ||
1389 | if (mopts.malloc_guard) { | ||
1390 | if (mprotect((char *)p + roldsz - | ||
1391 | mopts.malloc_guard, mopts.malloc_guard, | ||
1392 | PROT_READ | PROT_WRITE)) | ||
1393 | wrterror("mprotect"); | ||
1394 | if (mprotect((char *)p + rnewsz - | ||
1395 | mopts.malloc_guard, mopts.malloc_guard, | ||
1396 | PROT_NONE)) | ||
1397 | wrterror("mprotect"); | ||
1398 | } | ||
1399 | unmap(g_pool, (char *)p + rnewsz, roldsz - rnewsz); | ||
1400 | r->size = gnewsz; | ||
1401 | return p; | ||
1402 | } else { | ||
1403 | if (newsz > oldsz && mopts.malloc_junk) | ||
1404 | memset((char *)p + newsz, SOME_JUNK, | ||
1405 | rnewsz - mopts.malloc_guard - newsz); | ||
1406 | r->size = gnewsz; | ||
1407 | return p; | ||
1408 | } | ||
1409 | } | ||
1410 | if (newsz <= oldsz && newsz > oldsz / 2 && !mopts.malloc_realloc) { | ||
1411 | if (mopts.malloc_junk && newsz > 0) | ||
1412 | memset((char *)p + newsz, SOME_JUNK, oldsz - newsz); | ||
1413 | return p; | ||
1414 | } else if (newsz != oldsz || mopts.malloc_realloc) { | ||
1415 | q = omalloc(newsz, 0); | ||
1416 | if (q == NULL) | ||
1417 | return NULL; | ||
1418 | if (newsz != 0 && oldsz != 0) | ||
1419 | memcpy(q, p, oldsz < newsz ? oldsz : newsz); | ||
1420 | ofree(p); | ||
1421 | return q; | ||
1422 | } else | ||
1423 | return p; | ||
1424 | } | ||
1425 | |||
1426 | void * | ||
1427 | realloc(void *ptr, size_t size) | ||
1428 | { | ||
1429 | void *r; | ||
1430 | int saved_errno = errno; | ||
1431 | |||
1432 | _MALLOC_LOCK(); | ||
1433 | malloc_func = " in realloc():"; | ||
1434 | if (g_pool == NULL) { | ||
1435 | if (malloc_init() != 0) | ||
1436 | return NULL; | ||
1437 | } | ||
1438 | if (malloc_active++) { | ||
1439 | malloc_recurse(); | ||
1440 | return NULL; | ||
1441 | } | ||
1442 | r = orealloc(ptr, size); | ||
1443 | |||
1444 | malloc_active--; | ||
1445 | _MALLOC_UNLOCK(); | ||
1446 | if (r == NULL && mopts.malloc_xmalloc) { | ||
1447 | wrterror("out of memory"); | ||
1448 | errno = ENOMEM; | ||
1449 | } | ||
1450 | if (r != NULL) | ||
1451 | errno = saved_errno; | ||
1452 | return r; | ||
1453 | } | ||
1454 | |||
1455 | |||
1456 | #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) | ||
1457 | |||
1458 | void * | ||
1459 | calloc(size_t nmemb, size_t size) | ||
1460 | { | ||
1461 | void *r; | ||
1462 | int saved_errno = errno; | ||
1463 | |||
1464 | _MALLOC_LOCK(); | ||
1465 | malloc_func = " in calloc():"; | ||
1466 | if (g_pool == NULL) { | ||
1467 | if (malloc_init() != 0) | ||
1468 | return NULL; | ||
1469 | } | ||
1470 | if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && | ||
1471 | nmemb > 0 && SIZE_MAX / nmemb < size) { | ||
1472 | _MALLOC_UNLOCK(); | ||
1473 | if (mopts.malloc_xmalloc) | ||
1474 | wrterror("out of memory"); | ||
1475 | errno = ENOMEM; | ||
1476 | return NULL; | ||
1477 | } | ||
1478 | |||
1479 | if (malloc_active++) { | ||
1480 | malloc_recurse(); | ||
1481 | return NULL; | ||
1482 | } | ||
1483 | |||
1484 | size *= nmemb; | ||
1485 | r = omalloc(size, 1); | ||
1486 | |||
1487 | malloc_active--; | ||
1488 | _MALLOC_UNLOCK(); | ||
1489 | if (r == NULL && mopts.malloc_xmalloc) { | ||
1490 | wrterror("out of memory"); | ||
1491 | errno = ENOMEM; | ||
1492 | } | ||
1493 | if (r != NULL) | ||
1494 | errno = saved_errno; | ||
1495 | return r; | ||
1496 | } | ||
1497 | |||
diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c new file mode 100644 index 0000000000..623f652134 --- /dev/null +++ b/src/lib/libc/stdlib/merge.c | |||
@@ -0,0 +1,333 @@ | |||
1 | /* $OpenBSD: merge.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1992, 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 | * Peter McIlroy. | ||
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 | |||
34 | /* | ||
35 | * Hybrid exponential search/linear search merge sort with hybrid | ||
36 | * natural/pairwise first pass. Requires about .3% more comparisons | ||
37 | * for random data than LSMS with pairwise first pass alone. | ||
38 | * It works for objects as small as two bytes. | ||
39 | */ | ||
40 | |||
41 | #define NATURAL | ||
42 | #define THRESHOLD 16 /* Best choice for natural merge cut-off. */ | ||
43 | |||
44 | /* #define NATURAL to get hybrid natural merge. | ||
45 | * (The default is pairwise merging.) | ||
46 | */ | ||
47 | |||
48 | #include <sys/types.h> | ||
49 | |||
50 | #include <errno.h> | ||
51 | #include <stdlib.h> | ||
52 | #include <string.h> | ||
53 | |||
54 | static void setup(u_char *, u_char *, size_t, size_t, int (*)()); | ||
55 | static void insertionsort(u_char *, size_t, size_t, int (*)()); | ||
56 | |||
57 | #define ISIZE sizeof(int) | ||
58 | #define PSIZE sizeof(u_char *) | ||
59 | #define ICOPY_LIST(src, dst, last) \ | ||
60 | do \ | ||
61 | *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ | ||
62 | while(src < last) | ||
63 | #define ICOPY_ELT(src, dst, i) \ | ||
64 | do \ | ||
65 | *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ | ||
66 | while (i -= ISIZE) | ||
67 | |||
68 | #define CCOPY_LIST(src, dst, last) \ | ||
69 | do \ | ||
70 | *dst++ = *src++; \ | ||
71 | while (src < last) | ||
72 | #define CCOPY_ELT(src, dst, i) \ | ||
73 | do \ | ||
74 | *dst++ = *src++; \ | ||
75 | while (i -= 1) | ||
76 | |||
77 | /* | ||
78 | * Find the next possible pointer head. (Trickery for forcing an array | ||
79 | * to do double duty as a linked list when objects do not align with word | ||
80 | * boundaries. | ||
81 | */ | ||
82 | /* Assumption: PSIZE is a power of 2. */ | ||
83 | #define EVAL(p) (u_char **) \ | ||
84 | ((u_char *)0 + \ | ||
85 | (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1))) | ||
86 | |||
87 | /* | ||
88 | * Arguments are as for qsort. | ||
89 | */ | ||
90 | int | ||
91 | mergesort(void *base, size_t nmemb, size_t size, | ||
92 | int (*cmp)(const void *, const void *)) | ||
93 | { | ||
94 | int i, sense; | ||
95 | int big, iflag; | ||
96 | u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; | ||
97 | u_char *list2, *list1, *p2, *p, *last, **p1; | ||
98 | |||
99 | if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ | ||
100 | errno = EINVAL; | ||
101 | return (-1); | ||
102 | } | ||
103 | |||
104 | /* | ||
105 | * XXX | ||
106 | * Stupid subtraction for the Cray. | ||
107 | */ | ||
108 | iflag = 0; | ||
109 | if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) | ||
110 | iflag = 1; | ||
111 | |||
112 | if ((list2 = malloc(nmemb * size + PSIZE)) == NULL) | ||
113 | return (-1); | ||
114 | |||
115 | list1 = base; | ||
116 | setup(list1, list2, nmemb, size, cmp); | ||
117 | last = list2 + nmemb * size; | ||
118 | i = big = 0; | ||
119 | while (*EVAL(list2) != last) { | ||
120 | l2 = list1; | ||
121 | p1 = EVAL(list1); | ||
122 | for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { | ||
123 | p2 = *EVAL(p2); | ||
124 | f1 = l2; | ||
125 | f2 = l1 = list1 + (p2 - list2); | ||
126 | if (p2 != last) | ||
127 | p2 = *EVAL(p2); | ||
128 | l2 = list1 + (p2 - list2); | ||
129 | while (f1 < l1 && f2 < l2) { | ||
130 | if ((*cmp)(f1, f2) <= 0) { | ||
131 | q = f2; | ||
132 | b = f1, t = l1; | ||
133 | sense = -1; | ||
134 | } else { | ||
135 | q = f1; | ||
136 | b = f2, t = l2; | ||
137 | sense = 0; | ||
138 | } | ||
139 | if (!big) { /* here i = 0 */ | ||
140 | while ((b += size) < t && cmp(q, b) >sense) | ||
141 | if (++i == 6) { | ||
142 | big = 1; | ||
143 | goto EXPONENTIAL; | ||
144 | } | ||
145 | } else { | ||
146 | EXPONENTIAL: for (i = size; ; i <<= 1) | ||
147 | if ((p = (b + i)) >= t) { | ||
148 | if ((p = t - size) > b && | ||
149 | (*cmp)(q, p) <= sense) | ||
150 | t = p; | ||
151 | else | ||
152 | b = p; | ||
153 | break; | ||
154 | } else if ((*cmp)(q, p) <= sense) { | ||
155 | t = p; | ||
156 | if (i == size) | ||
157 | big = 0; | ||
158 | goto FASTCASE; | ||
159 | } else | ||
160 | b = p; | ||
161 | while (t > b+size) { | ||
162 | i = (((t - b) / size) >> 1) * size; | ||
163 | if ((*cmp)(q, p = b + i) <= sense) | ||
164 | t = p; | ||
165 | else | ||
166 | b = p; | ||
167 | } | ||
168 | goto COPY; | ||
169 | FASTCASE: while (i > size) | ||
170 | if ((*cmp)(q, | ||
171 | p = b + (i >>= 1)) <= sense) | ||
172 | t = p; | ||
173 | else | ||
174 | b = p; | ||
175 | COPY: b = t; | ||
176 | } | ||
177 | i = size; | ||
178 | if (q == f1) { | ||
179 | if (iflag) { | ||
180 | ICOPY_LIST(f2, tp2, b); | ||
181 | ICOPY_ELT(f1, tp2, i); | ||
182 | } else { | ||
183 | CCOPY_LIST(f2, tp2, b); | ||
184 | CCOPY_ELT(f1, tp2, i); | ||
185 | } | ||
186 | } else { | ||
187 | if (iflag) { | ||
188 | ICOPY_LIST(f1, tp2, b); | ||
189 | ICOPY_ELT(f2, tp2, i); | ||
190 | } else { | ||
191 | CCOPY_LIST(f1, tp2, b); | ||
192 | CCOPY_ELT(f2, tp2, i); | ||
193 | } | ||
194 | } | ||
195 | } | ||
196 | if (f2 < l2) { | ||
197 | if (iflag) | ||
198 | ICOPY_LIST(f2, tp2, l2); | ||
199 | else | ||
200 | CCOPY_LIST(f2, tp2, l2); | ||
201 | } else if (f1 < l1) { | ||
202 | if (iflag) | ||
203 | ICOPY_LIST(f1, tp2, l1); | ||
204 | else | ||
205 | CCOPY_LIST(f1, tp2, l1); | ||
206 | } | ||
207 | *p1 = l2; | ||
208 | } | ||
209 | tp2 = list1; /* swap list1, list2 */ | ||
210 | list1 = list2; | ||
211 | list2 = tp2; | ||
212 | last = list2 + nmemb*size; | ||
213 | } | ||
214 | if (base == list2) { | ||
215 | memmove(list2, list1, nmemb*size); | ||
216 | list2 = list1; | ||
217 | } | ||
218 | free(list2); | ||
219 | return (0); | ||
220 | } | ||
221 | |||
222 | #define swap(a, b) { \ | ||
223 | s = b; \ | ||
224 | i = size; \ | ||
225 | do { \ | ||
226 | tmp = *a; *a++ = *s; *s++ = tmp; \ | ||
227 | } while (--i); \ | ||
228 | a -= size; \ | ||
229 | } | ||
230 | #define reverse(bot, top) { \ | ||
231 | s = top; \ | ||
232 | do { \ | ||
233 | i = size; \ | ||
234 | do { \ | ||
235 | tmp = *bot; *bot++ = *s; *s++ = tmp; \ | ||
236 | } while (--i); \ | ||
237 | s -= size2; \ | ||
238 | } while(bot < s); \ | ||
239 | } | ||
240 | |||
241 | /* | ||
242 | * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of | ||
243 | * increasing order, list2 in a corresponding linked list. Checks for runs | ||
244 | * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL | ||
245 | * is defined. Otherwise simple pairwise merging is used.) | ||
246 | */ | ||
247 | void | ||
248 | setup(u_char *list1, u_char *list2, size_t n, size_t size, | ||
249 | int (*cmp)(const void *, const void *)) | ||
250 | { | ||
251 | int i, length, size2, tmp, sense; | ||
252 | u_char *f1, *f2, *s, *l2, *last, *p2; | ||
253 | |||
254 | size2 = size*2; | ||
255 | if (n <= 5) { | ||
256 | insertionsort(list1, n, size, cmp); | ||
257 | *EVAL(list2) = (u_char*) list2 + n*size; | ||
258 | return; | ||
259 | } | ||
260 | /* | ||
261 | * Avoid running pointers out of bounds; limit n to evens | ||
262 | * for simplicity. | ||
263 | */ | ||
264 | i = 4 + (n & 1); | ||
265 | insertionsort(list1 + (n - i) * size, i, size, cmp); | ||
266 | last = list1 + size * (n - i); | ||
267 | *EVAL(list2 + (last - list1)) = list2 + n * size; | ||
268 | |||
269 | #ifdef NATURAL | ||
270 | p2 = list2; | ||
271 | f1 = list1; | ||
272 | sense = (cmp(f1, f1 + size) > 0); | ||
273 | for (; f1 < last; sense = !sense) { | ||
274 | length = 2; | ||
275 | /* Find pairs with same sense. */ | ||
276 | for (f2 = f1 + size2; f2 < last; f2 += size2) { | ||
277 | if ((cmp(f2, f2+ size) > 0) != sense) | ||
278 | break; | ||
279 | length += 2; | ||
280 | } | ||
281 | if (length < THRESHOLD) { /* Pairwise merge */ | ||
282 | do { | ||
283 | p2 = *EVAL(p2) = f1 + size2 - list1 + list2; | ||
284 | if (sense > 0) | ||
285 | swap (f1, f1 + size); | ||
286 | } while ((f1 += size2) < f2); | ||
287 | } else { /* Natural merge */ | ||
288 | l2 = f2; | ||
289 | for (f2 = f1 + size2; f2 < l2; f2 += size2) { | ||
290 | if ((cmp(f2-size, f2) > 0) != sense) { | ||
291 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
292 | if (sense > 0) | ||
293 | reverse(f1, f2-size); | ||
294 | f1 = f2; | ||
295 | } | ||
296 | } | ||
297 | if (sense > 0) | ||
298 | reverse (f1, f2-size); | ||
299 | f1 = f2; | ||
300 | if (f2 < last || cmp(f2 - size, f2) > 0) | ||
301 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
302 | else | ||
303 | p2 = *EVAL(p2) = list2 + n*size; | ||
304 | } | ||
305 | } | ||
306 | #else /* pairwise merge only. */ | ||
307 | for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { | ||
308 | p2 = *EVAL(p2) = p2 + size2; | ||
309 | if (cmp (f1, f1 + size) > 0) | ||
310 | swap(f1, f1 + size); | ||
311 | } | ||
312 | #endif /* NATURAL */ | ||
313 | } | ||
314 | |||
315 | /* | ||
316 | * This is to avoid out-of-bounds addresses in sorting the | ||
317 | * last 4 elements. | ||
318 | */ | ||
319 | static void | ||
320 | insertionsort(u_char *a, size_t n, size_t size, | ||
321 | int (*cmp)(const void *, const void *)) | ||
322 | { | ||
323 | u_char *ai, *s, *t, *u, tmp; | ||
324 | int i; | ||
325 | |||
326 | for (ai = a+size; --n >= 1; ai += size) | ||
327 | for (t = ai; t > a; t -= size) { | ||
328 | u = t - size; | ||
329 | if (cmp(u, t) <= 0) | ||
330 | break; | ||
331 | swap(u, t); | ||
332 | } | ||
333 | } | ||
diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c new file mode 100644 index 0000000000..977264aba5 --- /dev/null +++ b/src/lib/libc/stdlib/mrand48.c | |||
@@ -0,0 +1,24 @@ | |||
1 | /* $OpenBSD: mrand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | extern unsigned short __rand48_seed[3]; | ||
18 | |||
19 | long | ||
20 | mrand48(void) | ||
21 | { | ||
22 | __dorand48(__rand48_seed); | ||
23 | return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1]; | ||
24 | } | ||
diff --git a/src/lib/libc/stdlib/nrand48.c b/src/lib/libc/stdlib/nrand48.c new file mode 100644 index 0000000000..f1f548c3af --- /dev/null +++ b/src/lib/libc/stdlib/nrand48.c | |||
@@ -0,0 +1,22 @@ | |||
1 | /* $OpenBSD: nrand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | long | ||
18 | nrand48(unsigned short xseed[3]) | ||
19 | { | ||
20 | __dorand48(xseed); | ||
21 | return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); | ||
22 | } | ||
diff --git a/src/lib/libc/stdlib/putenv.c b/src/lib/libc/stdlib/putenv.c new file mode 100644 index 0000000000..54482f6a14 --- /dev/null +++ b/src/lib/libc/stdlib/putenv.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: putenv.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1988, 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 | |||
31 | #include <stdlib.h> | ||
32 | #include <string.h> | ||
33 | |||
34 | int | ||
35 | putenv(const char *str) | ||
36 | { | ||
37 | char *p, *equal; | ||
38 | int rval; | ||
39 | |||
40 | if ((p = strdup(str)) == NULL) | ||
41 | return (-1); | ||
42 | if ((equal = strchr(p, '=')) == NULL) { | ||
43 | (void)free(p); | ||
44 | return (-1); | ||
45 | } | ||
46 | *equal = '\0'; | ||
47 | rval = setenv(p, equal + 1, 1); | ||
48 | (void)free(p); | ||
49 | return (rval); | ||
50 | } | ||
diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3 new file mode 100644 index 0000000000..e022e9c1f4 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.3 | |||
@@ -0,0 +1,57 @@ | |||
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.11 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 imaxabs 3 , | ||
54 | .Xr labs 3 , | ||
55 | .Xr math 3 | ||
56 | .Sh BUGS | ||
57 | 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..656b93c822 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* $OpenBSD: qabs.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | |||
33 | quad_t | ||
34 | qabs(quad_t j) | ||
35 | { | ||
36 | return(j < 0 ? -j : j); | ||
37 | } | ||
diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3 new file mode 100644 index 0000000000..59776bdf2d --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.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: qdiv.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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 / 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 imaxdiv 3 , | ||
60 | .Xr ldiv 3 , | ||
61 | .Xr lldiv 3 , | ||
62 | .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..f3db0915ed --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.c | |||
@@ -0,0 +1,50 @@ | |||
1 | /* $OpenBSD: qdiv.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1990 Regents of the University of California. | ||
4 | * 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 | |||
34 | #include <stdlib.h> /* qdiv_t */ | ||
35 | |||
36 | qdiv_t | ||
37 | qdiv(quad_t num, quad_t denom) | ||
38 | { | ||
39 | qdiv_t r; | ||
40 | |||
41 | /* see div.c for comments */ | ||
42 | |||
43 | r.quot = num / denom; | ||
44 | r.rem = num % denom; | ||
45 | if (num >= 0 && r.rem < 0) { | ||
46 | r.quot++; | ||
47 | r.rem -= denom; | ||
48 | } | ||
49 | return (r); | ||
50 | } | ||
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 new file mode 100644 index 0000000000..92c75d5365 --- /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.15 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..bb4a9a11f2 --- /dev/null +++ b/src/lib/libc/stdlib/qsort.c | |||
@@ -0,0 +1,161 @@ | |||
1 | /* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1992, 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 | |||
31 | #include <sys/types.h> | ||
32 | #include <stdlib.h> | ||
33 | |||
34 | static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *)); | ||
35 | static __inline void swapfunc(char *, char *, int, int); | ||
36 | |||
37 | #define min(a, b) (a) < (b) ? a : b | ||
38 | |||
39 | /* | ||
40 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". | ||
41 | */ | ||
42 | #define swapcode(TYPE, parmi, parmj, n) { \ | ||
43 | long i = (n) / sizeof (TYPE); \ | ||
44 | TYPE *pi = (TYPE *) (parmi); \ | ||
45 | TYPE *pj = (TYPE *) (parmj); \ | ||
46 | do { \ | ||
47 | TYPE t = *pi; \ | ||
48 | *pi++ = *pj; \ | ||
49 | *pj++ = t; \ | ||
50 | } while (--i > 0); \ | ||
51 | } | ||
52 | |||
53 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ | ||
54 | es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; | ||
55 | |||
56 | static __inline void | ||
57 | swapfunc(char *a, char *b, int n, int swaptype) | ||
58 | { | ||
59 | if (swaptype <= 1) | ||
60 | swapcode(long, a, b, n) | ||
61 | else | ||
62 | swapcode(char, a, b, n) | ||
63 | } | ||
64 | |||
65 | #define swap(a, b) \ | ||
66 | if (swaptype == 0) { \ | ||
67 | long t = *(long *)(a); \ | ||
68 | *(long *)(a) = *(long *)(b); \ | ||
69 | *(long *)(b) = t; \ | ||
70 | } else \ | ||
71 | swapfunc(a, b, es, swaptype) | ||
72 | |||
73 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) | ||
74 | |||
75 | static __inline char * | ||
76 | med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) | ||
77 | { | ||
78 | return cmp(a, b) < 0 ? | ||
79 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) | ||
80 | :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); | ||
81 | } | ||
82 | |||
83 | void | ||
84 | qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) | ||
85 | { | ||
86 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | ||
87 | int d, r, swaptype, swap_cnt; | ||
88 | char *a = aa; | ||
89 | |||
90 | loop: SWAPINIT(a, es); | ||
91 | swap_cnt = 0; | ||
92 | if (n < 7) { | ||
93 | for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) | ||
94 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
95 | pl -= es) | ||
96 | swap(pl, pl - es); | ||
97 | return; | ||
98 | } | ||
99 | pm = (char *)a + (n / 2) * es; | ||
100 | if (n > 7) { | ||
101 | pl = (char *)a; | ||
102 | pn = (char *)a + (n - 1) * es; | ||
103 | if (n > 40) { | ||
104 | d = (n / 8) * es; | ||
105 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | ||
106 | pm = med3(pm - d, pm, pm + d, cmp); | ||
107 | pn = med3(pn - 2 * d, pn - d, pn, cmp); | ||
108 | } | ||
109 | pm = med3(pl, pm, pn, cmp); | ||
110 | } | ||
111 | swap(a, pm); | ||
112 | pa = pb = (char *)a + es; | ||
113 | |||
114 | pc = pd = (char *)a + (n - 1) * es; | ||
115 | for (;;) { | ||
116 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { | ||
117 | if (r == 0) { | ||
118 | swap_cnt = 1; | ||
119 | swap(pa, pb); | ||
120 | pa += es; | ||
121 | } | ||
122 | pb += es; | ||
123 | } | ||
124 | while (pb <= pc && (r = cmp(pc, a)) >= 0) { | ||
125 | if (r == 0) { | ||
126 | swap_cnt = 1; | ||
127 | swap(pc, pd); | ||
128 | pd -= es; | ||
129 | } | ||
130 | pc -= es; | ||
131 | } | ||
132 | if (pb > pc) | ||
133 | break; | ||
134 | swap(pb, pc); | ||
135 | swap_cnt = 1; | ||
136 | pb += es; | ||
137 | pc -= es; | ||
138 | } | ||
139 | if (swap_cnt == 0) { /* Switch to insertion sort */ | ||
140 | for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) | ||
141 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
142 | pl -= es) | ||
143 | swap(pl, pl - es); | ||
144 | return; | ||
145 | } | ||
146 | |||
147 | pn = (char *)a + n * es; | ||
148 | r = min(pa - (char *)a, pb - pa); | ||
149 | vecswap(a, pb - r, r); | ||
150 | r = min(pd - pc, pn - pd - es); | ||
151 | vecswap(pb, pn - r, r); | ||
152 | if ((r = pb - pa) > es) | ||
153 | qsort(a, r / es, es, cmp); | ||
154 | if ((r = pd - pc) > es) { | ||
155 | /* Iterate rather than recurse to save stack space */ | ||
156 | a = pn - r; | ||
157 | n = r / es; | ||
158 | goto loop; | ||
159 | } | ||
160 | /* qsort(pn - r, r / es, es, cmp);*/ | ||
161 | } | ||
diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3 new file mode 100644 index 0000000000..b90a557718 --- /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.11 2007/05/31 19:19:31 jmc Exp $ | ||
29 | .\" | ||
30 | .Dd $Mdocdate: May 31 2007 $ | ||
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..49d03b52d5 --- /dev/null +++ b/src/lib/libc/stdlib/radixsort.c | |||
@@ -0,0 +1,294 @@ | |||
1 | /* $OpenBSD: radixsort.c,v 1.9 2007/09/02 15:19:17 deraadt Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990, 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 | * Peter McIlroy and by Dan Bernstein at New York University, | ||
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 | |||
34 | /* | ||
35 | * Radixsort routines. | ||
36 | * | ||
37 | * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. | ||
38 | * Use radixsort(a, n, trace, endchar) for this case. | ||
39 | * | ||
40 | * For stable sorting (using N extra pointers) use sradixsort(), which calls | ||
41 | * r_sort_b(). | ||
42 | * | ||
43 | * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, | ||
44 | * "Engineering Radix Sort". | ||
45 | */ | ||
46 | |||
47 | #include <sys/types.h> | ||
48 | #include <stdlib.h> | ||
49 | #include <errno.h> | ||
50 | |||
51 | typedef struct { | ||
52 | const u_char **sa; | ||
53 | int sn, si; | ||
54 | } stack; | ||
55 | |||
56 | static __inline void simplesort | ||
57 | (const u_char **, int, int, const u_char *, u_int); | ||
58 | static void r_sort_a(const u_char **, int, int, const u_char *, u_int); | ||
59 | static void r_sort_b(const u_char **, | ||
60 | const u_char **, int, int, const u_char *, u_int); | ||
61 | |||
62 | #define THRESHOLD 20 /* Divert to simplesort(). */ | ||
63 | #define SIZE 512 /* Default stack size. */ | ||
64 | |||
65 | #define SETUP { \ | ||
66 | if (tab == NULL) { \ | ||
67 | tr = tr0; \ | ||
68 | for (c = 0; c < endch; c++) \ | ||
69 | tr0[c] = c + 1; \ | ||
70 | tr0[c] = 0; \ | ||
71 | for (c++; c < 256; c++) \ | ||
72 | tr0[c] = c; \ | ||
73 | endch = 0; \ | ||
74 | } else { \ | ||
75 | endch = tab[endch]; \ | ||
76 | tr = tab; \ | ||
77 | if (endch != 0 && endch != 255) { \ | ||
78 | errno = EINVAL; \ | ||
79 | return (-1); \ | ||
80 | } \ | ||
81 | } \ | ||
82 | } | ||
83 | |||
84 | int | ||
85 | radixsort(const u_char **a, int n, const u_char *tab, u_int endch) | ||
86 | { | ||
87 | const u_char *tr; | ||
88 | int c; | ||
89 | u_char tr0[256]; | ||
90 | |||
91 | SETUP; | ||
92 | r_sort_a(a, n, 0, tr, endch); | ||
93 | return (0); | ||
94 | } | ||
95 | |||
96 | int | ||
97 | sradixsort(const u_char **a, int n, const u_char *tab, u_int endch) | ||
98 | { | ||
99 | const u_char *tr, **ta; | ||
100 | int c; | ||
101 | u_char tr0[256]; | ||
102 | |||
103 | SETUP; | ||
104 | if (n < THRESHOLD) | ||
105 | simplesort(a, n, 0, tr, endch); | ||
106 | else { | ||
107 | if ((ta = calloc(n, sizeof(a))) == NULL) | ||
108 | return (-1); | ||
109 | r_sort_b(a, ta, n, 0, tr, endch); | ||
110 | free(ta); | ||
111 | } | ||
112 | return (0); | ||
113 | } | ||
114 | |||
115 | #define empty(s) (s >= sp) | ||
116 | #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si | ||
117 | #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i | ||
118 | #define swap(a, b, t) t = a, a = b, b = t | ||
119 | |||
120 | /* Unstable, in-place sort. */ | ||
121 | void | ||
122 | r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch) | ||
123 | { | ||
124 | static int count[256], nc, bmin; | ||
125 | int c; | ||
126 | const u_char **ak, *r; | ||
127 | stack s[SIZE], *sp, *sp0, *sp1, temp; | ||
128 | int *cp, bigc; | ||
129 | const u_char **an, *t, **aj, **top[256]; | ||
130 | |||
131 | /* Set up stack. */ | ||
132 | sp = s; | ||
133 | push(a, n, i); | ||
134 | while (!empty(s)) { | ||
135 | pop(a, n, i); | ||
136 | if (n < THRESHOLD) { | ||
137 | simplesort(a, n, i, tr, endch); | ||
138 | continue; | ||
139 | } | ||
140 | an = a + n; | ||
141 | |||
142 | /* Make character histogram. */ | ||
143 | if (nc == 0) { | ||
144 | bmin = 255; /* First occupied bin, excluding eos. */ | ||
145 | for (ak = a; ak < an;) { | ||
146 | c = tr[(*ak++)[i]]; | ||
147 | if (++count[c] == 1 && c != endch) { | ||
148 | if (c < bmin) | ||
149 | bmin = c; | ||
150 | nc++; | ||
151 | } | ||
152 | } | ||
153 | if (sp + nc > s + SIZE) { /* Get more stack. */ | ||
154 | r_sort_a(a, n, i, tr, endch); | ||
155 | continue; | ||
156 | } | ||
157 | } | ||
158 | |||
159 | /* | ||
160 | * Set top[]; push incompletely sorted bins onto stack. | ||
161 | * top[] = pointers to last out-of-place element in bins. | ||
162 | * count[] = counts of elements in bins. | ||
163 | * Before permuting: top[c-1] + count[c] = top[c]; | ||
164 | * during deal: top[c] counts down to top[c-1]. | ||
165 | */ | ||
166 | sp0 = sp1 = sp; /* Stack position of biggest bin. */ | ||
167 | bigc = 2; /* Size of biggest bin. */ | ||
168 | if (endch == 0) /* Special case: set top[eos]. */ | ||
169 | top[0] = ak = a + count[0]; | ||
170 | else { | ||
171 | ak = a; | ||
172 | top[255] = an; | ||
173 | } | ||
174 | for (cp = count + bmin; nc > 0; cp++) { | ||
175 | while (*cp == 0) /* Find next non-empty pile. */ | ||
176 | cp++; | ||
177 | if (*cp > 1) { | ||
178 | if (*cp > bigc) { | ||
179 | bigc = *cp; | ||
180 | sp1 = sp; | ||
181 | } | ||
182 | push(ak, *cp, i+1); | ||
183 | } | ||
184 | top[cp-count] = ak += *cp; | ||
185 | nc--; | ||
186 | } | ||
187 | swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ | ||
188 | |||
189 | /* | ||
190 | * Permute misplacements home. Already home: everything | ||
191 | * before aj, and in bin[c], items from top[c] on. | ||
192 | * Inner loop: | ||
193 | * r = next element to put in place; | ||
194 | * ak = top[r[i]] = location to put the next element. | ||
195 | * aj = bottom of 1st disordered bin. | ||
196 | * Outer loop: | ||
197 | * Once the 1st disordered bin is done, ie. aj >= ak, | ||
198 | * aj<-aj + count[c] connects the bins in a linked list; | ||
199 | * reset count[c]. | ||
200 | */ | ||
201 | for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) | ||
202 | for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) | ||
203 | swap(*ak, r, t); | ||
204 | } | ||
205 | } | ||
206 | |||
207 | /* Stable sort, requiring additional memory. */ | ||
208 | void | ||
209 | r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr, | ||
210 | u_int endch) | ||
211 | { | ||
212 | static int count[256], nc, bmin; | ||
213 | int c; | ||
214 | const u_char **ak, **ai; | ||
215 | stack s[512], *sp, *sp0, *sp1, temp; | ||
216 | const u_char **top[256]; | ||
217 | int *cp, bigc; | ||
218 | |||
219 | sp = s; | ||
220 | push(a, n, i); | ||
221 | while (!empty(s)) { | ||
222 | pop(a, n, i); | ||
223 | if (n < THRESHOLD) { | ||
224 | simplesort(a, n, i, tr, endch); | ||
225 | continue; | ||
226 | } | ||
227 | |||
228 | if (nc == 0) { | ||
229 | bmin = 255; | ||
230 | for (ak = a + n; --ak >= a;) { | ||
231 | c = tr[(*ak)[i]]; | ||
232 | if (++count[c] == 1 && c != endch) { | ||
233 | if (c < bmin) | ||
234 | bmin = c; | ||
235 | nc++; | ||
236 | } | ||
237 | } | ||
238 | if (sp + nc > s + SIZE) { | ||
239 | r_sort_b(a, ta, n, i, tr, endch); | ||
240 | continue; | ||
241 | } | ||
242 | } | ||
243 | |||
244 | sp0 = sp1 = sp; | ||
245 | bigc = 2; | ||
246 | if (endch == 0) { | ||
247 | top[0] = ak = a + count[0]; | ||
248 | count[0] = 0; | ||
249 | } else { | ||
250 | ak = a; | ||
251 | top[255] = a + n; | ||
252 | count[255] = 0; | ||
253 | } | ||
254 | for (cp = count + bmin; nc > 0; cp++) { | ||
255 | while (*cp == 0) | ||
256 | cp++; | ||
257 | if ((c = *cp) > 1) { | ||
258 | if (c > bigc) { | ||
259 | bigc = c; | ||
260 | sp1 = sp; | ||
261 | } | ||
262 | push(ak, c, i+1); | ||
263 | } | ||
264 | top[cp-count] = ak += c; | ||
265 | *cp = 0; /* Reset count[]. */ | ||
266 | nc--; | ||
267 | } | ||
268 | swap(*sp0, *sp1, temp); | ||
269 | |||
270 | for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ | ||
271 | *--ak = *--ai; | ||
272 | for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ | ||
273 | *--top[tr[(*ak)[i]]] = *ak; | ||
274 | } | ||
275 | } | ||
276 | |||
277 | static __inline void | ||
278 | simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch) | ||
279 | /* insertion sort */ | ||
280 | { | ||
281 | u_char ch; | ||
282 | const u_char **ak, **ai, *s, *t; | ||
283 | |||
284 | for (ak = a+1; --n >= 1; ak++) | ||
285 | for (ai = ak; ai > a; ai--) { | ||
286 | for (s = ai[0] + b, t = ai[-1] + b; | ||
287 | (ch = tr[*s]) != endch; s++, t++) | ||
288 | if (ch != tr[*t]) | ||
289 | break; | ||
290 | if (ch >= tr[*t]) | ||
291 | break; | ||
292 | swap(ai[0], ai[-1], s); | ||
293 | } | ||
294 | } | ||
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3 new file mode 100644 index 0000000000..df4feaacdb --- /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.10 2007/05/31 19:19:31 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: May 31 2007 $ | ||
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..0f9c100807 --- /dev/null +++ b/src/lib/libc/stdlib/rand.c | |||
@@ -0,0 +1,52 @@ | |||
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 | #include <sys/types.h> | ||
31 | #include <stdlib.h> | ||
32 | |||
33 | static u_int next = 1; | ||
34 | |||
35 | int | ||
36 | rand_r(u_int *seed) | ||
37 | { | ||
38 | *seed = *seed * 1103515245 + 12345; | ||
39 | return (*seed % ((u_int)RAND_MAX + 1)); | ||
40 | } | ||
41 | |||
42 | int | ||
43 | rand(void) | ||
44 | { | ||
45 | return (rand_r(&next)); | ||
46 | } | ||
47 | |||
48 | void | ||
49 | srand(u_int seed) | ||
50 | { | ||
51 | next = seed; | ||
52 | } | ||
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 new file mode 100644 index 0000000000..0e8ca5389b --- /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.11 2007/05/31 19:19:31 jmc Exp $ | ||
13 | .\" | ||
14 | .Dd $Mdocdate: May 31 2007 $ | ||
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..ed05df162b --- /dev/null +++ b/src/lib/libc/stdlib/random.3 | |||
@@ -0,0 +1,185 @@ | |||
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.19 2007/05/31 19:19:31 jmc Exp $ | ||
29 | .\" | ||
30 | .Dd $Mdocdate: May 31 2007 $ | ||
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 | ||
93 | random numbers obtained from the kernel, | ||
94 | suitable for cryptographic use. | ||
95 | Note that this particular seeding procedure can generate | ||
96 | states which are impossible to reproduce by calling | ||
97 | .Fn srandom | ||
98 | with any value, since the succeeding terms in the | ||
99 | state buffer are no longer derived from the LC algorithm applied to | ||
100 | a fixed seed. | ||
101 | .Pp | ||
102 | The | ||
103 | .Fn initstate | ||
104 | routine allows a state array, passed in as an argument, to be initialized | ||
105 | for future use. | ||
106 | The size of the state array (in bytes) is used by | ||
107 | .Fn initstate | ||
108 | to decide how sophisticated a random number generator it should use \(em the | ||
109 | more state, the better the random numbers will be. | ||
110 | (Current "optimal" values for the amount of state information are | ||
111 | 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to | ||
112 | the nearest known amount. | ||
113 | Using less than 8 bytes will cause an error.) | ||
114 | The seed for the initialization (which specifies a starting point for | ||
115 | the random number sequence, and provides for restarting at the same | ||
116 | point) is also an argument. | ||
117 | The | ||
118 | .Fn initstate | ||
119 | function returns a pointer to the previous state information array. | ||
120 | .Pp | ||
121 | Once a state has been initialized, the | ||
122 | .Fn setstate | ||
123 | routine provides for rapid switching between states. | ||
124 | The | ||
125 | .Fn setstate | ||
126 | function returns a pointer to the previous state array; its | ||
127 | argument state array is used for further random number generation | ||
128 | until the next call to | ||
129 | .Fn initstate | ||
130 | or | ||
131 | .Fn setstate . | ||
132 | .Pp | ||
133 | Once a state array has been initialized, it may be restarted at a | ||
134 | different point either by calling | ||
135 | .Fn initstate | ||
136 | (with the desired seed, the state array, and its size) or by calling | ||
137 | both | ||
138 | .Fn setstate | ||
139 | (with the state array) and | ||
140 | .Fn srandom | ||
141 | (with the desired seed). | ||
142 | The advantage of calling both | ||
143 | .Fn setstate | ||
144 | and | ||
145 | .Fn srandom | ||
146 | is that the size of the state array does not have to be remembered after | ||
147 | it is initialized. | ||
148 | .Pp | ||
149 | With 256 bytes of state information, the period of the random number | ||
150 | generator is greater than 2**69 | ||
151 | which should be sufficient for most purposes. | ||
152 | .Sh DIAGNOSTICS | ||
153 | If | ||
154 | .Fn initstate | ||
155 | is called with less than 8 bytes of state information, or if | ||
156 | .Fn setstate | ||
157 | detects that the state information has been garbled, error | ||
158 | messages are printed on the standard error output. | ||
159 | .Sh SEE ALSO | ||
160 | .Xr arc4random 3 , | ||
161 | .Xr drand48 3 , | ||
162 | .Xr rand 3 , | ||
163 | .Xr random 4 | ||
164 | .Sh STANDARDS | ||
165 | The | ||
166 | .Fn random , | ||
167 | .Fn srandom , | ||
168 | .Fn initstate , | ||
169 | and | ||
170 | .Fn setstate | ||
171 | functions conform to | ||
172 | .St -xpg4.2 . | ||
173 | .Pp | ||
174 | The | ||
175 | .Fn srandomdev | ||
176 | function is an extension. | ||
177 | .Sh HISTORY | ||
178 | These | ||
179 | functions appeared in | ||
180 | .Bx 4.2 . | ||
181 | .Sh AUTHORS | ||
182 | .An Earl T. Cohen | ||
183 | .Sh BUGS | ||
184 | About 2/3 the speed of | ||
185 | .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..48e892042b --- /dev/null +++ b/src/lib/libc/stdlib/random.c | |||
@@ -0,0 +1,395 @@ | |||
1 | /* $OpenBSD: random.c,v 1.15 2005/11/30 07:51:02 otto Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1983 Regents of the University of California. | ||
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. 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 | #include <sys/param.h> | ||
32 | #include <sys/sysctl.h> | ||
33 | #include <sys/time.h> | ||
34 | #include <fcntl.h> | ||
35 | #include <stdio.h> | ||
36 | #include <stdlib.h> | ||
37 | #include <unistd.h> | ||
38 | |||
39 | /* | ||
40 | * random.c: | ||
41 | * | ||
42 | * An improved random number generation package. In addition to the standard | ||
43 | * rand()/srand() like interface, this package also has a special state info | ||
44 | * interface. The initstate() routine is called with a seed, an array of | ||
45 | * bytes, and a count of how many bytes are being passed in; this array is | ||
46 | * then initialized to contain information for random number generation with | ||
47 | * that much state information. Good sizes for the amount of state | ||
48 | * information are 32, 64, 128, and 256 bytes. The state can be switched by | ||
49 | * calling the setstate() routine with the same array as was initiallized | ||
50 | * with initstate(). By default, the package runs with 128 bytes of state | ||
51 | * information and generates far better random numbers than a linear | ||
52 | * congruential generator. If the amount of state information is less than | ||
53 | * 32 bytes, a simple linear congruential R.N.G. is used. | ||
54 | * | ||
55 | * Internally, the state information is treated as an array of int32_t; the | ||
56 | * zeroeth element of the array is the type of R.N.G. being used (small | ||
57 | * integer); the remainder of the array is the state information for the | ||
58 | * R.N.G. Thus, 32 bytes of state information will give 7 int32_ts worth of | ||
59 | * state information, which will allow a degree seven polynomial. (Note: | ||
60 | * the zeroeth word of state information also has some other information | ||
61 | * stored in it -- see setstate() for details). | ||
62 | * | ||
63 | * The random number generation technique is a linear feedback shift register | ||
64 | * approach, employing trinomials (since there are fewer terms to sum up that | ||
65 | * way). In this approach, the least significant bit of all the numbers in | ||
66 | * the state table will act as a linear feedback shift register, and will | ||
67 | * have period 2^deg - 1 (where deg is the degree of the polynomial being | ||
68 | * used, assuming that the polynomial is irreducible and primitive). The | ||
69 | * higher order bits will have longer periods, since their values are also | ||
70 | * influenced by pseudo-random carries out of the lower bits. The total | ||
71 | * period of the generator is approximately deg*(2**deg - 1); thus doubling | ||
72 | * the amount of state information has a vast influence on the period of the | ||
73 | * generator. Note: the deg*(2**deg - 1) is an approximation only good for | ||
74 | * large deg, when the period of the shift register is the dominant factor. | ||
75 | * With deg equal to seven, the period is actually much longer than the | ||
76 | * 7*(2**7 - 1) predicted by this formula. | ||
77 | */ | ||
78 | |||
79 | /* | ||
80 | * For each of the currently supported random number generators, we have a | ||
81 | * break value on the amount of state information (you need at least this | ||
82 | * many bytes of state info to support this random number generator), a degree | ||
83 | * for the polynomial (actually a trinomial) that the R.N.G. is based on, and | ||
84 | * the separation between the two lower order coefficients of the trinomial. | ||
85 | */ | ||
86 | #define TYPE_0 0 /* linear congruential */ | ||
87 | #define BREAK_0 8 | ||
88 | #define DEG_0 0 | ||
89 | #define SEP_0 0 | ||
90 | |||
91 | #define TYPE_1 1 /* x**7 + x**3 + 1 */ | ||
92 | #define BREAK_1 32 | ||
93 | #define DEG_1 7 | ||
94 | #define SEP_1 3 | ||
95 | |||
96 | #define TYPE_2 2 /* x**15 + x + 1 */ | ||
97 | #define BREAK_2 64 | ||
98 | #define DEG_2 15 | ||
99 | #define SEP_2 1 | ||
100 | |||
101 | #define TYPE_3 3 /* x**31 + x**3 + 1 */ | ||
102 | #define BREAK_3 128 | ||
103 | #define DEG_3 31 | ||
104 | #define SEP_3 3 | ||
105 | |||
106 | #define TYPE_4 4 /* x**63 + x + 1 */ | ||
107 | #define BREAK_4 256 | ||
108 | #define DEG_4 63 | ||
109 | #define SEP_4 1 | ||
110 | |||
111 | /* | ||
112 | * Array versions of the above information to make code run faster -- | ||
113 | * relies on fact that TYPE_i == i. | ||
114 | */ | ||
115 | #define MAX_TYPES 5 /* max number of types above */ | ||
116 | |||
117 | static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; | ||
118 | static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; | ||
119 | |||
120 | /* | ||
121 | * Initially, everything is set up as if from: | ||
122 | * | ||
123 | * initstate(1, &randtbl, 128); | ||
124 | * | ||
125 | * Note that this initialization takes advantage of the fact that srandom() | ||
126 | * advances the front and rear pointers 10*rand_deg times, and hence the | ||
127 | * rear pointer which starts at 0 will also end up at zero; thus the zeroeth | ||
128 | * element of the state information, which contains info about the current | ||
129 | * position of the rear pointer is just | ||
130 | * | ||
131 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. | ||
132 | */ | ||
133 | |||
134 | static int32_t randtbl[DEG_3 + 1] = { | ||
135 | TYPE_3, | ||
136 | 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, | ||
137 | 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, | ||
138 | 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471, | ||
139 | 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1, | ||
140 | 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41, | ||
141 | 0xf3bec5da, | ||
142 | }; | ||
143 | |||
144 | /* | ||
145 | * fptr and rptr are two pointers into the state info, a front and a rear | ||
146 | * pointer. These two pointers are always rand_sep places aparts, as they | ||
147 | * cycle cyclically through the state information. (Yes, this does mean we | ||
148 | * could get away with just one pointer, but the code for random() is more | ||
149 | * efficient this way). The pointers are left positioned as they would be | ||
150 | * from the call | ||
151 | * | ||
152 | * initstate(1, randtbl, 128); | ||
153 | * | ||
154 | * (The position of the rear pointer, rptr, is really 0 (as explained above | ||
155 | * in the initialization of randtbl) because the state table pointer is set | ||
156 | * to point to randtbl[1] (as explained below). | ||
157 | */ | ||
158 | static int32_t *fptr = &randtbl[SEP_3 + 1]; | ||
159 | static int32_t *rptr = &randtbl[1]; | ||
160 | |||
161 | /* | ||
162 | * The following things are the pointer to the state information table, the | ||
163 | * type of the current generator, the degree of the current polynomial being | ||
164 | * used, and the separation between the two pointers. Note that for efficiency | ||
165 | * of random(), we remember the first location of the state information, not | ||
166 | * the zeroeth. Hence it is valid to access state[-1], which is used to | ||
167 | * store the type of the R.N.G. Also, we remember the last location, since | ||
168 | * this is more efficient than indexing every time to find the address of | ||
169 | * the last element to see if the front and rear pointers have wrapped. | ||
170 | */ | ||
171 | static int32_t *state = &randtbl[1]; | ||
172 | static int32_t *end_ptr = &randtbl[DEG_3 + 1]; | ||
173 | static int rand_type = TYPE_3; | ||
174 | static int rand_deg = DEG_3; | ||
175 | static int rand_sep = SEP_3; | ||
176 | |||
177 | /* | ||
178 | * srandom: | ||
179 | * | ||
180 | * Initialize the random number generator based on the given seed. If the | ||
181 | * type is the trivial no-state-information type, just remember the seed. | ||
182 | * Otherwise, initializes state[] based on the given "seed" via a linear | ||
183 | * congruential generator. Then, the pointers are set to known locations | ||
184 | * that are exactly rand_sep places apart. Lastly, it cycles the state | ||
185 | * information a given number of times to get rid of any initial dependencies | ||
186 | * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] | ||
187 | * for default usage relies on values produced by this routine. | ||
188 | */ | ||
189 | void | ||
190 | srandom(unsigned int x) | ||
191 | { | ||
192 | int i; | ||
193 | int32_t test; | ||
194 | div_t val; | ||
195 | |||
196 | if (rand_type == TYPE_0) | ||
197 | state[0] = x; | ||
198 | else { | ||
199 | state[0] = x; | ||
200 | for (i = 1; i < rand_deg; i++) { | ||
201 | /* | ||
202 | * Implement the following, without overflowing 31 bits: | ||
203 | * | ||
204 | * state[i] = (16807 * state[i - 1]) % 2147483647; | ||
205 | * | ||
206 | * 2^31-1 (prime) = 2147483647 = 127773*16807+2836 | ||
207 | */ | ||
208 | val = div(state[i-1], 127773); | ||
209 | test = 16807 * val.rem - 2836 * val.quot; | ||
210 | state[i] = test + (test < 0 ? 2147483647 : 0); | ||
211 | } | ||
212 | fptr = &state[rand_sep]; | ||
213 | rptr = &state[0]; | ||
214 | for (i = 0; i < 10 * rand_deg; i++) | ||
215 | (void)random(); | ||
216 | } | ||
217 | } | ||
218 | |||
219 | /* | ||
220 | * srandomdev: | ||
221 | * | ||
222 | * Many programs choose the seed value in a totally predictable manner. | ||
223 | * This often causes problems. We seed the generator using random | ||
224 | * data from the kernel. | ||
225 | * Note that this particular seeding procedure can generate states | ||
226 | * which are impossible to reproduce by calling srandom() with any | ||
227 | * value, since the succeeding terms in the state buffer are no longer | ||
228 | * derived from the LC algorithm applied to a fixed seed. | ||
229 | */ | ||
230 | void | ||
231 | srandomdev(void) | ||
232 | { | ||
233 | int mib[2]; | ||
234 | size_t len; | ||
235 | |||
236 | if (rand_type == TYPE_0) | ||
237 | len = sizeof(state[0]); | ||
238 | else | ||
239 | len = rand_deg * sizeof(state[0]); | ||
240 | |||
241 | mib[0] = CTL_KERN; | ||
242 | mib[1] = KERN_ARND; | ||
243 | sysctl(mib, 2, state, &len, NULL, 0); | ||
244 | |||
245 | if (rand_type != TYPE_0) { | ||
246 | fptr = &state[rand_sep]; | ||
247 | rptr = &state[0]; | ||
248 | } | ||
249 | } | ||
250 | |||
251 | /* | ||
252 | * initstate: | ||
253 | * | ||
254 | * Initialize the state information in the given array of n bytes for future | ||
255 | * random number generation. Based on the number of bytes we are given, and | ||
256 | * the break values for the different R.N.G.'s, we choose the best (largest) | ||
257 | * one we can and set things up for it. srandom() is then called to | ||
258 | * initialize the state information. | ||
259 | * | ||
260 | * Note that on return from srandom(), we set state[-1] to be the type | ||
261 | * multiplexed with the current value of the rear pointer; this is so | ||
262 | * successive calls to initstate() won't lose this information and will be | ||
263 | * able to restart with setstate(). | ||
264 | * | ||
265 | * Note: the first thing we do is save the current state, if any, just like | ||
266 | * setstate() so that it doesn't matter when initstate is called. | ||
267 | * | ||
268 | * Returns a pointer to the old state. | ||
269 | */ | ||
270 | char * | ||
271 | initstate(u_int seed, char *arg_state, size_t n) | ||
272 | { | ||
273 | char *ostate = (char *)(&state[-1]); | ||
274 | |||
275 | if (rand_type == TYPE_0) | ||
276 | state[-1] = rand_type; | ||
277 | else | ||
278 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
279 | if (n < BREAK_0) | ||
280 | return(NULL); | ||
281 | if (n < BREAK_1) { | ||
282 | rand_type = TYPE_0; | ||
283 | rand_deg = DEG_0; | ||
284 | rand_sep = SEP_0; | ||
285 | } else if (n < BREAK_2) { | ||
286 | rand_type = TYPE_1; | ||
287 | rand_deg = DEG_1; | ||
288 | rand_sep = SEP_1; | ||
289 | } else if (n < BREAK_3) { | ||
290 | rand_type = TYPE_2; | ||
291 | rand_deg = DEG_2; | ||
292 | rand_sep = SEP_2; | ||
293 | } else if (n < BREAK_4) { | ||
294 | rand_type = TYPE_3; | ||
295 | rand_deg = DEG_3; | ||
296 | rand_sep = SEP_3; | ||
297 | } else { | ||
298 | rand_type = TYPE_4; | ||
299 | rand_deg = DEG_4; | ||
300 | rand_sep = SEP_4; | ||
301 | } | ||
302 | state = &(((int32_t *)arg_state)[1]); /* first location */ | ||
303 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ | ||
304 | srandom(seed); | ||
305 | if (rand_type == TYPE_0) | ||
306 | state[-1] = rand_type; | ||
307 | else | ||
308 | state[-1] = MAX_TYPES*(rptr - state) + rand_type; | ||
309 | return(ostate); | ||
310 | } | ||
311 | |||
312 | /* | ||
313 | * setstate: | ||
314 | * | ||
315 | * Restore the state from the given state array. | ||
316 | * | ||
317 | * Note: it is important that we also remember the locations of the pointers | ||
318 | * in the current state information, and restore the locations of the pointers | ||
319 | * from the old state information. This is done by multiplexing the pointer | ||
320 | * location into the zeroeth word of the state information. | ||
321 | * | ||
322 | * Note that due to the order in which things are done, it is OK to call | ||
323 | * setstate() with the same state as the current state. | ||
324 | * | ||
325 | * Returns a pointer to the old state information. | ||
326 | */ | ||
327 | char * | ||
328 | setstate(const char *arg_state) | ||
329 | { | ||
330 | int32_t *new_state = (int32_t *)arg_state; | ||
331 | int32_t type = new_state[0] % MAX_TYPES; | ||
332 | int32_t rear = new_state[0] / MAX_TYPES; | ||
333 | char *ostate = (char *)(&state[-1]); | ||
334 | |||
335 | if (rand_type == TYPE_0) | ||
336 | state[-1] = rand_type; | ||
337 | else | ||
338 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
339 | switch(type) { | ||
340 | case TYPE_0: | ||
341 | case TYPE_1: | ||
342 | case TYPE_2: | ||
343 | case TYPE_3: | ||
344 | case TYPE_4: | ||
345 | rand_type = type; | ||
346 | rand_deg = degrees[type]; | ||
347 | rand_sep = seps[type]; | ||
348 | break; | ||
349 | default: | ||
350 | return(NULL); | ||
351 | } | ||
352 | state = &new_state[1]; | ||
353 | if (rand_type != TYPE_0) { | ||
354 | rptr = &state[rear]; | ||
355 | fptr = &state[(rear + rand_sep) % rand_deg]; | ||
356 | } | ||
357 | end_ptr = &state[rand_deg]; /* set end_ptr too */ | ||
358 | return(ostate); | ||
359 | } | ||
360 | |||
361 | /* | ||
362 | * random: | ||
363 | * | ||
364 | * If we are using the trivial TYPE_0 R.N.G., just do the old linear | ||
365 | * congruential bit. Otherwise, we do our fancy trinomial stuff, which is | ||
366 | * the same in all the other cases due to all the global variables that have | ||
367 | * been set up. The basic operation is to add the number at the rear pointer | ||
368 | * into the one at the front pointer. Then both pointers are advanced to | ||
369 | * the next location cyclically in the table. The value returned is the sum | ||
370 | * generated, reduced to 31 bits by throwing away the "least random" low bit. | ||
371 | * | ||
372 | * Note: the code takes advantage of the fact that both the front and | ||
373 | * rear pointers can't wrap on the same call by not testing the rear | ||
374 | * pointer if the front one has wrapped. | ||
375 | * | ||
376 | * Returns a 31-bit random number. | ||
377 | */ | ||
378 | long | ||
379 | random(void) | ||
380 | { | ||
381 | int32_t i; | ||
382 | |||
383 | if (rand_type == TYPE_0) | ||
384 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; | ||
385 | else { | ||
386 | *fptr += *rptr; | ||
387 | i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ | ||
388 | if (++fptr >= end_ptr) { | ||
389 | fptr = state; | ||
390 | ++rptr; | ||
391 | } else if (++rptr >= end_ptr) | ||
392 | rptr = state; | ||
393 | } | ||
394 | return((long)i); | ||
395 | } | ||
diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3 new file mode 100644 index 0000000000..c64fc3d637 --- /dev/null +++ b/src/lib/libc/stdlib/realpath.3 | |||
@@ -0,0 +1,117 @@ | |||
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.15 2007/07/06 15:42:04 millert Exp $ | ||
32 | .\" | ||
33 | .Dd $Mdocdate: July 6 2007 $ | ||
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 <limits.h> | ||
41 | .Fd #include <stdlib.h> | ||
42 | .Ft "char *" | ||
43 | .Fn realpath "const char *pathname" "char resolved[PATH_MAX]" | ||
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 resolved . | ||
57 | The | ||
58 | .Fa resolved | ||
59 | argument | ||
60 | .Em must | ||
61 | refer to a buffer capable of storing at least | ||
62 | .Dv PATH_MAX | ||
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 resolved | ||
80 | on success. | ||
81 | If an error occurs, | ||
82 | .Fn realpath | ||
83 | returns | ||
84 | .Dv NULL | ||
85 | and the contents of | ||
86 | .Fa resolved | ||
87 | are undefined. | ||
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 lstat 2 , | ||
95 | .Xr readlink 2 , | ||
96 | and | ||
97 | .Xr getcwd 3 . | ||
98 | .Sh SEE ALSO | ||
99 | .Xr readlink 1 , | ||
100 | .Xr getcwd 3 | ||
101 | .Sh HISTORY | ||
102 | The | ||
103 | .Fn realpath | ||
104 | function call first appeared in | ||
105 | .Bx 4.4 . | ||
106 | .Sh CAVEATS | ||
107 | This implementation of | ||
108 | .Fn realpath | ||
109 | differs slightly from the Solaris implementation. | ||
110 | The | ||
111 | .Bx 4.4 | ||
112 | version always returns absolute pathnames, | ||
113 | whereas the Solaris implementation will, | ||
114 | under certain circumstances, return a relative | ||
115 | .Fa resolved | ||
116 | when given a relative | ||
117 | .Fa pathname . | ||
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c new file mode 100644 index 0000000000..4cb847b313 --- /dev/null +++ b/src/lib/libc/stdlib/realpath.c | |||
@@ -0,0 +1,190 @@ | |||
1 | /* $OpenBSD: realpath.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru> | ||
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 names of the authors may not be used to endorse or promote | ||
14 | * products derived from this software without specific prior written | ||
15 | * permission. | ||
16 | * | ||
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 | #include <sys/param.h> | ||
31 | #include <sys/stat.h> | ||
32 | |||
33 | #include <errno.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <unistd.h> | ||
37 | |||
38 | /* | ||
39 | * char *realpath(const char *path, char resolved[PATH_MAX]); | ||
40 | * | ||
41 | * Find the real name of path, by removing all ".", ".." and symlink | ||
42 | * components. Returns (resolved) on success, or (NULL) on failure, | ||
43 | * in which case the path which caused trouble is left in (resolved). | ||
44 | */ | ||
45 | char * | ||
46 | realpath(const char *path, char resolved[PATH_MAX]) | ||
47 | { | ||
48 | struct stat sb; | ||
49 | char *p, *q, *s; | ||
50 | size_t left_len, resolved_len; | ||
51 | unsigned symlinks; | ||
52 | int serrno, slen; | ||
53 | char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; | ||
54 | |||
55 | serrno = errno; | ||
56 | symlinks = 0; | ||
57 | if (path[0] == '/') { | ||
58 | resolved[0] = '/'; | ||
59 | resolved[1] = '\0'; | ||
60 | if (path[1] == '\0') | ||
61 | return (resolved); | ||
62 | resolved_len = 1; | ||
63 | left_len = strlcpy(left, path + 1, sizeof(left)); | ||
64 | } else { | ||
65 | if (getcwd(resolved, PATH_MAX) == NULL) { | ||
66 | strlcpy(resolved, ".", PATH_MAX); | ||
67 | return (NULL); | ||
68 | } | ||
69 | resolved_len = strlen(resolved); | ||
70 | left_len = strlcpy(left, path, sizeof(left)); | ||
71 | } | ||
72 | if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { | ||
73 | errno = ENAMETOOLONG; | ||
74 | return (NULL); | ||
75 | } | ||
76 | |||
77 | /* | ||
78 | * Iterate over path components in `left'. | ||
79 | */ | ||
80 | while (left_len != 0) { | ||
81 | /* | ||
82 | * Extract the next path component and adjust `left' | ||
83 | * and its length. | ||
84 | */ | ||
85 | p = strchr(left, '/'); | ||
86 | s = p ? p : left + left_len; | ||
87 | if (s - left >= sizeof(next_token)) { | ||
88 | errno = ENAMETOOLONG; | ||
89 | return (NULL); | ||
90 | } | ||
91 | memcpy(next_token, left, s - left); | ||
92 | next_token[s - left] = '\0'; | ||
93 | left_len -= s - left; | ||
94 | if (p != NULL) | ||
95 | memmove(left, s + 1, left_len + 1); | ||
96 | if (resolved[resolved_len - 1] != '/') { | ||
97 | if (resolved_len + 1 >= PATH_MAX) { | ||
98 | errno = ENAMETOOLONG; | ||
99 | return (NULL); | ||
100 | } | ||
101 | resolved[resolved_len++] = '/'; | ||
102 | resolved[resolved_len] = '\0'; | ||
103 | } | ||
104 | if (next_token[0] == '\0') | ||
105 | continue; | ||
106 | else if (strcmp(next_token, ".") == 0) | ||
107 | continue; | ||
108 | else if (strcmp(next_token, "..") == 0) { | ||
109 | /* | ||
110 | * Strip the last path component except when we have | ||
111 | * single "/" | ||
112 | */ | ||
113 | if (resolved_len > 1) { | ||
114 | resolved[resolved_len - 1] = '\0'; | ||
115 | q = strrchr(resolved, '/') + 1; | ||
116 | *q = '\0'; | ||
117 | resolved_len = q - resolved; | ||
118 | } | ||
119 | continue; | ||
120 | } | ||
121 | |||
122 | /* | ||
123 | * Append the next path component and lstat() it. If | ||
124 | * lstat() fails we still can return successfully if | ||
125 | * there are no more path components left. | ||
126 | */ | ||
127 | resolved_len = strlcat(resolved, next_token, PATH_MAX); | ||
128 | if (resolved_len >= PATH_MAX) { | ||
129 | errno = ENAMETOOLONG; | ||
130 | return (NULL); | ||
131 | } | ||
132 | if (lstat(resolved, &sb) != 0) { | ||
133 | if (errno == ENOENT && p == NULL) { | ||
134 | errno = serrno; | ||
135 | return (resolved); | ||
136 | } | ||
137 | return (NULL); | ||
138 | } | ||
139 | if (S_ISLNK(sb.st_mode)) { | ||
140 | if (symlinks++ > MAXSYMLINKS) { | ||
141 | errno = ELOOP; | ||
142 | return (NULL); | ||
143 | } | ||
144 | slen = readlink(resolved, symlink, sizeof(symlink) - 1); | ||
145 | if (slen < 0) | ||
146 | return (NULL); | ||
147 | symlink[slen] = '\0'; | ||
148 | if (symlink[0] == '/') { | ||
149 | resolved[1] = 0; | ||
150 | resolved_len = 1; | ||
151 | } else if (resolved_len > 1) { | ||
152 | /* Strip the last path component. */ | ||
153 | resolved[resolved_len - 1] = '\0'; | ||
154 | q = strrchr(resolved, '/') + 1; | ||
155 | *q = '\0'; | ||
156 | resolved_len = q - resolved; | ||
157 | } | ||
158 | |||
159 | /* | ||
160 | * If there are any path components left, then | ||
161 | * append them to symlink. The result is placed | ||
162 | * in `left'. | ||
163 | */ | ||
164 | if (p != NULL) { | ||
165 | if (symlink[slen - 1] != '/') { | ||
166 | if (slen + 1 >= sizeof(symlink)) { | ||
167 | errno = ENAMETOOLONG; | ||
168 | return (NULL); | ||
169 | } | ||
170 | symlink[slen] = '/'; | ||
171 | symlink[slen + 1] = 0; | ||
172 | } | ||
173 | left_len = strlcat(symlink, left, sizeof(left)); | ||
174 | if (left_len >= sizeof(left)) { | ||
175 | errno = ENAMETOOLONG; | ||
176 | return (NULL); | ||
177 | } | ||
178 | } | ||
179 | left_len = strlcpy(left, symlink, sizeof(left)); | ||
180 | } | ||
181 | } | ||
182 | |||
183 | /* | ||
184 | * Remove trailing slash except when the resolved pathname | ||
185 | * is a single "/". | ||
186 | */ | ||
187 | if (resolved_len > 1 && resolved[resolved_len - 1] == '/') | ||
188 | resolved[resolved_len - 1] = '\0'; | ||
189 | return (resolved); | ||
190 | } | ||
diff --git a/src/lib/libc/stdlib/remque.c b/src/lib/libc/stdlib/remque.c new file mode 100644 index 0000000000..ae249ae053 --- /dev/null +++ b/src/lib/libc/stdlib/remque.c | |||
@@ -0,0 +1,44 @@ | |||
1 | /* $OpenBSD: remque.c,v 1.2 2005/08/08 08:05:37 espie 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 | #include <search.h> | ||
32 | |||
33 | struct qelem { | ||
34 | struct qelem *q_forw; | ||
35 | struct qelem *q_back; | ||
36 | }; | ||
37 | |||
38 | void | ||
39 | remque(void *element) | ||
40 | { | ||
41 | struct qelem *e = (struct qelem *) element; | ||
42 | e->q_forw->q_back = e->q_back; | ||
43 | e->q_back->q_forw = e->q_forw; | ||
44 | } | ||
diff --git a/src/lib/libc/stdlib/seed48.c b/src/lib/libc/stdlib/seed48.c new file mode 100644 index 0000000000..583262f2d5 --- /dev/null +++ b/src/lib/libc/stdlib/seed48.c | |||
@@ -0,0 +1,37 @@ | |||
1 | /* $OpenBSD: seed48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | extern unsigned short __rand48_seed[3]; | ||
18 | extern unsigned short __rand48_mult[3]; | ||
19 | extern unsigned short __rand48_add; | ||
20 | |||
21 | unsigned short * | ||
22 | seed48(unsigned short xseed[3]) | ||
23 | { | ||
24 | static unsigned short sseed[3]; | ||
25 | |||
26 | sseed[0] = __rand48_seed[0]; | ||
27 | sseed[1] = __rand48_seed[1]; | ||
28 | sseed[2] = __rand48_seed[2]; | ||
29 | __rand48_seed[0] = xseed[0]; | ||
30 | __rand48_seed[1] = xseed[1]; | ||
31 | __rand48_seed[2] = xseed[2]; | ||
32 | __rand48_mult[0] = RAND48_MULT_0; | ||
33 | __rand48_mult[1] = RAND48_MULT_1; | ||
34 | __rand48_mult[2] = RAND48_MULT_2; | ||
35 | __rand48_add = RAND48_ADD; | ||
36 | return sseed; | ||
37 | } | ||
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c new file mode 100644 index 0000000000..36540ebb0c --- /dev/null +++ b/src/lib/libc/stdlib/setenv.c | |||
@@ -0,0 +1,103 @@ | |||
1 | /* $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1987 Regents of the University of California. | ||
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. 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 | #include <stdlib.h> | ||
32 | #include <string.h> | ||
33 | |||
34 | char *__findenv(const char *name, int *offset); | ||
35 | |||
36 | extern char **environ; | ||
37 | |||
38 | /* | ||
39 | * setenv -- | ||
40 | * Set the value of the environmental variable "name" to be | ||
41 | * "value". If rewrite is set, replace any current value. | ||
42 | */ | ||
43 | int | ||
44 | setenv(const char *name, const char *value, int rewrite) | ||
45 | { | ||
46 | static char **lastenv; /* last value of environ */ | ||
47 | char *C; | ||
48 | int l_value, offset; | ||
49 | |||
50 | if (*value == '=') /* no `=' in value */ | ||
51 | ++value; | ||
52 | l_value = strlen(value); | ||
53 | if ((C = __findenv(name, &offset))) { /* find if already exists */ | ||
54 | if (!rewrite) | ||
55 | return (0); | ||
56 | if (strlen(C) >= l_value) { /* old larger; copy over */ | ||
57 | while ((*C++ = *value++)) | ||
58 | ; | ||
59 | return (0); | ||
60 | } | ||
61 | } else { /* create new slot */ | ||
62 | size_t cnt; | ||
63 | char **P; | ||
64 | |||
65 | for (P = environ; *P != NULL; P++) | ||
66 | ; | ||
67 | cnt = P - environ; | ||
68 | P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2)); | ||
69 | if (!P) | ||
70 | return (-1); | ||
71 | if (lastenv != environ) | ||
72 | memcpy(P, environ, cnt * sizeof(char *)); | ||
73 | lastenv = environ = P; | ||
74 | offset = cnt; | ||
75 | environ[cnt + 1] = NULL; | ||
76 | } | ||
77 | for (C = (char *)name; *C && *C != '='; ++C) | ||
78 | ; /* no `=' in name */ | ||
79 | if (!(environ[offset] = /* name + `=' + value */ | ||
80 | malloc((size_t)((int)(C - name) + l_value + 2)))) | ||
81 | return (-1); | ||
82 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) | ||
83 | ; | ||
84 | for (*C++ = '='; (*C++ = *value++); ) | ||
85 | ; | ||
86 | return (0); | ||
87 | } | ||
88 | |||
89 | /* | ||
90 | * unsetenv(name) -- | ||
91 | * Delete environmental variable "name". | ||
92 | */ | ||
93 | void | ||
94 | unsetenv(const char *name) | ||
95 | { | ||
96 | char **P; | ||
97 | int offset; | ||
98 | |||
99 | while (__findenv(name, &offset)) /* if set multiple times */ | ||
100 | for (P = &environ[offset];; ++P) | ||
101 | if (!(*P = *(P + 1))) | ||
102 | break; | ||
103 | } | ||
diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c new file mode 100644 index 0000000000..f76b6cca86 --- /dev/null +++ b/src/lib/libc/stdlib/srand48.c | |||
@@ -0,0 +1,31 @@ | |||
1 | /* $OpenBSD: srand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1993 Martin Birgmeier | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * You may redistribute unmodified or modified versions of this source | ||
7 | * code provided that the above copyright notice and this and the | ||
8 | * following conditions are retained. | ||
9 | * | ||
10 | * This software is provided ``as is'', and comes with no warranties | ||
11 | * of any kind. I shall in no event be liable for anything that happens | ||
12 | * to anyone/anything when using this software. | ||
13 | */ | ||
14 | |||
15 | #include "rand48.h" | ||
16 | |||
17 | extern unsigned short __rand48_seed[3]; | ||
18 | extern unsigned short __rand48_mult[3]; | ||
19 | extern unsigned short __rand48_add; | ||
20 | |||
21 | void | ||
22 | srand48(long seed) | ||
23 | { | ||
24 | __rand48_seed[0] = RAND48_SEED_0; | ||
25 | __rand48_seed[1] = (unsigned short) seed; | ||
26 | __rand48_seed[2] = (unsigned short) (seed >> 16); | ||
27 | __rand48_mult[0] = RAND48_MULT_0; | ||
28 | __rand48_mult[1] = RAND48_MULT_1; | ||
29 | __rand48_mult[2] = RAND48_MULT_2; | ||
30 | __rand48_add = RAND48_ADD; | ||
31 | } | ||
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3 new file mode 100644 index 0000000000..f1fc781f7d --- /dev/null +++ b/src/lib/libc/stdlib/strtod.3 | |||
@@ -0,0 +1,145 @@ | |||
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.14 2008/09/13 22:48:45 martynas Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: September 13 2008 $ | ||
35 | .Dt STRTOD 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtod , | ||
39 | .Nm strtof , | ||
40 | .Nm strtold | ||
41 | .Nd convert | ||
42 | .Tn ASCII | ||
43 | string to double, float or long double | ||
44 | .Sh SYNOPSIS | ||
45 | .Fd #include <math.h> | ||
46 | .Fd #include <stdlib.h> | ||
47 | .Ft double | ||
48 | .Fn strtod "const char *nptr" "char **endptr" | ||
49 | .Pp | ||
50 | .Ft float | ||
51 | .Fn strtof "const char *nptr" "char **endptr" | ||
52 | .Pp | ||
53 | .Ft long double | ||
54 | .Fn strtold "const char *nptr" "char **endptr" | ||
55 | .Sh DESCRIPTION | ||
56 | The | ||
57 | .Fn strtod | ||
58 | function converts the initial portion of the string pointed to by | ||
59 | .Fa nptr | ||
60 | to | ||
61 | .Li double | ||
62 | representation. | ||
63 | The | ||
64 | .Fn strtof | ||
65 | function converts the initial portion of the string pointed to by | ||
66 | .Fa nptr | ||
67 | to | ||
68 | .Li float | ||
69 | representation. | ||
70 | The | ||
71 | .Fn strtold | ||
72 | function converts the initial portion of the string pointed to by | ||
73 | .Fa nptr | ||
74 | to | ||
75 | .Li long double | ||
76 | representation. | ||
77 | .Pp | ||
78 | The expected form of the string is an optional plus | ||
79 | .Pq Ql + | ||
80 | or minus sign | ||
81 | .Pq Ql - | ||
82 | followed by a sequence of digits optionally containing | ||
83 | a decimal-point character, optionally followed by an exponent. | ||
84 | An exponent consists of an | ||
85 | .Sq E | ||
86 | or | ||
87 | .Sq e , | ||
88 | followed by an optional plus or minus sign, followed by a sequence of digits. | ||
89 | .Pp | ||
90 | Leading whitespace characters in the string (as defined by the | ||
91 | .Xr isspace 3 | ||
92 | function) are skipped. | ||
93 | .Sh RETURN VALUES | ||
94 | The | ||
95 | .Fn strtod , | ||
96 | .Fn strtof | ||
97 | and | ||
98 | .Fn strtold | ||
99 | functions return the converted value, if any. | ||
100 | .Pp | ||
101 | If | ||
102 | .Fa endptr | ||
103 | is not | ||
104 | .Dv NULL , | ||
105 | a pointer to the character after the last character used | ||
106 | in the conversion is stored in the location referenced by | ||
107 | .Fa endptr . | ||
108 | .Pp | ||
109 | If no conversion is performed, zero is returned and the value of | ||
110 | .Fa nptr | ||
111 | is stored in the location referenced by | ||
112 | .Fa endptr . | ||
113 | .Pp | ||
114 | If the correct value would cause overflow, plus or minus | ||
115 | .Dv HUGE_VAL | ||
116 | is returned (according to the sign of the value), and | ||
117 | .Er ERANGE | ||
118 | is stored in | ||
119 | .Va errno . | ||
120 | If the correct value would cause underflow, zero is returned and | ||
121 | .Er ERANGE | ||
122 | is stored in | ||
123 | .Va errno . | ||
124 | .Sh ERRORS | ||
125 | .Bl -tag -width Er | ||
126 | .It Bq Er ERANGE | ||
127 | Overflow or underflow occurred. | ||
128 | .El | ||
129 | .Sh SEE ALSO | ||
130 | .Xr atof 3 , | ||
131 | .Xr atoi 3 , | ||
132 | .Xr atol 3 , | ||
133 | .Xr strtol 3 , | ||
134 | .Xr strtoul 3 | ||
135 | .Sh STANDARDS | ||
136 | The | ||
137 | .Fn strtod | ||
138 | function conforms to | ||
139 | .St -ansiC-89 . | ||
140 | The | ||
141 | .Fn strtof | ||
142 | and | ||
143 | .Fn strtold | ||
144 | functions conform to | ||
145 | .St -ansiC-99 . | ||
diff --git a/src/lib/libc/stdlib/strtoimax.c b/src/lib/libc/stdlib/strtoimax.c new file mode 100644 index 0000000000..2c77f41650 --- /dev/null +++ b/src/lib/libc/stdlib/strtoimax.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* $OpenBSD: strtoimax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1992 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. 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 <ctype.h> | ||
33 | #include <errno.h> | ||
34 | #include <inttypes.h> | ||
35 | |||
36 | /* | ||
37 | * Convert a string to an intmax_t | ||
38 | * | ||
39 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
40 | * alphabets and digits are each contiguous. | ||
41 | */ | ||
42 | intmax_t | ||
43 | strtoimax(const char *nptr, char **endptr, int base) | ||
44 | { | ||
45 | const char *s; | ||
46 | intmax_t acc, cutoff; | ||
47 | int c; | ||
48 | int neg, any, cutlim; | ||
49 | |||
50 | /* | ||
51 | * Skip white space and pick up leading +/- sign if any. | ||
52 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
53 | * assume decimal; if base is already 16, allow 0x. | ||
54 | */ | ||
55 | s = nptr; | ||
56 | do { | ||
57 | c = (unsigned char) *s++; | ||
58 | } while (isspace(c)); | ||
59 | if (c == '-') { | ||
60 | neg = 1; | ||
61 | c = *s++; | ||
62 | } else { | ||
63 | neg = 0; | ||
64 | if (c == '+') | ||
65 | c = *s++; | ||
66 | } | ||
67 | if ((base == 0 || base == 16) && | ||
68 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
69 | c = s[1]; | ||
70 | s += 2; | ||
71 | base = 16; | ||
72 | } | ||
73 | if (base == 0) | ||
74 | base = c == '0' ? 8 : 10; | ||
75 | |||
76 | /* | ||
77 | * Compute the cutoff value between legal numbers and illegal | ||
78 | * numbers. That is the largest legal value, divided by the | ||
79 | * base. An input number that is greater than this value, if | ||
80 | * followed by a legal input character, is too big. One that | ||
81 | * is equal to this value may be valid or not; the limit | ||
82 | * between valid and invalid numbers is then based on the last | ||
83 | * digit. For instance, if the range for intmax_t is | ||
84 | * [-9223372036854775808..9223372036854775807] and the input base | ||
85 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | ||
86 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | ||
87 | * accumulated a value > 922337203685477580, or equal but the | ||
88 | * next digit is > 7 (or 8), the number is too big, and we will | ||
89 | * return a range error. | ||
90 | * | ||
91 | * Set any if any `digits' consumed; make it negative to indicate | ||
92 | * overflow. | ||
93 | */ | ||
94 | cutoff = neg ? INTMAX_MIN : INTMAX_MAX; | ||
95 | cutlim = cutoff % base; | ||
96 | cutoff /= base; | ||
97 | if (neg) { | ||
98 | if (cutlim > 0) { | ||
99 | cutlim -= base; | ||
100 | cutoff += 1; | ||
101 | } | ||
102 | cutlim = -cutlim; | ||
103 | } | ||
104 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
105 | if (isdigit(c)) | ||
106 | c -= '0'; | ||
107 | else if (isalpha(c)) | ||
108 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
109 | else | ||
110 | break; | ||
111 | if (c >= base) | ||
112 | break; | ||
113 | if (any < 0) | ||
114 | continue; | ||
115 | if (neg) { | ||
116 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { | ||
117 | any = -1; | ||
118 | acc = INTMAX_MIN; | ||
119 | errno = ERANGE; | ||
120 | } else { | ||
121 | any = 1; | ||
122 | acc *= base; | ||
123 | acc -= c; | ||
124 | } | ||
125 | } else { | ||
126 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
127 | any = -1; | ||
128 | acc = INTMAX_MAX; | ||
129 | errno = ERANGE; | ||
130 | } else { | ||
131 | any = 1; | ||
132 | acc *= base; | ||
133 | acc += c; | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | if (endptr != 0) | ||
138 | *endptr = (char *) (any ? s - 1 : nptr); | ||
139 | return (acc); | ||
140 | } | ||
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 new file mode 100644 index 0000000000..84d9a0c427 --- /dev/null +++ b/src/lib/libc/stdlib/strtol.3 | |||
@@ -0,0 +1,264 @@ | |||
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.19 2007/11/13 18:30:04 tobias Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: November 13 2007 $ | ||
35 | .Dt STRTOL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtol , | ||
39 | .Nm strtoll , | ||
40 | .Nm strtoimax , | ||
41 | .Nm strtoq , | ||
42 | .Nd "convert string value to a long, long long or intmax_t integer" | ||
43 | .Sh SYNOPSIS | ||
44 | .Fd #include <limits.h> | ||
45 | .Fd #include <stdlib.h> | ||
46 | .Ft long | ||
47 | .Fn strtol "const char *nptr" "char **endptr" "int base" | ||
48 | .Pp | ||
49 | .Ft long long | ||
50 | .Fn strtoll "const char *nptr" "char **endptr" "int base" | ||
51 | .Pp | ||
52 | .Fd #include <inttypes.h> | ||
53 | .Ft intmax_t | ||
54 | .Fn strtoimax "const char *nptr" "char **endptr" "int base" | ||
55 | .Pp | ||
56 | .Fd #include <sys/types.h> | ||
57 | .Fd #include <limits.h> | ||
58 | .Fd #include <stdlib.h> | ||
59 | .Ft quad_t | ||
60 | .Fn strtoq "const char *nptr" "char **endptr" "int base" | ||
61 | .Sh DESCRIPTION | ||
62 | The | ||
63 | .Fn strtol | ||
64 | function converts the string in | ||
65 | .Fa nptr | ||
66 | to a | ||
67 | .Li long | ||
68 | value. | ||
69 | The | ||
70 | .Fn strtoll | ||
71 | function converts the string in | ||
72 | .Fa nptr | ||
73 | to a | ||
74 | .Li long long | ||
75 | value. | ||
76 | The | ||
77 | .Fn strtoimax | ||
78 | function converts the string in | ||
79 | .Fa nptr | ||
80 | to an | ||
81 | .Li intmax_t | ||
82 | value. | ||
83 | The | ||
84 | .Fn strtoq | ||
85 | function is a deprecated equivalent of | ||
86 | .Fn strtoll | ||
87 | and is provided for backwards compatibility with legacy programs. | ||
88 | The conversion is done according to the given | ||
89 | .Fa base , | ||
90 | which must be a number between 2 and 36 inclusive or the special value 0. | ||
91 | .Pp | ||
92 | The string may begin with an arbitrary amount of whitespace | ||
93 | (as determined by | ||
94 | .Xr isspace 3 ) | ||
95 | followed by a single optional | ||
96 | .Ql + | ||
97 | or | ||
98 | .Ql - | ||
99 | sign. | ||
100 | If | ||
101 | .Fa base | ||
102 | is zero or 16, the string may then include a | ||
103 | .Ql 0x | ||
104 | prefix, and the number will be read in base 16; otherwise, a zero | ||
105 | .Fa base | ||
106 | is taken as 10 (decimal) unless the next character is | ||
107 | .Ql 0 , | ||
108 | in which case it is taken as 8 (octal). | ||
109 | .Pp | ||
110 | The remainder of the string is converted to a | ||
111 | .Li long , | ||
112 | .Li long long , | ||
113 | or | ||
114 | .Li intmax_t , | ||
115 | value in the obvious manner, | ||
116 | stopping at the first character which is not a valid digit | ||
117 | in the given base. | ||
118 | (In bases above 10, the letter | ||
119 | .Ql A | ||
120 | in either upper or lower case represents 10, | ||
121 | .Ql B | ||
122 | represents 11, and so forth, with | ||
123 | .Ql Z | ||
124 | representing 35.) | ||
125 | .Pp | ||
126 | If | ||
127 | .Fa endptr | ||
128 | is non-null, | ||
129 | .Fn strtol | ||
130 | stores the address of the first invalid character in | ||
131 | .Fa *endptr . | ||
132 | If there were no digits at all, however, | ||
133 | .Fn strtol | ||
134 | stores the original value of | ||
135 | .Fa nptr | ||
136 | in | ||
137 | .Fa *endptr . | ||
138 | (Thus, if | ||
139 | .Fa *nptr | ||
140 | is not | ||
141 | .Ql \e0 | ||
142 | but | ||
143 | .Fa **endptr | ||
144 | is | ||
145 | .Ql \e0 | ||
146 | on return, the entire string was valid.) | ||
147 | .Sh RETURN VALUES | ||
148 | The | ||
149 | .Fn strtol , | ||
150 | .Fn strtoll , | ||
151 | .Fn strtoimax , | ||
152 | and | ||
153 | .Fn strtoq | ||
154 | functions return the result of the conversion, | ||
155 | unless the value would underflow or overflow. | ||
156 | If no conversion could be performed, 0 is returned; | ||
157 | the global variable | ||
158 | .Va errno | ||
159 | is also set to | ||
160 | .Er EINVAL, | ||
161 | though this is not portable across all platforms. | ||
162 | If overflow or underflow occurs, | ||
163 | .Va errno | ||
164 | is set to | ||
165 | .Er ERANGE | ||
166 | and the function return value is as follows: | ||
167 | .Bl -column -offset indent "strtoimax" "overflow" "underflow" | ||
168 | .It Sy Function Ta Sy underflow Ta Sy overflow | ||
169 | .It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX | ||
170 | .It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX | ||
171 | .It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX | ||
172 | .It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX | ||
173 | .El | ||
174 | .Sh EXAMPLES | ||
175 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
176 | trailing characters) requires clearing | ||
177 | .Va errno | ||
178 | beforehand explicitly since | ||
179 | .Va errno | ||
180 | is not changed on a successful call to | ||
181 | .Fn strtol , | ||
182 | and the return value of | ||
183 | .Fn strtol | ||
184 | cannot be used unambiguously to signal an error: | ||
185 | .Bd -literal -offset indent | ||
186 | char *ep; | ||
187 | long lval; | ||
188 | |||
189 | \&... | ||
190 | |||
191 | errno = 0; | ||
192 | lval = strtol(buf, &ep, 10); | ||
193 | if (buf[0] == '\e0' || *ep != '\e0') | ||
194 | goto not_a_number; | ||
195 | if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) | ||
196 | goto out_of_range; | ||
197 | .Ed | ||
198 | .Pp | ||
199 | This example will accept | ||
200 | .Dq 12 | ||
201 | but not | ||
202 | .Dq 12foo | ||
203 | or | ||
204 | .Dq 12\en . | ||
205 | If trailing whitespace is acceptable, further checks must be done on | ||
206 | .Va *ep ; | ||
207 | alternately, use | ||
208 | .Xr sscanf 3 . | ||
209 | .Pp | ||
210 | If | ||
211 | .Fn strtol | ||
212 | is being used instead of | ||
213 | .Xr atoi 3 , | ||
214 | error checking is further complicated because the desired return value is an | ||
215 | .Li int | ||
216 | rather than a | ||
217 | .Li long ; | ||
218 | however, on some architectures integers and long integers are the same size. | ||
219 | Thus the following is necessary: | ||
220 | .Bd -literal -offset indent | ||
221 | char *ep; | ||
222 | int ival; | ||
223 | long lval; | ||
224 | |||
225 | \&... | ||
226 | |||
227 | errno = 0; | ||
228 | lval = strtol(buf, &ep, 10); | ||
229 | if (buf[0] == '\e0' || *ep != '\e0') | ||
230 | goto not_a_number; | ||
231 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | ||
232 | (lval > INT_MAX || lval < INT_MIN)) | ||
233 | goto out_of_range; | ||
234 | ival = lval; | ||
235 | .Ed | ||
236 | .Sh ERRORS | ||
237 | .Bl -tag -width Er | ||
238 | .It Bq Er ERANGE | ||
239 | The given string was out of range; the value converted has been clamped. | ||
240 | .El | ||
241 | .Sh SEE ALSO | ||
242 | .Xr atof 3 , | ||
243 | .Xr atoi 3 , | ||
244 | .Xr atol 3 , | ||
245 | .Xr atoll 3 , | ||
246 | .Xr sscanf 3 , | ||
247 | .Xr strtod 3 , | ||
248 | .Xr strtonum 3 , | ||
249 | .Xr strtoul 3 | ||
250 | .Sh STANDARDS | ||
251 | The | ||
252 | .Fn strtol , | ||
253 | .Fn strtoll , | ||
254 | and | ||
255 | .Fn strtoimax | ||
256 | functions conform to | ||
257 | .St -ansiC-99 . | ||
258 | The | ||
259 | .Fn strtoq | ||
260 | function is a | ||
261 | .Bx | ||
262 | extension and is provided for backwards compatibility with legacy programs. | ||
263 | .Sh BUGS | ||
264 | 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..5a244766db --- /dev/null +++ b/src/lib/libc/stdlib/strtol.c | |||
@@ -0,0 +1,140 @@ | |||
1 | /* $OpenBSD: strtol.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
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. 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 | #include <ctype.h> | ||
32 | #include <errno.h> | ||
33 | #include <limits.h> | ||
34 | #include <stdlib.h> | ||
35 | |||
36 | |||
37 | /* | ||
38 | * Convert a string to a long integer. | ||
39 | * | ||
40 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
41 | * alphabets and digits are each contiguous. | ||
42 | */ | ||
43 | long | ||
44 | strtol(const char *nptr, char **endptr, int base) | ||
45 | { | ||
46 | const char *s; | ||
47 | long acc, cutoff; | ||
48 | int c; | ||
49 | int neg, any, cutlim; | ||
50 | |||
51 | /* | ||
52 | * Skip white space and pick up leading +/- sign if any. | ||
53 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
54 | * assume decimal; if base is already 16, allow 0x. | ||
55 | */ | ||
56 | s = nptr; | ||
57 | do { | ||
58 | c = (unsigned char) *s++; | ||
59 | } while (isspace(c)); | ||
60 | if (c == '-') { | ||
61 | neg = 1; | ||
62 | c = *s++; | ||
63 | } else { | ||
64 | neg = 0; | ||
65 | if (c == '+') | ||
66 | c = *s++; | ||
67 | } | ||
68 | if ((base == 0 || base == 16) && | ||
69 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
70 | c = s[1]; | ||
71 | s += 2; | ||
72 | base = 16; | ||
73 | } | ||
74 | if (base == 0) | ||
75 | base = c == '0' ? 8 : 10; | ||
76 | |||
77 | /* | ||
78 | * Compute the cutoff value between legal numbers and illegal | ||
79 | * numbers. That is the largest legal value, divided by the | ||
80 | * base. An input number that is greater than this value, if | ||
81 | * followed by a legal input character, is too big. One that | ||
82 | * is equal to this value may be valid or not; the limit | ||
83 | * between valid and invalid numbers is then based on the last | ||
84 | * digit. For instance, if the range for longs is | ||
85 | * [-2147483648..2147483647] and the input base is 10, | ||
86 | * cutoff will be set to 214748364 and cutlim to either | ||
87 | * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated | ||
88 | * a value > 214748364, or equal but the next digit is > 7 (or 8), | ||
89 | * the number is too big, and we will return a range error. | ||
90 | * | ||
91 | * Set any if any `digits' consumed; make it negative to indicate | ||
92 | * overflow. | ||
93 | */ | ||
94 | cutoff = neg ? LONG_MIN : LONG_MAX; | ||
95 | cutlim = cutoff % base; | ||
96 | cutoff /= base; | ||
97 | if (neg) { | ||
98 | if (cutlim > 0) { | ||
99 | cutlim -= base; | ||
100 | cutoff += 1; | ||
101 | } | ||
102 | cutlim = -cutlim; | ||
103 | } | ||
104 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
105 | if (isdigit(c)) | ||
106 | c -= '0'; | ||
107 | else if (isalpha(c)) | ||
108 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
109 | else | ||
110 | break; | ||
111 | if (c >= base) | ||
112 | break; | ||
113 | if (any < 0) | ||
114 | continue; | ||
115 | if (neg) { | ||
116 | if (acc < cutoff || acc == cutoff && c > cutlim) { | ||
117 | any = -1; | ||
118 | acc = LONG_MIN; | ||
119 | errno = ERANGE; | ||
120 | } else { | ||
121 | any = 1; | ||
122 | acc *= base; | ||
123 | acc -= c; | ||
124 | } | ||
125 | } else { | ||
126 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
127 | any = -1; | ||
128 | acc = LONG_MAX; | ||
129 | errno = ERANGE; | ||
130 | } else { | ||
131 | any = 1; | ||
132 | acc *= base; | ||
133 | acc += c; | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | if (endptr != 0) | ||
138 | *endptr = (char *) (any ? s - 1 : nptr); | ||
139 | return (acc); | ||
140 | } | ||
diff --git a/src/lib/libc/stdlib/strtoll.c b/src/lib/libc/stdlib/strtoll.c new file mode 100644 index 0000000000..37379252e3 --- /dev/null +++ b/src/lib/libc/stdlib/strtoll.c | |||
@@ -0,0 +1,153 @@ | |||
1 | /* $OpenBSD: strtoll.c,v 1.6 2005/11/10 10:00:17 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1992 The Regents of the University of California. | ||
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. 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 | #include <sys/types.h> | ||
32 | |||
33 | #include <ctype.h> | ||
34 | #include <errno.h> | ||
35 | #include <limits.h> | ||
36 | #include <stdlib.h> | ||
37 | |||
38 | /* | ||
39 | * Convert a string to a long long. | ||
40 | * | ||
41 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
42 | * alphabets and digits are each contiguous. | ||
43 | */ | ||
44 | long long | ||
45 | strtoll(const char *nptr, char **endptr, int base) | ||
46 | { | ||
47 | const char *s; | ||
48 | long long acc, cutoff; | ||
49 | int c; | ||
50 | int neg, any, cutlim; | ||
51 | |||
52 | /* | ||
53 | * Skip white space and pick up leading +/- sign if any. | ||
54 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
55 | * assume decimal; if base is already 16, allow 0x. | ||
56 | */ | ||
57 | s = nptr; | ||
58 | do { | ||
59 | c = (unsigned char) *s++; | ||
60 | } while (isspace(c)); | ||
61 | if (c == '-') { | ||
62 | neg = 1; | ||
63 | c = *s++; | ||
64 | } else { | ||
65 | neg = 0; | ||
66 | if (c == '+') | ||
67 | c = *s++; | ||
68 | } | ||
69 | if ((base == 0 || base == 16) && | ||
70 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
71 | c = s[1]; | ||
72 | s += 2; | ||
73 | base = 16; | ||
74 | } | ||
75 | if (base == 0) | ||
76 | base = c == '0' ? 8 : 10; | ||
77 | |||
78 | /* | ||
79 | * Compute the cutoff value between legal numbers and illegal | ||
80 | * numbers. That is the largest legal value, divided by the | ||
81 | * base. An input number that is greater than this value, if | ||
82 | * followed by a legal input character, is too big. One that | ||
83 | * is equal to this value may be valid or not; the limit | ||
84 | * between valid and invalid numbers is then based on the last | ||
85 | * digit. For instance, if the range for long longs is | ||
86 | * [-9223372036854775808..9223372036854775807] and the input base | ||
87 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | ||
88 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | ||
89 | * accumulated a value > 922337203685477580, or equal but the | ||
90 | * next digit is > 7 (or 8), the number is too big, and we will | ||
91 | * return a range error. | ||
92 | * | ||
93 | * Set any if any `digits' consumed; make it negative to indicate | ||
94 | * overflow. | ||
95 | */ | ||
96 | cutoff = neg ? LLONG_MIN : LLONG_MAX; | ||
97 | cutlim = cutoff % base; | ||
98 | cutoff /= base; | ||
99 | if (neg) { | ||
100 | if (cutlim > 0) { | ||
101 | cutlim -= base; | ||
102 | cutoff += 1; | ||
103 | } | ||
104 | cutlim = -cutlim; | ||
105 | } | ||
106 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
107 | if (isdigit(c)) | ||
108 | c -= '0'; | ||
109 | else if (isalpha(c)) | ||
110 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
111 | else | ||
112 | break; | ||
113 | if (c >= base) | ||
114 | break; | ||
115 | if (any < 0) | ||
116 | continue; | ||
117 | if (neg) { | ||
118 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { | ||
119 | any = -1; | ||
120 | acc = LLONG_MIN; | ||
121 | errno = ERANGE; | ||
122 | } else { | ||
123 | any = 1; | ||
124 | acc *= base; | ||
125 | acc -= c; | ||
126 | } | ||
127 | } else { | ||
128 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
129 | any = -1; | ||
130 | acc = LLONG_MAX; | ||
131 | errno = ERANGE; | ||
132 | } else { | ||
133 | any = 1; | ||
134 | acc *= base; | ||
135 | acc += c; | ||
136 | } | ||
137 | } | ||
138 | } | ||
139 | if (endptr != 0) | ||
140 | *endptr = (char *) (any ? s - 1 : nptr); | ||
141 | return (acc); | ||
142 | } | ||
143 | |||
144 | #ifdef __weak_alias | ||
145 | __weak_alias(strtoq, strtoll); | ||
146 | #else | ||
147 | quad_t | ||
148 | strtoq(const char *nptr, char **endptr, int base) | ||
149 | { | ||
150 | |||
151 | return ((quad_t)strtoll(nptr, endptr, base)); | ||
152 | } | ||
153 | #endif | ||
diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3 new file mode 100644 index 0000000000..58b4112ac3 --- /dev/null +++ b/src/lib/libc/stdlib/strtonum.3 | |||
@@ -0,0 +1,152 @@ | |||
1 | .\" $OpenBSD: strtonum.3,v 1.14 2007/05/31 19:19:31 jmc Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 2004 Ted Unangst | ||
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 $Mdocdate: May 31 2007 $ | ||
18 | .Dt STRTONUM 3 | ||
19 | .Os | ||
20 | .Sh NAME | ||
21 | .Nm strtonum | ||
22 | .Nd "reliably convert string value to an integer" | ||
23 | .Sh SYNOPSIS | ||
24 | .Fd #include <stdlib.h> | ||
25 | .Ft long long | ||
26 | .Fo strtonum | ||
27 | .Fa "const char *nptr" | ||
28 | .Fa "long long minval" | ||
29 | .Fa "long long maxval" | ||
30 | .Fa "const char **errstr" | ||
31 | .Fc | ||
32 | .Sh DESCRIPTION | ||
33 | The | ||
34 | .Fn strtonum | ||
35 | function converts the string in | ||
36 | .Fa nptr | ||
37 | to a | ||
38 | .Li long long | ||
39 | value. | ||
40 | The | ||
41 | .Fn strtonum | ||
42 | function was designed to facilitate safe, robust programming | ||
43 | and overcome the shortcomings of the | ||
44 | .Xr atoi 3 | ||
45 | and | ||
46 | .Xr strtol 3 | ||
47 | family of interfaces. | ||
48 | .Pp | ||
49 | The string may begin with an arbitrary amount of whitespace | ||
50 | (as determined by | ||
51 | .Xr isspace 3 ) | ||
52 | followed by a single optional | ||
53 | .Ql + | ||
54 | or | ||
55 | .Ql - | ||
56 | sign. | ||
57 | .Pp | ||
58 | The remainder of the string is converted to a | ||
59 | .Li long long | ||
60 | value according to base 10. | ||
61 | .Pp | ||
62 | The value obtained is then checked against the provided | ||
63 | .Fa minval | ||
64 | and | ||
65 | .Fa maxval | ||
66 | bounds. | ||
67 | If | ||
68 | .Fa errstr | ||
69 | is non-null, | ||
70 | .Fn strtonum | ||
71 | stores an error string in | ||
72 | .Fa *errstr | ||
73 | indicating the failure. | ||
74 | .Sh RETURN VALUES | ||
75 | The | ||
76 | .Fn strtonum | ||
77 | function returns the result of the conversion, | ||
78 | unless the value would exceed the provided bounds or is invalid. | ||
79 | On error, 0 is returned, | ||
80 | .Va errno | ||
81 | is set, and | ||
82 | .Fa errstr | ||
83 | will point to an error message. | ||
84 | .Fa *errstr | ||
85 | will be set to | ||
86 | .Dv NULL | ||
87 | on success; | ||
88 | this fact can be used to differentiate | ||
89 | a successful return of 0 from an error. | ||
90 | .Sh EXAMPLES | ||
91 | Using | ||
92 | .Fn strtonum | ||
93 | correctly is meant to be simpler than the alternative functions. | ||
94 | .Bd -literal -offset indent | ||
95 | int iterations; | ||
96 | const char *errstr; | ||
97 | |||
98 | iterations = strtonum(optarg, 1, 64, &errstr); | ||
99 | if (errstr) | ||
100 | errx(1, "number of iterations is %s: %s", errstr, optarg); | ||
101 | .Ed | ||
102 | .Pp | ||
103 | The above example will guarantee that the value of iterations is between | ||
104 | 1 and 64 (inclusive). | ||
105 | .Sh ERRORS | ||
106 | .Bl -tag -width Er | ||
107 | .It Bq Er ERANGE | ||
108 | The given string was out of range. | ||
109 | .It Bq Er EINVAL | ||
110 | The given string did not consist solely of digit characters. | ||
111 | .It Bq Er EINVAL | ||
112 | .Ar minval | ||
113 | was larger than | ||
114 | .Ar maxval . | ||
115 | .El | ||
116 | .Pp | ||
117 | If an error occurs, | ||
118 | .Fa errstr | ||
119 | will be set to one of the following strings: | ||
120 | .Pp | ||
121 | .Bl -tag -width "too largeXX" -compact | ||
122 | .It too large | ||
123 | The result was larger than the provided maximum value. | ||
124 | .It too small | ||
125 | The result was smaller than the provided minimum value. | ||
126 | .It invalid | ||
127 | The string did not consist solely of digit characters. | ||
128 | .El | ||
129 | .Sh SEE ALSO | ||
130 | .Xr atof 3 , | ||
131 | .Xr atoi 3 , | ||
132 | .Xr atol 3 , | ||
133 | .Xr atoll 3 , | ||
134 | .Xr sscanf 3 , | ||
135 | .Xr strtod 3 , | ||
136 | .Xr strtol 3 , | ||
137 | .Xr strtoul 3 | ||
138 | .Sh STANDARDS | ||
139 | .Fn strtonum | ||
140 | is an | ||
141 | .Ox | ||
142 | extension. | ||
143 | The existing alternatives, such as | ||
144 | .Xr atoi 3 | ||
145 | and | ||
146 | .Xr strtol 3 , | ||
147 | are either impossible or difficult to use safely. | ||
148 | .Sh HISTORY | ||
149 | The | ||
150 | .Fn strtonum | ||
151 | function first appeared in | ||
152 | .Ox 3.6 . | ||
diff --git a/src/lib/libc/stdlib/strtonum.c b/src/lib/libc/stdlib/strtonum.c new file mode 100644 index 0000000000..e426388ed0 --- /dev/null +++ b/src/lib/libc/stdlib/strtonum.c | |||
@@ -0,0 +1,65 @@ | |||
1 | /* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 2004 Ted Unangst and Todd Miller | ||
5 | * All rights reserved. | ||
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 | |||
20 | #include <errno.h> | ||
21 | #include <limits.h> | ||
22 | #include <stdlib.h> | ||
23 | |||
24 | #define INVALID 1 | ||
25 | #define TOOSMALL 2 | ||
26 | #define TOOLARGE 3 | ||
27 | |||
28 | long long | ||
29 | strtonum(const char *numstr, long long minval, long long maxval, | ||
30 | const char **errstrp) | ||
31 | { | ||
32 | long long ll = 0; | ||
33 | char *ep; | ||
34 | int error = 0; | ||
35 | struct errval { | ||
36 | const char *errstr; | ||
37 | int err; | ||
38 | } ev[4] = { | ||
39 | { NULL, 0 }, | ||
40 | { "invalid", EINVAL }, | ||
41 | { "too small", ERANGE }, | ||
42 | { "too large", ERANGE }, | ||
43 | }; | ||
44 | |||
45 | ev[0].err = errno; | ||
46 | errno = 0; | ||
47 | if (minval > maxval) | ||
48 | error = INVALID; | ||
49 | else { | ||
50 | ll = strtoll(numstr, &ep, 10); | ||
51 | if (numstr == ep || *ep != '\0') | ||
52 | error = INVALID; | ||
53 | else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) | ||
54 | error = TOOSMALL; | ||
55 | else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) | ||
56 | error = TOOLARGE; | ||
57 | } | ||
58 | if (errstrp != NULL) | ||
59 | *errstrp = ev[error].errstr; | ||
60 | errno = ev[error].err; | ||
61 | if (error) | ||
62 | ll = 0; | ||
63 | |||
64 | return (ll); | ||
65 | } | ||
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 new file mode 100644 index 0000000000..a7c6bbfa18 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.3 | |||
@@ -0,0 +1,248 @@ | |||
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.19 2007/11/13 18:30:04 tobias Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: November 13 2007 $ | ||
35 | .Dt STRTOUL 3 | ||
36 | .Os | ||
37 | .Sh NAME | ||
38 | .Nm strtoul , | ||
39 | .Nm strtoull , | ||
40 | .Nm strtoumax , | ||
41 | .Nm strtouq | ||
42 | .Nd "convert a string to an unsigned long, unsigned long long or uintmax_t integer" | ||
43 | .Sh SYNOPSIS | ||
44 | .Fd #include <limits.h> | ||
45 | .Fd #include <stdlib.h> | ||
46 | .Ft unsigned long | ||
47 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | ||
48 | .Pp | ||
49 | .Ft unsigned long long | ||
50 | .Fn strtoull "const char *nptr" "char **endptr" "int base" | ||
51 | .Pp | ||
52 | .Fd #include <inttypes.h> | ||
53 | .Ft uintmax_t | ||
54 | .Fn strtoumax "const char *nptr" "char **endptr" "int base" | ||
55 | .Pp | ||
56 | .Fd #include <sys/types.h> | ||
57 | .Fd #include <limits.h> | ||
58 | .Fd #include <stdlib.h> | ||
59 | .Ft u_quad_t | ||
60 | .Fn strtouq "const char *nptr" "char **endptr" "int base" | ||
61 | .Sh DESCRIPTION | ||
62 | The | ||
63 | .Fn strtoul | ||
64 | function converts the string in | ||
65 | .Fa nptr | ||
66 | to an | ||
67 | .Li unsigned long | ||
68 | value. | ||
69 | The | ||
70 | .Fn strtoull | ||
71 | function converts the string in | ||
72 | .Fa nptr | ||
73 | to an | ||
74 | .Li unsigned long long | ||
75 | value. | ||
76 | The | ||
77 | .Fn strtoumax | ||
78 | function converts the string in | ||
79 | .Fa nptr | ||
80 | to a | ||
81 | .Li umaxint_t | ||
82 | value. | ||
83 | The | ||
84 | .Fn strtouq | ||
85 | function is a deprecated equivalent of | ||
86 | .Fn strtoull | ||
87 | and is provided for backwards compatibility with legacy programs. | ||
88 | The conversion is done according to the given | ||
89 | .Fa base , | ||
90 | which must be a number between 2 and 36 inclusive | ||
91 | or the special value 0. | ||
92 | If the string in | ||
93 | .Fa nptr | ||
94 | represents a negative number, it will be converted to its unsigned equivalent. | ||
95 | This behavior is consistent with what happens when a signed integer type is | ||
96 | cast to its unsigned counterpart. | ||
97 | .Pp | ||
98 | The string may begin with an arbitrary amount of whitespace | ||
99 | (as determined by | ||
100 | .Xr isspace 3 ) | ||
101 | followed by a single optional | ||
102 | .Ql + | ||
103 | or | ||
104 | .Ql - | ||
105 | sign. | ||
106 | If | ||
107 | .Fa base | ||
108 | is zero or 16, the string may then include a | ||
109 | .Ql 0x | ||
110 | prefix, and the number will be read in base 16; otherwise, a zero | ||
111 | .Fa base | ||
112 | is taken as 10 (decimal) unless the next character is | ||
113 | .Ql 0 , | ||
114 | in which case it is taken as 8 (octal). | ||
115 | .Pp | ||
116 | The remainder of the string is converted to an | ||
117 | .Li unsigned long | ||
118 | value in the obvious manner, stopping at the end of the string | ||
119 | or at the first character that does not produce a valid digit | ||
120 | in the given base. | ||
121 | (In bases above 10, the letter | ||
122 | .Ql A | ||
123 | in either upper or lower case represents 10, | ||
124 | .Ql B | ||
125 | represents 11, and so forth, with | ||
126 | .Ql Z | ||
127 | representing 35.) | ||
128 | .Pp | ||
129 | If | ||
130 | .Fa endptr | ||
131 | is non-null, | ||
132 | .Fn strtoul | ||
133 | stores the address of the first invalid character in | ||
134 | .Fa *endptr . | ||
135 | If there were no digits at all, however, | ||
136 | .Fn strtoul | ||
137 | stores the original value of | ||
138 | .Fa nptr | ||
139 | in | ||
140 | .Fa *endptr . | ||
141 | (Thus, if | ||
142 | .Fa *nptr | ||
143 | is not | ||
144 | .Ql \e0 | ||
145 | but | ||
146 | .Fa **endptr | ||
147 | is | ||
148 | .Ql \e0 | ||
149 | on return, the entire string was valid.) | ||
150 | .Sh RETURN VALUES | ||
151 | The | ||
152 | .Fn strtoul , | ||
153 | .Fn strtoull , | ||
154 | .Fn strtoumax | ||
155 | and | ||
156 | .Fn strtouq | ||
157 | functions return either the result of the conversion or, | ||
158 | if there was a leading minus sign, | ||
159 | the negation of the result of the conversion, | ||
160 | unless the original (non-negated) value would overflow. | ||
161 | If overflow occurs, | ||
162 | .Fn strtoul | ||
163 | returns | ||
164 | .Dv ULONG_MAX , | ||
165 | .Fn strtoull | ||
166 | returns | ||
167 | .Dv ULLONG_MAX , | ||
168 | .Fn strtoumax | ||
169 | returns | ||
170 | .Dv UINTMAX_MAX , | ||
171 | .Fn strtouq | ||
172 | returns | ||
173 | .Dv ULLONG_MAX | ||
174 | and the global variable | ||
175 | .Va errno | ||
176 | is set to | ||
177 | .Er ERANGE . | ||
178 | If no conversion could be performed, 0 is returned; | ||
179 | the global variable | ||
180 | .Va errno | ||
181 | is also set to | ||
182 | .Er EINVAL, | ||
183 | though this is not portable across all platforms. | ||
184 | .Pp | ||
185 | There is no way to determine if | ||
186 | .Fn strtoul | ||
187 | has processed a negative number (and returned an unsigned value) short of | ||
188 | examining the string in | ||
189 | .Fa nptr | ||
190 | directly. | ||
191 | .Sh EXAMPLES | ||
192 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
193 | trailing characters) requires clearing | ||
194 | .Va errno | ||
195 | beforehand explicitly since | ||
196 | .Va errno | ||
197 | is not changed on a successful call to | ||
198 | .Fn strtoul , | ||
199 | and the return value of | ||
200 | .Fn strtoul | ||
201 | cannot be used unambiguously to signal an error: | ||
202 | .Bd -literal -offset indent | ||
203 | char *ep; | ||
204 | unsigned long ulval; | ||
205 | |||
206 | \&... | ||
207 | |||
208 | errno = 0; | ||
209 | ulval = strtoul(buf, &ep, 10); | ||
210 | if (buf[0] == '\e0' || *ep != '\e0') | ||
211 | goto not_a_number; | ||
212 | if (errno == ERANGE && ulval == ULONG_MAX) | ||
213 | goto out_of_range; | ||
214 | .Ed | ||
215 | .Pp | ||
216 | This example will accept | ||
217 | .Dq 12 | ||
218 | but not | ||
219 | .Dq 12foo | ||
220 | or | ||
221 | .Dq 12\en . | ||
222 | If trailing whitespace is acceptable, further checks must be done on | ||
223 | .Va *ep ; | ||
224 | alternately, use | ||
225 | .Xr sscanf 3 . | ||
226 | .Sh ERRORS | ||
227 | .Bl -tag -width Er | ||
228 | .It Bq Er ERANGE | ||
229 | The given string was out of range; the value converted has been clamped. | ||
230 | .El | ||
231 | .Sh SEE ALSO | ||
232 | .Xr sscanf 3 , | ||
233 | .Xr strtol 3 | ||
234 | .Sh STANDARDS | ||
235 | The | ||
236 | .Fn strtoul , | ||
237 | .Fn strtoull , | ||
238 | and | ||
239 | .Fn strtoumax | ||
240 | functions conform to | ||
241 | .St -ansiC-99 . | ||
242 | The | ||
243 | .Fn strtouq | ||
244 | function is a | ||
245 | .Bx | ||
246 | extension and is provided for backwards compatibility with legacy programs. | ||
247 | .Sh BUGS | ||
248 | 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..d7dddab778 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.c | |||
@@ -0,0 +1,102 @@ | |||
1 | /* $OpenBSD: strtoul.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1990 Regents of the University of California. | ||
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. 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 | #include <ctype.h> | ||
32 | #include <errno.h> | ||
33 | #include <limits.h> | ||
34 | #include <stdlib.h> | ||
35 | |||
36 | /* | ||
37 | * Convert a string to an unsigned long integer. | ||
38 | * | ||
39 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
40 | * alphabets and digits are each contiguous. | ||
41 | */ | ||
42 | unsigned long | ||
43 | strtoul(const char *nptr, char **endptr, int base) | ||
44 | { | ||
45 | const char *s; | ||
46 | unsigned long acc, cutoff; | ||
47 | int c; | ||
48 | int neg, any, cutlim; | ||
49 | |||
50 | /* | ||
51 | * See strtol for comments as to the logic used. | ||
52 | */ | ||
53 | s = nptr; | ||
54 | do { | ||
55 | c = (unsigned char) *s++; | ||
56 | } while (isspace(c)); | ||
57 | if (c == '-') { | ||
58 | neg = 1; | ||
59 | c = *s++; | ||
60 | } else { | ||
61 | neg = 0; | ||
62 | if (c == '+') | ||
63 | c = *s++; | ||
64 | } | ||
65 | if ((base == 0 || base == 16) && | ||
66 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
67 | c = s[1]; | ||
68 | s += 2; | ||
69 | base = 16; | ||
70 | } | ||
71 | if (base == 0) | ||
72 | base = c == '0' ? 8 : 10; | ||
73 | |||
74 | cutoff = ULONG_MAX / (unsigned long)base; | ||
75 | cutlim = ULONG_MAX % (unsigned long)base; | ||
76 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
77 | if (isdigit(c)) | ||
78 | c -= '0'; | ||
79 | else if (isalpha(c)) | ||
80 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
81 | else | ||
82 | break; | ||
83 | if (c >= base) | ||
84 | break; | ||
85 | if (any < 0) | ||
86 | continue; | ||
87 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
88 | any = -1; | ||
89 | acc = ULONG_MAX; | ||
90 | errno = ERANGE; | ||
91 | } else { | ||
92 | any = 1; | ||
93 | acc *= (unsigned long)base; | ||
94 | acc += c; | ||
95 | } | ||
96 | } | ||
97 | if (neg && any > 0) | ||
98 | acc = -acc; | ||
99 | if (endptr != 0) | ||
100 | *endptr = (char *) (any ? s - 1 : nptr); | ||
101 | return (acc); | ||
102 | } | ||
diff --git a/src/lib/libc/stdlib/strtoull.c b/src/lib/libc/stdlib/strtoull.c new file mode 100644 index 0000000000..37859776f9 --- /dev/null +++ b/src/lib/libc/stdlib/strtoull.c | |||
@@ -0,0 +1,115 @@ | |||
1 | /* $OpenBSD: strtoull.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1992 The Regents of the University of California. | ||
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. 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 | #include <sys/types.h> | ||
32 | |||
33 | #include <ctype.h> | ||
34 | #include <errno.h> | ||
35 | #include <limits.h> | ||
36 | #include <stdlib.h> | ||
37 | |||
38 | /* | ||
39 | * Convert a string to an unsigned long long. | ||
40 | * | ||
41 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
42 | * alphabets and digits are each contiguous. | ||
43 | */ | ||
44 | unsigned long long | ||
45 | strtoull(const char *nptr, char **endptr, int base) | ||
46 | { | ||
47 | const char *s; | ||
48 | unsigned long long acc, cutoff; | ||
49 | int c; | ||
50 | int neg, any, cutlim; | ||
51 | |||
52 | /* | ||
53 | * See strtoq for comments as to the logic used. | ||
54 | */ | ||
55 | s = nptr; | ||
56 | do { | ||
57 | c = (unsigned char) *s++; | ||
58 | } while (isspace(c)); | ||
59 | if (c == '-') { | ||
60 | neg = 1; | ||
61 | c = *s++; | ||
62 | } else { | ||
63 | neg = 0; | ||
64 | if (c == '+') | ||
65 | c = *s++; | ||
66 | } | ||
67 | if ((base == 0 || base == 16) && | ||
68 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
69 | c = s[1]; | ||
70 | s += 2; | ||
71 | base = 16; | ||
72 | } | ||
73 | if (base == 0) | ||
74 | base = c == '0' ? 8 : 10; | ||
75 | |||
76 | cutoff = ULLONG_MAX / (unsigned long long)base; | ||
77 | cutlim = ULLONG_MAX % (unsigned long long)base; | ||
78 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
79 | if (isdigit(c)) | ||
80 | c -= '0'; | ||
81 | else if (isalpha(c)) | ||
82 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
83 | else | ||
84 | break; | ||
85 | if (c >= base) | ||
86 | break; | ||
87 | if (any < 0) | ||
88 | continue; | ||
89 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
90 | any = -1; | ||
91 | acc = ULLONG_MAX; | ||
92 | errno = ERANGE; | ||
93 | } else { | ||
94 | any = 1; | ||
95 | acc *= (unsigned long long)base; | ||
96 | acc += c; | ||
97 | } | ||
98 | } | ||
99 | if (neg && any > 0) | ||
100 | acc = -acc; | ||
101 | if (endptr != 0) | ||
102 | *endptr = (char *) (any ? s - 1 : nptr); | ||
103 | return (acc); | ||
104 | } | ||
105 | |||
106 | #ifdef __weak_alias | ||
107 | __weak_alias(strtouq, strtoull); | ||
108 | #else | ||
109 | u_quad_t | ||
110 | strtouq(const char *nptr, char **endptr, int base) | ||
111 | { | ||
112 | |||
113 | return ((u_quad_t)strtoull(nptr, endptr, base)); | ||
114 | } | ||
115 | #endif | ||
diff --git a/src/lib/libc/stdlib/strtoumax.c b/src/lib/libc/stdlib/strtoumax.c new file mode 100644 index 0000000000..ce6e2c00f1 --- /dev/null +++ b/src/lib/libc/stdlib/strtoumax.c | |||
@@ -0,0 +1,102 @@ | |||
1 | /* $OpenBSD: strtoumax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ | ||
2 | |||
3 | /*- | ||
4 | * Copyright (c) 1992 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. 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 <ctype.h> | ||
33 | #include <errno.h> | ||
34 | #include <inttypes.h> | ||
35 | |||
36 | /* | ||
37 | * Convert a string to a uintmax_t. | ||
38 | * | ||
39 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
40 | * alphabets and digits are each contiguous. | ||
41 | */ | ||
42 | uintmax_t | ||
43 | strtoumax(const char *nptr, char **endptr, int base) | ||
44 | { | ||
45 | const char *s; | ||
46 | uintmax_t acc, cutoff; | ||
47 | int c; | ||
48 | int neg, any, cutlim; | ||
49 | |||
50 | /* | ||
51 | * See strtoq for comments as to the logic used. | ||
52 | */ | ||
53 | s = nptr; | ||
54 | do { | ||
55 | c = (unsigned char) *s++; | ||
56 | } while (isspace(c)); | ||
57 | if (c == '-') { | ||
58 | neg = 1; | ||
59 | c = *s++; | ||
60 | } else { | ||
61 | neg = 0; | ||
62 | if (c == '+') | ||
63 | c = *s++; | ||
64 | } | ||
65 | if ((base == 0 || base == 16) && | ||
66 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
67 | c = s[1]; | ||
68 | s += 2; | ||
69 | base = 16; | ||
70 | } | ||
71 | if (base == 0) | ||
72 | base = c == '0' ? 8 : 10; | ||
73 | |||
74 | cutoff = UINTMAX_MAX / (uintmax_t)base; | ||
75 | cutlim = UINTMAX_MAX % (uintmax_t)base; | ||
76 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
77 | if (isdigit(c)) | ||
78 | c -= '0'; | ||
79 | else if (isalpha(c)) | ||
80 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
81 | else | ||
82 | break; | ||
83 | if (c >= base) | ||
84 | break; | ||
85 | if (any < 0) | ||
86 | continue; | ||
87 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
88 | any = -1; | ||
89 | acc = UINTMAX_MAX; | ||
90 | errno = ERANGE; | ||
91 | } else { | ||
92 | any = 1; | ||
93 | acc *= (uintmax_t)base; | ||
94 | acc += c; | ||
95 | } | ||
96 | } | ||
97 | if (neg && any > 0) | ||
98 | acc = -acc; | ||
99 | if (endptr != 0) | ||
100 | *endptr = (char *) (any ? s - 1 : nptr); | ||
101 | return (acc); | ||
102 | } | ||
diff --git a/src/lib/libc/stdlib/system.3 b/src/lib/libc/stdlib/system.3 new file mode 100644 index 0000000000..a49e595bc0 --- /dev/null +++ b/src/lib/libc/stdlib/system.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 | .\" 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.11 2008/04/04 19:30:41 kurt Exp $ | ||
33 | .\" | ||
34 | .Dd $Mdocdate: April 4 2008 $ | ||
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 | .Pp | ||
70 | Note that fork handlers established using | ||
71 | .Xr pthread_atfork 3 | ||
72 | are not called when a multithreaded program calls | ||
73 | .Fn system . | ||
74 | .Sh RETURN VALUES | ||
75 | If a child process cannot be created, or the termination status of | ||
76 | the shell cannot be obtained, | ||
77 | .Fn system | ||
78 | returns \-1 and sets | ||
79 | .Va errno | ||
80 | to indicate the error. | ||
81 | If execution of the shell fails, | ||
82 | .Fn system | ||
83 | returns the termination status for a program that terminates with a call of | ||
84 | .Fn exit 127 . | ||
85 | .Sh SEE ALSO | ||
86 | .Xr sh 1 , | ||
87 | .Xr execve 2 , | ||
88 | .Xr waitpid 2 , | ||
89 | .Xr popen 3 | ||
90 | .Sh STANDARDS | ||
91 | The | ||
92 | .Fn system | ||
93 | function conforms to | ||
94 | .St -ansiC | ||
95 | and | ||
96 | .St -p1003.2-92 . | ||
97 | .Sh CAVEATS | ||
98 | Never supply the | ||
99 | .Fn system | ||
100 | function with a command containing any part of an unsanitized user-supplied | ||
101 | string. | ||
102 | Shell meta-characters present will be honored by the | ||
103 | .Xr sh 1 | ||
104 | command interpreter. | ||
diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c new file mode 100644 index 0000000000..14ddcae8d3 --- /dev/null +++ b/src/lib/libc/stdlib/system.c | |||
@@ -0,0 +1,74 @@ | |||
1 | /* $OpenBSD: system.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 The Regents of the University of California. | ||
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. 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 | #include <sys/types.h> | ||
32 | #include <sys/wait.h> | ||
33 | #include <signal.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <unistd.h> | ||
36 | #include <paths.h> | ||
37 | |||
38 | extern char **environ; | ||
39 | |||
40 | int | ||
41 | system(const char *command) | ||
42 | { | ||
43 | pid_t pid; | ||
44 | sig_t intsave, quitsave; | ||
45 | sigset_t mask, omask; | ||
46 | int pstat; | ||
47 | char *argp[] = {"sh", "-c", NULL, NULL}; | ||
48 | |||
49 | if (!command) /* just checking... */ | ||
50 | return(1); | ||
51 | |||
52 | argp[2] = (char *)command; | ||
53 | |||
54 | sigemptyset(&mask); | ||
55 | sigaddset(&mask, SIGCHLD); | ||
56 | sigprocmask(SIG_BLOCK, &mask, &omask); | ||
57 | switch (pid = vfork()) { | ||
58 | case -1: /* error */ | ||
59 | sigprocmask(SIG_SETMASK, &omask, NULL); | ||
60 | return(-1); | ||
61 | case 0: /* child */ | ||
62 | sigprocmask(SIG_SETMASK, &omask, NULL); | ||
63 | execve(_PATH_BSHELL, argp, environ); | ||
64 | _exit(127); | ||
65 | } | ||
66 | |||
67 | intsave = signal(SIGINT, SIG_IGN); | ||
68 | quitsave = signal(SIGQUIT, SIG_IGN); | ||
69 | pid = waitpid(pid, (int *)&pstat, 0); | ||
70 | sigprocmask(SIG_SETMASK, &omask, NULL); | ||
71 | (void)signal(SIGINT, intsave); | ||
72 | (void)signal(SIGQUIT, quitsave); | ||
73 | return (pid == -1 ? -1 : pstat); | ||
74 | } | ||
diff --git a/src/lib/libc/stdlib/tfind.c b/src/lib/libc/stdlib/tfind.c new file mode 100644 index 0000000000..ff6bcd742d --- /dev/null +++ b/src/lib/libc/stdlib/tfind.c | |||
@@ -0,0 +1,41 @@ | |||
1 | /* $OpenBSD: tfind.c,v 1.5 2005/03/30 18:51:49 pat Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Tree search generalized from Knuth (6.2.2) Algorithm T just like | ||
5 | * the AT&T man page says. | ||
6 | * | ||
7 | * The node_t structure is for internal use only, lint doesn't grok it. | ||
8 | * | ||
9 | * Written by reading the System V Interface Definition, not the code. | ||
10 | * | ||
11 | * Totally public domain. | ||
12 | */ | ||
13 | /*LINTLIBRARY*/ | ||
14 | #include <search.h> | ||
15 | |||
16 | typedef struct node_t | ||
17 | { | ||
18 | char *key; | ||
19 | struct node_t *llink, *rlink; | ||
20 | } node; | ||
21 | |||
22 | /* find a node, or return 0 */ | ||
23 | void * | ||
24 | tfind(const void *vkey, void * const *vrootp, | ||
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..0a52822620 --- /dev/null +++ b/src/lib/libc/stdlib/tsearch.3 | |||
@@ -0,0 +1,127 @@ | |||
1 | .\" $OpenBSD: tsearch.3,v 1.16 2007/05/31 19:19:32 jmc 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 $Mdocdate: May 31 2007 $ | ||
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 and a pointer to the new root will be returned. | ||
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 | ||
117 | .Sh STANDARDS | ||
118 | These functions conform to | ||
119 | .St -p1003.1-2004 . | ||
120 | .Sh CAVEATS | ||
121 | The | ||
122 | .St -p1003.1-2004 | ||
123 | standard does not specify what value should be returned when deleting the | ||
124 | root node. | ||
125 | Since implementations vary, the user of the | ||
126 | .Fn tdelete | ||
127 | function should not rely on a specific behaviour. | ||
diff --git a/src/lib/libc/stdlib/tsearch.c b/src/lib/libc/stdlib/tsearch.c new file mode 100644 index 0000000000..667c57731b --- /dev/null +++ b/src/lib/libc/stdlib/tsearch.c | |||
@@ -0,0 +1,121 @@ | |||
1 | /* $OpenBSD: tsearch.c,v 1.6 2006/04/04 11:21:50 moritz Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Tree search generalized from Knuth (6.2.2) Algorithm T just like | ||
5 | * the AT&T man page says. | ||
6 | * | ||
7 | * The node_t structure is for internal use only, lint doesn't grok it. | ||
8 | * | ||
9 | * Written by reading the System V Interface Definition, not the code. | ||
10 | * | ||
11 | * Totally public domain. | ||
12 | */ | ||
13 | /*LINTLIBRARY*/ | ||
14 | |||
15 | #include <search.h> | ||
16 | #include <stdlib.h> | ||
17 | |||
18 | typedef struct node_t { | ||
19 | char *key; | ||
20 | struct node_t *left, *right; | ||
21 | } node; | ||
22 | |||
23 | /* find or insert datum into search tree */ | ||
24 | void * | ||
25 | tsearch(const void *vkey, void **vrootp, | ||
26 | int (*compar)(const void *, const void *)) | ||
27 | { | ||
28 | 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(const void *vkey, void **vrootp, | ||
55 | int (*compar)(const void *, const void *)) | ||
56 | { | ||
57 | node **rootp = (node **)vrootp; | ||
58 | char *key = (char *)vkey; | ||
59 | node *p; | ||
60 | node *q; | ||
61 | node *r; | ||
62 | int cmp; | ||
63 | |||
64 | if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0) | ||
65 | return ((struct node_t *)0); | ||
66 | while ((cmp = (*compar)(key, (*rootp)->key)) != 0) { | ||
67 | p = *rootp; | ||
68 | rootp = (cmp < 0) ? | ||
69 | &(*rootp)->left : /* follow left branch */ | ||
70 | &(*rootp)->right; /* follow right branch */ | ||
71 | if (*rootp == (struct node_t *)0) | ||
72 | return ((void *)0); /* key not found */ | ||
73 | } | ||
74 | r = (*rootp)->right; /* D1: */ | ||
75 | if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */ | ||
76 | q = r; | ||
77 | else if (r != (struct node_t *)0) { /* Right link is null? */ | ||
78 | if (r->left == (struct node_t *)0) { /* D2: Find successor */ | ||
79 | r->left = q; | ||
80 | q = r; | ||
81 | } else { /* D3: Find (struct node_t *)0 link */ | ||
82 | for (q = r->left; q->left != (struct node_t *)0; q = r->left) | ||
83 | r = q; | ||
84 | r->left = q->right; | ||
85 | q->left = (*rootp)->left; | ||
86 | q->right = (*rootp)->right; | ||
87 | } | ||
88 | } | ||
89 | if (p == *rootp) | ||
90 | p = q; | ||
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(node *root, void (*action)(const void *, VISIT, int), int level) | ||
99 | { | ||
100 | if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0) | ||
101 | (*action)(root, leaf, level); | ||
102 | else { | ||
103 | (*action)(root, preorder, level); | ||
104 | if (root->left != (struct node_t *)0) | ||
105 | trecurse(root->left, action, level + 1); | ||
106 | (*action)(root, postorder, level); | ||
107 | if (root->right != (struct node_t *)0) | ||
108 | trecurse(root->right, action, level + 1); | ||
109 | (*action)(root, endorder, level); | ||
110 | } | ||
111 | } | ||
112 | |||
113 | /* Walk the nodes of a tree */ | ||
114 | void | ||
115 | twalk(const void *vroot, void (*action)(const void *, VISIT, int)) | ||
116 | { | ||
117 | node *root = (node *)vroot; | ||
118 | |||
119 | if (root != (node *)0 && action != (void (*)(const void *, VISIT, int))0) | ||
120 | trecurse(root, action, 0); | ||
121 | } | ||