diff options
Diffstat (limited to 'src/lib/libc/stdlib')
108 files changed, 7862 insertions, 2583 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index 782a4ab022..c7ee0a80ec 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc | |||
@@ -1,24 +1,29 @@ | |||
1 | # from: @(#)Makefile.inc 5.6 (Berkeley) 6/4/91 | 1 | # $OpenBSD: Makefile.inc,v 1.35 2006/01/13 17:58:09 millert Exp $ |
2 | # $Id: Makefile.inc,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
3 | 2 | ||
4 | # stdlib sources | 3 | # stdlib sources |
5 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib |
6 | 5 | ||
7 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ | 6 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ |
8 | exit.c getenv.c getopt.c heapsort.c l64a.c malloc.c merge.c \ | 7 | calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ |
9 | multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ | 8 | getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ |
10 | setenv.c strtod.c strtol.c strtoq.c strtoul.c strtouq.c system.c \ | 9 | lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \ |
11 | _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ | 10 | random.c realpath.c setenv.c strtoimax.c strtod.c strtol.c strtoll.c \ |
12 | mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c | 11 | 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 | ||
13 | 14 | ||
14 | .if (${MACHINE_ARCH} == "m68k") | 15 | .if (${MACHINE_ARCH} == "m68k") |
15 | SRCS+= abs.S div.c labs.c ldiv.c | 16 | SRCS+= abs.S div.c labs.c ldiv.c |
17 | LSRCS+= abs.c | ||
16 | .elif (${MACHINE_ARCH} == "i386") | 18 | .elif (${MACHINE_ARCH} == "i386") |
17 | SRCS+= abs.S div.S labs.S ldiv.S | 19 | SRCS+= abs.S div.S labs.S ldiv.S |
20 | LSRCS+= abs.c div.c labs.c ldiv.c | ||
18 | .elif (${MACHINE_ARCH} == "ns32k") | 21 | .elif (${MACHINE_ARCH} == "ns32k") |
19 | SRCS+= abs.S div.c labs.c ldiv.c | 22 | SRCS+= abs.S div.c labs.c ldiv.c |
23 | LSRCS+= abs.c | ||
20 | .elif (${MACHINE_ARCH} == "tahoe") | 24 | .elif (${MACHINE_ARCH} == "tahoe") |
21 | SRCS+= abs.S div.c labs.c ldiv.c | 25 | SRCS+= abs.S div.c labs.c ldiv.c |
26 | LSRCS+= abs.c | ||
22 | .elif (${MACHINE_ARCH} == "vax") | 27 | .elif (${MACHINE_ARCH} == "vax") |
23 | SRCS+= abs.c div.c labs.c ldiv.c | 28 | SRCS+= abs.c div.c labs.c ldiv.c |
24 | .elif (${MACHINE_ARCH} == "alpha") | 29 | .elif (${MACHINE_ARCH} == "alpha") |
@@ -28,18 +33,40 @@ SRCS+= abs.c div.c labs.c ldiv.c | |||
28 | SRCS+= abs.c div.c labs.c ldiv.c | 33 | SRCS+= abs.c div.c labs.c ldiv.c |
29 | .endif | 34 | .endif |
30 | 35 | ||
31 | MAN+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ | 36 | .if (${MACHINE_ARCH} == "vax") || (${MACHINE_ARCH} == "m68k") |
32 | calloc.3 div.3 exit.3 free.3 getenv.3 getopt.3 labs.3 ldiv.3 \ | 37 | SRCS+= insque.S remque.S |
33 | malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ | 38 | .else |
34 | rand.3 random.3 realloc.3 realpath.3 strtod.3 strtol.3 strtoul.3 \ | 39 | SRCS+= insque.c remque.c |
35 | system.3 | 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 | ||
36 | 48 | ||
49 | MLINKS+=exit.3 _Exit.3 | ||
50 | MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 | ||
37 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.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 | ||
38 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 | 59 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 |
39 | MLINKS+=rand.3 srand.3 | 60 | MLINKS+=radixsort.3 sradixsort.3 |
40 | MLINKS+=strtol.3 strtoq.3 | 61 | MLINKS+=rand.3 srand.3 rand.3 rand_r.3 |
41 | MLINKS+=strtoul.3 strtouq.3 | 62 | MLINKS+=random.3 initstate.3 random.3 setstate.3 |
42 | MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 | 63 | MLINKS+=random.3 srandom.3 random.3 srandomdev.3 |
43 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 | 64 | MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 |
44 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 | 65 | MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 |
45 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 | 66 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 |
67 | MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3 | ||
68 | MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3 | ||
69 | MLINKS+=tsearch.3 tfind.3 | ||
70 | MLINKS+=tsearch.3 tdelete.3 | ||
71 | MLINKS+=tsearch.3 twalk.3 | ||
72 | 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 index 83ade4645a..7c950f7cee 100644 --- a/src/lib/libc/stdlib/_rand48.c +++ b/src/lib/libc/stdlib/_rand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: _rand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/a64l.3 b/src/lib/libc/stdlib/a64l.3 new file mode 100644 index 0000000000..f2957a7ad4 --- /dev/null +++ b/src/lib/libc/stdlib/a64l.3 | |||
@@ -0,0 +1,133 @@ | |||
1 | .\" $OpenBSD: a64l.3,v 1.9 2005/02/25 03:12:44 cloder Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | .\" | ||
17 | .Dd August 17, 1997 | ||
18 | .Dt A64L 3 | ||
19 | .Os | ||
20 | .Sh NAME | ||
21 | .Nm a64l , | ||
22 | .Nm l64a | ||
23 | .Nd convert between 32-bit integer and radix-64 ASCII string | ||
24 | .Sh SYNOPSIS | ||
25 | .Fd #include <stdlib.h> | ||
26 | .Ft long | ||
27 | .Fn a64l "const char *s" | ||
28 | .Ft char * | ||
29 | .Fn l64a "long l" | ||
30 | .Sh DESCRIPTION | ||
31 | The | ||
32 | .Fn a64l | ||
33 | and | ||
34 | .Fn l64a | ||
35 | functions are used to maintain numbers stored in radix-64 | ||
36 | .Tn ASCII | ||
37 | characters. | ||
38 | This is a notation by which 32-bit integers | ||
39 | can be represented by up to six characters; each character represents a | ||
40 | .Dq digit | ||
41 | in a radix-64 notation. | ||
42 | .Pp | ||
43 | The characters used to represent digits are | ||
44 | .Ql \&. | ||
45 | for 0, | ||
46 | .Ql / | ||
47 | for 1, | ||
48 | .Ql 0 | ||
49 | through | ||
50 | .Ql 9 | ||
51 | for 2-11, | ||
52 | .Ql A | ||
53 | through | ||
54 | .Ql Z | ||
55 | for 12-37, and | ||
56 | .Ql a | ||
57 | through | ||
58 | .Ql z | ||
59 | for 38-63. | ||
60 | .Pp | ||
61 | The | ||
62 | .Fn a64l | ||
63 | function takes a pointer to a 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 index 03fc77e034..5312929c6f 100644 --- a/src/lib/libc/stdlib/a64l.c +++ b/src/lib/libc/stdlib/a64l.c | |||
@@ -1,34 +1,42 @@ | |||
1 | /* $OpenBSD: a64l.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | 3 | * Written by J.T. Conklin <jtc@netbsd.org>. |
3 | * Public domain. | 4 | * Public domain. |
4 | */ | 5 | */ |
5 | 6 | ||
6 | #if defined(LIBC_SCCS) && !defined(lint) | 7 | #include <errno.h> |
7 | static char *rcsid = "$NetBSD: a64l.c,v 1.3 1995/05/11 23:04:47 jtc Exp $"; | 8 | #include <stdlib.h> |
8 | #endif | ||
9 | 9 | ||
10 | long | 10 | long |
11 | a64l(s) | 11 | a64l(const char *s) |
12 | const char *s; | ||
13 | { | 12 | { |
14 | long value, digit, shift; | 13 | long value, digit, shift; |
15 | int i; | 14 | int i; |
16 | 15 | ||
16 | if (s == NULL) { | ||
17 | errno = EINVAL; | ||
18 | return(-1L); | ||
19 | } | ||
20 | |||
17 | value = 0; | 21 | value = 0; |
18 | shift = 0; | 22 | shift = 0; |
19 | for (i = 0; *s && i < 6; i++, s++) { | 23 | for (i = 0; *s && i < 6; i++, s++) { |
20 | if (*s <= '/') | 24 | if (*s >= '.' && *s <= '/') |
21 | digit = *s - '.'; | 25 | digit = *s - '.'; |
22 | else if (*s <= '9') | 26 | else if (*s >= '0' && *s <= '9') |
23 | digit = *s - '0' + 2; | 27 | digit = *s - '0' + 2; |
24 | else if (*s <= 'Z') | 28 | else if (*s >= 'A' && *s <= 'Z') |
25 | digit = *s - 'A' + 12; | 29 | digit = *s - 'A' + 12; |
26 | else | 30 | else if (*s >= 'a' && *s <= 'z') |
27 | digit = *s - 'a' + 38; | 31 | digit = *s - 'a' + 38; |
32 | else { | ||
33 | errno = EINVAL; | ||
34 | return(-1L); | ||
35 | } | ||
28 | 36 | ||
29 | value |= digit << shift; | 37 | value |= digit << shift; |
30 | shift += 6; | 38 | shift += 6; |
31 | } | 39 | } |
32 | 40 | ||
33 | return (long) value; | 41 | return(value); |
34 | } | 42 | } |
diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3 index ab57327585..d411b859eb 100644 --- a/src/lib/libc/stdlib/abort.3 +++ b/src/lib/libc/stdlib/abort.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)abort.3 6.7 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: abort.3,v 1.7 2003/06/02 20:18:37 millert Exp $ |
37 | .\" $Id: abort.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt ABORT 3 | 35 | .Dt ABORT 3 |
@@ -49,23 +44,20 @@ | |||
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn abort | 46 | .Fn abort |
52 | function causes abnormal program termination to occur, unless the | 47 | function causes abnormal program termination to occur, unless the signal |
53 | signal | ||
54 | .Dv SIGABRT | 48 | .Dv SIGABRT |
55 | is being caught and the signal handler does not return. | 49 | is being caught and the signal handler does not return. |
56 | .Pp | 50 | .Pp |
57 | No open streams are closed or flushed. | 51 | Any open streams are flushed and closed. |
58 | .Sh RETURN VALUES | 52 | .Sh RETURN VALUES |
59 | The | 53 | The |
60 | .Nm abort | 54 | .Fn abort |
61 | function | 55 | function never returns. |
62 | never returns. | ||
63 | .Sh SEE ALSO | 56 | .Sh SEE ALSO |
64 | .Xr sigaction 2 , | 57 | .Xr sigaction 2 , |
65 | .Xr exit 2 | 58 | .Xr exit 3 |
66 | .Sh STANDARDS | 59 | .Sh STANDARDS |
67 | The | 60 | The |
68 | .Fn abort | 61 | .Fn abort |
69 | function | 62 | function conforms to |
70 | conforms to | 63 | .St -p1003.1-90 . |
71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c index c298e016b4..072a9fa8c1 100644 --- a/src/lib/libc/stdlib/abort.c +++ b/src/lib/libc/stdlib/abort.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: abort.c,v 1.14 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1985 Regents of the University of California. | 3 | * Copyright (c) 1985 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,27 +28,40 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)abort.c 5.11 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: abort.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <signal.h> | 31 | #include <signal.h> |
40 | #include <stdlib.h> | 32 | #include <stdlib.h> |
41 | #include <unistd.h> | 33 | #include <unistd.h> |
34 | #include "thread_private.h" | ||
35 | #include "atexit.h" | ||
42 | 36 | ||
43 | void | 37 | void |
44 | abort() | 38 | abort(void) |
45 | { | 39 | { |
40 | struct atexit *p = __atexit; | ||
41 | static int cleanup_called = 0; | ||
46 | sigset_t mask; | 42 | sigset_t mask; |
47 | 43 | ||
44 | |||
48 | sigfillset(&mask); | 45 | sigfillset(&mask); |
49 | /* | 46 | /* |
50 | * don't block SIGABRT to give any handler a chance; we ignore | 47 | * don't block SIGABRT to give any handler a chance; we ignore |
51 | * any errors -- X311J doesn't allow abort to return anyway. | 48 | * any errors -- X311J doesn't allow abort to return anyway. |
52 | */ | 49 | */ |
53 | sigdelset(&mask, SIGABRT); | 50 | sigdelset(&mask, SIGABRT); |
54 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | 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 | while (p != NULL && p->next != NULL) | ||
58 | p = p->next; | ||
59 | if (p != NULL && p->fns[0] != NULL) { | ||
60 | cleanup_called = 1; | ||
61 | (*p->fns[0])(); | ||
62 | } | ||
63 | } | ||
64 | |||
55 | (void)kill(getpid(), SIGABRT); | 65 | (void)kill(getpid(), SIGABRT); |
56 | 66 | ||
57 | /* | 67 | /* |
@@ -59,7 +69,7 @@ abort() | |||
59 | * it again, only harder. | 69 | * it again, only harder. |
60 | */ | 70 | */ |
61 | (void)signal(SIGABRT, SIG_DFL); | 71 | (void)signal(SIGABRT, SIG_DFL); |
62 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | 72 | (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); |
63 | (void)kill(getpid(), SIGABRT); | 73 | (void)kill(getpid(), SIGABRT); |
64 | exit(1); | 74 | _exit(1); |
65 | } | 75 | } |
diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3 index 4748d89e77..fc0b20b7d5 100644 --- a/src/lib/libc/stdlib/abs.3 +++ b/src/lib/libc/stdlib/abs.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)abs.3 6.4 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: abs.3,v 1.7 2006/01/13 17:58:09 millert Exp $ |
37 | .\" $Id: abs.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt ABS 3 | 35 | .Dt ABS 3 |
@@ -49,21 +44,18 @@ | |||
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn abs | 46 | .Fn abs |
52 | function | 47 | function computes the absolute value of the integer |
53 | computes | 48 | .Fa j . |
54 | the absolute value of the integer | ||
55 | .Ar j . | ||
56 | .Sh RETURN VALUES | 49 | .Sh RETURN VALUES |
57 | The | 50 | The |
58 | .Fn abs | 51 | .Fn abs |
59 | function | 52 | function returns the absolute value. |
60 | returns | ||
61 | the absolute value. | ||
62 | .Sh SEE ALSO | 53 | .Sh SEE ALSO |
63 | .Xr floor 3 , | ||
64 | .Xr labs 3 , | ||
65 | .Xr cabs 3 , | 54 | .Xr cabs 3 , |
55 | .Xr floor 3 , | ||
66 | .Xr hypot 3 , | 56 | .Xr hypot 3 , |
57 | .Xr imaxabs 3 , | ||
58 | .Xr labs 3 , | ||
67 | .Xr math 3 | 59 | .Xr math 3 |
68 | .Sh STANDARDS | 60 | .Sh STANDARDS |
69 | The | 61 | The |
diff --git a/src/lib/libc/stdlib/abs.c b/src/lib/libc/stdlib/abs.c index 64468e0224..5d2fbae69f 100644 --- a/src/lib/libc/stdlib/abs.c +++ b/src/lib/libc/stdlib/abs.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: abs.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,16 +28,10 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)abs.c 5.2 (Berkeley) 5/17/90";*/ | ||
36 | static char *rcsid = "$Id: abs.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | 32 | ||
41 | int | 33 | int |
42 | abs(j) | 34 | abs(int j) |
43 | int j; | ||
44 | { | 35 | { |
45 | return(j < 0 ? -j : j); | 36 | return(j < 0 ? -j : j); |
46 | } | 37 | } |
diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3 index dcb97ab11c..4136dc405c 100644 --- a/src/lib/libc/stdlib/alloca.3 +++ b/src/lib/libc/stdlib/alloca.3 | |||
@@ -9,11 +9,7 @@ | |||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer in the | 10 | .\" notice, this list of conditions and the following disclaimer in the |
11 | .\" documentation and/or other materials provided with the distribution. | 11 | .\" documentation and/or other materials provided with the distribution. |
12 | .\" 3. All advertising materials mentioning features or use of this software | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
13 | .\" must display the following acknowledgement: | ||
14 | .\" This product includes software developed by the University of | ||
15 | .\" California, Berkeley and its contributors. | ||
16 | .\" 4. 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 | 13 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | 14 | .\" without specific prior written permission. |
19 | .\" | 15 | .\" |
@@ -29,12 +25,11 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
31 | .\" | 27 | .\" |
32 | .\" from: @(#)alloca.3 5.1 (Berkeley) 5/2/91 | 28 | .\" $OpenBSD: alloca.3,v 1.10 2003/06/02 20:18:37 millert Exp $ |
33 | .\" $Id: alloca.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
34 | .\" | 29 | .\" |
35 | .Dd May 2, 1991 | 30 | .Dd May 2, 1991 |
36 | .Dt ALLOCA 3 | 31 | .Dt ALLOCA 3 |
37 | .Os BSD 4 | 32 | .Os |
38 | .Sh NAME | 33 | .Sh NAME |
39 | .Nm alloca | 34 | .Nm alloca |
40 | .Nd memory allocator | 35 | .Nd memory allocator |
@@ -45,35 +40,41 @@ | |||
45 | .Sh DESCRIPTION | 40 | .Sh DESCRIPTION |
46 | The | 41 | The |
47 | .Fn alloca | 42 | .Fn alloca |
48 | function | 43 | function allocates |
49 | allocates | ||
50 | .Fa size | 44 | .Fa size |
51 | bytes of space in the stack frame of the caller. | 45 | bytes of space in the stack frame of the caller. |
52 | This temporary space is automatically freed on | 46 | This temporary space is automatically freed on return. |
53 | return. | ||
54 | .Sh RETURN VALUES | 47 | .Sh RETURN VALUES |
55 | The | 48 | The |
56 | .Fn alloca | 49 | .Fn alloca |
57 | function returns a pointer to the beginning of the allocated space. | 50 | function returns a pointer to the beginning of the allocated space. |
58 | If the allocation failed, a | ||
59 | .Dv NULL | ||
60 | pointer is returned. | ||
61 | .Sh SEE ALSO | 51 | .Sh SEE ALSO |
52 | .Xr pagesize 1 , | ||
62 | .Xr brk 2 , | 53 | .Xr brk 2 , |
63 | .Xr pagesize 2 | ||
64 | .Xr calloc 3 , | 54 | .Xr calloc 3 , |
65 | .Xr malloc 3 , | 55 | .Xr malloc 3 , |
66 | .Xr realloc 3 , | 56 | .Xr realloc 3 |
67 | .Sh BUGS | 57 | .Sh BUGS |
68 | The | 58 | The |
69 | .Fn alloca | 59 | .Fn alloca |
70 | function | 60 | function is machine dependent; its use is discouraged. |
71 | is machine dependent; its use is discouraged. | ||
72 | .\" .Sh HISTORY | 61 | .\" .Sh HISTORY |
73 | .\" The | 62 | .\" The |
74 | .\" .Fn alloca | 63 | .\" .Fn alloca |
75 | .\" function appeared in | 64 | .\" function appeared in |
76 | .\" .Bx ?? . | 65 | .\" .Bx ?? . |
77 | .\" The function appeared in 32v, pwb and pwb.2 and in 3bsd 4bsd | 66 | .\" The function appeared in 32v, pwb and pwb.2 and in 3bsd 4bsd |
78 | .\" The first man page (or link to a man page that I can find at the | 67 | .\" The first man page (or link to a man page that I can find at the |
79 | .\" moment is 4.3... | 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 index 07de054d3c..4272c8b66d 100644 --- a/src/lib/libc/stdlib/atexit.3 +++ b/src/lib/libc/stdlib/atexit.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)atexit.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: atexit.3,v 1.7 2005/08/22 14:17:31 jmc Exp $ |
37 | .\" $Id: atexit.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt ATEXIT 3 | 35 | .Dt ATEXIT 3 |
@@ -49,17 +44,22 @@ | |||
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn atexit | 46 | .Fn atexit |
52 | function | 47 | function registers the given |
53 | registers the given | 48 | .Fa function |
54 | .Ar function | ||
55 | to be called at program exit, whether via | 49 | to be called at program exit, whether via |
56 | .Xr exit 3 | 50 | .Xr exit 3 |
57 | or via return from the program's | 51 | or via return from the program's |
58 | .Em main . | 52 | .Fn main . |
59 | Functions so registered are called in reverse order; | 53 | Functions so registered are called in reverse order; |
60 | no arguments are passed. | 54 | no arguments are passed. |
61 | At least 32 functions can always be registered, | 55 | At least 32 functions can always be registered, |
62 | and more are allowed as long as sufficient memory can be allocated. | 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 | 63 | .Sh RETURN VALUES |
64 | .Rv -std atexit | 64 | .Rv -std atexit |
65 | .Sh ERRORS | 65 | .Sh ERRORS |
@@ -73,6 +73,5 @@ The existing list of functions is unmodified. | |||
73 | .Sh STANDARDS | 73 | .Sh STANDARDS |
74 | The | 74 | The |
75 | .Fn atexit | 75 | .Fn atexit |
76 | function | 76 | function conforms to |
77 | conforms to | ||
78 | .St -ansiC . | 77 | .St -ansiC . |
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index 4da1eb0d9c..50f8ec9372 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c | |||
@@ -1,68 +1,133 @@ | |||
1 | /*- | 1 | /* $OpenBSD: atexit.c,v 1.12 2006/02/22 07:16:32 otto Exp $ */ |
2 | * Copyright (c) 1990 The Regents of the University of California. | 2 | /* |
3 | * Copyright (c) 2002 Daniel Hartmeier | ||
3 | * All rights reserved. | 4 | * All rights reserved. |
4 | * | 5 | * |
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Chris Torek. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | 7 | * modification, are permitted provided that the following conditions |
10 | * are met: | 8 | * are met: |
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. All advertising materials mentioning features or use of this software | ||
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | ||
22 | * without specific prior written permission. | ||
23 | * | 9 | * |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 10 | * - Redistributions of source code must retain the above copyright |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 11 | * notice, this list of conditions and the following disclaimer. |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 12 | * - Redistributions in binary form must reproduce the above |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 13 | * copyright notice, this list of conditions and the following |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 14 | * disclaimer in the documentation and/or other materials provided |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 15 | * with the distribution. |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 16 | * |
31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
34 | * SUCH DAMAGE. | 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 | * | ||
35 | */ | 30 | */ |
36 | 31 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | 32 | #include <sys/types.h> |
38 | /*static char *sccsid = "from: @(#)atexit.c 5.2 (Berkeley) 11/14/90";*/ | 33 | #include <sys/mman.h> |
39 | static char *rcsid = "$Id: atexit.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | #include <stdlib.h> | 34 | #include <stdlib.h> |
35 | #include <unistd.h> | ||
43 | #include "atexit.h" | 36 | #include "atexit.h" |
37 | #include "thread_private.h" | ||
44 | 38 | ||
39 | int __atexit_invalid = 1; | ||
45 | struct atexit *__atexit; | 40 | struct atexit *__atexit; |
46 | 41 | ||
47 | /* | 42 | /* |
43 | * Function pointers are stored in a linked list of pages. The list | ||
44 | * is initially empty, and pages are allocated on demand. The first | ||
45 | * function pointer in the first allocated page (the last one in | ||
46 | * the linked list) is reserved for the cleanup function. | ||
47 | * | ||
48 | * Outside the following two functions, all pages are mprotect()'ed | ||
49 | * to prevent unintentional/malicious corruption. | ||
50 | */ | ||
51 | |||
52 | /* | ||
48 | * Register a function to be performed at exit. | 53 | * Register a function to be performed at exit. |
49 | */ | 54 | */ |
50 | int | 55 | int |
51 | atexit(fn) | 56 | atexit(void (*fn)(void)) |
52 | void (*fn)(); | ||
53 | { | 57 | { |
54 | static struct atexit __atexit0; /* one guaranteed table */ | 58 | struct atexit *p; |
55 | register struct atexit *p; | 59 | int pgsize = getpagesize(); |
60 | int ret = -1; | ||
56 | 61 | ||
57 | if ((p = __atexit) == NULL) | 62 | if (pgsize < sizeof(*p)) |
58 | __atexit = p = &__atexit0; | 63 | return (-1); |
59 | else if (p->ind >= ATEXIT_SIZE) { | 64 | _ATEXIT_LOCK(); |
60 | if ((p = malloc(sizeof(*p))) == NULL) | 65 | p = __atexit; |
61 | return (-1); | 66 | if (p != NULL) { |
62 | p->ind = 0; | 67 | if (p->ind + 1 >= p->max) |
68 | p = NULL; | ||
69 | else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
70 | goto unlock; | ||
71 | } | ||
72 | if (p == NULL) { | ||
73 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
74 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
75 | if (p == MAP_FAILED) | ||
76 | goto unlock; | ||
77 | if (__atexit == NULL) { | ||
78 | p->fns[0] = NULL; | ||
79 | p->ind = 1; | ||
80 | } else | ||
81 | p->ind = 0; | ||
82 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
83 | sizeof(p->fns[0]); | ||
63 | p->next = __atexit; | 84 | p->next = __atexit; |
64 | __atexit = p; | 85 | __atexit = p; |
86 | if (__atexit_invalid) | ||
87 | __atexit_invalid = 0; | ||
65 | } | 88 | } |
66 | p->fns[p->ind++] = fn; | 89 | p->fns[p->ind++] = fn; |
67 | return (0); | 90 | if (mprotect(p, pgsize, PROT_READ)) |
91 | goto unlock; | ||
92 | ret = 0; | ||
93 | unlock: | ||
94 | _ATEXIT_UNLOCK(); | ||
95 | return (ret); | ||
96 | } | ||
97 | |||
98 | /* | ||
99 | * Register the cleanup function | ||
100 | */ | ||
101 | void | ||
102 | __atexit_register_cleanup(void (*fn)(void)) | ||
103 | { | ||
104 | struct atexit *p; | ||
105 | int pgsize = getpagesize(); | ||
106 | |||
107 | if (pgsize < sizeof(*p)) | ||
108 | return; | ||
109 | _ATEXIT_LOCK(); | ||
110 | p = __atexit; | ||
111 | while (p != NULL && p->next != NULL) | ||
112 | p = p->next; | ||
113 | if (p == NULL) { | ||
114 | p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, | ||
115 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
116 | if (p == MAP_FAILED) | ||
117 | goto unlock; | ||
118 | p->ind = 1; | ||
119 | p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / | ||
120 | sizeof(p->fns[0]); | ||
121 | p->next = NULL; | ||
122 | __atexit = p; | ||
123 | if (__atexit_invalid) | ||
124 | __atexit_invalid = 0; | ||
125 | } else { | ||
126 | if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) | ||
127 | goto unlock; | ||
128 | } | ||
129 | p->fns[0] = fn; | ||
130 | mprotect(p, pgsize, PROT_READ); | ||
131 | unlock: | ||
132 | _ATEXIT_UNLOCK(); | ||
68 | } | 133 | } |
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h index 8b756e8fe2..21b0c2e532 100644 --- a/src/lib/libc/stdlib/atexit.h +++ b/src/lib/libc/stdlib/atexit.h | |||
@@ -1,46 +1,41 @@ | |||
1 | /*- | 1 | /* $OpenBSD: atexit.h,v 1.6 2003/07/31 07:08:42 deraadt Exp $ */ |
2 | * Copyright (c) 1990 The Regents of the University of California. | 2 | |
3 | /* | ||
4 | * Copyright (c) 2002 Daniel Hartmeier | ||
3 | * All rights reserved. | 5 | * All rights reserved. |
4 | * | 6 | * |
5 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | 8 | * modification, are permitted provided that the following conditions |
7 | * are met: | 9 | * 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. All advertising materials mentioning features or use of this software | ||
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | * | 10 | * |
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 11 | * - Redistributions of source code must retain the above copyright |
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 12 | * notice, this list of conditions and the following disclaimer. |
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 13 | * - Redistributions in binary form must reproduce the above |
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 14 | * copyright notice, this list of conditions and the following |
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 15 | * disclaimer in the documentation and/or other materials provided |
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 16 | * with the distribution. |
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 17 | * |
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
31 | * SUCH DAMAGE. | 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. | ||
32 | * | 30 | * |
33 | * from: @(#)atexit.h 5.1 (Berkeley) 5/15/90 | ||
34 | * $Id: atexit.h,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
35 | */ | 31 | */ |
36 | 32 | ||
37 | /* must be at least 32 to guarantee ANSI conformance */ | ||
38 | #define ATEXIT_SIZE 32 | ||
39 | |||
40 | struct atexit { | 33 | struct atexit { |
41 | struct atexit *next; /* next in list */ | 34 | struct atexit *next; /* next in list */ |
42 | int ind; /* next index in this table */ | 35 | int ind; /* next index in this table */ |
43 | void (*fns[ATEXIT_SIZE])(); /* the table itself */ | 36 | int max; /* max entries >= ATEXIT_SIZE */ |
37 | void (*fns[1])(void); /* the table itself */ | ||
44 | }; | 38 | }; |
45 | 39 | ||
40 | extern int __atexit_invalid; | ||
46 | extern struct atexit *__atexit; /* points to head of LIFO stack */ | 41 | extern struct atexit *__atexit; /* points to head of LIFO stack */ |
diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3 index 53e04f71c5..c60b9e9fd7 100644 --- a/src/lib/libc/stdlib/atof.3 +++ b/src/lib/libc/stdlib/atof.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)atof.3 6.4 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: atof.3,v 1.4 2003/06/02 20:18:37 millert Exp $ |
37 | .\" $Id: atof.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt ATOF 3 | 35 | .Dt ATOF 3 |
@@ -52,9 +47,9 @@ string to double | |||
52 | The | 47 | The |
53 | .Fn atof | 48 | .Fn atof |
54 | function converts the initial portion of the string pointed to by | 49 | function converts the initial portion of the string pointed to by |
55 | .Ar nptr | 50 | .Fa nptr |
56 | to | 51 | to |
57 | .Ar double | 52 | .Li double |
58 | representation. | 53 | representation. |
59 | .Pp | 54 | .Pp |
60 | It is equivalent to: | 55 | It is equivalent to: |
diff --git a/src/lib/libc/stdlib/atof.c b/src/lib/libc/stdlib/atof.c index 9202de50bb..d14b58b070 100644 --- a/src/lib/libc/stdlib/atof.c +++ b/src/lib/libc/stdlib/atof.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: atof.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1988 The Regents of the University of California. | 3 | * Copyright (c) 1988 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,16 +28,10 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)atof.c 5.3 (Berkeley) 1/8/93";*/ | ||
36 | static char *rcsid = "$Id: atof.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | 32 | ||
41 | double | 33 | double |
42 | atof(ascii) | 34 | atof(const char *ascii) |
43 | const char *ascii; | ||
44 | { | 35 | { |
45 | return(strtod(ascii, (char **)NULL)); | 36 | return(strtod(ascii, (char **)NULL)); |
46 | } | 37 | } |
diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3 index 219ba73c00..dee8b637a3 100644 --- a/src/lib/libc/stdlib/atoi.3 +++ b/src/lib/libc/stdlib/atoi.3 | |||
@@ -1,5 +1,5 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | 1 | .\" Copyright (c) 1990, 1991, 1993 |
2 | .\" All rights reserved. | 2 | .\" The Regents of the University of California. All rights reserved. |
3 | .\" | 3 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | 4 | .\" This code is derived from software contributed to Berkeley by |
5 | .\" the American National Standards Committee X3, on Information | 5 | .\" the American National Standards Committee X3, on Information |
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,10 +29,9 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)atoi.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: atoi.3,v 1.8 2004/08/22 21:47:41 jaredy Exp $ |
37 | .\" $Id: atoi.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 4, 1993 |
40 | .Dt ATOI 3 | 35 | .Dt ATOI 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
@@ -52,13 +47,12 @@ string to integer | |||
52 | The | 47 | The |
53 | .Fn atoi | 48 | .Fn atoi |
54 | function converts the initial portion of the string pointed to by | 49 | function converts the initial portion of the string pointed to by |
55 | .Em nptr | 50 | .Fa nptr |
56 | to | 51 | to |
57 | .Em integer | 52 | .Li integer |
58 | representation. | 53 | representation. |
59 | .Pp | 54 | .Pp |
60 | It is equivalent to: | 55 | It is equivalent to: |
61 | .Pp | ||
62 | .Bd -literal -offset indent | 56 | .Bd -literal -offset indent |
63 | (int)strtol(nptr, (char **)NULL, 10); | 57 | (int)strtol(nptr, (char **)NULL, 10); |
64 | .Ed | 58 | .Ed |
@@ -67,9 +61,26 @@ It is equivalent to: | |||
67 | .Xr atol 3 , | 61 | .Xr atol 3 , |
68 | .Xr strtod 3 , | 62 | .Xr strtod 3 , |
69 | .Xr strtol 3 , | 63 | .Xr strtol 3 , |
64 | .Xr strtonum 3 , | ||
70 | .Xr strtoul 3 | 65 | .Xr strtoul 3 |
71 | .Sh STANDARDS | 66 | .Sh STANDARDS |
72 | The | 67 | The |
73 | .Fn atoi | 68 | .Fn atoi |
74 | function conforms to | 69 | function conforms to |
75 | .St -ansiC . | 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 index df7845f90c..b0842678e2 100644 --- a/src/lib/libc/stdlib/atoi.c +++ b/src/lib/libc/stdlib/atoi.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: atoi.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1988 Regents of the University of California. | 3 | * Copyright (c) 1988 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,16 +28,10 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)atoi.c 5.7 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: atoi.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | 32 | ||
41 | int | 33 | int |
42 | atoi(str) | 34 | atoi(const char *str) |
43 | const char *str; | ||
44 | { | 35 | { |
45 | return((int)strtol(str, (char **)NULL, 10)); | 36 | return((int)strtol(str, (char **)NULL, 10)); |
46 | } | 37 | } |
diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3 index 86e3d324a3..80e54c892d 100644 --- a/src/lib/libc/stdlib/atol.3 +++ b/src/lib/libc/stdlib/atol.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)atol.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: atol.3,v 1.6 2003/06/02 20:18:37 millert Exp $ |
37 | .\" $Id: atol.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt ATOL 3 | 35 | .Dt ATOL 3 |
@@ -52,9 +47,9 @@ string to long integer | |||
52 | The | 47 | The |
53 | .Fn atol | 48 | .Fn atol |
54 | function converts the initial portion of the string pointed to by | 49 | function converts the initial portion of the string pointed to by |
55 | .Ar nptr | 50 | .Fa nptr |
56 | to | 51 | to |
57 | .Em long integer | 52 | .Li long integer |
58 | representation. | 53 | representation. |
59 | .Pp | 54 | .Pp |
60 | It is equivalent to: | 55 | It is equivalent to: |
@@ -64,12 +59,12 @@ strtol(nptr, (char **)NULL, 10); | |||
64 | .Sh SEE ALSO | 59 | .Sh SEE ALSO |
65 | .Xr atof 3 , | 60 | .Xr atof 3 , |
66 | .Xr atoi 3 , | 61 | .Xr atoi 3 , |
62 | .Xr atoll 3 , | ||
67 | .Xr strtod 3 , | 63 | .Xr strtod 3 , |
68 | .Xr strtol 3 , | 64 | .Xr strtol 3 , |
69 | .Xr strtoul 3 | 65 | .Xr strtoul 3 |
70 | .Sh STANDARDS | 66 | .Sh STANDARDS |
71 | The | 67 | The |
72 | .Fn atol | 68 | .Fn atol |
73 | function | 69 | function conforms to |
74 | conforms to | 70 | .St -ansiC-99 . |
75 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atol.c b/src/lib/libc/stdlib/atol.c index 31ed06298b..1970804401 100644 --- a/src/lib/libc/stdlib/atol.c +++ b/src/lib/libc/stdlib/atol.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: atol.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1988 Regents of the University of California. | 3 | * Copyright (c) 1988 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,16 +28,10 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)atol.c 5.7 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: atol.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | 32 | ||
41 | long | 33 | long |
42 | atol(str) | 34 | atol(const char *str) |
43 | const char *str; | ||
44 | { | 35 | { |
45 | return(strtol(str, (char **)NULL, 10)); | 36 | return(strtol(str, (char **)NULL, 10)); |
46 | } | 37 | } |
diff --git a/src/lib/libc/stdlib/calloc.3 b/src/lib/libc/stdlib/atoll.3 index d0754b46a0..2b53e9b694 100644 --- a/src/lib/libc/stdlib/calloc.3 +++ b/src/lib/libc/stdlib/atoll.3 | |||
@@ -1,4 +1,4 @@ | |||
1 | .\" Copyright (c) 1991 The Regents of the University of California. | 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. |
2 | .\" All rights reserved. | 2 | .\" All rights reserved. |
3 | .\" | 3 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | 4 | .\" This code is derived from software contributed to Berkeley by |
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,39 +29,42 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)calloc.3 5.2 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: atoll.3,v 1.4 2005/07/26 04:20:23 jaredy Exp $ |
37 | .\" $Id: calloc.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt CALLOC 3 | 35 | .Dt ATOLL 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
43 | .Nm calloc | 38 | .Nm atoll |
44 | .Nd allocate clean memory (zero initialized space) | 39 | .Nd convert |
40 | .Tn ASCII | ||
41 | string to long long integer | ||
45 | .Sh SYNOPSIS | 42 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 43 | .Fd #include <stdlib.h> |
47 | .Ft void * | 44 | .Ft long long |
48 | .Fn calloc "size_t nmemb" "size_t size" | 45 | .Fn atoll "const char *nptr" |
49 | .Sh DESCRIPTION | 46 | .Sh DESCRIPTION |
50 | The | 47 | The |
51 | .Fn calloc | 48 | .Fn atoll |
52 | function allocates space for an array of | 49 | function converts the initial portion of the string pointed to by |
53 | .Fa nmemb | 50 | .Fa nptr |
54 | objects, each of whose size is | 51 | to |
55 | .Fa size . | 52 | .Li long long integer |
56 | The space is initialized to all bits zero. | 53 | representation. |
57 | .Sh RETURN VALUES | 54 | .Pp |
58 | The | 55 | It is equivalent to: |
59 | .Fn calloc | 56 | .Bd -literal -offset indent |
60 | function returns | 57 | strtoll(nptr, (char **)NULL, 10); |
61 | a pointer to the | 58 | .Ed |
62 | the allocated space if successful; otherwise a null pointer is returned. | ||
63 | .Sh SEE ALSO | 59 | .Sh SEE ALSO |
64 | .Xr malloc 3 , | 60 | .Xr atof 3 , |
65 | .Xr realloc 3 , | 61 | .Xr atoi 3 , |
66 | .Xr free 3 | 62 | .Xr atol 3 , |
63 | .Xr strtod 3 , | ||
64 | .Xr strtol 3 , | ||
65 | .Xr strtoul 3 | ||
67 | .Sh STANDARDS | 66 | .Sh STANDARDS |
68 | The | 67 | The |
69 | .Fn calloc | 68 | .Fn atoll |
70 | function conforms to | 69 | function conforms to |
71 | .St -ansiC . | 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 index 1622c96c6b..d1dd9173db 100644 --- a/src/lib/libc/stdlib/bsearch.3 +++ b/src/lib/libc/stdlib/bsearch.3 | |||
@@ -1,5 +1,5 @@ | |||
1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | 1 | .\" Copyright (c) 1990, 1991, 1993, 1994 |
2 | .\" All rights reserved. | 2 | .\" The Regents of the University of California. All rights reserved. |
3 | .\" | 3 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | 4 | .\" This code is derived from software contributed to Berkeley by |
5 | .\" the American National Standards Committee X3, on Information | 5 | .\" the American National Standards Committee X3, on Information |
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,10 +29,9 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)bsearch.3 5.6 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: bsearch.3,v 1.6 2003/06/02 20:18:37 millert Exp $ |
37 | .\" $Id: bsearch.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd April 19, 1994 |
40 | .Dt BSEARCH 3 | 35 | .Dt BSEARCH 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
@@ -51,12 +46,12 @@ The | |||
51 | .Fn bsearch | 46 | .Fn bsearch |
52 | function searches an array of | 47 | function searches an array of |
53 | .Fa nmemb | 48 | .Fa nmemb |
54 | objects, the initial member of which is | 49 | objects, the initial member of which is |
55 | pointed to by | 50 | pointed to by |
56 | .Fa base , | 51 | .Fa base , |
57 | for a member that matches the object pointed to by | 52 | for a member that matches the object pointed to by |
58 | .Fa key . | 53 | .Fa key . |
59 | The size of each member of the array is specified by | 54 | The size of each member of the array is specified by |
60 | .Fa size . | 55 | .Fa size . |
61 | .Pp | 56 | .Pp |
62 | The contents of the array should be in ascending sorted order according | 57 | The contents of the array should be in ascending sorted order according |
@@ -64,9 +59,7 @@ to the comparison function referenced by | |||
64 | .Fa compar . | 59 | .Fa compar . |
65 | The | 60 | The |
66 | .Fa compar | 61 | .Fa compar |
67 | routine | 62 | routine is expected to have two arguments which point to the |
68 | is expected to have two | ||
69 | two arguments which point to the | ||
70 | .Fa key | 63 | .Fa key |
71 | object and to an array member, in that order, and should return an integer | 64 | object and to an array member, in that order, and should return an integer |
72 | less than, equal to, or greater than zero if the | 65 | less than, equal to, or greater than zero if the |
@@ -83,7 +76,7 @@ If two members compare as equal, which member is matched is unspecified. | |||
83 | .Xr db 3 , | 76 | .Xr db 3 , |
84 | .Xr lsearch 3 , | 77 | .Xr lsearch 3 , |
85 | .Xr qsort 3 , | 78 | .Xr qsort 3 , |
86 | .\" .Xr tsearch 3 | 79 | .Xr tsearch 3 |
87 | .Sh STANDARDS | 80 | .Sh STANDARDS |
88 | The | 81 | The |
89 | .Fn bsearch | 82 | .Fn bsearch |
diff --git a/src/lib/libc/stdlib/bsearch.c b/src/lib/libc/stdlib/bsearch.c index fac03f694f..8193d27c60 100644 --- a/src/lib/libc/stdlib/bsearch.c +++ b/src/lib/libc/stdlib/bsearch.c | |||
@@ -10,11 +10,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 13 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 14 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 15 | * without specific prior written permission. |
20 | * | 16 | * |
@@ -31,11 +27,6 @@ | |||
31 | * SUCH DAMAGE. | 27 | * SUCH DAMAGE. |
32 | */ | 28 | */ |
33 | 29 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)bsearch.c 5.4 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: bsearch.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 30 | #include <stdlib.h> |
40 | 31 | ||
41 | /* | 32 | /* |
@@ -55,16 +46,12 @@ static char *rcsid = "$Id: bsearch.c,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | |||
55 | * look at item 3. | 46 | * look at item 3. |
56 | */ | 47 | */ |
57 | void * | 48 | void * |
58 | bsearch(key, base0, nmemb, size, compar) | 49 | bsearch(const void *key, const void *base0, size_t nmemb, size_t size, |
59 | register const void *key; | 50 | int (*compar)(const void *, const void *)) |
60 | const void *base0; | ||
61 | size_t nmemb; | ||
62 | register size_t size; | ||
63 | register int (*compar) __P((const void *, const void *)); | ||
64 | { | 51 | { |
65 | register const char *base = base0; | 52 | const char *base = base0; |
66 | register int lim, cmp; | 53 | int lim, cmp; |
67 | register const void *p; | 54 | const void *p; |
68 | 55 | ||
69 | for (lim = nmemb; lim != 0; lim >>= 1) { | 56 | for (lim = nmemb; lim != 0; lim >>= 1) { |
70 | p = base + (lim >> 1) * size; | 57 | p = base + (lim >> 1) * size; |
diff --git a/src/lib/libc/stdlib/calloc.c b/src/lib/libc/stdlib/calloc.c index 3353fab052..43a0d1632c 100644 --- a/src/lib/libc/stdlib/calloc.c +++ b/src/lib/libc/stdlib/calloc.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: calloc.c,v 1.11 2006/04/02 18:22:14 otto Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,23 +28,23 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)calloc.c 5.6 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: calloc.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | #include <string.h> | 32 | #include <string.h> |
33 | #include <limits.h> | ||
34 | #include <errno.h> | ||
41 | 35 | ||
42 | void * | 36 | void * |
43 | calloc(num, size) | 37 | calloc(size_t num, size_t size) |
44 | size_t num; | ||
45 | register size_t size; | ||
46 | { | 38 | { |
47 | register void *p; | 39 | void *p; |
48 | 40 | ||
41 | if (num && SIZE_MAX / num < size) { | ||
42 | errno = ENOMEM; | ||
43 | return NULL; | ||
44 | } | ||
49 | size *= num; | 45 | size *= num; |
50 | if (p = malloc(size)) | 46 | p = malloc(size); |
51 | memset(p, '\0', size); | 47 | if (p) |
48 | memset(p, 0, size); | ||
52 | return(p); | 49 | return(p); |
53 | } | 50 | } |
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 index a4730694a5..d6a9a0029d 100644 --- a/src/lib/libc/stdlib/div.3 +++ b/src/lib/libc/stdlib/div.3 | |||
@@ -11,11 +11,7 @@ | |||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | 11 | .\" 2. Redistributions in binary form must reproduce the above copyright |
12 | .\" notice, this list of conditions and the following disclaimer in the | 12 | .\" notice, this list of conditions and the following disclaimer in the |
13 | .\" documentation and/or other materials provided with the distribution. | 13 | .\" documentation and/or other materials provided with the distribution. |
14 | .\" 3. All advertising materials mentioning features or use of this software | 14 | .\" 3. Neither the name of the University nor the names of its contributors |
15 | .\" must display the following acknowledgement: | ||
16 | .\" This product includes software developed by the University of | ||
17 | .\" California, Berkeley and its contributors. | ||
18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
19 | .\" may be used to endorse or promote products derived from this software | 15 | .\" may be used to endorse or promote products derived from this software |
20 | .\" without specific prior written permission. | 16 | .\" without specific prior written permission. |
21 | .\" | 17 | .\" |
@@ -31,8 +27,7 @@ | |||
31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
32 | .\" SUCH DAMAGE. | 28 | .\" SUCH DAMAGE. |
33 | .\" | 29 | .\" |
34 | .\" from: @(#)div.3 5.2 (Berkeley) 4/19/91 | 30 | .\" $OpenBSD: div.3,v 1.8 2006/01/13 17:58:09 millert Exp $ |
35 | .\" $Id: div.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
36 | .\" | 31 | .\" |
37 | .Dd April 19, 1991 | 32 | .Dd April 19, 1991 |
38 | .Dt DIV 3 | 33 | .Dt DIV 3 |
@@ -47,24 +42,24 @@ | |||
47 | .Sh DESCRIPTION | 42 | .Sh DESCRIPTION |
48 | The | 43 | The |
49 | .Fn div | 44 | .Fn div |
50 | function | 45 | function computes the value |
51 | computes the value | 46 | .Fa num Ns / Ns Fa denom |
52 | .Fa num/denom | ||
53 | and returns the quotient and remainder in a structure named | 47 | and returns the quotient and remainder in a structure named |
54 | .Fa div_t | 48 | .Fa div_t |
55 | that contains two | 49 | that contains two |
56 | .Em int | 50 | .Li int |
57 | members named | 51 | members named |
58 | .Fa quot | 52 | .Fa quot |
59 | and | 53 | and |
60 | .Fa rem . | 54 | .Fa rem . |
61 | .Sh SEE ALSO | 55 | .Sh SEE ALSO |
56 | .Xr imaxdiv 3 , | ||
62 | .Xr ldiv 3 , | 57 | .Xr ldiv 3 , |
63 | .Xr qdiv 3 , | 58 | .Xr lldiv 3 , |
64 | .Xr math 3 | 59 | .Xr math 3 , |
60 | .Xr qdiv 3 | ||
65 | .Sh STANDARDS | 61 | .Sh STANDARDS |
66 | The | 62 | The |
67 | .Fn div | 63 | .Fn div |
68 | function | 64 | function conforms to |
69 | conforms to | ||
70 | .St -ansiC . | 65 | .St -ansiC . |
diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c index 122ac0deec..f7ac2db4b0 100644 --- a/src/lib/libc/stdlib/div.c +++ b/src/lib/libc/stdlib/div.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: div.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1990 Regents of the University of California. | 3 | * Copyright (c) 1990 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -13,11 +14,7 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 15 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 16 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 17 | * 3. Neither the name of the University nor the names of its contributors |
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | 18 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. | 19 | * without specific prior written permission. |
23 | * | 20 | * |
@@ -34,16 +31,10 @@ | |||
34 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
35 | */ | 32 | */ |
36 | 33 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char *sccsid = "from: @(#)div.c 5.2 (Berkeley) 4/16/91";*/ | ||
39 | static char *rcsid = "$Id: div.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | #include <stdlib.h> /* div_t */ | 34 | #include <stdlib.h> /* div_t */ |
43 | 35 | ||
44 | div_t | 36 | div_t |
45 | div(num, denom) | 37 | div(int num, int denom) |
46 | int num, denom; | ||
47 | { | 38 | { |
48 | div_t r; | 39 | div_t r; |
49 | 40 | ||
diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c index ae1a8634dc..b6c046c831 100644 --- a/src/lib/libc/stdlib/drand48.c +++ b/src/lib/libc/stdlib/drand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: drand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/ecvt.3 b/src/lib/libc/stdlib/ecvt.3 new file mode 100644 index 0000000000..689cb1c6f9 --- /dev/null +++ b/src/lib/libc/stdlib/ecvt.3 | |||
@@ -0,0 +1,165 @@ | |||
1 | .\" $OpenBSD: ecvt.3,v 1.7 2004/01/25 14:48:32 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 December 1, 2002 | ||
22 | .Dt ECVT 3 | ||
23 | .Os | ||
24 | .Sh NAME | ||
25 | .Nm ecvt , | ||
26 | .Nm fcvt , | ||
27 | .Nm gcvt | ||
28 | .Nd convert double to | ||
29 | .Tn ASCII | ||
30 | string | ||
31 | .Sh SYNOPSIS | ||
32 | .Fd #include <stdlib.h> | ||
33 | .Ft char * | ||
34 | .Fn ecvt "double value" "int ndigit" "int *decpt" "int *sign" | ||
35 | .Ft char * | ||
36 | .Fn fcvt "double value" "int ndigit" "int *decpt" "int *sign" | ||
37 | .Ft char * | ||
38 | .Fn gcvt "double value" "int ndigit" "char *buf" | ||
39 | .Sh DESCRIPTION | ||
40 | .Bf -symbolic | ||
41 | These functions are provided for compatibility with legacy code. | ||
42 | New code should use the | ||
43 | .Xr snprintf 3 | ||
44 | function for improved safety and portability. | ||
45 | .Ef | ||
46 | .Pp | ||
47 | The | ||
48 | .Fn ecvt , | ||
49 | .Fn fcvt | ||
50 | and | ||
51 | .Fn gcvt | ||
52 | functions convert the double precision floating-point number | ||
53 | .Fa value | ||
54 | to a NUL-terminated | ||
55 | .Tn ASCII | ||
56 | string. | ||
57 | .Pp | ||
58 | The | ||
59 | .Fn ecvt | ||
60 | function converts | ||
61 | .Fa value | ||
62 | to a NUL-terminated string of exactly | ||
63 | .Fa ndigit | ||
64 | digits and returns a pointer to that string. | ||
65 | The result is padded with zeroes from left to right as needed. | ||
66 | There are no leading zeroes unless | ||
67 | .Fa value | ||
68 | itself is 0. | ||
69 | The least significant digit is rounded in an implementation-dependent manner. | ||
70 | The position of the decimal point relative to the beginning of the string | ||
71 | is stored in | ||
72 | .Fa decpt . | ||
73 | A negative value indicates that the decimal point is located | ||
74 | to the left of the returned digits (this occurs when there is no | ||
75 | whole number component to | ||
76 | .Fa value ) . | ||
77 | If | ||
78 | .Fa value | ||
79 | is zero, it is unspecified whether the integer pointed to by | ||
80 | .Fa decpt | ||
81 | will be 0 or 1. | ||
82 | The decimal point itself is not included in the returned string. | ||
83 | If the sign of the result is negative, the integer pointed to by | ||
84 | .Fa sign | ||
85 | is non-zero; otherwise, it is 0. | ||
86 | .Pp | ||
87 | If the converted value is out of range or is not representable, | ||
88 | the contents of the returned string are unspecified. | ||
89 | .Pp | ||
90 | The | ||
91 | .Fn fcvt | ||
92 | function is identical to | ||
93 | .Fn ecvt | ||
94 | with the exception that | ||
95 | .Fa ndigit | ||
96 | specifies the number of digits after the decimal point (zero-padded as | ||
97 | needed). | ||
98 | .Pp | ||
99 | The | ||
100 | .Fn gcvt | ||
101 | function converts | ||
102 | .Fa value | ||
103 | to a NUL-terminated string similar to the %g | ||
104 | .Xr printf 3 | ||
105 | format specifier and stores the result in | ||
106 | .Fa buf . | ||
107 | It produces | ||
108 | .Fa ndigit | ||
109 | significant digits similar to the %f | ||
110 | .Xr printf 3 | ||
111 | format specifier where possible. | ||
112 | If | ||
113 | .Fa ndigit | ||
114 | does allow sufficient precision, the result is stored in | ||
115 | exponential notation similar to the %e | ||
116 | .Xr printf 3 | ||
117 | format specifier. | ||
118 | If | ||
119 | .Fa value | ||
120 | is less than zero, | ||
121 | .Fa buf | ||
122 | will be prefixed with a minus sign. | ||
123 | A decimal point is included in the returned string if | ||
124 | .Fa value | ||
125 | is not a whole number. | ||
126 | Unlike the | ||
127 | .Fn ecvt | ||
128 | and | ||
129 | .Fn fcvt | ||
130 | functions, | ||
131 | .Fa buf | ||
132 | is not zero-padded. | ||
133 | .Sh RETURN VALUES | ||
134 | The | ||
135 | .Fn ecvt , | ||
136 | .Fn fcvt | ||
137 | and | ||
138 | .Fn gcvt | ||
139 | functions return a NUL-terminated string representation of | ||
140 | .Fa value . | ||
141 | .Sh WARNINGS | ||
142 | The | ||
143 | .Fn ecvt | ||
144 | and | ||
145 | .Fn fcvt | ||
146 | functions return a pointer to internal storage space that will be | ||
147 | overwritten by subsequent calls to either function. | ||
148 | .Pp | ||
149 | The maximum possible precision of the return value is limited by the | ||
150 | precision of a double and may not be the same on all architectures. | ||
151 | .Pp | ||
152 | The | ||
153 | .Xr snprintf 3 | ||
154 | function is preferred over these functions for new code. | ||
155 | .Sh SEE ALSO | ||
156 | .Xr printf 3 , | ||
157 | .Xr strtod 3 | ||
158 | .Sh STANDARDS | ||
159 | The | ||
160 | .Fn ecvt , | ||
161 | .Fn fcvt | ||
162 | and | ||
163 | .Fn gcvt | ||
164 | functions conform to | ||
165 | .St -p1003.1-2001 . | ||
diff --git a/src/lib/libc/stdlib/ecvt.c b/src/lib/libc/stdlib/ecvt.c new file mode 100644 index 0000000000..eb0e428996 --- /dev/null +++ b/src/lib/libc/stdlib/ecvt.c | |||
@@ -0,0 +1,99 @@ | |||
1 | /* $OpenBSD: ecvt.c,v 1.5 2006/01/10 16:18:37 millert 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 | static char *__cvt(double, int, int *, int *, int, int); | ||
29 | |||
30 | static char * | ||
31 | __cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad) | ||
32 | { | ||
33 | static char *s; | ||
34 | char *p, *rve; | ||
35 | size_t siz; | ||
36 | |||
37 | if (ndigit == 0) { | ||
38 | *sign = value < 0.0; | ||
39 | *decpt = 0; | ||
40 | return (""); | ||
41 | } | ||
42 | |||
43 | if (s) { | ||
44 | free(s); | ||
45 | s = NULL; | ||
46 | } | ||
47 | |||
48 | if (ndigit < 0) | ||
49 | siz = -ndigit + 1; | ||
50 | else | ||
51 | siz = ndigit + 1; | ||
52 | |||
53 | |||
54 | /* __dtoa() doesn't allocate space for 0 so we do it by hand */ | ||
55 | if (value == 0.0) { | ||
56 | *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */ | ||
57 | *sign = 0; | ||
58 | if ((rve = s = (char *)malloc(siz)) == NULL) | ||
59 | return(NULL); | ||
60 | *rve++ = '0'; | ||
61 | *rve = '\0'; | ||
62 | } else { | ||
63 | p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve); | ||
64 | if (*decpt == 9999) { | ||
65 | /* Infinity or Nan, convert to inf or nan like printf */ | ||
66 | *decpt = 0; | ||
67 | return(*p == 'I' ? "inf" : "nan"); | ||
68 | } | ||
69 | /* Make a local copy and adjust rve to be in terms of s */ | ||
70 | if (pad && fmode) | ||
71 | siz += *decpt; | ||
72 | if ((s = (char *)malloc(siz)) == NULL) | ||
73 | return(NULL); | ||
74 | (void) strlcpy(s, p, siz); | ||
75 | rve = s + (rve - p); | ||
76 | } | ||
77 | |||
78 | /* Add trailing zeros */ | ||
79 | if (pad) { | ||
80 | siz -= rve - s; | ||
81 | while (--siz) | ||
82 | *rve++ = '0'; | ||
83 | *rve = '\0'; | ||
84 | } | ||
85 | |||
86 | return(s); | ||
87 | } | ||
88 | |||
89 | char * | ||
90 | ecvt(double value, int ndigit, int *decpt, int *sign) | ||
91 | { | ||
92 | return(__cvt(value, ndigit, decpt, sign, 0, 1)); | ||
93 | } | ||
94 | |||
95 | char * | ||
96 | fcvt(double value, int ndigit, int *decpt, int *sign) | ||
97 | { | ||
98 | return(__cvt(value, ndigit, decpt, sign, 1, 1)); | ||
99 | } | ||
diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c index cc9fbf770c..2ffeaa6e71 100644 --- a/src/lib/libc/stdlib/erand48.c +++ b/src/lib/libc/stdlib/erand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: erand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3 index adb81ffcb4..fa233ac75f 100644 --- a/src/lib/libc/stdlib/exit.3 +++ b/src/lib/libc/stdlib/exit.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,25 +29,30 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)exit.3 6.6 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: exit.3,v 1.10 2004/05/03 17:21:13 millert Exp $ |
37 | .\" $Id: exit.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd January 21, 2004 |
40 | .Dt EXIT 3 | 35 | .Dt EXIT 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
43 | .Nm exit | 38 | .Nm exit, _Exit |
44 | .Nd perform normal program termination | 39 | .Nd perform normal program termination |
45 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 41 | .Fd #include <stdlib.h> |
47 | .Ft void | 42 | .Ft void |
48 | .Fn exit "int status" | 43 | .Fn exit "int status" |
44 | .Ft void | ||
45 | .Fn _Exit "int status" | ||
49 | .Sh DESCRIPTION | 46 | .Sh DESCRIPTION |
50 | .Fn Exit | 47 | The |
51 | terminates a process. | 48 | .Fn exit |
49 | and | ||
50 | .Fn _Exit | ||
51 | functions terminate a process. | ||
52 | .Pp | 52 | .Pp |
53 | Before termination it performs the following functions in the | 53 | Before termination, |
54 | order listed: | 54 | .Fn exit |
55 | performs the following operations in the order listed: | ||
55 | .Bl -enum -offset indent | 56 | .Bl -enum -offset indent |
56 | .It | 57 | .It |
57 | Call the functions registered with the | 58 | Call the functions registered with the |
@@ -66,19 +67,48 @@ Unlink all files created with the | |||
66 | .Xr tmpfile 3 | 67 | .Xr tmpfile 3 |
67 | function. | 68 | function. |
68 | .El | 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. | ||
69 | .Sh RETURN VALUES | 96 | .Sh RETURN VALUES |
70 | The | 97 | The |
71 | .Fn exit | 98 | .Fn exit |
72 | function | 99 | and |
73 | never returns. | 100 | .Fn _Exit |
101 | functions never return. | ||
74 | .Sh SEE ALSO | 102 | .Sh SEE ALSO |
75 | .Xr _exit 2 , | 103 | .Xr _exit 2 , |
76 | .Xr atexit 3 , | 104 | .Xr atexit 3 , |
77 | .Xr intro 3 , | 105 | .Xr intro 3 , |
106 | .Xr sysexits 3 , | ||
78 | .Xr tmpfile 3 | 107 | .Xr tmpfile 3 |
79 | .Sh STANDARDS | 108 | .Sh STANDARDS |
80 | The | 109 | The |
81 | .Fn exit | 110 | .Fn exit |
82 | function | 111 | and |
83 | conforms to | 112 | .Fn _Exit |
84 | .St -ansiC . | 113 | functions conform to |
114 | .St -ansiC-99 . | ||
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c index b1412f42bb..90b7d5adc2 100644 --- a/src/lib/libc/stdlib/exit.c +++ b/src/lib/libc/stdlib/exit.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: exit.c,v 1.11 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,31 +28,42 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 31 | #include <sys/types.h> |
35 | /*static char *sccsid = "from: @(#)exit.c 5.4 (Berkeley) 2/23/91";*/ | 32 | #include <sys/mman.h> |
36 | static char *rcsid = "$Id: exit.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 33 | #include <stdlib.h> |
40 | #include <unistd.h> | 34 | #include <unistd.h> |
41 | #include "atexit.h" | 35 | #include "atexit.h" |
36 | #include "thread_private.h" | ||
42 | 37 | ||
43 | void (*__cleanup)(); | 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; | ||
44 | 46 | ||
45 | /* | 47 | /* |
46 | * Exit, flushing stdio buffers if necessary. | 48 | * Exit, flushing stdio buffers if necessary. |
47 | */ | 49 | */ |
48 | void | 50 | void |
49 | exit(status) | 51 | exit(int status) |
50 | int status; | ||
51 | { | 52 | { |
52 | register struct atexit *p; | 53 | struct atexit *p, *q; |
53 | register int n; | 54 | int n, pgsize = getpagesize(); |
54 | 55 | ||
55 | for (p = __atexit; p; p = p->next) | 56 | if (!__atexit_invalid) { |
56 | for (n = p->ind; --n >= 0;) | 57 | p = __atexit; |
57 | (*p->fns[n])(); | 58 | while (p != NULL) { |
58 | if (__cleanup) | 59 | for (n = p->ind; --n >= 0;) |
59 | (*__cleanup)(); | 60 | if (p->fns[n] != NULL) |
61 | (*p->fns[n])(); | ||
62 | q = p; | ||
63 | p = p->next; | ||
64 | munmap(q, pgsize); | ||
65 | } | ||
66 | } | ||
67 | /* cleanup, if registered, was called through fns[0] in the last page */ | ||
60 | _exit(status); | 68 | _exit(status); |
61 | } | 69 | } |
diff --git a/src/lib/libc/stdlib/gcvt.c b/src/lib/libc/stdlib/gcvt.c new file mode 100644 index 0000000000..bc6295c03d --- /dev/null +++ b/src/lib/libc/stdlib/gcvt.c | |||
@@ -0,0 +1,108 @@ | |||
1 | /* $OpenBSD: gcvt.c,v 1.9 2006/01/10 16:18:37 millert 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 | |||
30 | char * | ||
31 | gcvt(double value, int ndigit, char *buf) | ||
32 | { | ||
33 | char *digits, *dst, *src; | ||
34 | int i, decpt, sign; | ||
35 | struct lconv *lconv; | ||
36 | |||
37 | lconv = localeconv(); | ||
38 | if (ndigit == 0) { | ||
39 | buf[0] = '\0'; | ||
40 | return (buf); | ||
41 | } | ||
42 | |||
43 | digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL); | ||
44 | if (decpt == 9999) { | ||
45 | /* | ||
46 | * Infinity or NaN, convert to inf or nan with sign. | ||
47 | * We assume the buffer is at least ndigit long. | ||
48 | */ | ||
49 | snprintf(buf, ndigit + 1, "%s%s", sign ? "-" : "", | ||
50 | *digits == 'I' ? "inf" : "nan"); | ||
51 | return (buf); | ||
52 | } | ||
53 | |||
54 | dst = buf; | ||
55 | if (sign) | ||
56 | *dst++ = '-'; | ||
57 | |||
58 | if (decpt < 0 || decpt > ndigit) { | ||
59 | /* exponential format (e.g. 1.2345e+13) */ | ||
60 | if (--decpt < 0) { | ||
61 | sign = 1; | ||
62 | decpt = -decpt; | ||
63 | } else | ||
64 | sign = 0; | ||
65 | src = digits; | ||
66 | *dst++ = *src++; | ||
67 | *dst++ = *lconv->decimal_point; | ||
68 | while (*src != '\0') | ||
69 | *dst++ = *src++; | ||
70 | *dst++ = 'e'; | ||
71 | if (sign) | ||
72 | *dst++ = '-'; | ||
73 | else | ||
74 | *dst++ = '+'; | ||
75 | if (decpt < 10) { | ||
76 | *dst++ = '0'; | ||
77 | *dst++ = '0' + decpt; | ||
78 | *dst = '\0'; | ||
79 | } else { | ||
80 | /* XXX - optimize */ | ||
81 | for (sign = decpt, i = 0; (sign /= 10) != 0; i++) | ||
82 | continue; | ||
83 | dst[i + 1] = '\0'; | ||
84 | while (decpt != 0) { | ||
85 | dst[i--] = '0' + decpt % 10; | ||
86 | decpt /= 10; | ||
87 | } | ||
88 | } | ||
89 | } else { | ||
90 | /* standard format */ | ||
91 | for (i = 0, src = digits; i < decpt; i++) { | ||
92 | if (*src != '\0') | ||
93 | *dst++ = *src++; | ||
94 | else | ||
95 | *dst++ = '0'; | ||
96 | } | ||
97 | if (*src != '\0') { | ||
98 | if (src == digits) | ||
99 | *dst++ = '0'; /* zero before decimal point */ | ||
100 | *dst++ = *lconv->decimal_point; | ||
101 | for (i = decpt; digits[i] != '\0'; i++) { | ||
102 | *dst++ = digits[i]; | ||
103 | } | ||
104 | } | ||
105 | *dst = '\0'; | ||
106 | } | ||
107 | return (buf); | ||
108 | } | ||
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3 index 411eb35da4..f131bbf513 100644 --- a/src/lib/libc/stdlib/getenv.3 +++ b/src/lib/libc/stdlib/getenv.3 | |||
@@ -1,5 +1,5 @@ | |||
1 | .\" Copyright (c) 1988, 1991 The Regents of the University of California. | 1 | .\" Copyright (c) 1988, 1991, 1993 |
2 | .\" All rights reserved. | 2 | .\" The Regents of the University of California. All rights reserved. |
3 | .\" | 3 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | 4 | .\" This code is derived from software contributed to Berkeley by |
5 | .\" the American National Standards Committee X3, on Information | 5 | .\" the American National Standards Committee X3, on Information |
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,10 +29,9 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)getenv.3 6.11 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: getenv.3,v 1.11 2005/07/26 04:20:23 jaredy Exp $ |
37 | .\" $Id: getenv.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd December 11, 1993 |
40 | .Dt GETENV 3 | 35 | .Dt GETENV 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
@@ -56,59 +51,51 @@ | |||
56 | .Ft void | 51 | .Ft void |
57 | .Fn unsetenv "const char *name" | 52 | .Fn unsetenv "const char *name" |
58 | .Sh DESCRIPTION | 53 | .Sh DESCRIPTION |
59 | These functions set, unset and fetch environment variables from the | 54 | These functions set, unset, and fetch environment variables from the host |
60 | host | ||
61 | .Em environment list . | 55 | .Em environment list . |
62 | For compatibility with differing environment conventions, | 56 | For compatibility with differing environment conventions, the given arguments |
63 | the given arguments | 57 | .Fa name |
64 | .Ar name | ||
65 | and | 58 | and |
66 | .Ar value | 59 | .Fa value |
67 | may be appended and prepended, | 60 | may be appended and prepended, respectively, with an equal sign |
68 | respectively, | ||
69 | with an equal sign | ||
70 | .Dq Li \&= . | 61 | .Dq Li \&= . |
71 | .Pp | 62 | .Pp |
72 | The | 63 | The |
73 | .Fn getenv | 64 | .Fn getenv |
74 | function obtains the current value of the environment variable, | 65 | function obtains the current value of the environment variable |
75 | .Ar name . | 66 | .Fa name . |
76 | If the variable | 67 | If the variable |
77 | .Ar name | 68 | .Fa name |
78 | is not in the current environment , | 69 | is not in the current environment, a null pointer is returned. |
79 | a null pointer is returned. | ||
80 | .Pp | 70 | .Pp |
81 | The | 71 | The |
82 | .Fn setenv | 72 | .Fn setenv |
83 | function inserts or resets the environment variable | 73 | function inserts or resets the environment variable |
84 | .Ar name | 74 | .Fa name |
85 | in the current environment list. | 75 | in the current environment list. |
86 | If the variable | 76 | If the variable |
87 | .Ar name | 77 | .Fa name |
88 | does not exist in the list, | 78 | does not exist in the list, it is inserted with the given |
89 | it is inserted with the given | 79 | .Fa value . |
90 | .Ar value. | ||
91 | If the variable does exist, the argument | 80 | If the variable does exist, the argument |
92 | .Ar overwrite | 81 | .Fa overwrite |
93 | is tested; if | 82 | is tested; if |
94 | .Ar overwrite is | 83 | .Fa overwrite |
95 | zero, the | 84 | is zero, the variable is not reset, otherwise it is reset to the given |
96 | variable is not reset, otherwise it is reset | 85 | .Fa value . |
97 | to the given | ||
98 | .Ar value . | ||
99 | .Pp | 86 | .Pp |
100 | The | 87 | The |
101 | .Fn putenv | 88 | .Fn putenv |
102 | function takes an argument of the form ``name=value'' and is | 89 | function takes an argument of the form |
103 | equivalent to: | 90 | .Ar name Ns = Ns Ar value |
91 | and is equivalent to: | ||
104 | .Bd -literal -offset indent | 92 | .Bd -literal -offset indent |
105 | setenv(name, value, 1); | 93 | setenv(name, value, 1); |
106 | .Ed | 94 | .Ed |
107 | .Pp | 95 | .Pp |
108 | The | 96 | The |
109 | .Fn unsetenv | 97 | .Fn unsetenv |
110 | function | 98 | function deletes all instances of the variable name pointed to by |
111 | deletes all instances of the variable name pointed to by | ||
112 | .Fa name | 99 | .Fa name |
113 | from the list. | 100 | from the list. |
114 | .Sh RETURN VALUES | 101 | .Sh RETURN VALUES |
@@ -118,8 +105,11 @@ and | |||
118 | .Fn putenv | 105 | .Fn putenv |
119 | return zero if successful; otherwise the global variable | 106 | return zero if successful; otherwise the global variable |
120 | .Va errno | 107 | .Va errno |
121 | is set to indicate the error and a | 108 | is set to indicate the error and \-1 is returned. |
122 | \-1 is returned. | 109 | .Pp |
110 | If | ||
111 | .Fn getenv | ||
112 | is successful, the string returned should be considered read-only. | ||
123 | .Sh ERRORS | 113 | .Sh ERRORS |
124 | .Bl -tag -width Er | 114 | .Bl -tag -width Er |
125 | .It Bq Er ENOMEM | 115 | .It Bq Er ENOMEM |
@@ -140,12 +130,18 @@ The | |||
140 | function conforms to | 130 | function conforms to |
141 | .St -ansiC . | 131 | .St -ansiC . |
142 | .Sh HISTORY | 132 | .Sh HISTORY |
133 | The function | ||
134 | .Fn getenv | ||
135 | appeared in | ||
136 | .At v7 | ||
137 | and | ||
138 | .Bx 3 . | ||
143 | The functions | 139 | The functions |
144 | .Fn setenv | 140 | .Fn setenv |
145 | and | 141 | and |
146 | .Fn unsetenv | 142 | .Fn unsetenv |
147 | appeared in | 143 | appeared in |
148 | .At v7 . | 144 | .Bx 4.3 Tahoe . |
149 | The | 145 | The |
150 | .Fn putenv | 146 | .Fn putenv |
151 | function appeared in | 147 | function appeared in |
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c index 09d47f2149..72367b34e2 100644 --- a/src/lib/libc/stdlib/getenv.c +++ b/src/lib/libc/stdlib/getenv.c | |||
@@ -1,6 +1,7 @@ | |||
1 | /* $OpenBSD: getenv.c,v 1.8 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | 3 | * Copyright (c) 1987, 1993 |
3 | * All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
4 | * | 5 | * |
5 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | 7 | * modification, are permitted provided that the following conditions |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,27 +28,10 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)getenv.c 5.8 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: getenv.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | #include <string.h> | 32 | #include <string.h> |
41 | 33 | ||
42 | /* | 34 | char *__findenv(const char *name, int *offset); |
43 | * getenv -- | ||
44 | * Returns ptr to value associated with name, if any, else NULL. | ||
45 | */ | ||
46 | char * | ||
47 | getenv(name) | ||
48 | const char *name; | ||
49 | { | ||
50 | int offset; | ||
51 | char *__findenv(); | ||
52 | |||
53 | return(__findenv(name, &offset)); | ||
54 | } | ||
55 | 35 | ||
56 | /* | 36 | /* |
57 | * __findenv -- | 37 | * __findenv -- |
@@ -63,20 +43,38 @@ getenv(name) | |||
63 | * This routine *should* be a static; don't use it. | 43 | * This routine *should* be a static; don't use it. |
64 | */ | 44 | */ |
65 | char * | 45 | char * |
66 | __findenv(name, offset) | 46 | __findenv(const char *name, int *offset) |
67 | register char *name; | ||
68 | int *offset; | ||
69 | { | 47 | { |
70 | extern char **environ; | 48 | extern char **environ; |
71 | register int len; | 49 | int len, i; |
72 | register char **P, *C; | 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; | ||
73 | 78 | ||
74 | for (C = name, len = 0; *C && *C != '='; ++C, ++len); | 79 | return (__findenv(name, &offset)); |
75 | for (P = environ; *P; ++P) | ||
76 | if (!strncmp(*P, name, len)) | ||
77 | if (*(C = *P + len) == '=') { | ||
78 | *offset = P - environ; | ||
79 | return(++C); | ||
80 | } | ||
81 | return(NULL); | ||
82 | } | 80 | } |
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 index f843881afd..796541184f 100644 --- a/src/lib/libc/stdlib/getopt.3 +++ b/src/lib/libc/stdlib/getopt.3 | |||
@@ -9,11 +9,7 @@ | |||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer in the | 10 | .\" notice, this list of conditions and the following disclaimer in the |
11 | .\" documentation and/or other materials provided with the distribution. | 11 | .\" documentation and/or other materials provided with the distribution. |
12 | .\" 3. All advertising materials mentioning features or use of this software | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
13 | .\" must display the following acknowledgement: | ||
14 | .\" This product includes software developed by the University of | ||
15 | .\" California, Berkeley and its contributors. | ||
16 | .\" 4. 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 | 13 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | 14 | .\" without specific prior written permission. |
19 | .\" | 15 | .\" |
@@ -29,20 +25,20 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
31 | .\" | 27 | .\" |
32 | .\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 | 28 | .\" $OpenBSD: getopt.3,v 1.38 2006/03/15 02:50:25 ray Exp $ |
33 | .\" | 29 | .\" |
34 | .Dd April 19, 1994 | 30 | .Dd December 17, 2002 |
35 | .Dt GETOPT 3 | 31 | .Dt GETOPT 3 |
36 | .Os BSD 4.3 | 32 | .Os |
37 | .Sh NAME | 33 | .Sh NAME |
38 | .Nm getopt | 34 | .Nm getopt |
39 | .Nd get option character from command line argument list | 35 | .Nd get option character from command line argument list |
40 | .Sh SYNOPSIS | 36 | .Sh SYNOPSIS |
41 | .Fd #include <unistd.h> | 37 | .Fd #include <unistd.h> |
42 | .Vt extern char *optarg; | 38 | .Vt extern char *optarg; |
39 | .Vt extern int opterr; | ||
43 | .Vt extern int optind; | 40 | .Vt extern int optind; |
44 | .Vt extern int optopt; | 41 | .Vt extern int optopt; |
45 | .Vt extern int opterr; | ||
46 | .Vt extern int optreset; | 42 | .Vt extern int optreset; |
47 | .Ft int | 43 | .Ft int |
48 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" | 44 | .Fn getopt "int argc" "char * const *argv" "const char *optstring" |
@@ -61,20 +57,23 @@ if it has been specified in the string of accepted option characters, | |||
61 | .Pp | 57 | .Pp |
62 | The option string | 58 | The option string |
63 | .Fa optstring | 59 | .Fa optstring |
64 | may contain the following elements: individual characters, and | 60 | may contain the following elements: individual characters, |
65 | characters followed by a colon to indicate an option argument | 61 | characters followed by a colon, and characters followed by two colons. |
66 | is to follow. | 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. | ||
67 | For example, an option string | 66 | For example, an option string |
68 | .Li "\&""x"" | 67 | .Qq x |
69 | recognizes an option | 68 | recognizes an option |
70 | .Dq Fl x , | 69 | .Fl x , |
71 | and an option string | 70 | and an option string |
72 | .Li "\&""x:"" | 71 | .Qq Li x: |
73 | recognizes an option and argument | 72 | recognizes an option and argument |
74 | .Dq Fl x Ar argument . | 73 | .Fl x Ar argument . |
75 | It does not matter to | 74 | It does not matter to |
76 | .Fn getopt | 75 | .Fn getopt |
77 | if a following argument has leading white space. | 76 | if a following argument has leading whitespace. |
78 | .Pp | 77 | .Pp |
79 | On return from | 78 | On return from |
80 | .Fn getopt , | 79 | .Fn getopt , |
@@ -87,23 +86,23 @@ contains the index to the next | |||
87 | argument for a subsequent call | 86 | argument for a subsequent call |
88 | to | 87 | to |
89 | .Fn getopt . | 88 | .Fn getopt . |
90 | The variable | ||
91 | .Va optopt | ||
92 | saves the last | ||
93 | .Em known | ||
94 | option character returned by | ||
95 | .Fn getopt . | ||
96 | .Pp | 89 | .Pp |
97 | The variable | 90 | The variables |
98 | .Va opterr | 91 | .Va opterr |
99 | and | 92 | and |
100 | .Va optind | 93 | .Va optind |
101 | are both initialized to 1. | 94 | are both initialized to 1. |
102 | The | 95 | The |
103 | .Va optind | 96 | .Va optind |
104 | variable may be set to another value before a set of calls to | 97 | variable may be set to another value larger than 0 before a set of calls to |
105 | .Fn getopt | 98 | .Fn getopt |
106 | in order to skip over more or less argv entries. | 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 . | ||
107 | .Pp | 106 | .Pp |
108 | In order to use | 107 | In order to use |
109 | .Fn getopt | 108 | .Fn getopt |
@@ -119,101 +118,163 @@ must be reinitialized. | |||
119 | .Pp | 118 | .Pp |
120 | The | 119 | The |
121 | .Fn getopt | 120 | .Fn getopt |
122 | function | 121 | function returns \-1 when the argument list is exhausted. |
123 | returns \-1 | ||
124 | when the argument list is exhausted, or a non-recognized | ||
125 | option is encountered. | ||
126 | The interpretation of options in the argument list may be cancelled | 122 | The interpretation of options in the argument list may be cancelled |
127 | by the option | 123 | by the option |
128 | .Ql -- | 124 | .Ql -- |
129 | (double dash) which causes | 125 | (double dash) which causes |
130 | .Fn getopt | 126 | .Fn getopt |
131 | to signal the end of argument processing and returns \-1. | 127 | to signal the end of argument processing and return \-1. |
132 | When all options have been processed (i.e., up to the first non-option | 128 | When all options have been processed (i.e., up to the first non-option |
133 | argument), | 129 | argument), |
134 | .Fn getopt | 130 | .Fn getopt |
135 | returns \-1. | 131 | returns \-1. |
136 | .Sh DIAGNOSTICS | 132 | .Sh RETURN VALUES |
137 | If the | 133 | The |
138 | .Fn getopt | 134 | .Fn getopt |
139 | function encounters a character not found in the string | 135 | function returns the next known option character in |
140 | .Va optarg | 136 | .Fa optstring . |
141 | or detects | ||
142 | a missing option argument it writes an error message and returns | ||
143 | .Ql ? | ||
144 | to the | ||
145 | .Em stderr . | ||
146 | Setting | ||
147 | .Va opterr | ||
148 | to a zero will disable these error messages. | ||
149 | If | 137 | If |
150 | .Va optstring | 138 | .Fn getopt |
151 | has a leading | 139 | encounters a character not found in |
152 | .Ql \&: | 140 | .Fa optstring |
153 | then a missing option argument causes a | 141 | or if it detects a missing option argument, |
154 | .Ql \&: | 142 | it returns |
155 | to be returned in addition to suppressing any error messages. | 143 | .Sq \&? |
156 | .Pp | 144 | (question mark). |
157 | Option arguments are allowed to begin with | 145 | If |
158 | .Dq Li \- ; | 146 | .Fa optstring |
159 | this is reasonable but | 147 | has a leading |
160 | reduces the amount of error checking possible. | 148 | .Sq \&: |
161 | .Sh EXTENSIONS | 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. | ||
162 | The | 156 | The |
163 | .Va optreset | ||
164 | variable was added to make it possible to call the | ||
165 | .Fn getopt | 157 | .Fn getopt |
166 | function multiple times. | 158 | function returns \-1 when the argument list is exhausted. |
167 | This is an extension to the | 159 | .Sh ENVIRONMENT |
168 | .St -p1003.2 | 160 | .Bl -tag -width POSIXLY_CORRECTXX |
169 | specification. | 161 | .It Ev POSIXLY_CORRECT |
170 | .Sh EXAMPLE | 162 | If set, a leading |
171 | .Bd -literal -compact | 163 | .Sq - |
172 | extern char *optarg; | 164 | in |
173 | extern int optind; | 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 | ||
174 | int bflag, ch, fd; | 179 | int bflag, ch, fd; |
175 | 180 | ||
176 | bflag = 0; | 181 | bflag = 0; |
177 | while ((ch = getopt(argc, argv, "bf:")) != -1) | 182 | while ((ch = getopt(argc, argv, "bf:")) != -1) { |
178 | switch(ch) { | 183 | switch (ch) { |
179 | case 'b': | 184 | case 'b': |
180 | bflag = 1; | 185 | bflag = 1; |
181 | break; | 186 | break; |
182 | case 'f': | 187 | case 'f': |
183 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | 188 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) |
184 | (void)fprintf(stderr, | 189 | err(1, "%s", optarg); |
185 | "myname: %s: %s\en", optarg, strerror(errno)); | ||
186 | exit(1); | ||
187 | } | ||
188 | break; | 190 | break; |
189 | case '?': | ||
190 | default: | 191 | default: |
191 | usage(); | 192 | usage(); |
193 | /* NOTREACHED */ | ||
194 | } | ||
192 | } | 195 | } |
193 | argc -= optind; | 196 | argc -= optind; |
194 | argv += optind; | 197 | argv += optind; |
195 | .Ed | 198 | .Ed |
196 | .Sh HISTORY | 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 | ||
197 | The | 228 | The |
198 | .Fn getopt | 229 | .Fn getopt |
199 | function appeared | 230 | function implements a superset of the functionality specified by |
200 | .Bx 4.3 . | 231 | .St -p1003.1 . |
201 | .Sh BUGS | 232 | .Pp |
233 | The following extensions are supported: | ||
234 | .Bl -tag -width "xxx" | ||
235 | .It Li o | ||
202 | The | 236 | The |
237 | .Va optreset | ||
238 | variable was added to make it possible to call the | ||
203 | .Fn getopt | 239 | .Fn getopt |
204 | function was once specified to return | 240 | function multiple times. |
205 | .Dv EOF | 241 | .It Li o |
206 | instead of \-1. | 242 | If the |
207 | This was changed by | 243 | .Va optind |
208 | .St -p1003.2-92 | 244 | variable is set to 0, |
209 | to decouple | ||
210 | .Fn getopt | 245 | .Fn getopt |
211 | from | 246 | will behave as if the |
212 | .Pa <stdio.h> . | 247 | .Va optreset |
213 | .Pp | 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 | ||
214 | A single dash | 275 | A single dash |
215 | .Dq Li - | 276 | .Pq Ql - |
216 | may be specified as an character in | 277 | may be specified as a character in |
217 | .Fa optstring , | 278 | .Fa optstring , |
218 | however it should | 279 | however it should |
219 | .Em never | 280 | .Em never |
@@ -221,40 +282,106 @@ have an argument associated with it. | |||
221 | This allows | 282 | This allows |
222 | .Fn getopt | 283 | .Fn getopt |
223 | to be used with programs that expect | 284 | to be used with programs that expect |
224 | .Dq Li - | 285 | .Ql - |
225 | as an option flag. | 286 | as an option flag. |
226 | This practice is wrong, and should not be used in any current development. | 287 | This practice is wrong, and should not be used in any current development. |
227 | It is provided for backward compatibility | 288 | It is provided for backward compatibility |
228 | .Em only . | 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). | ||
229 | By default, a single dash causes | 298 | By default, a single dash causes |
230 | .Fn getopt | 299 | .Fn getopt |
231 | to return \-1. | 300 | to return \-1. |
232 | This is, we believe, compatible with System V. | 301 | .El |
302 | .Pp | ||
303 | Unlike | ||
304 | .Tn GNU | ||
305 | .Fn getopt , | ||
306 | .Ox | ||
307 | does not permute the argument vector to allow non-options to be | ||
308 | interspersed with options on the command line. | ||
309 | Programs requiring this behavior should use | ||
310 | .Xr getopt_long 3 | ||
311 | instead. | ||
312 | Because of this (and unlike | ||
313 | .Tn GNU ) , | ||
314 | the | ||
315 | .Ox | ||
316 | .Fn getopt | ||
317 | supports optional arguments separated by whitespace. | ||
318 | .Pp | ||
319 | Historic | ||
320 | .Bx | ||
321 | versions of | ||
322 | .Fn getopt | ||
323 | set | ||
324 | .Fa optopt | ||
325 | to the last option character processed. | ||
326 | However, this conflicts with | ||
327 | .St -p1003.1 | ||
328 | which stipulates that | ||
329 | .Fa optopt | ||
330 | be set to the last character that caused an error. | ||
331 | .Sh HISTORY | ||
332 | The | ||
333 | .Fn getopt | ||
334 | function appeared in | ||
335 | .Bx 4.3 . | ||
336 | .Sh BUGS | ||
337 | The | ||
338 | .Fn getopt | ||
339 | function was once specified to return | ||
340 | .Dv EOF | ||
341 | instead of \-1. | ||
342 | This was changed by | ||
343 | .St -p1003.2-92 | ||
344 | to decouple | ||
345 | .Fn getopt | ||
346 | from | ||
347 | .Aq Pa stdio.h . | ||
233 | .Pp | 348 | .Pp |
234 | It is also possible to handle digits as option letters. | 349 | It is possible to handle digits as option letters. |
235 | This allows | 350 | This allows |
236 | .Fn getopt | 351 | .Fn getopt |
237 | to be used with programs that expect a number | 352 | to be used with programs that expect a number |
238 | .Pq Dq Li \&-\&3 | 353 | .Pq Dq Li \-3 |
239 | as an option. | 354 | as an option. |
240 | This practice is wrong, and should not be used in any current development. | 355 | This practice is wrong, and should not be used in any current development. |
241 | It is provided for backward compatibility | 356 | It is provided for backward compatibility |
242 | .Em only . | 357 | .Em only . |
243 | The following code fragment works in most cases. | 358 | The following code fragment works in most cases and can handle mixed |
359 | number and letter arguments. | ||
244 | .Bd -literal -offset indent | 360 | .Bd -literal -offset indent |
245 | int length; | 361 | int aflag = 0, bflag = 0, ch, lastch = '\e0'; |
246 | char *p; | 362 | int length = -1, newarg = 1, prevoptind = 1; |
247 | 363 | ||
248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | 364 | while ((ch = getopt(argc, argv, "0123456789ab")) != -1) { |
249 | switch (c) { | 365 | switch (ch) { |
250 | case '0': case '1': case '2': case '3': case '4': | 366 | case '0': case '1': case '2': case '3': case '4': |
251 | case '5': case '6': case '7': case '8': case '9': | 367 | case '5': case '6': case '7': case '8': case '9': |
252 | p = argv[optind - 1]; | 368 | if (newarg || !isdigit(lastch)) |
253 | if (p[0] == '-' && p[1] == ch && !p[2]) | 369 | length = 0; |
254 | length = atoi(++p); | 370 | else if (length > INT_MAX / 10) |
255 | else | 371 | usage(); |
256 | length = atoi(argv[optind] + 1); | 372 | length = (length * 10) + (ch - '0'); |
257 | break; | 373 | break; |
374 | case 'a': | ||
375 | aflag = 1; | ||
376 | break; | ||
377 | case 'b': | ||
378 | bflag = 1; | ||
379 | break; | ||
380 | default: | ||
381 | usage(); | ||
258 | } | 382 | } |
383 | lastch = ch; | ||
384 | newarg = optind != prevoptind; | ||
385 | prevoptind = optind; | ||
259 | } | 386 | } |
260 | .Ed | 387 | .Ed |
diff --git a/src/lib/libc/stdlib/getopt.c b/src/lib/libc/stdlib/getopt.c deleted file mode 100644 index 63c5e6a479..0000000000 --- a/src/lib/libc/stdlib/getopt.c +++ /dev/null | |||
@@ -1,118 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright (c) 1987, 1993, 1994 | ||
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions | ||
7 | * are met: | ||
8 | * 1. Redistributions of source code must retain the above copyright | ||
9 | * notice, this list of conditions and the following disclaimer. | ||
10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
11 | * notice, this list of conditions and the following disclaimer in the | ||
12 | * documentation and/or other materials provided with the distribution. | ||
13 | * 3. All advertising materials mentioning features or use of this software | ||
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /* static char sccsid[] = "from: @(#)getopt.c 8.2 (Berkeley) 4/2/94"; */ | ||
36 | static char *rcsid = "$Id: getopt.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdio.h> | ||
40 | #include <stdlib.h> | ||
41 | #include <string.h> | ||
42 | |||
43 | int opterr = 1, /* if error message should be printed */ | ||
44 | optind = 1, /* index into parent argv vector */ | ||
45 | optopt, /* character checked for validity */ | ||
46 | optreset; /* reset getopt */ | ||
47 | char *optarg; /* argument associated with option */ | ||
48 | |||
49 | #define BADCH (int)'?' | ||
50 | #define BADARG (int)':' | ||
51 | #define EMSG "" | ||
52 | |||
53 | /* | ||
54 | * getopt -- | ||
55 | * Parse argc/argv argument vector. | ||
56 | */ | ||
57 | int | ||
58 | getopt(nargc, nargv, ostr) | ||
59 | int nargc; | ||
60 | char * const *nargv; | ||
61 | const char *ostr; | ||
62 | { | ||
63 | extern char *__progname; | ||
64 | static char *place = EMSG; /* option letter processing */ | ||
65 | char *oli; /* option letter list index */ | ||
66 | |||
67 | if (optreset || !*place) { /* update scanning pointer */ | ||
68 | optreset = 0; | ||
69 | if (optind >= nargc || *(place = nargv[optind]) != '-') { | ||
70 | place = EMSG; | ||
71 | return (-1); | ||
72 | } | ||
73 | if (place[1] && *++place == '-') { /* found "--" */ | ||
74 | ++optind; | ||
75 | place = EMSG; | ||
76 | return (-1); | ||
77 | } | ||
78 | } /* option letter okay? */ | ||
79 | if ((optopt = (int)*place++) == (int)':' || | ||
80 | !(oli = strchr(ostr, optopt))) { | ||
81 | /* | ||
82 | * if the user didn't specify '-' as an option, | ||
83 | * assume it means -1. | ||
84 | */ | ||
85 | if (optopt == (int)'-') | ||
86 | return (-1); | ||
87 | if (!*place) | ||
88 | ++optind; | ||
89 | if (opterr && *ostr != ':') | ||
90 | (void)fprintf(stderr, | ||
91 | "%s: illegal option -- %c\n", __progname, optopt); | ||
92 | return (BADCH); | ||
93 | } | ||
94 | if (*++oli != ':') { /* don't need argument */ | ||
95 | optarg = NULL; | ||
96 | if (!*place) | ||
97 | ++optind; | ||
98 | } | ||
99 | else { /* need an argument */ | ||
100 | if (*place) /* no white space */ | ||
101 | optarg = place; | ||
102 | else if (nargc <= ++optind) { /* no arg */ | ||
103 | place = EMSG; | ||
104 | if (*ostr == ':') | ||
105 | return (BADARG); | ||
106 | if (opterr) | ||
107 | (void)fprintf(stderr, | ||
108 | "%s: option requires an argument -- %c\n", | ||
109 | __progname, optopt); | ||
110 | return (BADCH); | ||
111 | } | ||
112 | else /* white space */ | ||
113 | optarg = nargv[optind]; | ||
114 | place = EMSG; | ||
115 | ++optind; | ||
116 | } | ||
117 | return (optopt); /* dump back option letter */ | ||
118 | } | ||
diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3 new file mode 100644 index 0000000000..bf5ba22ece --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.3 | |||
@@ -0,0 +1,459 @@ | |||
1 | .\" $OpenBSD: getopt_long.3,v 1.12 2005/10/11 01:23:41 jaredy Exp $ | ||
2 | .\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $ | ||
3 | .\" | ||
4 | .\" Copyright (c) 1988, 1991, 1993 | ||
5 | .\" The Regents of the University of California. All rights reserved. | ||
6 | .\" | ||
7 | .\" Redistribution and use in source and binary forms, with or without | ||
8 | .\" modification, are permitted provided that the following conditions | ||
9 | .\" are met: | ||
10 | .\" 1. Redistributions of source code must retain the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer. | ||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
13 | .\" notice, this list of conditions and the following disclaimer in the | ||
14 | .\" documentation and/or other materials provided with the distribution. | ||
15 | .\" 3. Neither the name of the University nor the names of its contributors | ||
16 | .\" may be used to endorse or promote products derived from this software | ||
17 | .\" without specific prior written permission. | ||
18 | .\" | ||
19 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
20 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
21 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
22 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
23 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
24 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
25 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
26 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
27 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
29 | .\" SUCH DAMAGE. | ||
30 | .\" | ||
31 | .\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 | ||
32 | .\" | ||
33 | .Dd April 1, 2000 | ||
34 | .Dt GETOPT_LONG 3 | ||
35 | .Os | ||
36 | .Sh NAME | ||
37 | .Nm getopt_long , | ||
38 | .Nm getopt_long_only | ||
39 | .Nd get long options from command line argument list | ||
40 | .Sh SYNOPSIS | ||
41 | .Fd #include <getopt.h> | ||
42 | .Vt extern char *optarg; | ||
43 | .Vt extern int optind; | ||
44 | .Vt extern int optopt; | ||
45 | .Vt extern int opterr; | ||
46 | .Vt extern int optreset; | ||
47 | .Ft int | ||
48 | .Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *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 | The | ||
97 | .Fn getopt_long | ||
98 | call requires a structure to be initialized describing the long | ||
99 | options. | ||
100 | The structure is: | ||
101 | .Bd -literal -offset indent | ||
102 | struct option { | ||
103 | char *name; | ||
104 | int has_arg; | ||
105 | int *flag; | ||
106 | int val; | ||
107 | }; | ||
108 | .Ed | ||
109 | .Pp | ||
110 | The | ||
111 | .Fa name | ||
112 | field should contain the option name without the leading double dash. | ||
113 | .Pp | ||
114 | The | ||
115 | .Fa has_arg | ||
116 | field should be one of: | ||
117 | .Pp | ||
118 | .Bl -tag -width "optional_argument" -compact -offset indent | ||
119 | .It Dv no_argument | ||
120 | no argument to the option is expected. | ||
121 | .It Dv required_argument | ||
122 | an argument to the option is required. | ||
123 | .It Dv optional_argument | ||
124 | an argument to the option may be presented. | ||
125 | .El | ||
126 | .Pp | ||
127 | If | ||
128 | .Fa flag | ||
129 | is not | ||
130 | .Dv NULL , | ||
131 | then the integer pointed to by it will be set to the value in the | ||
132 | .Fa val | ||
133 | field. | ||
134 | If the | ||
135 | .Fa flag | ||
136 | field is | ||
137 | .Dv NULL , | ||
138 | then the | ||
139 | .Fa val | ||
140 | field will be returned. | ||
141 | Setting | ||
142 | .Fa flag | ||
143 | to | ||
144 | .Dv NULL | ||
145 | and setting | ||
146 | .Fa val | ||
147 | to the corresponding short option will make this function act just | ||
148 | like | ||
149 | .Xr getopt 3 . | ||
150 | .Pp | ||
151 | If the | ||
152 | .Fa longindex | ||
153 | field is not | ||
154 | .Dv NULL , | ||
155 | then the integer pointed to by it will be set to the index of the long | ||
156 | option relative to | ||
157 | .Fa longopts . | ||
158 | .Pp | ||
159 | The last element of the | ||
160 | .Fa longopts | ||
161 | array has to be filled with zeroes. | ||
162 | .Pp | ||
163 | The | ||
164 | .Fn getopt_long_only | ||
165 | function behaves identically to | ||
166 | .Fn getopt_long | ||
167 | with the exception that long options may start with | ||
168 | .Sq - | ||
169 | in addition to | ||
170 | .Sq -- . | ||
171 | If an option starting with | ||
172 | .Sq - | ||
173 | does not match a long option but does match a single-character option, | ||
174 | the single-character option is returned. | ||
175 | .Sh RETURN VALUES | ||
176 | If the | ||
177 | .Fa flag | ||
178 | field in | ||
179 | .Li struct option | ||
180 | is | ||
181 | .Dv NULL , | ||
182 | .Fn getopt_long | ||
183 | and | ||
184 | .Fn getopt_long_only | ||
185 | return the value specified in the | ||
186 | .Fa val | ||
187 | field, which is usually just the corresponding short option. | ||
188 | If | ||
189 | .Fa flag | ||
190 | is not | ||
191 | .Dv NULL , | ||
192 | these functions return 0 and store | ||
193 | .Fa val | ||
194 | in the location pointed to by | ||
195 | .Fa flag . | ||
196 | These functions return | ||
197 | .Sq \: | ||
198 | if there was a missing option argument, | ||
199 | .Sq \&? | ||
200 | if the user specified an unknown or ambiguous option, and | ||
201 | \-1 when the argument list has been exhausted. | ||
202 | .Sh EXAMPLES | ||
203 | .Bd -literal -compact | ||
204 | int bflag, ch, fd; | ||
205 | int daggerset; | ||
206 | |||
207 | /* options descriptor */ | ||
208 | static struct option longopts[] = { | ||
209 | { "buffy", no_argument, NULL, 'b' }, | ||
210 | { "fluoride", required_argument, NULL, 'f' }, | ||
211 | { "daggerset", no_argument, &daggerset, 1 }, | ||
212 | { NULL, 0, NULL, 0 } | ||
213 | }; | ||
214 | |||
215 | bflag = 0; | ||
216 | while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) | ||
217 | switch (ch) { | ||
218 | case 'b': | ||
219 | bflag = 1; | ||
220 | break; | ||
221 | case 'f': | ||
222 | if ((fd = open(optarg, O_RDONLY, 0)) == -1) | ||
223 | err(1, "unable to open %s", optarg); | ||
224 | break; | ||
225 | case 0: | ||
226 | if (daggerset) | ||
227 | fprintf(stderr, "Buffy will use her dagger to " | ||
228 | "apply fluoride to dracula's teeth\en"); | ||
229 | break; | ||
230 | default: | ||
231 | usage(); | ||
232 | /* NOTREACHED */ | ||
233 | } | ||
234 | argc -= optind; | ||
235 | argv += optind; | ||
236 | .Ed | ||
237 | .Sh IMPLEMENTATION DIFFERENCES | ||
238 | This section describes differences to the GNU implementation | ||
239 | found in glibc-2.1.3: | ||
240 | .Bl -bullet | ||
241 | .It | ||
242 | handling of | ||
243 | .Ql - | ||
244 | as the first character of the option string in the presence of the | ||
245 | environment variable | ||
246 | .Ev POSIXLY_CORRECT : | ||
247 | .Bl -tag -width "OpenBSD" | ||
248 | .It GNU | ||
249 | ignores | ||
250 | .Ev POSIXLY_CORRECT | ||
251 | and returns non-options as arguments to option | ||
252 | .Ql \e1 . | ||
253 | .It OpenBSD | ||
254 | honors | ||
255 | .Ev POSIXLY_CORRECT | ||
256 | and stops at the first non-option. | ||
257 | .El | ||
258 | .It | ||
259 | handling of | ||
260 | .Ql - | ||
261 | within the option string (not the first character): | ||
262 | .Bl -tag -width "OpenBSD" | ||
263 | .It GNU | ||
264 | treats a | ||
265 | .Ql - | ||
266 | on the command line as a non-argument. | ||
267 | .It OpenBSD | ||
268 | a | ||
269 | .Ql - | ||
270 | within the option string matches a | ||
271 | .Ql - | ||
272 | (single dash) on the command line. | ||
273 | This functionality is provided for backward compatibility with | ||
274 | programs, such as | ||
275 | .Xr su 1 , | ||
276 | that use | ||
277 | .Ql - | ||
278 | as an option flag. | ||
279 | This practice is wrong, and should not be used in any current development. | ||
280 | .El | ||
281 | .It | ||
282 | handling of | ||
283 | .Ql :: | ||
284 | in the option string in the presence of | ||
285 | .Ev POSIXLY_CORRECT : | ||
286 | .Bl -tag -width "OpenBSD" | ||
287 | .It Both | ||
288 | GNU and | ||
289 | .Ox | ||
290 | ignore | ||
291 | .Ev POSIXLY_CORRECT | ||
292 | here and take | ||
293 | .Ql :: | ||
294 | to mean the preceding option takes an optional argument. | ||
295 | .El | ||
296 | .It | ||
297 | return value in case of missing argument if first character | ||
298 | (after | ||
299 | .Ql + | ||
300 | or | ||
301 | .Ql - ) | ||
302 | in the option string is not | ||
303 | .Ql \&: : | ||
304 | .Bl -tag -width "OpenBSD" | ||
305 | .It GNU | ||
306 | returns | ||
307 | .Ql \&? | ||
308 | .It OpenBSD | ||
309 | returns | ||
310 | .Ql \&: | ||
311 | (since | ||
312 | .Ox Ns 's | ||
313 | .Xr getopt 3 | ||
314 | does). | ||
315 | .El | ||
316 | .It | ||
317 | handling of | ||
318 | .Ql --a | ||
319 | in | ||
320 | .Xr getopt 3 : | ||
321 | .Bl -tag -width "OpenBSD" | ||
322 | .It GNU | ||
323 | parses this as option | ||
324 | .Ql - , | ||
325 | option | ||
326 | .Ql a . | ||
327 | .It OpenBSD | ||
328 | parses this as | ||
329 | .Ql -- , | ||
330 | and returns \-1 (ignoring the | ||
331 | .Ql a ) | ||
332 | (because the original | ||
333 | .Fn getopt | ||
334 | did.) | ||
335 | .El | ||
336 | .It | ||
337 | setting of | ||
338 | .Va optopt | ||
339 | for long options with | ||
340 | .Va flag | ||
341 | .No non- Ns Dv NULL : | ||
342 | .Bl -tag -width "OpenBSD" | ||
343 | .It GNU | ||
344 | sets | ||
345 | .Va optopt | ||
346 | to | ||
347 | .Va val . | ||
348 | .It OpenBSD | ||
349 | sets | ||
350 | .Va optopt | ||
351 | to 0 (since | ||
352 | .Va val | ||
353 | would never be returned). | ||
354 | .El | ||
355 | .It | ||
356 | handling of | ||
357 | .Ql -W | ||
358 | with | ||
359 | .Ql W; | ||
360 | in the option string in | ||
361 | .Xr getopt 3 | ||
362 | (not | ||
363 | .Fn getopt_long ) : | ||
364 | .Bl -tag -width "OpenBSD" | ||
365 | .It GNU | ||
366 | causes a segmentation fault. | ||
367 | .It OpenBSD | ||
368 | no special handling is done; | ||
369 | .Ql W; | ||
370 | is interpreted as two separate options, neither of which take an argument. | ||
371 | .El | ||
372 | .It | ||
373 | setting of | ||
374 | .Va optarg | ||
375 | for long options without an argument that are invoked via | ||
376 | .Ql -W | ||
377 | (with | ||
378 | .Ql W; | ||
379 | in the option string): | ||
380 | .Bl -tag -width "OpenBSD" | ||
381 | .It GNU | ||
382 | sets | ||
383 | .Va optarg | ||
384 | to the option name (the argument of | ||
385 | .Ql -W ) . | ||
386 | .It OpenBSD | ||
387 | sets | ||
388 | .Va optarg | ||
389 | to | ||
390 | .Dv NULL | ||
391 | (the argument of the long option). | ||
392 | .El | ||
393 | .It | ||
394 | handling of | ||
395 | .Ql -W | ||
396 | with an argument that is not (a prefix to) a known long option | ||
397 | (with | ||
398 | .Ql W; | ||
399 | in the option string): | ||
400 | .Bl -tag -width "OpenBSD" | ||
401 | .It GNU | ||
402 | returns | ||
403 | .Ql -W | ||
404 | with | ||
405 | .Va optarg | ||
406 | set to the unknown option. | ||
407 | .It OpenBSD | ||
408 | treats this as an error (unknown option) and returns | ||
409 | .Ql \&? | ||
410 | with | ||
411 | .Va optopt | ||
412 | set to 0 and | ||
413 | .Va optarg | ||
414 | set to | ||
415 | .Dv NULL | ||
416 | (as GNU's man page documents). | ||
417 | .El | ||
418 | .It | ||
419 | The error messages are different. | ||
420 | .It | ||
421 | .Ox | ||
422 | does not permute the argument vector at the same points in | ||
423 | the calling sequence as GNU does. | ||
424 | The aspects normally used by the caller | ||
425 | (ordering after \-1 is returned, value of | ||
426 | .Va optind | ||
427 | relative to current positions) are the same, though. | ||
428 | (We do fewer variable swaps.) | ||
429 | .El | ||
430 | .Sh ENVIRONMENT | ||
431 | .Bl -tag -width Ev | ||
432 | .It Ev POSIXLY_CORRECT | ||
433 | If set, option processing stops when the first non-option is found and | ||
434 | a leading | ||
435 | .Sq - | ||
436 | or | ||
437 | .Sq + | ||
438 | in the | ||
439 | .Ar optstring | ||
440 | is ignored. | ||
441 | .El | ||
442 | .Sh SEE ALSO | ||
443 | .Xr getopt 3 | ||
444 | .Sh HISTORY | ||
445 | The | ||
446 | .Fn getopt_long | ||
447 | and | ||
448 | .Fn getopt_long_only | ||
449 | functions first appeared in GNU libiberty. | ||
450 | This implementation first appeared in | ||
451 | .Ox 3.3 . | ||
452 | .Sh BUGS | ||
453 | The | ||
454 | .Ar argv | ||
455 | argument is not really | ||
456 | .Dv const | ||
457 | as its elements may be permuted (unless | ||
458 | .Ev POSIXLY_CORRECT | ||
459 | 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..41b6cd9f8d --- /dev/null +++ b/src/lib/libc/stdlib/getopt_long.c | |||
@@ -0,0 +1,533 @@ | |||
1 | /* $OpenBSD: getopt_long.c,v 1.20 2005/10/25 15:49:37 jmc Exp $ */ | ||
2 | /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ | ||
3 | |||
4 | /* | ||
5 | * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> | ||
6 | * | ||
7 | * Permission to use, copy, modify, and distribute this software for any | ||
8 | * purpose with or without fee is hereby granted, provided that the above | ||
9 | * copyright notice and this permission notice appear in all copies. | ||
10 | * | ||
11 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
12 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
13 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
14 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
15 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
16 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
17 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
18 | * | ||
19 | * Sponsored in part by the Defense Advanced Research Projects | ||
20 | * Agency (DARPA) and Air Force Research Laboratory, Air Force | ||
21 | * Materiel Command, USAF, under agreement number F39502-99-1-0512. | ||
22 | */ | ||
23 | /*- | ||
24 | * Copyright (c) 2000 The NetBSD Foundation, Inc. | ||
25 | * All rights reserved. | ||
26 | * | ||
27 | * This code is derived from software contributed to The NetBSD Foundation | ||
28 | * by Dieter Baron and Thomas Klausner. | ||
29 | * | ||
30 | * Redistribution and use in source and binary forms, with or without | ||
31 | * modification, are permitted provided that the following conditions | ||
32 | * are met: | ||
33 | * 1. Redistributions of source code must retain the above copyright | ||
34 | * notice, this list of conditions and the following disclaimer. | ||
35 | * 2. Redistributions in binary form must reproduce the above copyright | ||
36 | * notice, this list of conditions and the following disclaimer in the | ||
37 | * documentation and/or other materials provided with the distribution. | ||
38 | * 3. All advertising materials mentioning features or use of this software | ||
39 | * must display the following acknowledgement: | ||
40 | * This product includes software developed by the NetBSD | ||
41 | * Foundation, Inc. and its contributors. | ||
42 | * 4. Neither the name of The NetBSD Foundation nor the names of its | ||
43 | * contributors may be used to endorse or promote products derived | ||
44 | * from this software without specific prior written permission. | ||
45 | * | ||
46 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
47 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
48 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
49 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
50 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
51 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
52 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
53 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
54 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
55 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
56 | * POSSIBILITY OF SUCH DAMAGE. | ||
57 | */ | ||
58 | |||
59 | #include <err.h> | ||
60 | #include <errno.h> | ||
61 | #include <getopt.h> | ||
62 | #include <stdlib.h> | ||
63 | #include <string.h> | ||
64 | |||
65 | #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ | ||
66 | |||
67 | #ifdef REPLACE_GETOPT | ||
68 | int opterr = 1; /* if error message should be printed */ | ||
69 | int optind = 1; /* index into parent argv vector */ | ||
70 | int optopt = '?'; /* character checked for validity */ | ||
71 | int optreset; /* reset getopt */ | ||
72 | char *optarg; /* argument associated with option */ | ||
73 | #endif | ||
74 | |||
75 | #define PRINT_ERROR ((opterr) && (*options != ':')) | ||
76 | |||
77 | #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ | ||
78 | #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ | ||
79 | #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ | ||
80 | |||
81 | /* return values */ | ||
82 | #define BADCH (int)'?' | ||
83 | #define BADARG ((*options == ':') ? (int)':' : (int)'?') | ||
84 | #define INORDER (int)1 | ||
85 | |||
86 | #define EMSG "" | ||
87 | |||
88 | static int getopt_internal(int, char * const *, const char *, | ||
89 | const struct option *, int *, int); | ||
90 | static int parse_long_options(char * const *, const char *, | ||
91 | const struct option *, int *, int); | ||
92 | static int gcd(int, int); | ||
93 | static void permute_args(int, int, int, char * const *); | ||
94 | |||
95 | static char *place = EMSG; /* option letter processing */ | ||
96 | |||
97 | /* XXX: set optreset to 1 rather than these two */ | ||
98 | static int nonopt_start = -1; /* first non option argument (for permute) */ | ||
99 | static int nonopt_end = -1; /* first option after non options (for permute) */ | ||
100 | |||
101 | /* Error messages */ | ||
102 | static const char recargchar[] = "option requires an argument -- %c"; | ||
103 | static const char recargstring[] = "option requires an argument -- %s"; | ||
104 | static const char ambig[] = "ambiguous option -- %.*s"; | ||
105 | static const char noarg[] = "option doesn't take an argument -- %.*s"; | ||
106 | static const char illoptchar[] = "unknown option -- %c"; | ||
107 | static const char illoptstring[] = "unknown option -- %s"; | ||
108 | |||
109 | /* | ||
110 | * Compute the greatest common divisor of a and b. | ||
111 | */ | ||
112 | static int | ||
113 | gcd(int a, int b) | ||
114 | { | ||
115 | int c; | ||
116 | |||
117 | c = a % b; | ||
118 | while (c != 0) { | ||
119 | a = b; | ||
120 | b = c; | ||
121 | c = a % b; | ||
122 | } | ||
123 | |||
124 | return (b); | ||
125 | } | ||
126 | |||
127 | /* | ||
128 | * Exchange the block from nonopt_start to nonopt_end with the block | ||
129 | * from nonopt_end to opt_end (keeping the same order of arguments | ||
130 | * in each block). | ||
131 | */ | ||
132 | static void | ||
133 | permute_args(int panonopt_start, int panonopt_end, int opt_end, | ||
134 | char * const *nargv) | ||
135 | { | ||
136 | int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; | ||
137 | char *swap; | ||
138 | |||
139 | /* | ||
140 | * compute lengths of blocks and number and size of cycles | ||
141 | */ | ||
142 | nnonopts = panonopt_end - panonopt_start; | ||
143 | nopts = opt_end - panonopt_end; | ||
144 | ncycle = gcd(nnonopts, nopts); | ||
145 | cyclelen = (opt_end - panonopt_start) / ncycle; | ||
146 | |||
147 | for (i = 0; i < ncycle; i++) { | ||
148 | cstart = panonopt_end+i; | ||
149 | pos = cstart; | ||
150 | for (j = 0; j < cyclelen; j++) { | ||
151 | if (pos >= panonopt_end) | ||
152 | pos -= nnonopts; | ||
153 | else | ||
154 | pos += nopts; | ||
155 | swap = nargv[pos]; | ||
156 | /* LINTED const cast */ | ||
157 | ((char **) nargv)[pos] = nargv[cstart]; | ||
158 | /* LINTED const cast */ | ||
159 | ((char **)nargv)[cstart] = swap; | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | |||
164 | /* | ||
165 | * parse_long_options -- | ||
166 | * Parse long options in argc/argv argument vector. | ||
167 | * Returns -1 if short_too is set and the option does not match long_options. | ||
168 | */ | ||
169 | static int | ||
170 | parse_long_options(char * const *nargv, const char *options, | ||
171 | const struct option *long_options, int *idx, int short_too) | ||
172 | { | ||
173 | char *current_argv, *has_equal; | ||
174 | size_t current_argv_len; | ||
175 | int i, match; | ||
176 | |||
177 | current_argv = place; | ||
178 | match = -1; | ||
179 | |||
180 | optind++; | ||
181 | |||
182 | if ((has_equal = strchr(current_argv, '=')) != NULL) { | ||
183 | /* argument found (--option=arg) */ | ||
184 | current_argv_len = has_equal - current_argv; | ||
185 | has_equal++; | ||
186 | } else | ||
187 | current_argv_len = strlen(current_argv); | ||
188 | |||
189 | for (i = 0; long_options[i].name; i++) { | ||
190 | /* find matching long option */ | ||
191 | if (strncmp(current_argv, long_options[i].name, | ||
192 | current_argv_len)) | ||
193 | continue; | ||
194 | |||
195 | if (strlen(long_options[i].name) == current_argv_len) { | ||
196 | /* exact match */ | ||
197 | match = i; | ||
198 | break; | ||
199 | } | ||
200 | /* | ||
201 | * If this is a known short option, don't allow | ||
202 | * a partial match of a single character. | ||
203 | */ | ||
204 | if (short_too && current_argv_len == 1) | ||
205 | continue; | ||
206 | |||
207 | if (match == -1) /* partial match */ | ||
208 | match = i; | ||
209 | else { | ||
210 | /* ambiguous abbreviation */ | ||
211 | if (PRINT_ERROR) | ||
212 | warnx(ambig, (int)current_argv_len, | ||
213 | current_argv); | ||
214 | optopt = 0; | ||
215 | return (BADCH); | ||
216 | } | ||
217 | } | ||
218 | if (match != -1) { /* option found */ | ||
219 | if (long_options[match].has_arg == no_argument | ||
220 | && has_equal) { | ||
221 | if (PRINT_ERROR) | ||
222 | warnx(noarg, (int)current_argv_len, | ||
223 | current_argv); | ||
224 | /* | ||
225 | * XXX: GNU sets optopt to val regardless of flag | ||
226 | */ | ||
227 | if (long_options[match].flag == NULL) | ||
228 | optopt = long_options[match].val; | ||
229 | else | ||
230 | optopt = 0; | ||
231 | return (BADARG); | ||
232 | } | ||
233 | if (long_options[match].has_arg == required_argument || | ||
234 | long_options[match].has_arg == optional_argument) { | ||
235 | if (has_equal) | ||
236 | optarg = has_equal; | ||
237 | else if (long_options[match].has_arg == | ||
238 | required_argument) { | ||
239 | /* | ||
240 | * optional argument doesn't use next nargv | ||
241 | */ | ||
242 | optarg = nargv[optind++]; | ||
243 | } | ||
244 | } | ||
245 | if ((long_options[match].has_arg == required_argument) | ||
246 | && (optarg == NULL)) { | ||
247 | /* | ||
248 | * Missing argument; leading ':' indicates no error | ||
249 | * should be generated. | ||
250 | */ | ||
251 | if (PRINT_ERROR) | ||
252 | warnx(recargstring, | ||
253 | current_argv); | ||
254 | /* | ||
255 | * XXX: GNU sets optopt to val regardless of flag | ||
256 | */ | ||
257 | if (long_options[match].flag == NULL) | ||
258 | optopt = long_options[match].val; | ||
259 | else | ||
260 | optopt = 0; | ||
261 | --optind; | ||
262 | return (BADARG); | ||
263 | } | ||
264 | } else { /* unknown option */ | ||
265 | if (short_too) { | ||
266 | --optind; | ||
267 | return (-1); | ||
268 | } | ||
269 | if (PRINT_ERROR) | ||
270 | warnx(illoptstring, current_argv); | ||
271 | optopt = 0; | ||
272 | return (BADCH); | ||
273 | } | ||
274 | if (idx) | ||
275 | *idx = match; | ||
276 | if (long_options[match].flag) { | ||
277 | *long_options[match].flag = long_options[match].val; | ||
278 | return (0); | ||
279 | } else | ||
280 | return (long_options[match].val); | ||
281 | } | ||
282 | |||
283 | /* | ||
284 | * getopt_internal -- | ||
285 | * Parse argc/argv argument vector. Called by user level routines. | ||
286 | */ | ||
287 | static int | ||
288 | getopt_internal(int nargc, char * const *nargv, const char *options, | ||
289 | const struct option *long_options, int *idx, int flags) | ||
290 | { | ||
291 | char *oli; /* option letter list index */ | ||
292 | int optchar, short_too; | ||
293 | static int posixly_correct = -1; | ||
294 | |||
295 | if (options == NULL) | ||
296 | return (-1); | ||
297 | |||
298 | /* | ||
299 | * Disable GNU extensions if POSIXLY_CORRECT is set or options | ||
300 | * string begins with a '+'. | ||
301 | */ | ||
302 | if (posixly_correct == -1) | ||
303 | posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); | ||
304 | if (posixly_correct || *options == '+') | ||
305 | flags &= ~FLAG_PERMUTE; | ||
306 | else if (*options == '-') | ||
307 | flags |= FLAG_ALLARGS; | ||
308 | if (*options == '+' || *options == '-') | ||
309 | options++; | ||
310 | |||
311 | /* | ||
312 | * XXX Some GNU programs (like cvs) set optind to 0 instead of | ||
313 | * XXX using optreset. Work around this braindamage. | ||
314 | */ | ||
315 | if (optind == 0) | ||
316 | optind = optreset = 1; | ||
317 | |||
318 | optarg = NULL; | ||
319 | if (optreset) | ||
320 | nonopt_start = nonopt_end = -1; | ||
321 | start: | ||
322 | if (optreset || !*place) { /* update scanning pointer */ | ||
323 | optreset = 0; | ||
324 | if (optind >= nargc) { /* end of argument vector */ | ||
325 | place = EMSG; | ||
326 | if (nonopt_end != -1) { | ||
327 | /* do permutation, if we have to */ | ||
328 | permute_args(nonopt_start, nonopt_end, | ||
329 | optind, nargv); | ||
330 | optind -= nonopt_end - nonopt_start; | ||
331 | } | ||
332 | else if (nonopt_start != -1) { | ||
333 | /* | ||
334 | * If we skipped non-options, set optind | ||
335 | * to the first of them. | ||
336 | */ | ||
337 | optind = nonopt_start; | ||
338 | } | ||
339 | nonopt_start = nonopt_end = -1; | ||
340 | return (-1); | ||
341 | } | ||
342 | if (*(place = nargv[optind]) != '-' || | ||
343 | (place[1] == '\0' && strchr(options, '-') == NULL)) { | ||
344 | place = EMSG; /* found non-option */ | ||
345 | if (flags & FLAG_ALLARGS) { | ||
346 | /* | ||
347 | * GNU extension: | ||
348 | * return non-option as argument to option 1 | ||
349 | */ | ||
350 | optarg = nargv[optind++]; | ||
351 | return (INORDER); | ||
352 | } | ||
353 | if (!(flags & FLAG_PERMUTE)) { | ||
354 | /* | ||
355 | * If no permutation wanted, stop parsing | ||
356 | * at first non-option. | ||
357 | */ | ||
358 | return (-1); | ||
359 | } | ||
360 | /* do permutation */ | ||
361 | if (nonopt_start == -1) | ||
362 | nonopt_start = optind; | ||
363 | else if (nonopt_end != -1) { | ||
364 | permute_args(nonopt_start, nonopt_end, | ||
365 | optind, nargv); | ||
366 | nonopt_start = optind - | ||
367 | (nonopt_end - nonopt_start); | ||
368 | nonopt_end = -1; | ||
369 | } | ||
370 | optind++; | ||
371 | /* process next argument */ | ||
372 | goto start; | ||
373 | } | ||
374 | if (nonopt_start != -1 && nonopt_end == -1) | ||
375 | nonopt_end = optind; | ||
376 | |||
377 | /* | ||
378 | * If we have "-" do nothing, if "--" we are done. | ||
379 | */ | ||
380 | if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { | ||
381 | optind++; | ||
382 | place = EMSG; | ||
383 | /* | ||
384 | * We found an option (--), so if we skipped | ||
385 | * non-options, we have to permute. | ||
386 | */ | ||
387 | if (nonopt_end != -1) { | ||
388 | permute_args(nonopt_start, nonopt_end, | ||
389 | optind, nargv); | ||
390 | optind -= nonopt_end - nonopt_start; | ||
391 | } | ||
392 | nonopt_start = nonopt_end = -1; | ||
393 | return (-1); | ||
394 | } | ||
395 | } | ||
396 | |||
397 | /* | ||
398 | * Check long options if: | ||
399 | * 1) we were passed some | ||
400 | * 2) the arg is not just "-" | ||
401 | * 3) either the arg starts with -- we are getopt_long_only() | ||
402 | */ | ||
403 | if (long_options != NULL && place != nargv[optind] && | ||
404 | (*place == '-' || (flags & FLAG_LONGONLY))) { | ||
405 | short_too = 0; | ||
406 | if (*place == '-') | ||
407 | place++; /* --foo long option */ | ||
408 | else if (*place != ':' && strchr(options, *place) != NULL) | ||
409 | short_too = 1; /* could be short option too */ | ||
410 | |||
411 | optchar = parse_long_options(nargv, options, long_options, | ||
412 | idx, short_too); | ||
413 | if (optchar != -1) { | ||
414 | place = EMSG; | ||
415 | return (optchar); | ||
416 | } | ||
417 | } | ||
418 | |||
419 | if ((optchar = (int)*place++) == (int)':' || | ||
420 | optchar == (int)'-' && *place != '\0' || | ||
421 | (oli = strchr(options, optchar)) == NULL) { | ||
422 | /* | ||
423 | * If the user specified "-" and '-' isn't listed in | ||
424 | * options, return -1 (non-option) as per POSIX. | ||
425 | * Otherwise, it is an unknown option character (or ':'). | ||
426 | */ | ||
427 | if (optchar == (int)'-' && *place == '\0') | ||
428 | return (-1); | ||
429 | if (!*place) | ||
430 | ++optind; | ||
431 | if (PRINT_ERROR) | ||
432 | warnx(illoptchar, optchar); | ||
433 | optopt = optchar; | ||
434 | return (BADCH); | ||
435 | } | ||
436 | if (long_options != NULL && optchar == 'W' && oli[1] == ';') { | ||
437 | /* -W long-option */ | ||
438 | if (*place) /* no space */ | ||
439 | /* NOTHING */; | ||
440 | else if (++optind >= nargc) { /* no arg */ | ||
441 | place = EMSG; | ||
442 | if (PRINT_ERROR) | ||
443 | warnx(recargchar, optchar); | ||
444 | optopt = optchar; | ||
445 | return (BADARG); | ||
446 | } else /* white space */ | ||
447 | place = nargv[optind]; | ||
448 | optchar = parse_long_options(nargv, options, long_options, | ||
449 | idx, 0); | ||
450 | place = EMSG; | ||
451 | return (optchar); | ||
452 | } | ||
453 | if (*++oli != ':') { /* doesn't take argument */ | ||
454 | if (!*place) | ||
455 | ++optind; | ||
456 | } else { /* takes (optional) argument */ | ||
457 | optarg = NULL; | ||
458 | if (*place) /* no white space */ | ||
459 | optarg = place; | ||
460 | /* XXX: disable test for :: if PC? (GNU doesn't) */ | ||
461 | else if (oli[1] != ':') { /* arg not optional */ | ||
462 | if (++optind >= nargc) { /* no arg */ | ||
463 | place = EMSG; | ||
464 | if (PRINT_ERROR) | ||
465 | warnx(recargchar, optchar); | ||
466 | optopt = optchar; | ||
467 | return (BADARG); | ||
468 | } else | ||
469 | optarg = nargv[optind]; | ||
470 | } else if (!(flags & FLAG_PERMUTE)) { | ||
471 | /* | ||
472 | * If permutation is disabled, we can accept an | ||
473 | * optional arg separated by whitespace so long | ||
474 | * as it does not start with a dash (-). | ||
475 | */ | ||
476 | if (optind + 1 < nargc && *nargv[optind + 1] != '-') | ||
477 | optarg = nargv[++optind]; | ||
478 | } | ||
479 | place = EMSG; | ||
480 | ++optind; | ||
481 | } | ||
482 | /* dump back option letter */ | ||
483 | return (optchar); | ||
484 | } | ||
485 | |||
486 | #ifdef REPLACE_GETOPT | ||
487 | /* | ||
488 | * getopt -- | ||
489 | * Parse argc/argv argument vector. | ||
490 | * | ||
491 | * [eventually this will replace the BSD getopt] | ||
492 | */ | ||
493 | int | ||
494 | getopt(int nargc, char * const *nargv, const char *options) | ||
495 | { | ||
496 | |||
497 | /* | ||
498 | * We don't pass FLAG_PERMUTE to getopt_internal() since | ||
499 | * the BSD getopt(3) (unlike GNU) has never done this. | ||
500 | * | ||
501 | * Furthermore, since many privileged programs call getopt() | ||
502 | * before dropping privileges it makes sense to keep things | ||
503 | * as simple (and bug-free) as possible. | ||
504 | */ | ||
505 | return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); | ||
506 | } | ||
507 | #endif /* REPLACE_GETOPT */ | ||
508 | |||
509 | /* | ||
510 | * getopt_long -- | ||
511 | * Parse argc/argv argument vector. | ||
512 | */ | ||
513 | int | ||
514 | getopt_long(int nargc, char * const *nargv, const char *options, | ||
515 | const struct option *long_options, int *idx) | ||
516 | { | ||
517 | |||
518 | return (getopt_internal(nargc, nargv, options, long_options, idx, | ||
519 | FLAG_PERMUTE)); | ||
520 | } | ||
521 | |||
522 | /* | ||
523 | * getopt_long_only -- | ||
524 | * Parse argc/argv argument vector. | ||
525 | */ | ||
526 | int | ||
527 | getopt_long_only(int nargc, char * const *nargv, const char *options, | ||
528 | const struct option *long_options, int *idx) | ||
529 | { | ||
530 | |||
531 | return (getopt_internal(nargc, nargv, options, long_options, idx, | ||
532 | FLAG_PERMUTE|FLAG_LONGONLY)); | ||
533 | } | ||
diff --git a/src/lib/libc/stdlib/getsubopt.3 b/src/lib/libc/stdlib/getsubopt.3 new file mode 100644 index 0000000000..6a7c28ff4d --- /dev/null +++ b/src/lib/libc/stdlib/getsubopt.3 | |||
@@ -0,0 +1,145 @@ | |||
1 | .\" $OpenBSD: getsubopt.3,v 1.11 2005/10/11 01:23:41 jaredy Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1990, 1991, 1993 | ||
4 | .\" The Regents of the University of California. All rights reserved. | ||
5 | .\" | ||
6 | .\" Redistribution and use in source and binary forms, with or without | ||
7 | .\" modification, are permitted provided that the following conditions | ||
8 | .\" are met: | ||
9 | .\" 1. Redistributions of source code must retain the above copyright | ||
10 | .\" notice, this list of conditions and the following disclaimer. | ||
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
12 | .\" notice, this list of conditions and the following disclaimer in the | ||
13 | .\" documentation and/or other materials provided with the distribution. | ||
14 | .\" 3. Neither the name of the University nor the names of its contributors | ||
15 | .\" may be used to endorse or promote products derived from this software | ||
16 | .\" without specific prior written permission. | ||
17 | .\" | ||
18 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | .\" SUCH DAMAGE. | ||
29 | .\" | ||
30 | .\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93 | ||
31 | .\" | ||
32 | .Dd June 9, 1993 | ||
33 | .Dt GETSUBOPT 3 | ||
34 | .Os | ||
35 | .Sh NAME | ||
36 | .Nm getsubopt | ||
37 | .Nd get sub options from an argument | ||
38 | .Sh SYNOPSIS | ||
39 | .Fd #include <stdlib.h> | ||
40 | .Vt extern char *suboptarg; | ||
41 | .Ft int | ||
42 | .Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep" | ||
43 | .Sh DESCRIPTION | ||
44 | The | ||
45 | .Fn getsubopt | ||
46 | function parses a string containing tokens delimited by one or more | ||
47 | tab, space, or comma | ||
48 | .Pq Ql \&, | ||
49 | characters. | ||
50 | It is intended for use in parsing groups of option arguments provided | ||
51 | as part of a utility command line. | ||
52 | .Pp | ||
53 | The argument | ||
54 | .Fa optionp | ||
55 | is a pointer to a pointer to the string. | ||
56 | The argument | ||
57 | .Fa tokens | ||
58 | is a pointer to a null-terminated array of pointers to strings. | ||
59 | .Pp | ||
60 | The | ||
61 | .Fn getsubopt | ||
62 | function returns the zero-based offset of the pointer in the | ||
63 | .Fa tokens | ||
64 | array referencing a string which matches the first token | ||
65 | in the string, or \-1 if the string contains no tokens or | ||
66 | .Fa tokens | ||
67 | does not contain a matching string. | ||
68 | .Pp | ||
69 | If the token is of the form | ||
70 | .Ar name Ns = 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..66b1dd126d --- /dev/null +++ b/src/lib/libc/stdlib/hcreate.3 | |||
@@ -0,0 +1,191 @@ | |||
1 | .\" $OpenBSD: hcreate.3,v 1.3 2006/01/30 19:50:41 jmc 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 | .\" 3. All advertising materials mentioning features or use of this software | ||
19 | .\" must display the following acknowledgement: | ||
20 | .\" This product includes software developed by the NetBSD | ||
21 | .\" Foundation, Inc. and its contributors. | ||
22 | .\" 4. Neither the name of The NetBSD Foundation nor the names of its | ||
23 | .\" contributors may be used to endorse or promote products derived | ||
24 | .\" from this software without specific prior written permission. | ||
25 | .\" | ||
26 | .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS | ||
27 | .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | ||
28 | .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
29 | .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS | ||
30 | .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
31 | .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
32 | .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
33 | .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
34 | .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
35 | .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
36 | .\" POSSIBILITY OF SUCH DAMAGE. | ||
37 | .\" | ||
38 | .Dd February 13, 2001 | ||
39 | .Dt HCREATE 3 | ||
40 | .Os | ||
41 | .Sh NAME | ||
42 | .Nm hcreate , | ||
43 | .Nm hdestroy , | ||
44 | .Nm hsearch | ||
45 | .Nd manage hash search table | ||
46 | .Sh SYNOPSIS | ||
47 | .In search.h | ||
48 | .Ft int | ||
49 | .Fn hcreate "size_t nel" | ||
50 | .Ft void | ||
51 | .Fn hdestroy "void" | ||
52 | .Ft ENTRY * | ||
53 | .Fn hsearch "ENTRY item" "ACTION action" | ||
54 | .Sh DESCRIPTION | ||
55 | The | ||
56 | .Fn hcreate , | ||
57 | .Fn hdestroy , | ||
58 | and | ||
59 | .Fn hsearch | ||
60 | functions manage hash search tables. | ||
61 | .Pp | ||
62 | The | ||
63 | .Fn hcreate | ||
64 | function allocates and initializes the table. | ||
65 | The | ||
66 | .Fa nel | ||
67 | argument specifies an estimate of the maximum number of entries to be held | ||
68 | by the table. | ||
69 | Unless further memory allocation fails, supplying an insufficient | ||
70 | .Fa nel | ||
71 | value will not result in functional harm, although a performance degradation | ||
72 | may occur. | ||
73 | Initialization using the | ||
74 | .Fn hcreate | ||
75 | function is mandatory prior to any access operations using | ||
76 | .Fn hsearch . | ||
77 | .Pp | ||
78 | The | ||
79 | .Fn hdestroy | ||
80 | function destroys a table previously created using | ||
81 | .Fn hcreate . | ||
82 | After a call to | ||
83 | .Fn hdestroy , | ||
84 | the data can no longer be accessed. | ||
85 | .Pp | ||
86 | The | ||
87 | .Fn hsearch | ||
88 | function is used to search to the hash table. | ||
89 | It returns a pointer into the | ||
90 | hash table indicating the address of an item. | ||
91 | The | ||
92 | .Fa item | ||
93 | argument is of type | ||
94 | .Dv ENTRY , | ||
95 | a structural type which contains the following members: | ||
96 | .Pp | ||
97 | .Bl -tag -compact -offset indent -width voidX*dataXX | ||
98 | .It Fa char *key | ||
99 | comparison key. | ||
100 | .It Fa void *data | ||
101 | pointer to data associated with | ||
102 | .Fa key . | ||
103 | .El | ||
104 | .Pp | ||
105 | The key comparison function used by | ||
106 | .Fn hsearch | ||
107 | is | ||
108 | .Xr strcmp 3 . | ||
109 | .Pp | ||
110 | The | ||
111 | .Fa action | ||
112 | argument is of type | ||
113 | .Dv ACTION , | ||
114 | an enumeration type which defines the following values: | ||
115 | .Bl -tag -offset indent -width ENTERXX | ||
116 | .It Dv ENTER | ||
117 | Insert | ||
118 | .Fa item | ||
119 | into the hash table. | ||
120 | If an existing item with the same key is found, it is not replaced. | ||
121 | Note that the | ||
122 | .Fa key | ||
123 | and | ||
124 | .Fa data | ||
125 | elements of | ||
126 | .Fa item | ||
127 | are used directly by the new table entry. | ||
128 | The storage for the | ||
129 | key must not be modified during the lifetime of the hash table. | ||
130 | .It Dv FIND | ||
131 | Search the hash table without inserting | ||
132 | .Fa item . | ||
133 | .El | ||
134 | .Sh RETURN VALUES | ||
135 | If successful, the | ||
136 | .Fn hcreate | ||
137 | function returns a non-zero value. | ||
138 | Otherwise, a value of 0 is returned and | ||
139 | .Va errno | ||
140 | is set to indicate the error. | ||
141 | .Pp | ||
142 | The | ||
143 | .Fn hdestroy | ||
144 | functions | ||
145 | returns no value. | ||
146 | .Pp | ||
147 | If successful, the | ||
148 | .Fn hsearch | ||
149 | function returns a pointer to a hash table entry matching | ||
150 | the provided key. | ||
151 | If the action is | ||
152 | .Dv FIND | ||
153 | and the item was not found, or if the action is | ||
154 | .Dv ENTER | ||
155 | and the insertion failed, | ||
156 | .Dv NULL | ||
157 | is returned and | ||
158 | .Va errno | ||
159 | is set to indicate the error. | ||
160 | If the action is | ||
161 | .Dv ENTER | ||
162 | and an entry already existed in the table matching the given | ||
163 | key, the existing entry is returned and is not replaced. | ||
164 | .Sh ERRORS | ||
165 | The | ||
166 | .Fn hcreate | ||
167 | and | ||
168 | .Fn hsearch | ||
169 | functions will fail if: | ||
170 | .Bl -tag -width Er | ||
171 | .It Bq Er ENOMEM | ||
172 | Insufficient memory is available. | ||
173 | .El | ||
174 | .Sh SEE ALSO | ||
175 | .Xr bsearch 3 , | ||
176 | .Xr lsearch 3 , | ||
177 | .Xr malloc 3 , | ||
178 | .Xr strcmp 3 | ||
179 | .Sh STANDARDS | ||
180 | These functions conform to | ||
181 | .St -p1003.1-2004 . | ||
182 | .Sh HISTORY | ||
183 | The | ||
184 | .Fn hcreate , | ||
185 | .Fn hdestroy | ||
186 | and | ||
187 | .Fn hsearch | ||
188 | functions first appeared in | ||
189 | .At V . | ||
190 | .Sh BUGS | ||
191 | 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..f8df1bcd7c --- /dev/null +++ b/src/lib/libc/stdlib/hcreate.c | |||
@@ -0,0 +1,191 @@ | |||
1 | /* $OpenBSD: hcreate.c,v 1.3 2005/10/10 17:37:44 espie 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 = malloc(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 index bd998fa357..e6fb61ba2f 100644 --- a/src/lib/libc/stdlib/heapsort.c +++ b/src/lib/libc/stdlib/heapsort.c | |||
@@ -13,11 +13,7 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 13 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 14 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 15 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 16 | * 3. Neither the name of the University nor the names of its contributors |
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | 17 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. | 18 | * without specific prior written permission. |
23 | * | 19 | * |
@@ -34,11 +30,6 @@ | |||
34 | * SUCH DAMAGE. | 30 | * SUCH DAMAGE. |
35 | */ | 31 | */ |
36 | 32 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char sccsid[] = "from: @(#)heapsort.c 8.1 (Berkeley) 6/4/93";*/ | ||
39 | static char *rcsid = "$Id: heapsort.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | #include <sys/types.h> | 33 | #include <sys/types.h> |
43 | #include <errno.h> | 34 | #include <errno.h> |
44 | #include <stdlib.h> | 35 | #include <stdlib.h> |
@@ -73,7 +64,7 @@ static char *rcsid = "$Id: heapsort.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp | |||
73 | * Build the list into a heap, where a heap is defined such that for | 64 | * Build the list into a heap, where a heap is defined such that for |
74 | * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N. | 65 | * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N. |
75 | * | 66 | * |
76 | * There two cases. If j == nmemb, select largest of Ki and Kj. If | 67 | * There are two cases. If j == nmemb, select largest of Ki and Kj. If |
77 | * j < nmemb, select largest of Ki, Kj and Kj+1. | 68 | * j < nmemb, select largest of Ki, Kj and Kj+1. |
78 | */ | 69 | */ |
79 | #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ | 70 | #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ |
@@ -95,12 +86,12 @@ static char *rcsid = "$Id: heapsort.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp | |||
95 | * Select the top of the heap and 'heapify'. Since by far the most expensive | 86 | * Select the top of the heap and 'heapify'. Since by far the most expensive |
96 | * action is the call to the compar function, a considerable optimization | 87 | * action is the call to the compar function, a considerable optimization |
97 | * in the average case can be achieved due to the fact that k, the displaced | 88 | * in the average case can be achieved due to the fact that k, the displaced |
98 | * elememt, is ususally quite small, so it would be preferable to first | 89 | * element, is usually quite small, so it would be preferable to first |
99 | * heapify, always maintaining the invariant that the larger child is copied | 90 | * heapify, always maintaining the invariant that the larger child is copied |
100 | * over its parent's record. | 91 | * over its parent's record. |
101 | * | 92 | * |
102 | * Then, starting from the *bottom* of the heap, finding k's correct place, | 93 | * Then, starting from the *bottom* of the heap, finding k's correct place, |
103 | * again maintianing the invariant. As a result of the invariant no element | 94 | * again maintaining the invariant. As a result of the invariant no element |
104 | * is 'lost' when k is assigned its correct place in the heap. | 95 | * is 'lost' when k is assigned its correct place in the heap. |
105 | * | 96 | * |
106 | * The time savings from this optimization are on the order of 15-20% for the | 97 | * The time savings from this optimization are on the order of 15-20% for the |
@@ -139,13 +130,11 @@ static char *rcsid = "$Id: heapsort.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp | |||
139 | * only advantage over quicksort is that it requires little additional memory. | 130 | * only advantage over quicksort is that it requires little additional memory. |
140 | */ | 131 | */ |
141 | int | 132 | int |
142 | heapsort(vbase, nmemb, size, compar) | 133 | heapsort(void *vbase, size_t nmemb, size_t size, |
143 | void *vbase; | 134 | int (*compar)(const void *, const void *)) |
144 | size_t nmemb, size; | ||
145 | int (*compar) __P((const void *, const void *)); | ||
146 | { | 135 | { |
147 | register int cnt, i, j, l; | 136 | int cnt, i, j, l; |
148 | register char tmp, *tmp1, *tmp2; | 137 | char tmp, *tmp1, *tmp2; |
149 | char *base, *k, *p, *t; | 138 | char *base, *k, *p, *t; |
150 | 139 | ||
151 | if (nmemb <= 1) | 140 | if (nmemb <= 1) |
diff --git a/src/lib/libc/stdlib/free.3 b/src/lib/libc/stdlib/imaxabs.3 index 3d0131d7de..5384d0c9a6 100644 --- a/src/lib/libc/stdlib/free.3 +++ b/src/lib/libc/stdlib/imaxabs.3 | |||
@@ -1,4 +1,4 @@ | |||
1 | .\" Copyright (c) 1991 The Regents of the University of California. | 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. |
2 | .\" All rights reserved. | 2 | .\" All rights reserved. |
3 | .\" | 3 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | 4 | .\" This code is derived from software contributed to Berkeley by |
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,50 +29,38 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)free.3 5.2 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: imaxabs.3,v 1.1 2006/01/13 17:58:09 millert Exp $ |
37 | .\" $Id: free.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd January 6, 2005 |
40 | .Dt FREE 3 | 35 | .Dt IMAXABS 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
43 | .Nm free | 38 | .Nm imaxabs |
44 | .Nd free up memory allocated with malloc, calloc or realloc | 39 | .Nd integer absolute value function |
45 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 41 | .Fd #include <inttypes.h> |
47 | .Ft void | 42 | .Ft int |
48 | .Fn free "void *ptr" | 43 | .Fn imaxabs "int j" |
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn free | 46 | .Fn imaxabs |
52 | function causes the space pointed to by | 47 | function computes the absolute value of the intmax_t variable |
53 | .Fa ptr | 48 | .Fa j . |
54 | to be deallocated, that is, made available | ||
55 | for further allocation. | ||
56 | If | ||
57 | .Fa ptr | ||
58 | is a null pointer, no action occurs. | ||
59 | Otherwise, if the argument does not match a pointer earlier | ||
60 | returned by the | ||
61 | .Xr calloc , | ||
62 | .Xr malloc , | ||
63 | or | ||
64 | .Xr realloc | ||
65 | function, or if the space has been deallocated by a call to | ||
66 | .Fn free | ||
67 | or | ||
68 | .Xr realloc , | ||
69 | general havoc may occur. | ||
70 | .Sh RETURN VALUES | 49 | .Sh RETURN VALUES |
71 | The | 50 | The |
72 | .Fn free | 51 | .Fn imaxabs |
73 | function returns no value. | 52 | function returns the absolute value. |
74 | .Sh SEE ALSO | 53 | .Sh SEE ALSO |
75 | .Xr calloc 3 , | 54 | .Xr abs 3 , |
76 | .Xr malloc 3 , | 55 | .Xr cabs 3 , |
77 | .Xr realloc 3 | 56 | .Xr floor 3 , |
57 | .Xr hypot 3 , | ||
58 | .Xr labs 3 , | ||
59 | .Xr math 3 | ||
78 | .Sh STANDARDS | 60 | .Sh STANDARDS |
79 | The | 61 | The |
80 | .Fn free | 62 | .Fn abs |
81 | function conforms to | 63 | function conforms to |
82 | .St -ansiC . | 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..63f25d52de --- /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.2 2006/01/14 13:04:53 jmc Exp $ | ||
33 | .\" | ||
34 | .Dd January 3, 2006 | ||
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..fb4f55112d --- /dev/null +++ b/src/lib/libc/stdlib/insque.3 | |||
@@ -0,0 +1,106 @@ | |||
1 | .\" $OpenBSD: insque.3,v 1.6 2005/07/26 04:20:23 jaredy Exp $ | ||
2 | .\" Copyright (c) 1993 John Brezak | ||
3 | .\" All rights reserved. | ||
4 | .\" | ||
5 | .\" Redistribution and use in source and binary forms, with or without | ||
6 | .\" modification, are permitted provided that the following conditions | ||
7 | .\" are met: | ||
8 | .\" 1. Redistributions of source code must retain the above copyright | ||
9 | .\" notice, this list of conditions and the following disclaimer. | ||
10 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
11 | .\" notice, this list of conditions and the following disclaimer in the | ||
12 | .\" documentation and/or other materials provided with the distribution. | ||
13 | .\" 3. The name of the author may be used to endorse or promote products | ||
14 | .\" derived from this software without specific prior written permission. | ||
15 | .\" | ||
16 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND | ||
17 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
18 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
19 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE | ||
20 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
21 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
22 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
23 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
24 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
26 | .\" SUCH DAMAGE. | ||
27 | .\" | ||
28 | .\" | ||
29 | .Dd August 12, 1993 | ||
30 | .Dt INSQUE 3 | ||
31 | .Os | ||
32 | .Sh NAME | ||
33 | .Nm insque , | ||
34 | .Nm remque | ||
35 | .Nd insert/remove element from a queue | ||
36 | .Sh SYNOPSIS | ||
37 | .Fd #include <search.h> | ||
38 | .Ft void | ||
39 | .Fn insque "void *elem" "void *pred" | ||
40 | .Ft void | ||
41 | .Fn remque "void *elem" | ||
42 | .Sh DESCRIPTION | ||
43 | .Bf -symbolic | ||
44 | These interfaces have been superceded by the | ||
45 | .Xr queue 3 | ||
46 | macros and are provided for compatibility with legacy code. | ||
47 | .Ef | ||
48 | .Pp | ||
49 | .Fn insque | ||
50 | and | ||
51 | .Fn remque | ||
52 | manipulate queues built from doubly linked lists. | ||
53 | The queue can be either circular or linear. | ||
54 | Each element in the queue must be of the following form: | ||
55 | .Bd -literal -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 index 205781e0ee..cb8c592750 100644 --- a/src/lib/libc/stdlib/jrand48.c +++ b/src/lib/libc/stdlib/jrand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: jrand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c index 3069b31bf6..4f33df37b2 100644 --- a/src/lib/libc/stdlib/l64a.c +++ b/src/lib/libc/stdlib/l64a.c | |||
@@ -1,25 +1,24 @@ | |||
1 | /* $OpenBSD: l64a.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Written by J.T. Conklin <jtc@netbsd.org>. | 3 | * Written by J.T. Conklin <jtc@netbsd.org>. |
3 | * Public domain. | 4 | * Public domain. |
4 | */ | 5 | */ |
5 | 6 | ||
6 | #if defined(LIBC_SCCS) && !defined(lint) | 7 | #include <errno.h> |
7 | static char *rcsid = "$NetBSD: l64a.c,v 1.4 1995/05/11 23:04:52 jtc Exp $"; | ||
8 | #endif | ||
9 | |||
10 | #include <stdlib.h> | 8 | #include <stdlib.h> |
11 | 9 | ||
12 | char * | 10 | char * |
13 | l64a (value) | 11 | l64a(long value) |
14 | long value; | ||
15 | { | 12 | { |
16 | static char buf[8]; | 13 | static char buf[8]; |
17 | char *s = buf; | 14 | char *s = buf; |
18 | int digit; | 15 | int digit; |
19 | int i; | 16 | int i; |
20 | 17 | ||
21 | if (!value) | 18 | if (value < 0) { |
22 | return NULL; | 19 | errno = EINVAL; |
20 | return(NULL); | ||
21 | } | ||
23 | 22 | ||
24 | for (i = 0; value != 0 && i < 6; i++) { | 23 | for (i = 0; value != 0 && i < 6; i++) { |
25 | digit = value & 0x3f; | 24 | digit = value & 0x3f; |
@@ -39,5 +38,5 @@ l64a (value) | |||
39 | 38 | ||
40 | *s = '\0'; | 39 | *s = '\0'; |
41 | 40 | ||
42 | return buf; | 41 | return(buf); |
43 | } | 42 | } |
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3 index 28e4d2053c..ab1ccaaf53 100644 --- a/src/lib/libc/stdlib/labs.3 +++ b/src/lib/libc/stdlib/labs.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,35 +29,41 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: labs.3,v 1.8 2006/01/13 17:58:09 millert Exp $ |
37 | .\" $Id: labs.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd May 14, 2003 |
40 | .Dt LABS 3 | 35 | .Dt LABS 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
43 | .Nm labs | 38 | .Nm labs, llabs |
44 | .Nd return the absolute value of a long integer | 39 | .Nd return the absolute value of a long integer |
45 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 41 | .Fd #include <stdlib.h> |
47 | .Ft long | 42 | .Ft long |
48 | .Fn labs "long j" | 43 | .Fn labs "long i" |
44 | .Ft long long | ||
45 | .Fn llabs "long long j" | ||
49 | .Sh DESCRIPTION | 46 | .Sh DESCRIPTION |
50 | The | 47 | The |
51 | .Fn labs | 48 | .Fn labs |
52 | function | 49 | function returns the absolute value of the long integer |
53 | returns the absolute value of the long integer | 50 | .Fa i . |
54 | .Ar j . | 51 | The |
52 | .Fn llabs | ||
53 | function returns the absolute value of the long long integer | ||
54 | .Fa j . | ||
55 | .Sh SEE ALSO | 55 | .Sh SEE ALSO |
56 | .Xr abs 3 , | 56 | .Xr abs 3 , |
57 | .Xr floor 3 , | ||
58 | .Xr cabs 3 , | 57 | .Xr cabs 3 , |
58 | .Xr floor 3 , | ||
59 | .Xr imaxabs 3 , | ||
59 | .Xr math 3 | 60 | .Xr math 3 |
60 | .Sh STANDARDS | 61 | .Sh STANDARDS |
61 | The | 62 | The |
62 | .Fn labs | 63 | .Fn labs |
63 | function | 64 | and |
64 | conforms to | 65 | .Fn llabs |
65 | .St -ansiC . | 66 | functions conform to |
67 | .St -ansiC-99 . | ||
66 | .Sh BUGS | 68 | .Sh BUGS |
67 | The absolute value of the most negative integer remains negative. | 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 index ccf1415792..ca60b9aba2 100644 --- a/src/lib/libc/stdlib/labs.c +++ b/src/lib/libc/stdlib/labs.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: labs.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,16 +28,10 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)labs.c 5.2 (Berkeley) 5/17/90";*/ | ||
36 | static char *rcsid = "$Id: labs.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | 32 | ||
41 | long | 33 | long |
42 | labs(j) | 34 | labs(long j) |
43 | long j; | ||
44 | { | 35 | { |
45 | return(j < 0 ? -j : j); | 36 | return(j < 0 ? -j : j); |
46 | } | 37 | } |
diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c index 965d46b17a..2cf5c271ba 100644 --- a/src/lib/libc/stdlib/lcong48.c +++ b/src/lib/libc/stdlib/lcong48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: lcong48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3 index a7b5ccf878..951a5807e0 100644 --- a/src/lib/libc/stdlib/ldiv.3 +++ b/src/lib/libc/stdlib/ldiv.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)ldiv.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: ldiv.3,v 1.8 2006/01/13 17:58:09 millert Exp $ |
37 | .\" $Id: ldiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt LDIV 3 | 35 | .Dt LDIV 3 |
@@ -49,24 +44,24 @@ | |||
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn ldiv | 46 | .Fn ldiv |
52 | function | 47 | function computes the value |
53 | computes the value | 48 | .Fa num Ns / Ns Fa denom |
54 | .Ar num/denom | ||
55 | and returns the quotient and remainder in a structure named | 49 | and returns the quotient and remainder in a structure named |
56 | .Ar ldiv_t | 50 | .Li ldiv_t |
57 | that contains two | 51 | that contains two |
58 | .Em long integer | 52 | .Li long integer |
59 | members named | 53 | members named |
60 | .Ar quot | 54 | .Fa quot |
61 | and | 55 | and |
62 | .Ar rem . | 56 | .Fa rem . |
63 | .Sh SEE ALSO | 57 | .Sh SEE ALSO |
64 | .Xr div 3 , | 58 | .Xr div 3 , |
65 | .Xr qdiv 3 , | 59 | .Xr imaxdiv 3 , |
66 | .Xr math 3 | 60 | .Xr lldiv 3 , |
61 | .Xr math 3 , | ||
62 | .Xr qdiv 3 | ||
67 | .Sh STANDARDS | 63 | .Sh STANDARDS |
68 | The | 64 | The |
69 | .Fn ldiv | 65 | .Fn ldiv |
70 | function | 66 | function conforms to |
71 | conforms to | ||
72 | .St -ansiC . | 67 | .St -ansiC . |
diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c index f7074507e5..775065f525 100644 --- a/src/lib/libc/stdlib/ldiv.c +++ b/src/lib/libc/stdlib/ldiv.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: ldiv.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1990 Regents of the University of California. | 3 | * Copyright (c) 1990 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -13,11 +14,7 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 15 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 16 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 17 | * 3. Neither the name of the University nor the names of its contributors |
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | 18 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. | 19 | * without specific prior written permission. |
23 | * | 20 | * |
@@ -34,16 +31,10 @@ | |||
34 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
35 | */ | 32 | */ |
36 | 33 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char *sccsid = "from: @(#)ldiv.c 5.2 (Berkeley) 4/16/91";*/ | ||
39 | static char *rcsid = "$Id: ldiv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | #include <stdlib.h> /* ldiv_t */ | 34 | #include <stdlib.h> /* ldiv_t */ |
43 | 35 | ||
44 | ldiv_t | 36 | ldiv_t |
45 | ldiv(num, denom) | 37 | ldiv(long num, long denom) |
46 | long num, denom; | ||
47 | { | 38 | { |
48 | ldiv_t r; | 39 | ldiv_t r; |
49 | 40 | ||
diff --git a/src/lib/libc/stdlib/multibyte.c b/src/lib/libc/stdlib/llabs.c index fe1cd5781b..0a0ff784af 100644 --- a/src/lib/libc/stdlib/multibyte.c +++ b/src/lib/libc/stdlib/llabs.c | |||
@@ -1,5 +1,7 @@ | |||
1 | /* | 1 | /* $OpenBSD: llabs.c,v 1.2 2005/08/08 08:05:36 espie Exp $ */ |
2 | * Copyright (c) 1991 The Regents of the University of California. | 2 | |
3 | /*- | ||
4 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * All rights reserved. | 5 | * All rights reserved. |
4 | * | 6 | * |
5 | * Redistribution and use in source and binary forms, with or without | 7 | * Redistribution and use in source and binary forms, with or without |
@@ -31,101 +33,10 @@ | |||
31 | * SUCH DAMAGE. | 33 | * SUCH DAMAGE. |
32 | */ | 34 | */ |
33 | 35 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)multibyte.c 5.1 (Berkeley) 2/18/91";*/ | ||
36 | static char *rcsid = "$Id: multibyte.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 36 | #include <stdlib.h> |
40 | 37 | ||
41 | /* | 38 | long long |
42 | * Stub multibyte character functions. | 39 | llabs(long long j) |
43 | * This cheezy implementation is fixed to the native single-byte | ||
44 | * character set. | ||
45 | */ | ||
46 | |||
47 | int | ||
48 | mblen(s, n) | ||
49 | const char *s; | ||
50 | size_t n; | ||
51 | { | 40 | { |
52 | if (s == NULL || *s == '\0') | 41 | return (j < 0 ? -j : j); |
53 | return 0; | ||
54 | if (n == 0) | ||
55 | return -1; | ||
56 | return 1; | ||
57 | } | ||
58 | |||
59 | /*ARGSUSED*/ | ||
60 | int | ||
61 | mbtowc(pwc, s, n) | ||
62 | wchar_t *pwc; | ||
63 | const char *s; | ||
64 | size_t n; | ||
65 | { | ||
66 | if (s == NULL) | ||
67 | return 0; | ||
68 | if (n == 0) | ||
69 | return -1; | ||
70 | if (pwc) | ||
71 | *pwc = (wchar_t) *s; | ||
72 | return (*s != '\0'); | ||
73 | } | ||
74 | |||
75 | /*ARGSUSED*/ | ||
76 | int | ||
77 | #ifdef __STDC__ | ||
78 | wctomb(char *s, wchar_t wchar) | ||
79 | #else | ||
80 | wctomb(s, wchar) | ||
81 | char *s; | ||
82 | wchar_t wchar; | ||
83 | #endif | ||
84 | { | ||
85 | if (s == NULL) | ||
86 | return 0; | ||
87 | |||
88 | *s = (char) wchar; | ||
89 | return 1; | ||
90 | } | 42 | } |
91 | |||
92 | /*ARGSUSED*/ | ||
93 | size_t | ||
94 | mbstowcs(pwcs, s, n) | ||
95 | wchar_t *pwcs; | ||
96 | const char *s; | ||
97 | size_t n; | ||
98 | { | ||
99 | int count = 0; | ||
100 | |||
101 | if (n != 0) { | ||
102 | do { | ||
103 | if ((*pwcs++ = (wchar_t) *s++) == 0) | ||
104 | break; | ||
105 | count++; | ||
106 | } while (--n != 0); | ||
107 | } | ||
108 | |||
109 | return count; | ||
110 | } | ||
111 | |||
112 | /*ARGSUSED*/ | ||
113 | size_t | ||
114 | wcstombs(s, pwcs, n) | ||
115 | char *s; | ||
116 | const wchar_t *pwcs; | ||
117 | size_t n; | ||
118 | { | ||
119 | int count = 0; | ||
120 | |||
121 | if (n != 0) { | ||
122 | do { | ||
123 | if ((*s++ = (char) *pwcs++) == 0) | ||
124 | break; | ||
125 | count++; | ||
126 | } while (--n != 0); | ||
127 | } | ||
128 | |||
129 | return count; | ||
130 | } | ||
131 | |||
diff --git a/src/lib/libc/stdlib/memory.3 b/src/lib/libc/stdlib/lldiv.3 index 735252c837..53bb3d35ae 100644 --- a/src/lib/libc/stdlib/memory.3 +++ b/src/lib/libc/stdlib/lldiv.3 | |||
@@ -1,6 +1,10 @@ | |||
1 | .\" Copyright (c) 1991 Regents of the University of California. | 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. |
2 | .\" All rights reserved. | 2 | .\" All rights reserved. |
3 | .\" | 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 | .\" | ||
4 | .\" Redistribution and use in source and binary forms, with or without | 8 | .\" Redistribution and use in source and binary forms, with or without |
5 | .\" modification, are permitted provided that the following conditions | 9 | .\" modification, are permitted provided that the following conditions |
6 | .\" are met: | 10 | .\" are met: |
@@ -9,11 +13,7 @@ | |||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
11 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
12 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
13 | .\" must display the following acknowledgement: | ||
14 | .\" This product includes software developed by the University of | ||
15 | .\" California, Berkeley and its contributors. | ||
16 | .\" 4. 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 | 17 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
19 | .\" | 19 | .\" |
@@ -29,43 +29,39 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
31 | .\" | 31 | .\" |
32 | .\" from: @(#)memory.3 5.1 (Berkeley) 5/2/91 | 32 | .\" $OpenBSD: lldiv.3,v 1.1 2006/01/13 17:58:09 millert Exp $ |
33 | .\" $Id: memory.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
34 | .\" | 33 | .\" |
35 | .Dd May 2, 1991 | 34 | .Dd January 3, 2006 |
36 | .Dt MEMORY 3 | 35 | .Dt LLDIV 3 |
37 | .Os BSD 4 | 36 | .Os |
38 | .Sh NAME | 37 | .Sh NAME |
39 | .Nm malloc , | 38 | .Nm lldiv |
40 | .Nm free , | 39 | .Nd return quotient and remainder from division |
41 | .Nm realloc , | ||
42 | .Nm calloc , | ||
43 | .Nm alloca | ||
44 | .Nd general memory allocation operations | ||
45 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 41 | .Fd #include <stdlib.h> |
47 | .Ft void * | 42 | .Ft lldiv_t |
48 | .Fn malloc "size_t size" | 43 | .Fn lldiv "long long num" "long long denom" |
49 | .Ft void | ||
50 | .Fn free "void *ptr" | ||
51 | .Ft void * | ||
52 | .Fn realloc "void *ptr" "size_t size" | ||
53 | .Ft void * | ||
54 | .Fn calloc "size_t nelem" "size_t elsize" | ||
55 | .Ft void * | ||
56 | .Fn alloca "size_t size" | ||
57 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
58 | These functions allocate and free memory for the calling process. | 45 | The |
59 | They are described in the | 46 | .Fn lldiv |
60 | individual manual pages. | 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 . | ||
61 | .Sh SEE ALSO | 57 | .Sh SEE ALSO |
62 | .Xr calloc 3 , | 58 | .Xr div 3 , |
63 | .Xr free 3 , | 59 | .Xr imaxdiv 3 , |
64 | .Xr malloc 3 , | 60 | .Xr ldiv 3 , |
65 | .Xr realloc 3 , | 61 | .Xr math 3 , |
66 | .Xr alloca 3 , | 62 | .Xr qdiv 3 |
67 | .Sh STANDARDS | 63 | .Sh STANDARDS |
68 | These functions, with the exception of | 64 | The |
69 | .Fn alloca | 65 | .Fn lldiv |
70 | conform to | 66 | function conforms to |
71 | .St -ansiC . | 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 index 8e7f26237f..21beb858ca 100644 --- a/src/lib/libc/stdlib/lrand48.c +++ b/src/lib/libc/stdlib/lrand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: lrand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/lsearch.3 b/src/lib/libc/stdlib/lsearch.3 new file mode 100644 index 0000000000..eda67ada01 --- /dev/null +++ b/src/lib/libc/stdlib/lsearch.3 | |||
@@ -0,0 +1,108 @@ | |||
1 | .\" $OpenBSD: lsearch.3,v 1.7 2005/12/23 21:34:16 millert 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 June 4, 1993 | ||
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 index 3bbf2bf65e..e5bb1315df 100644 --- a/src/lib/libc/stdlib/malloc.3 +++ b/src/lib/libc/stdlib/malloc.3 | |||
@@ -1,5 +1,6 @@ | |||
1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | 1 | .\" |
2 | .\" All rights reserved. | 2 | .\" Copyright (c) 1980, 1991, 1993 |
3 | .\" The Regents of the University of California. All rights reserved. | ||
3 | .\" | 4 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | 5 | .\" This code is derived from software contributed to Berkeley by |
5 | .\" the American National Standards Committee X3, on Information | 6 | .\" the American National Standards Committee X3, on Information |
@@ -13,11 +14,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 14 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 15 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 16 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 17 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 18 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 19 | .\" without specific prior written permission. |
23 | .\" | 20 | .\" |
@@ -33,19 +30,32 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 30 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 31 | .\" SUCH DAMAGE. |
35 | .\" | 32 | .\" |
36 | .\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 | 33 | .\" $OpenBSD: malloc.3,v 1.45 2006/06/06 14:49:26 pedro Exp $ |
37 | .\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
38 | .\" | 34 | .\" |
39 | .Dd June 29, 1991 | 35 | .Dd August 27, 1996 |
40 | .Dt MALLOC 3 | 36 | .Dt MALLOC 3 |
41 | .Os BSD 4 | 37 | .Os |
42 | .Sh NAME | 38 | .Sh NAME |
43 | .Nm malloc | 39 | .Nm malloc , |
44 | .Nd general memory allocation function | 40 | .Nm calloc , |
41 | .Nm realloc , | ||
42 | .Nm free , | ||
43 | .Nm cfree | ||
44 | .Nd memory allocation and deallocation | ||
45 | .Sh SYNOPSIS | 45 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 46 | .Fd #include <stdlib.h> |
47 | .Ft void * | 47 | .Ft void * |
48 | .Fn malloc "size_t size" | 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 ; | ||
49 | .Sh DESCRIPTION | 59 | .Sh DESCRIPTION |
50 | The | 60 | The |
51 | .Fn malloc | 61 | .Fn malloc |
@@ -59,33 +69,398 @@ space from the appropriate list. | |||
59 | .Pp | 69 | .Pp |
60 | The allocated space is | 70 | The allocated space is |
61 | suitably aligned (after possible pointer | 71 | suitably aligned (after possible pointer |
62 | coercion) for storage of any type of object. If the space is of | 72 | coercion) for storage of any type of object. |
73 | If the space is of | ||
63 | .Em pagesize | 74 | .Em pagesize |
64 | or larger, the memory returned will be page-aligned. | 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 (num && SIZE_MAX / num < 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 all bits 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 deallocated, that is, at least made available for further allocation, | ||
126 | but if possible, it will be passed back to the kernel with | ||
127 | .Xr sbrk 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 failure. | ||
219 | This is a very handy debugging aid, since the core file will represent the | ||
220 | time of failure, rather than when the null pointer was accessed. | ||
221 | .It Cm D | ||
222 | .Dq Dump . | ||
223 | .Fn malloc | ||
224 | will dump statistics in a file called | ||
225 | .Pa malloc.out | ||
226 | at exit. | ||
227 | This option requires the library to have been compiled with -DMALLOC_STATS in | ||
228 | order to have any effect. | ||
229 | .It Cm F | ||
230 | .Dq Freeguard . | ||
231 | Enable use after free protection. | ||
232 | Unused pages on the freelist are read and write protected to | ||
233 | cause a segmentation fault upon access. | ||
234 | .It Cm G | ||
235 | .Dq Guard . | ||
236 | Enable guard pages and chunk randomization. | ||
237 | Each page size or larger allocation is followed by a guard page that will | ||
238 | cause a segmentation fault upon any access. | ||
239 | Smaller than page size chunks are returned in a random order. | ||
240 | .It Cm H | ||
241 | .Dq Hint . | ||
242 | Pass a hint to the kernel about pages we don't use. | ||
243 | If the machine is paging a lot this may help a bit. | ||
244 | .It Cm J | ||
245 | .Dq Junk . | ||
246 | Fill some junk into the area allocated. | ||
247 | Currently junk is bytes of 0xd0; this is pronounced | ||
248 | .Dq Duh . | ||
249 | \&:-) | ||
250 | .It Cm N | ||
251 | Do not output warning messages when encountering possible corruption | ||
252 | or bad pointers. | ||
253 | .It Cm P | ||
254 | .Dq Pointer Protection . | ||
255 | Pointer sized allocations are aligned to the end of a page to catch | ||
256 | sizeof(ptr) errors where sizeof(*ptr) is meant. | ||
257 | .It Cm R | ||
258 | .Dq realloc . | ||
259 | Always reallocate when | ||
260 | .Fn realloc | ||
261 | is called, even if the initial allocation was big enough. | ||
262 | This can substantially aid in compacting memory. | ||
263 | .\".Pp | ||
264 | .\".It Cm U | ||
265 | .\".Dq utrace . | ||
266 | .\"Generate entries for | ||
267 | .\".Xr ktrace 1 | ||
268 | .\"for all operations. | ||
269 | .\"Consult the source for this one. | ||
270 | .It Cm X | ||
271 | .Dq xmalloc . | ||
272 | Rather than return failure, | ||
273 | .Xr abort 3 | ||
274 | the program with a diagnostic message on stderr. | ||
275 | It is the intention that this option be set at compile time by | ||
276 | including in the source: | ||
277 | .Bd -literal -offset indent | ||
278 | extern char *malloc_options; | ||
279 | malloc_options = "X"; | ||
280 | .Ed | ||
281 | .It Cm Z | ||
282 | .Dq Zero . | ||
283 | Fill some junk into the area allocated (see | ||
284 | .Cm J ) , | ||
285 | except for the exact length the user asked for, which is zeroed. | ||
286 | .It Cm < | ||
287 | .Dq Half the cache size . | ||
288 | Decrease the size of the free page cache by a factor of two. | ||
289 | .It Cm > | ||
290 | .Dq Double the cache size . | ||
291 | Increase the size of the free page cache by a factor of two. | ||
292 | .El | ||
293 | .Pp | ||
294 | So to set a systemwide reduction of cache size and coredumps on problems: | ||
295 | .Li ln -s 'A<' /etc/malloc.conf | ||
296 | .Pp | ||
297 | The | ||
298 | .Cm J | ||
299 | and | ||
300 | .Cm Z | ||
301 | flags are mostly for testing and debugging. | ||
302 | If a program changes behavior if either of these options are used, | ||
303 | it is buggy. | ||
304 | .Pp | ||
305 | The default number of free pages cached is 16. | ||
65 | .Sh RETURN VALUES | 306 | .Sh RETURN VALUES |
66 | The | 307 | The |
67 | .Fn malloc | 308 | .Fn malloc |
68 | function returns | 309 | and |
69 | a pointer to the allocated space if successful; otherwise | 310 | .Fn calloc |
70 | a null pointer is returned. | 311 | functions return a pointer to the allocated space if successful; otherwise, |
312 | a null pointer is returned and | ||
313 | .Va errno | ||
314 | is set to | ||
315 | .Er ENOMEM . | ||
316 | .Pp | ||
317 | The | ||
318 | .Fn free | ||
319 | and | ||
320 | .Fn cfree | ||
321 | functions return no value. | ||
322 | .Pp | ||
323 | The | ||
324 | .Fn realloc | ||
325 | function returns a pointer to the (possibly moved) allocated space | ||
326 | if successful; otherwise, a null pointer is returned and | ||
327 | .Va errno | ||
328 | is set to | ||
329 | .Er ENOMEM . | ||
330 | .Sh ENVIRONMENT | ||
331 | .Bl -tag -width Ev | ||
332 | .It Ev MALLOC_OPTIONS | ||
333 | See above. | ||
334 | .El | ||
335 | .Sh FILES | ||
336 | .Bl -tag -width "/etc/malloc.conf" | ||
337 | .It Pa /etc/malloc.conf | ||
338 | symbolic link to filename containing option flags | ||
339 | .El | ||
340 | .Sh DIAGNOSTICS | ||
341 | If | ||
342 | .Fn malloc , | ||
343 | .Fn calloc , | ||
344 | .Fn realloc , | ||
345 | or | ||
346 | .Fn free | ||
347 | detect an error or warning condition, | ||
348 | a message will be printed to file descriptor | ||
349 | 2 (not using stdio). | ||
350 | Errors will always result in the process being | ||
351 | .Xr abort 3 'ed. | ||
352 | If the | ||
353 | .Cm A | ||
354 | option has been specified, warnings will also | ||
355 | .Xr abort 3 | ||
356 | the process. | ||
357 | .Pp | ||
358 | Here is a brief description of the error messages and what they mean: | ||
359 | .Bl -tag -width Ds | ||
360 | .It Dq (ES): mumble mumble mumble | ||
361 | .Fn malloc | ||
362 | has been compiled with | ||
363 | .Dv \&-DEXTRA_SANITY | ||
364 | and something looks fishy in there. | ||
365 | Consult sources and/or wizards. | ||
366 | .It Dq allocation failed | ||
367 | If the | ||
368 | .Cm A | ||
369 | option is specified it is an error for | ||
370 | .Fn malloc , | ||
371 | .Fn calloc , | ||
372 | or | ||
373 | .Fn realloc | ||
374 | to return | ||
375 | .Dv NULL . | ||
376 | .It Dq mmap(2) failed, check limits. | ||
377 | This is a rather weird condition that is most likely to indicate a | ||
378 | seriously overloaded system or a | ||
379 | .Xr ulimit 1 | ||
380 | restriction. | ||
381 | .It Dq freelist is destroyed. | ||
382 | .Fn malloc Ns 's | ||
383 | internal freelist has been stomped on. | ||
384 | .El | ||
385 | .Pp | ||
386 | Here is a brief description of the warning messages and what they mean: | ||
387 | .Bl -tag -width Ds | ||
388 | .It Dq chunk/page is already free. | ||
389 | There was an attempt to free a chunk that had already been freed. | ||
390 | .It Dq junk pointer, too high to make sense. | ||
391 | The pointer doesn't make sense. | ||
392 | It's above the area of memory that | ||
393 | .Fn malloc | ||
394 | knows something about. | ||
395 | This could be a pointer from some | ||
396 | .Xr mmap 2 'ed | ||
397 | memory. | ||
398 | .It Dq junk pointer, too low to make sense. | ||
399 | The pointer doesn't make sense. | ||
400 | It's below the area of memory that | ||
401 | .Fn malloc | ||
402 | knows something about. | ||
403 | This pointer probably came from your data or bss segments. | ||
404 | .It Dq malloc() has never been called. | ||
405 | Nothing has ever been allocated, yet something is being freed or | ||
406 | realloc'ed. | ||
407 | .It Dq modified (chunk-/page-) pointer. | ||
408 | The pointer passed to | ||
409 | .Fn free | ||
410 | or | ||
411 | .Fn realloc | ||
412 | has been modified. | ||
413 | .It Dq pointer to wrong page. | ||
414 | The pointer that | ||
415 | .Fn malloc | ||
416 | is trying to free is not pointing to | ||
417 | a sensible page. | ||
418 | .It Dq recursive call. | ||
419 | An attempt was made to call recursively into these functions, i.e., from a | ||
420 | signal handler. | ||
421 | This behavior is not supported. | ||
422 | In particular, signal handlers should | ||
423 | .Em not | ||
424 | use any of the | ||
425 | .Fn malloc | ||
426 | functions nor utilize any other functions which may call | ||
427 | .Fn malloc | ||
428 | (e.g., | ||
429 | .Xr stdio 3 | ||
430 | routines). | ||
431 | .It Dq unknown char in MALLOC_OPTIONS | ||
432 | We found something we didn't understand. | ||
433 | .El | ||
71 | .Sh SEE ALSO | 434 | .Sh SEE ALSO |
72 | .Xr brk 2 , | 435 | .Xr brk 2 , |
73 | .Xr getpagesize 2 , | ||
74 | .Xr free 3 , | ||
75 | .Xr calloc 3 , | ||
76 | .Xr alloca 3 , | 436 | .Xr alloca 3 , |
77 | .Xr realloc 3 , | 437 | .Xr getpagesize 3 |
78 | .Xr memory 3 | ||
79 | .Sh STANDARDS | 438 | .Sh STANDARDS |
80 | The | 439 | The |
81 | .Fn malloc | 440 | .Fn malloc |
82 | function conforms to | 441 | function conforms to |
83 | .St -ansiC . | 442 | .St -ansiC . |
84 | .Sh BUGS | 443 | .Sh HISTORY |
85 | The current implementation of | 444 | The present implementation of |
86 | .Xr malloc | 445 | .Fn malloc |
87 | does not always fail gracefully when system | 446 | started out as a filesystem on a drum |
88 | memory limits are approached. | 447 | attached to a 20-bit binary challenged computer built with discrete germanium |
89 | It may fail to allocate memory when larger free blocks could be broken | 448 | transistors, and it has since graduated to handle primary storage rather than |
90 | up, or when limits are exceeded because the size is rounded up. | 449 | secondary. |
91 | It is optimized for sizes that are powers of two. | 450 | .Pp |
451 | The main difference from other | ||
452 | .Fn malloc | ||
453 | implementations are believed to be that | ||
454 | the free pages are not accessed until allocated. | ||
455 | Most | ||
456 | .Fn malloc | ||
457 | implementations will store a data structure containing a, | ||
458 | possibly double-, linked list in the free chunks of memory, used to tie | ||
459 | all the free memory together. | ||
460 | That is a quite suboptimal thing to do. | ||
461 | Every time the free-list is traversed, all the otherwise unused, and very | ||
462 | likely paged out, pages get faulted into primary memory, just to see what | ||
463 | lies after them in the list. | ||
464 | .Pp | ||
465 | On systems which are paging, this can increase the page-faults | ||
466 | of a process by a factor of five. | ||
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 3c57fad024..028eff2b2d 100644 --- a/src/lib/libc/stdlib/malloc.c +++ b/src/lib/libc/stdlib/malloc.c | |||
@@ -1,421 +1,1870 @@ | |||
1 | /* $OpenBSD: malloc.c,v 1.83 2006/05/14 19:53:40 otto Exp $ */ | ||
2 | |||
1 | /* | 3 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | 4 | * ---------------------------------------------------------------------------- |
3 | * All rights reserved. | 5 | * "THE BEER-WARE LICENSE" (Revision 42): |
4 | * | 6 | * <phk@FreeBSD.ORG> wrote this file. As long as you retain this notice you |
5 | * Redistribution and use in source and binary forms, with or without | 7 | * can do whatever you want with this stuff. If we meet some day, and you think |
6 | * modification, are permitted provided that the following conditions | 8 | * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp |
7 | * are met: | 9 | * ---------------------------------------------------------------------------- |
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. All advertising materials mentioning features or use of this software | ||
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | */ | 10 | */ |
33 | 11 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 12 | /* |
35 | /*static char *sccsid = "from: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/ | 13 | * Defining MALLOC_EXTRA_SANITY will enable extra checks which are |
36 | static char *rcsid = "$Id: malloc.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | 14 | * related to internal conditions and consistency in malloc.c. This has |
37 | #endif /* LIBC_SCCS and not lint */ | 15 | * a noticeable runtime performance hit, and generally will not do you |
16 | * any good unless you fiddle with the internals of malloc or want | ||
17 | * to catch random pointer corruption as early as possible. | ||
18 | */ | ||
19 | #ifndef MALLOC_EXTRA_SANITY | ||
20 | #undef MALLOC_EXTRA_SANITY | ||
21 | #endif | ||
38 | 22 | ||
39 | /* | 23 | /* |
40 | * malloc.c (Caltech) 2/21/82 | 24 | * Defining MALLOC_STATS will enable you to call malloc_dump() and set |
41 | * Chris Kingsley, kingsley@cit-20. | 25 | * the [dD] options in the MALLOC_OPTIONS environment variable. |
42 | * | 26 | * It has no run-time performance hit, but does pull in stdio... |
43 | * This is a very fast storage allocator. It allocates blocks of a small | ||
44 | * number of different sizes, and keeps free lists of each size. Blocks that | ||
45 | * don't exactly fit are passed up to the next larger size. In this | ||
46 | * implementation, the available sizes are 2^n-4 (or 2^n-10) bytes long. | ||
47 | * This is designed for use in a virtual memory environment. | ||
48 | */ | 27 | */ |
28 | #ifndef MALLOC_STATS | ||
29 | #undef MALLOC_STATS | ||
30 | #endif | ||
31 | |||
32 | /* | ||
33 | * What to use for Junk. This is the byte value we use to fill with | ||
34 | * when the 'J' option is enabled. | ||
35 | */ | ||
36 | #define SOME_JUNK 0xd0 /* as in "Duh" :-) */ | ||
49 | 37 | ||
50 | #include <sys/types.h> | 38 | #include <sys/types.h> |
39 | #include <sys/time.h> | ||
40 | #include <sys/resource.h> | ||
41 | #include <sys/param.h> | ||
42 | #include <sys/mman.h> | ||
43 | #include <sys/uio.h> | ||
44 | #include <stdio.h> | ||
51 | #include <stdlib.h> | 45 | #include <stdlib.h> |
52 | #include <string.h> | 46 | #include <string.h> |
53 | #include <unistd.h> | 47 | #include <unistd.h> |
48 | #include <fcntl.h> | ||
49 | #include <limits.h> | ||
50 | #include <errno.h> | ||
51 | #include <err.h> | ||
54 | 52 | ||
55 | #define NULL 0 | 53 | #include "thread_private.h" |
56 | |||
57 | static void morecore(); | ||
58 | static int findbucket(); | ||
59 | 54 | ||
60 | /* | 55 | /* |
61 | * The overhead on a block is at least 4 bytes. When free, this space | 56 | * The basic parameters you can tweak. |
62 | * contains a pointer to the next free block, and the bottom two bits must | 57 | * |
63 | * be zero. When in use, the first byte is set to MAGIC, and the second | 58 | * malloc_pageshift pagesize = 1 << malloc_pageshift |
64 | * byte is the size index. The remaining bytes are for alignment. | 59 | * It's probably best if this is the native |
65 | * If range checking is enabled then a second word holds the size of the | 60 | * page size, but it shouldn't have to be. |
66 | * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC). | 61 | * |
67 | * The order of elements is critical: ov_magic must overlay the low order | 62 | * malloc_minsize minimum size of an allocation in bytes. |
68 | * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern. | 63 | * If this is too small it's too much work |
64 | * to manage them. This is also the smallest | ||
65 | * unit of alignment used for the storage | ||
66 | * returned by malloc/realloc. | ||
67 | * | ||
69 | */ | 68 | */ |
70 | union overhead { | ||
71 | union overhead *ov_next; /* when free */ | ||
72 | struct { | ||
73 | u_char ovu_magic; /* magic number */ | ||
74 | u_char ovu_index; /* bucket # */ | ||
75 | #ifdef RCHECK | ||
76 | u_short ovu_rmagic; /* range magic number */ | ||
77 | u_long ovu_size; /* actual block size */ | ||
78 | #endif | ||
79 | } ovu; | ||
80 | #define ov_magic ovu.ovu_magic | ||
81 | #define ov_index ovu.ovu_index | ||
82 | #define ov_rmagic ovu.ovu_rmagic | ||
83 | #define ov_size ovu.ovu_size | ||
84 | }; | ||
85 | 69 | ||
86 | #define MAGIC 0xef /* magic # on accounting info */ | 70 | #if defined(__sparc__) |
87 | #define RMAGIC 0x5555 /* magic # on range info */ | 71 | #define malloc_pageshift 13U |
72 | #endif /* __sparc__ */ | ||
88 | 73 | ||
89 | #ifdef RCHECK | 74 | #ifndef malloc_pageshift |
90 | #define RSLOP sizeof (u_short) | 75 | #define malloc_pageshift (PGSHIFT) |
91 | #else | ||
92 | #define RSLOP 0 | ||
93 | #endif | 76 | #endif |
94 | 77 | ||
95 | /* | 78 | /* |
96 | * nextf[i] is the pointer to the next free block of size 2^(i+3). The | 79 | * No user serviceable parts behind this point. |
97 | * smallest allocatable block is 8 bytes. The overhead information | 80 | * |
98 | * precedes the data area returned to the user. | 81 | * This structure describes a page worth of chunks. |
99 | */ | 82 | */ |
100 | #define NBUCKETS 30 | 83 | struct pginfo { |
101 | static union overhead *nextf[NBUCKETS]; | 84 | struct pginfo *next; /* next on the free list */ |
102 | extern char *sbrk(); | 85 | void *page; /* Pointer to the page */ |
86 | u_short size; /* size of this page's chunks */ | ||
87 | u_short shift; /* How far to shift for this size chunks */ | ||
88 | u_short free; /* How many free chunks */ | ||
89 | u_short total; /* How many chunk */ | ||
90 | u_long bits[1];/* Which chunks are free */ | ||
91 | }; | ||
103 | 92 | ||
104 | static int pagesz; /* page size */ | 93 | /* How many bits per u_long in the bitmap */ |
105 | static int pagebucket; /* page size bucket */ | 94 | #define MALLOC_BITS (NBBY * sizeof(u_long)) |
106 | 95 | ||
107 | #ifdef MSTATS | ||
108 | /* | 96 | /* |
109 | * nmalloc[i] is the difference between the number of mallocs and frees | 97 | * This structure describes a number of free pages. |
110 | * for a given block size. | ||
111 | */ | 98 | */ |
112 | static u_int nmalloc[NBUCKETS]; | 99 | struct pgfree { |
113 | #include <stdio.h> | 100 | struct pgfree *next; /* next run of free pages */ |
101 | struct pgfree *prev; /* prev run of free pages */ | ||
102 | void *page; /* pointer to free pages */ | ||
103 | void *pdir; /* pointer to the base page's dir */ | ||
104 | size_t size; /* number of bytes free */ | ||
105 | }; | ||
106 | |||
107 | /* | ||
108 | * Magic values to put in the page_directory | ||
109 | */ | ||
110 | #define MALLOC_NOT_MINE ((struct pginfo*) 0) | ||
111 | #define MALLOC_FREE ((struct pginfo*) 1) | ||
112 | #define MALLOC_FIRST ((struct pginfo*) 2) | ||
113 | #define MALLOC_FOLLOW ((struct pginfo*) 3) | ||
114 | #define MALLOC_MAGIC ((struct pginfo*) 4) | ||
115 | |||
116 | #ifndef malloc_minsize | ||
117 | #define malloc_minsize 16UL | ||
114 | #endif | 118 | #endif |
115 | 119 | ||
116 | #if defined(DEBUG) || defined(RCHECK) | 120 | #if !defined(malloc_pagesize) |
117 | #define ASSERT(p) if (!(p)) botch("p") | 121 | #define malloc_pagesize (1UL<<malloc_pageshift) |
118 | #include <stdio.h> | 122 | #endif |
119 | static | 123 | |
120 | botch(s) | 124 | #if ((1UL<<malloc_pageshift) != malloc_pagesize) |
121 | char *s; | 125 | #error "(1UL<<malloc_pageshift) != malloc_pagesize" |
126 | #endif | ||
127 | |||
128 | #ifndef malloc_maxsize | ||
129 | #define malloc_maxsize ((malloc_pagesize)>>1) | ||
130 | #endif | ||
131 | |||
132 | /* A mask for the offset inside a page. */ | ||
133 | #define malloc_pagemask ((malloc_pagesize)-1) | ||
134 | |||
135 | #define pageround(foo) (((foo) + (malloc_pagemask)) & ~malloc_pagemask) | ||
136 | #define ptr2index(foo) (((u_long)(foo) >> malloc_pageshift)+malloc_pageshift) | ||
137 | #define index2ptr(idx) ((void*)(((idx)-malloc_pageshift)<<malloc_pageshift)) | ||
138 | |||
139 | /* Set when initialization has been done */ | ||
140 | static unsigned int malloc_started; | ||
141 | |||
142 | /* Number of free pages we cache */ | ||
143 | static unsigned int malloc_cache = 16; | ||
144 | |||
145 | /* Structure used for linking discrete directory pages. */ | ||
146 | struct pdinfo { | ||
147 | struct pginfo **base; | ||
148 | struct pdinfo *prev; | ||
149 | struct pdinfo *next; | ||
150 | u_long dirnum; | ||
151 | }; | ||
152 | static struct pdinfo *last_dir; /* Caches to the last and previous */ | ||
153 | static struct pdinfo *prev_dir; /* referenced directory pages. */ | ||
154 | |||
155 | static size_t pdi_off; | ||
156 | static u_long pdi_mod; | ||
157 | #define PD_IDX(num) ((num) / (malloc_pagesize/sizeof(struct pginfo *))) | ||
158 | #define PD_OFF(num) ((num) & ((malloc_pagesize/sizeof(struct pginfo *))-1)) | ||
159 | #define PI_IDX(index) ((index) / pdi_mod) | ||
160 | #define PI_OFF(index) ((index) % pdi_mod) | ||
161 | |||
162 | /* The last index in the page directory we care about */ | ||
163 | static u_long last_index; | ||
164 | |||
165 | /* Pointer to page directory. Allocated "as if with" malloc */ | ||
166 | static struct pginfo **page_dir; | ||
167 | |||
168 | /* Free pages line up here */ | ||
169 | static struct pgfree free_list; | ||
170 | |||
171 | /* Abort(), user doesn't handle problems. */ | ||
172 | static int malloc_abort = 2; | ||
173 | |||
174 | /* Are we trying to die ? */ | ||
175 | static int suicide; | ||
176 | |||
177 | #ifdef MALLOC_STATS | ||
178 | /* dump statistics */ | ||
179 | static int malloc_stats; | ||
180 | #endif | ||
181 | |||
182 | /* avoid outputting warnings? */ | ||
183 | static int malloc_silent; | ||
184 | |||
185 | /* always realloc ? */ | ||
186 | static int malloc_realloc; | ||
187 | |||
188 | /* mprotect free pages PROT_NONE? */ | ||
189 | static int malloc_freeprot; | ||
190 | |||
191 | /* use guard pages after allocations? */ | ||
192 | static size_t malloc_guard = 0; | ||
193 | static size_t malloc_guarded; | ||
194 | /* align pointers to end of page? */ | ||
195 | static int malloc_ptrguard; | ||
196 | |||
197 | static int malloc_hint; | ||
198 | |||
199 | /* xmalloc behaviour ? */ | ||
200 | static int malloc_xmalloc; | ||
201 | |||
202 | /* zero fill ? */ | ||
203 | static int malloc_zero; | ||
204 | |||
205 | /* junk fill ? */ | ||
206 | static int malloc_junk; | ||
207 | |||
208 | #ifdef __FreeBSD__ | ||
209 | /* utrace ? */ | ||
210 | static int malloc_utrace; | ||
211 | |||
212 | struct ut { | ||
213 | void *p; | ||
214 | size_t s; | ||
215 | void *r; | ||
216 | }; | ||
217 | |||
218 | void utrace(struct ut *, int); | ||
219 | |||
220 | #define UTRACE(a, b, c) \ | ||
221 | if (malloc_utrace) \ | ||
222 | {struct ut u; u.p=a; u.s = b; u.r=c; utrace(&u, sizeof u);} | ||
223 | #else /* !__FreeBSD__ */ | ||
224 | #define UTRACE(a,b,c) | ||
225 | #endif | ||
226 | |||
227 | /* Status of malloc. */ | ||
228 | static int malloc_active; | ||
229 | |||
230 | /* Allocated memory. */ | ||
231 | static size_t malloc_used; | ||
232 | |||
233 | /* My last break. */ | ||
234 | static caddr_t malloc_brk; | ||
235 | |||
236 | /* One location cache for free-list holders. */ | ||
237 | static struct pgfree *px; | ||
238 | |||
239 | /* Compile-time options. */ | ||
240 | char *malloc_options; | ||
241 | |||
242 | /* Name of the current public function. */ | ||
243 | static char *malloc_func; | ||
244 | |||
245 | #define MMAP(size) \ | ||
246 | mmap((void *)0, (size), PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, \ | ||
247 | -1, (off_t)0) | ||
248 | |||
249 | /* | ||
250 | * Necessary function declarations. | ||
251 | */ | ||
252 | static void *imalloc(size_t size); | ||
253 | static void ifree(void *ptr); | ||
254 | static void *irealloc(void *ptr, size_t size); | ||
255 | static void *malloc_bytes(size_t size); | ||
256 | |||
257 | /* | ||
258 | * Function for page directory lookup. | ||
259 | */ | ||
260 | static int | ||
261 | pdir_lookup(u_long index, struct pdinfo ** pdi) | ||
122 | { | 262 | { |
123 | fprintf(stderr, "\r\nassertion botched: %s\r\n", s); | 263 | struct pdinfo *spi; |
124 | (void) fflush(stderr); /* just in case user buffered it */ | 264 | u_long pidx = PI_IDX(index); |
125 | abort(); | 265 | |
266 | if (last_dir != NULL && PD_IDX(last_dir->dirnum) == pidx) | ||
267 | *pdi = last_dir; | ||
268 | else if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) == pidx) | ||
269 | *pdi = prev_dir; | ||
270 | else if (last_dir != NULL && prev_dir != NULL) { | ||
271 | if ((PD_IDX(last_dir->dirnum) > pidx) ? | ||
272 | (PD_IDX(last_dir->dirnum) - pidx) : | ||
273 | (pidx - PD_IDX(last_dir->dirnum)) | ||
274 | < (PD_IDX(prev_dir->dirnum) > pidx) ? | ||
275 | (PD_IDX(prev_dir->dirnum) - pidx) : | ||
276 | (pidx - PD_IDX(prev_dir->dirnum))) | ||
277 | *pdi = last_dir; | ||
278 | else | ||
279 | *pdi = prev_dir; | ||
280 | |||
281 | if (PD_IDX((*pdi)->dirnum) > pidx) { | ||
282 | for (spi = (*pdi)->prev; | ||
283 | spi != NULL && PD_IDX(spi->dirnum) > pidx; | ||
284 | spi = spi->prev) | ||
285 | *pdi = spi; | ||
286 | if (spi != NULL) | ||
287 | *pdi = spi; | ||
288 | } else | ||
289 | for (spi = (*pdi)->next; | ||
290 | spi != NULL && PD_IDX(spi->dirnum) <= pidx; | ||
291 | spi = spi->next) | ||
292 | *pdi = spi; | ||
293 | } else { | ||
294 | *pdi = (struct pdinfo *) ((caddr_t) page_dir + pdi_off); | ||
295 | for (spi = *pdi; | ||
296 | spi != NULL && PD_IDX(spi->dirnum) <= pidx; | ||
297 | spi = spi->next) | ||
298 | *pdi = spi; | ||
299 | } | ||
300 | |||
301 | return ((PD_IDX((*pdi)->dirnum) == pidx) ? 0 : | ||
302 | (PD_IDX((*pdi)->dirnum) > pidx) ? 1 : -1); | ||
126 | } | 303 | } |
127 | #else | ||
128 | #define ASSERT(p) | ||
129 | #endif | ||
130 | 304 | ||
131 | void * | 305 | #ifdef MALLOC_STATS |
132 | malloc(nbytes) | 306 | void |
133 | size_t nbytes; | 307 | malloc_dump(int fd) |
134 | { | 308 | { |
135 | register union overhead *op; | 309 | char buf[1024]; |
136 | register long bucket, n; | 310 | struct pginfo **pd; |
137 | register unsigned amt; | 311 | struct pgfree *pf; |
312 | struct pdinfo *pi; | ||
313 | u_long j; | ||
138 | 314 | ||
139 | /* | 315 | pd = page_dir; |
140 | * First time malloc is called, setup page size and | 316 | pi = (struct pdinfo *) ((caddr_t) pd + pdi_off); |
141 | * align break pointer so all data will be page aligned. | 317 | |
142 | */ | 318 | /* print out all the pages */ |
143 | if (pagesz == 0) { | 319 | for (j = 0; j <= last_index;) { |
144 | pagesz = n = getpagesize(); | 320 | snprintf(buf, sizeof buf, "%08lx %5lu ", j << malloc_pageshift, j); |
145 | op = (union overhead *)sbrk(0); | 321 | write(fd, buf, strlen(buf)); |
146 | n = n - sizeof (*op) - ((long)op & (n - 1)); | 322 | if (pd[PI_OFF(j)] == MALLOC_NOT_MINE) { |
147 | if (n < 0) | 323 | for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_NOT_MINE;) { |
148 | n += pagesz; | 324 | if (!PI_OFF(++j)) { |
149 | if (n) { | 325 | if ((pi = pi->next) == NULL || |
150 | if (sbrk(n) == (char *)-1) | 326 | PD_IDX(pi->dirnum) != PI_IDX(j)) |
151 | return (NULL); | 327 | break; |
328 | pd = pi->base; | ||
329 | j += pdi_mod; | ||
330 | } | ||
331 | } | ||
332 | j--; | ||
333 | snprintf(buf, sizeof buf, ".. %5lu not mine\n", j); | ||
334 | write(fd, buf, strlen(buf)); | ||
335 | } else if (pd[PI_OFF(j)] == MALLOC_FREE) { | ||
336 | for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FREE;) { | ||
337 | if (!PI_OFF(++j)) { | ||
338 | if ((pi = pi->next) == NULL || | ||
339 | PD_IDX(pi->dirnum) != PI_IDX(j)) | ||
340 | break; | ||
341 | pd = pi->base; | ||
342 | j += pdi_mod; | ||
343 | } | ||
344 | } | ||
345 | j--; | ||
346 | snprintf(buf, sizeof buf, ".. %5lu free\n", j); | ||
347 | write(fd, buf, strlen(buf)); | ||
348 | } else if (pd[PI_OFF(j)] == MALLOC_FIRST) { | ||
349 | for (j++; j <= last_index && pd[PI_OFF(j)] == MALLOC_FOLLOW;) { | ||
350 | if (!PI_OFF(++j)) { | ||
351 | if ((pi = pi->next) == NULL || | ||
352 | PD_IDX(pi->dirnum) != PI_IDX(j)) | ||
353 | break; | ||
354 | pd = pi->base; | ||
355 | j += pdi_mod; | ||
356 | } | ||
357 | } | ||
358 | j--; | ||
359 | snprintf(buf, sizeof buf, ".. %5lu in use\n", j); | ||
360 | write(fd, buf, strlen(buf)); | ||
361 | } else if (pd[PI_OFF(j)] < MALLOC_MAGIC) { | ||
362 | snprintf(buf, sizeof buf, "(%p)\n", pd[PI_OFF(j)]); | ||
363 | write(fd, buf, strlen(buf)); | ||
364 | } else { | ||
365 | snprintf(buf, sizeof buf, "%p %d (of %d) x %d @ %p --> %p\n", | ||
366 | pd[PI_OFF(j)], pd[PI_OFF(j)]->free, | ||
367 | pd[PI_OFF(j)]->total, pd[PI_OFF(j)]->size, | ||
368 | pd[PI_OFF(j)]->page, pd[PI_OFF(j)]->next); | ||
369 | write(fd, buf, strlen(buf)); | ||
152 | } | 370 | } |
153 | bucket = 0; | 371 | if (!PI_OFF(++j)) { |
154 | amt = 8; | 372 | if ((pi = pi->next) == NULL) |
155 | while (pagesz > amt) { | 373 | break; |
156 | amt <<= 1; | 374 | pd = pi->base; |
157 | bucket++; | 375 | j += (1 + PD_IDX(pi->dirnum) - PI_IDX(j)) * pdi_mod; |
158 | } | 376 | } |
159 | pagebucket = bucket; | ||
160 | } | 377 | } |
161 | /* | 378 | |
162 | * Convert amount of memory requested into closest block size | 379 | for (pf = free_list.next; pf; pf = pf->next) { |
163 | * stored in hash buckets which satisfies request. | 380 | snprintf(buf, sizeof buf, "Free: @%p [%p...%p[ %ld ->%p <-%p\n", |
164 | * Account for space used per block for accounting. | 381 | pf, pf->page, (char *)pf->page + pf->size, |
165 | */ | 382 | pf->size, pf->prev, pf->next); |
166 | if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { | 383 | write(fd, buf, strlen(buf)); |
167 | #ifndef RCHECK | 384 | if (pf == pf->next) { |
168 | amt = 8; /* size of first bucket */ | 385 | snprintf(buf, sizeof buf, "Free_list loops\n"); |
169 | bucket = 0; | 386 | write(fd, buf, strlen(buf)); |
170 | #else | 387 | break; |
171 | amt = 16; /* size of first bucket */ | 388 | } |
172 | bucket = 1; | 389 | } |
390 | |||
391 | /* print out various info */ | ||
392 | snprintf(buf, sizeof buf, "Minsize\t%lu\n", malloc_minsize); | ||
393 | write(fd, buf, strlen(buf)); | ||
394 | snprintf(buf, sizeof buf, "Maxsize\t%lu\n", malloc_maxsize); | ||
395 | write(fd, buf, strlen(buf)); | ||
396 | snprintf(buf, sizeof buf, "Pagesize\t%lu\n", malloc_pagesize); | ||
397 | write(fd, buf, strlen(buf)); | ||
398 | snprintf(buf, sizeof buf, "Pageshift\t%u\n", malloc_pageshift); | ||
399 | write(fd, buf, strlen(buf)); | ||
400 | snprintf(buf, sizeof buf, "In use\t%lu\n", (u_long) malloc_used); | ||
401 | write(fd, buf, strlen(buf)); | ||
402 | snprintf(buf, sizeof buf, "Guarded\t%lu\n", (u_long) malloc_guarded); | ||
403 | write(fd, buf, strlen(buf)); | ||
404 | } | ||
405 | #endif /* MALLOC_STATS */ | ||
406 | |||
407 | extern char *__progname; | ||
408 | |||
409 | static void | ||
410 | wrterror(char *p) | ||
411 | { | ||
412 | char *q = " error: "; | ||
413 | struct iovec iov[5]; | ||
414 | |||
415 | iov[0].iov_base = __progname; | ||
416 | iov[0].iov_len = strlen(__progname); | ||
417 | iov[1].iov_base = malloc_func; | ||
418 | iov[1].iov_len = strlen(malloc_func); | ||
419 | iov[2].iov_base = q; | ||
420 | iov[2].iov_len = strlen(q); | ||
421 | iov[3].iov_base = p; | ||
422 | iov[3].iov_len = strlen(p); | ||
423 | iov[4].iov_base = "\n"; | ||
424 | iov[4].iov_len = 1; | ||
425 | writev(STDERR_FILENO, iov, 5); | ||
426 | |||
427 | suicide = 1; | ||
428 | #ifdef MALLOC_STATS | ||
429 | if (malloc_stats) | ||
430 | malloc_dump(STDERR_FILENO); | ||
431 | #endif /* MALLOC_STATS */ | ||
432 | malloc_active--; | ||
433 | if (malloc_abort) | ||
434 | abort(); | ||
435 | } | ||
436 | |||
437 | static void | ||
438 | wrtwarning(char *p) | ||
439 | { | ||
440 | char *q = " warning: "; | ||
441 | struct iovec iov[5]; | ||
442 | |||
443 | if (malloc_abort) | ||
444 | wrterror(p); | ||
445 | else if (malloc_silent) | ||
446 | return; | ||
447 | |||
448 | iov[0].iov_base = __progname; | ||
449 | iov[0].iov_len = strlen(__progname); | ||
450 | iov[1].iov_base = malloc_func; | ||
451 | iov[1].iov_len = strlen(malloc_func); | ||
452 | iov[2].iov_base = q; | ||
453 | iov[2].iov_len = strlen(q); | ||
454 | iov[3].iov_base = p; | ||
455 | iov[3].iov_len = strlen(p); | ||
456 | iov[4].iov_base = "\n"; | ||
457 | iov[4].iov_len = 1; | ||
458 | |||
459 | writev(STDERR_FILENO, iov, 5); | ||
460 | } | ||
461 | |||
462 | #ifdef MALLOC_STATS | ||
463 | static void | ||
464 | malloc_exit(void) | ||
465 | { | ||
466 | char *q = "malloc() warning: Couldn't dump stats\n"; | ||
467 | int save_errno = errno, fd; | ||
468 | |||
469 | fd = open("malloc.out", O_RDWR|O_APPEND); | ||
470 | if (fd != -1) { | ||
471 | malloc_dump(fd); | ||
472 | close(fd); | ||
473 | } else | ||
474 | write(STDERR_FILENO, q, strlen(q)); | ||
475 | errno = save_errno; | ||
476 | } | ||
477 | #endif /* MALLOC_STATS */ | ||
478 | |||
479 | /* | ||
480 | * Allocate a number of pages from the OS | ||
481 | */ | ||
482 | static void * | ||
483 | map_pages(size_t pages) | ||
484 | { | ||
485 | struct pdinfo *pi, *spi; | ||
486 | struct pginfo **pd; | ||
487 | u_long idx, pidx, lidx; | ||
488 | caddr_t result, tail; | ||
489 | u_long index, lindex; | ||
490 | void *pdregion = NULL; | ||
491 | size_t dirs, cnt; | ||
492 | |||
493 | pages <<= malloc_pageshift; | ||
494 | result = MMAP(pages + malloc_guard); | ||
495 | if (result == MAP_FAILED) { | ||
496 | #ifdef MALLOC_EXTRA_SANITY | ||
497 | wrtwarning("(ES): map_pages fails"); | ||
498 | #endif /* MALLOC_EXTRA_SANITY */ | ||
499 | errno = ENOMEM; | ||
500 | return (NULL); | ||
501 | } | ||
502 | index = ptr2index(result); | ||
503 | tail = result + pages + malloc_guard; | ||
504 | lindex = ptr2index(tail) - 1; | ||
505 | if (malloc_guard) | ||
506 | mprotect(result + pages, malloc_guard, PROT_NONE); | ||
507 | |||
508 | pidx = PI_IDX(index); | ||
509 | lidx = PI_IDX(lindex); | ||
510 | |||
511 | if (tail > malloc_brk) { | ||
512 | malloc_brk = tail; | ||
513 | last_index = lindex; | ||
514 | } | ||
515 | |||
516 | dirs = lidx - pidx; | ||
517 | |||
518 | /* Insert directory pages, if needed. */ | ||
519 | if (pdir_lookup(index, &pi) != 0) | ||
520 | dirs++; | ||
521 | |||
522 | if (dirs > 0) { | ||
523 | pdregion = MMAP(malloc_pagesize * dirs); | ||
524 | if (pdregion == MAP_FAILED) { | ||
525 | munmap(result, tail - result); | ||
526 | #ifdef MALLOC_EXTRA_SANITY | ||
527 | wrtwarning("(ES): map_pages fails"); | ||
173 | #endif | 528 | #endif |
174 | n = -((long)sizeof (*op) + RSLOP); | 529 | errno = ENOMEM; |
175 | } else { | 530 | return (NULL); |
176 | amt = pagesz; | 531 | } |
177 | bucket = pagebucket; | ||
178 | } | 532 | } |
179 | while (nbytes > amt + n) { | 533 | |
180 | amt <<= 1; | 534 | cnt = 0; |
181 | if (amt == 0) | 535 | for (idx = pidx, spi = pi; idx <= lidx; idx++) { |
536 | if (pi == NULL || PD_IDX(pi->dirnum) != idx) { | ||
537 | pd = (struct pginfo **)((char *)pdregion + | ||
538 | cnt * malloc_pagesize); | ||
539 | cnt++; | ||
540 | memset(pd, 0, malloc_pagesize); | ||
541 | pi = (struct pdinfo *) ((caddr_t) pd + pdi_off); | ||
542 | pi->base = pd; | ||
543 | pi->prev = spi; | ||
544 | pi->next = spi->next; | ||
545 | pi->dirnum = idx * (malloc_pagesize / | ||
546 | sizeof(struct pginfo *)); | ||
547 | |||
548 | if (spi->next != NULL) | ||
549 | spi->next->prev = pi; | ||
550 | spi->next = pi; | ||
551 | } | ||
552 | if (idx > pidx && idx < lidx) { | ||
553 | pi->dirnum += pdi_mod; | ||
554 | } else if (idx == pidx) { | ||
555 | if (pidx == lidx) { | ||
556 | pi->dirnum += (u_long)(tail - result) >> | ||
557 | malloc_pageshift; | ||
558 | } else { | ||
559 | pi->dirnum += pdi_mod - PI_OFF(index); | ||
560 | } | ||
561 | } else { | ||
562 | pi->dirnum += PI_OFF(ptr2index(tail - 1)) + 1; | ||
563 | } | ||
564 | #ifdef MALLOC_EXTRA_SANITY | ||
565 | if (PD_OFF(pi->dirnum) > pdi_mod || PD_IDX(pi->dirnum) > idx) { | ||
566 | wrterror("(ES): pages directory overflow"); | ||
567 | errno = EFAULT; | ||
182 | return (NULL); | 568 | return (NULL); |
183 | bucket++; | 569 | } |
570 | #endif /* MALLOC_EXTRA_SANITY */ | ||
571 | if (idx == pidx && pi != last_dir) { | ||
572 | prev_dir = last_dir; | ||
573 | last_dir = pi; | ||
574 | } | ||
575 | spi = pi; | ||
576 | pi = spi->next; | ||
184 | } | 577 | } |
185 | /* | 578 | #ifdef MALLOC_EXTRA_SANITY |
186 | * If nothing in hash bucket right now, | 579 | if (cnt > dirs) |
187 | * request more memory from the system. | 580 | wrtwarning("(ES): cnt > dirs"); |
188 | */ | 581 | #endif /* MALLOC_EXTRA_SANITY */ |
189 | if ((op = nextf[bucket]) == NULL) { | 582 | if (cnt < dirs) |
190 | morecore(bucket); | 583 | munmap((char *)pdregion + cnt * malloc_pagesize, |
191 | if ((op = nextf[bucket]) == NULL) | 584 | (dirs - cnt) * malloc_pagesize); |
192 | return (NULL); | 585 | |
193 | } | 586 | return (result); |
194 | /* remove from linked list */ | ||
195 | nextf[bucket] = op->ov_next; | ||
196 | op->ov_magic = MAGIC; | ||
197 | op->ov_index = bucket; | ||
198 | #ifdef MSTATS | ||
199 | nmalloc[bucket]++; | ||
200 | #endif | ||
201 | #ifdef RCHECK | ||
202 | /* | ||
203 | * Record allocated size of block and | ||
204 | * bound space with magic numbers. | ||
205 | */ | ||
206 | op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); | ||
207 | op->ov_rmagic = RMAGIC; | ||
208 | *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; | ||
209 | #endif | ||
210 | return ((char *)(op + 1)); | ||
211 | } | 587 | } |
212 | 588 | ||
213 | /* | 589 | /* |
214 | * Allocate more memory to the indicated bucket. | 590 | * Initialize the world |
215 | */ | 591 | */ |
216 | static void | 592 | static void |
217 | morecore(bucket) | 593 | malloc_init(void) |
218 | int bucket; | ||
219 | { | 594 | { |
220 | register union overhead *op; | 595 | char *p, b[64]; |
221 | register long sz; /* size of desired block */ | 596 | int i, j, save_errno = errno; |
222 | long amt; /* amount to allocate */ | 597 | |
223 | int nblks; /* how many blocks we get */ | 598 | _MALLOC_LOCK_INIT(); |
599 | |||
600 | #ifdef MALLOC_EXTRA_SANITY | ||
601 | malloc_junk = 1; | ||
602 | #endif /* MALLOC_EXTRA_SANITY */ | ||
603 | |||
604 | for (i = 0; i < 3; i++) { | ||
605 | switch (i) { | ||
606 | case 0: | ||
607 | j = readlink("/etc/malloc.conf", b, sizeof b - 1); | ||
608 | if (j <= 0) | ||
609 | continue; | ||
610 | b[j] = '\0'; | ||
611 | p = b; | ||
612 | break; | ||
613 | case 1: | ||
614 | if (issetugid() == 0) | ||
615 | p = getenv("MALLOC_OPTIONS"); | ||
616 | else | ||
617 | continue; | ||
618 | break; | ||
619 | case 2: | ||
620 | p = malloc_options; | ||
621 | break; | ||
622 | default: | ||
623 | p = NULL; | ||
624 | } | ||
625 | |||
626 | for (; p != NULL && *p != '\0'; p++) { | ||
627 | switch (*p) { | ||
628 | case '>': | ||
629 | malloc_cache <<= 1; | ||
630 | break; | ||
631 | case '<': | ||
632 | malloc_cache >>= 1; | ||
633 | break; | ||
634 | case 'a': | ||
635 | malloc_abort = 0; | ||
636 | break; | ||
637 | case 'A': | ||
638 | malloc_abort = 1; | ||
639 | break; | ||
640 | #ifdef MALLOC_STATS | ||
641 | case 'd': | ||
642 | malloc_stats = 0; | ||
643 | break; | ||
644 | case 'D': | ||
645 | malloc_stats = 1; | ||
646 | break; | ||
647 | #endif /* MALLOC_STATS */ | ||
648 | case 'f': | ||
649 | malloc_freeprot = 0; | ||
650 | break; | ||
651 | case 'F': | ||
652 | malloc_freeprot = 1; | ||
653 | break; | ||
654 | case 'g': | ||
655 | malloc_guard = 0; | ||
656 | break; | ||
657 | case 'G': | ||
658 | malloc_guard = malloc_pagesize; | ||
659 | break; | ||
660 | case 'h': | ||
661 | malloc_hint = 0; | ||
662 | break; | ||
663 | case 'H': | ||
664 | malloc_hint = 1; | ||
665 | break; | ||
666 | case 'j': | ||
667 | malloc_junk = 0; | ||
668 | break; | ||
669 | case 'J': | ||
670 | malloc_junk = 1; | ||
671 | break; | ||
672 | case 'n': | ||
673 | malloc_silent = 0; | ||
674 | break; | ||
675 | case 'N': | ||
676 | malloc_silent = 1; | ||
677 | break; | ||
678 | case 'p': | ||
679 | malloc_ptrguard = 0; | ||
680 | break; | ||
681 | case 'P': | ||
682 | malloc_ptrguard = 1; | ||
683 | break; | ||
684 | case 'r': | ||
685 | malloc_realloc = 0; | ||
686 | break; | ||
687 | case 'R': | ||
688 | malloc_realloc = 1; | ||
689 | break; | ||
690 | #ifdef __FreeBSD__ | ||
691 | case 'u': | ||
692 | malloc_utrace = 0; | ||
693 | break; | ||
694 | case 'U': | ||
695 | malloc_utrace = 1; | ||
696 | break; | ||
697 | #endif /* __FreeBSD__ */ | ||
698 | case 'x': | ||
699 | malloc_xmalloc = 0; | ||
700 | break; | ||
701 | case 'X': | ||
702 | malloc_xmalloc = 1; | ||
703 | break; | ||
704 | case 'z': | ||
705 | malloc_zero = 0; | ||
706 | break; | ||
707 | case 'Z': | ||
708 | malloc_zero = 1; | ||
709 | break; | ||
710 | default: | ||
711 | j = malloc_abort; | ||
712 | malloc_abort = 0; | ||
713 | wrtwarning("unknown char in MALLOC_OPTIONS"); | ||
714 | malloc_abort = j; | ||
715 | break; | ||
716 | } | ||
717 | } | ||
718 | } | ||
719 | |||
720 | UTRACE(0, 0, 0); | ||
224 | 721 | ||
225 | /* | 722 | /* |
226 | * sbrk_size <= 0 only for big, FLUFFY, requests (about | 723 | * We want junk in the entire allocation, and zero only in the part |
227 | * 2^30 bytes on a VAX, I think) or for a negative arg. | 724 | * the user asked for. |
228 | */ | 725 | */ |
229 | sz = 1 << (bucket + 3); | 726 | if (malloc_zero) |
230 | #ifdef DEBUG | 727 | malloc_junk = 1; |
231 | ASSERT(sz > 0); | 728 | |
232 | #else | 729 | #ifdef MALLOC_STATS |
233 | if (sz <= 0) | 730 | if (malloc_stats && (atexit(malloc_exit) == -1)) |
731 | wrtwarning("atexit(2) failed." | ||
732 | " Will not be able to dump malloc stats on exit"); | ||
733 | #endif /* MALLOC_STATS */ | ||
734 | |||
735 | /* Allocate one page for the page directory. */ | ||
736 | page_dir = (struct pginfo **)MMAP(malloc_pagesize); | ||
737 | |||
738 | if (page_dir == MAP_FAILED) { | ||
739 | wrterror("mmap(2) failed, check limits"); | ||
740 | errno = ENOMEM; | ||
234 | return; | 741 | return; |
235 | #endif | ||
236 | if (sz < pagesz) { | ||
237 | amt = pagesz; | ||
238 | nblks = amt / sz; | ||
239 | } else { | ||
240 | amt = sz + pagesz; | ||
241 | nblks = 1; | ||
242 | } | 742 | } |
243 | op = (union overhead *)sbrk(amt); | 743 | pdi_off = (malloc_pagesize - sizeof(struct pdinfo)) & ~(malloc_minsize - 1); |
244 | /* no more room! */ | 744 | pdi_mod = pdi_off / sizeof(struct pginfo *); |
245 | if ((long)op == -1) | 745 | |
246 | return; | 746 | last_dir = (struct pdinfo *) ((caddr_t) page_dir + pdi_off); |
747 | last_dir->base = page_dir; | ||
748 | last_dir->prev = last_dir->next = NULL; | ||
749 | last_dir->dirnum = malloc_pageshift; | ||
750 | |||
751 | /* Been here, done that. */ | ||
752 | malloc_started++; | ||
753 | |||
754 | /* Recalculate the cache size in bytes, and make sure it's nonzero. */ | ||
755 | if (!malloc_cache) | ||
756 | malloc_cache++; | ||
757 | malloc_cache <<= malloc_pageshift; | ||
758 | errno = save_errno; | ||
759 | } | ||
760 | |||
761 | /* | ||
762 | * Allocate a number of complete pages | ||
763 | */ | ||
764 | static void * | ||
765 | malloc_pages(size_t size) | ||
766 | { | ||
767 | void *p, *delay_free = NULL, *tp; | ||
768 | int i; | ||
769 | struct pginfo **pd; | ||
770 | struct pdinfo *pi; | ||
771 | u_long pidx, index; | ||
772 | struct pgfree *pf; | ||
773 | |||
774 | size = pageround(size) + malloc_guard; | ||
775 | |||
776 | p = NULL; | ||
777 | /* Look for free pages before asking for more */ | ||
778 | for (pf = free_list.next; pf; pf = pf->next) { | ||
779 | |||
780 | #ifdef MALLOC_EXTRA_SANITY | ||
781 | if (pf->size & malloc_pagemask) { | ||
782 | wrterror("(ES): junk length entry on free_list"); | ||
783 | errno = EFAULT; | ||
784 | return (NULL); | ||
785 | } | ||
786 | if (!pf->size) { | ||
787 | wrterror("(ES): zero length entry on free_list"); | ||
788 | errno = EFAULT; | ||
789 | return (NULL); | ||
790 | } | ||
791 | if (pf->page > (pf->page + pf->size)) { | ||
792 | wrterror("(ES): sick entry on free_list"); | ||
793 | errno = EFAULT; | ||
794 | return (NULL); | ||
795 | } | ||
796 | if ((pi = pf->pdir) == NULL) { | ||
797 | wrterror("(ES): invalid page directory on free-list"); | ||
798 | errno = EFAULT; | ||
799 | return (NULL); | ||
800 | } | ||
801 | if ((pidx = PI_IDX(ptr2index(pf->page))) != PD_IDX(pi->dirnum)) { | ||
802 | wrterror("(ES): directory index mismatch on free-list"); | ||
803 | errno = EFAULT; | ||
804 | return (NULL); | ||
805 | } | ||
806 | pd = pi->base; | ||
807 | if (pd[PI_OFF(ptr2index(pf->page))] != MALLOC_FREE) { | ||
808 | wrterror("(ES): non-free first page on free-list"); | ||
809 | errno = EFAULT; | ||
810 | return (NULL); | ||
811 | } | ||
812 | pidx = PI_IDX(ptr2index((pf->page) + (pf->size)) - 1); | ||
813 | for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx; | ||
814 | pi = pi->next) | ||
815 | ; | ||
816 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
817 | wrterror("(ES): last page not referenced in page directory"); | ||
818 | errno = EFAULT; | ||
819 | return (NULL); | ||
820 | } | ||
821 | pd = pi->base; | ||
822 | if (pd[PI_OFF(ptr2index((pf->page) + (pf->size)) - 1)] != MALLOC_FREE) { | ||
823 | wrterror("(ES): non-free last page on free-list"); | ||
824 | errno = EFAULT; | ||
825 | return (NULL); | ||
826 | } | ||
827 | #endif /* MALLOC_EXTRA_SANITY */ | ||
828 | |||
829 | if (pf->size < size) | ||
830 | continue; | ||
831 | |||
832 | if (pf->size == size) { | ||
833 | p = pf->page; | ||
834 | pi = pf->pdir; | ||
835 | if (pf->next != NULL) | ||
836 | pf->next->prev = pf->prev; | ||
837 | pf->prev->next = pf->next; | ||
838 | delay_free = pf; | ||
839 | break; | ||
840 | } | ||
841 | p = pf->page; | ||
842 | pf->page = (char *) pf->page + size; | ||
843 | pf->size -= size; | ||
844 | pidx = PI_IDX(ptr2index(pf->page)); | ||
845 | for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx; | ||
846 | pi = pi->next) | ||
847 | ; | ||
848 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
849 | wrterror("(ES): hole in directories"); | ||
850 | errno = EFAULT; | ||
851 | return (NULL); | ||
852 | } | ||
853 | tp = pf->pdir; | ||
854 | pf->pdir = pi; | ||
855 | pi = tp; | ||
856 | break; | ||
857 | } | ||
858 | |||
859 | size -= malloc_guard; | ||
860 | |||
861 | #ifdef MALLOC_EXTRA_SANITY | ||
862 | if (p != NULL && pi != NULL) { | ||
863 | pidx = PD_IDX(pi->dirnum); | ||
864 | pd = pi->base; | ||
865 | } | ||
866 | if (p != NULL && pd[PI_OFF(ptr2index(p))] != MALLOC_FREE) { | ||
867 | wrterror("(ES): allocated non-free page on free-list"); | ||
868 | errno = EFAULT; | ||
869 | return (NULL); | ||
870 | } | ||
871 | #endif /* MALLOC_EXTRA_SANITY */ | ||
872 | |||
873 | if (p != NULL && (malloc_guard || malloc_freeprot)) | ||
874 | mprotect(p, size, PROT_READ | PROT_WRITE); | ||
875 | |||
876 | size >>= malloc_pageshift; | ||
877 | |||
878 | /* Map new pages */ | ||
879 | if (p == NULL) | ||
880 | p = map_pages(size); | ||
881 | |||
882 | if (p != NULL) { | ||
883 | index = ptr2index(p); | ||
884 | pidx = PI_IDX(index); | ||
885 | pdir_lookup(index, &pi); | ||
886 | #ifdef MALLOC_EXTRA_SANITY | ||
887 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
888 | wrterror("(ES): mapped pages not found in directory"); | ||
889 | errno = EFAULT; | ||
890 | return (NULL); | ||
891 | } | ||
892 | #endif /* MALLOC_EXTRA_SANITY */ | ||
893 | if (pi != last_dir) { | ||
894 | prev_dir = last_dir; | ||
895 | last_dir = pi; | ||
896 | } | ||
897 | pd = pi->base; | ||
898 | pd[PI_OFF(index)] = MALLOC_FIRST; | ||
899 | for (i = 1; i < size; i++) { | ||
900 | if (!PI_OFF(index + i)) { | ||
901 | pidx++; | ||
902 | pi = pi->next; | ||
903 | #ifdef MALLOC_EXTRA_SANITY | ||
904 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
905 | wrterror("(ES): hole in mapped pages directory"); | ||
906 | errno = EFAULT; | ||
907 | return (NULL); | ||
908 | } | ||
909 | #endif /* MALLOC_EXTRA_SANITY */ | ||
910 | pd = pi->base; | ||
911 | } | ||
912 | pd[PI_OFF(index + i)] = MALLOC_FOLLOW; | ||
913 | } | ||
914 | if (malloc_guard) { | ||
915 | if (!PI_OFF(index + i)) { | ||
916 | pidx++; | ||
917 | pi = pi->next; | ||
918 | #ifdef MALLOC_EXTRA_SANITY | ||
919 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
920 | wrterror("(ES): hole in mapped pages directory"); | ||
921 | errno = EFAULT; | ||
922 | return (NULL); | ||
923 | } | ||
924 | #endif /* MALLOC_EXTRA_SANITY */ | ||
925 | pd = pi->base; | ||
926 | } | ||
927 | pd[PI_OFF(index + i)] = MALLOC_FIRST; | ||
928 | } | ||
929 | malloc_used += size << malloc_pageshift; | ||
930 | malloc_guarded += malloc_guard; | ||
931 | |||
932 | if (malloc_junk) | ||
933 | memset(p, SOME_JUNK, size << malloc_pageshift); | ||
934 | } | ||
935 | if (delay_free) { | ||
936 | if (px == NULL) | ||
937 | px = delay_free; | ||
938 | else | ||
939 | ifree(delay_free); | ||
940 | } | ||
941 | return (p); | ||
942 | } | ||
943 | |||
944 | /* | ||
945 | * Allocate a page of fragments | ||
946 | */ | ||
947 | |||
948 | static __inline__ int | ||
949 | malloc_make_chunks(int bits) | ||
950 | { | ||
951 | struct pginfo *bp, **pd; | ||
952 | struct pdinfo *pi; | ||
953 | #ifdef MALLOC_EXTRA_SANITY | ||
954 | u_long pidx; | ||
955 | #endif /* MALLOC_EXTRA_SANITY */ | ||
956 | void *pp; | ||
957 | long i, k; | ||
958 | size_t l; | ||
959 | |||
960 | /* Allocate a new bucket */ | ||
961 | pp = malloc_pages((size_t) malloc_pagesize); | ||
962 | if (pp == NULL) | ||
963 | return (0); | ||
964 | |||
965 | /* Find length of admin structure */ | ||
966 | l = sizeof *bp - sizeof(u_long); | ||
967 | l += sizeof(u_long) * | ||
968 | (((malloc_pagesize >> bits) + MALLOC_BITS - 1) / MALLOC_BITS); | ||
969 | |||
970 | /* Don't waste more than two chunks on this */ | ||
971 | |||
247 | /* | 972 | /* |
248 | * Add new memory allocated to that on | 973 | * If we are to allocate a memory protected page for the malloc(0) |
249 | * free list for this hash bucket. | 974 | * case (when bits=0), it must be from a different page than the |
975 | * pginfo page. | ||
976 | * --> Treat it like the big chunk alloc, get a second data page. | ||
250 | */ | 977 | */ |
251 | nextf[bucket] = op; | 978 | if (bits != 0 && (1UL << (bits)) <= l + l) { |
252 | while (--nblks > 0) { | 979 | bp = (struct pginfo *) pp; |
253 | op->ov_next = (union overhead *)((caddr_t)op + sz); | 980 | } else { |
254 | op = (union overhead *)((caddr_t)op + sz); | 981 | bp = (struct pginfo *) imalloc(l); |
255 | } | 982 | if (bp == NULL) { |
983 | ifree(pp); | ||
984 | return (0); | ||
985 | } | ||
986 | } | ||
987 | |||
988 | /* memory protect the page allocated in the malloc(0) case */ | ||
989 | if (bits == 0) { | ||
990 | bp->size = 0; | ||
991 | bp->shift = 1; | ||
992 | i = malloc_minsize - 1; | ||
993 | while (i >>= 1) | ||
994 | bp->shift++; | ||
995 | bp->total = bp->free = malloc_pagesize >> bp->shift; | ||
996 | bp->page = pp; | ||
997 | |||
998 | k = mprotect(pp, malloc_pagesize, PROT_NONE); | ||
999 | if (k < 0) { | ||
1000 | ifree(pp); | ||
1001 | ifree(bp); | ||
1002 | return (0); | ||
1003 | } | ||
1004 | } else { | ||
1005 | bp->size = (1UL << bits); | ||
1006 | bp->shift = bits; | ||
1007 | bp->total = bp->free = malloc_pagesize >> bits; | ||
1008 | bp->page = pp; | ||
1009 | } | ||
1010 | |||
1011 | /* set all valid bits in the bitmap */ | ||
1012 | k = bp->total; | ||
1013 | i = 0; | ||
1014 | |||
1015 | /* Do a bunch at a time */ | ||
1016 | for (; (k - i) >= MALLOC_BITS; i += MALLOC_BITS) | ||
1017 | bp->bits[i / MALLOC_BITS] = ~0UL; | ||
1018 | |||
1019 | for (; i < k; i++) | ||
1020 | bp->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); | ||
1021 | |||
1022 | k = (long)l; | ||
1023 | if (bp == bp->page) { | ||
1024 | /* Mark the ones we stole for ourselves */ | ||
1025 | for (i = 0; k > 0; i++) { | ||
1026 | bp->bits[i / MALLOC_BITS] &= ~(1UL << (i % MALLOC_BITS)); | ||
1027 | bp->free--; | ||
1028 | bp->total--; | ||
1029 | k -= (1 << bits); | ||
1030 | } | ||
1031 | } | ||
1032 | /* MALLOC_LOCK */ | ||
1033 | |||
1034 | pdir_lookup(ptr2index(pp), &pi); | ||
1035 | #ifdef MALLOC_EXTRA_SANITY | ||
1036 | pidx = PI_IDX(ptr2index(pp)); | ||
1037 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
1038 | wrterror("(ES): mapped pages not found in directory"); | ||
1039 | errno = EFAULT; | ||
1040 | return (0); | ||
1041 | } | ||
1042 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1043 | if (pi != last_dir) { | ||
1044 | prev_dir = last_dir; | ||
1045 | last_dir = pi; | ||
1046 | } | ||
1047 | pd = pi->base; | ||
1048 | pd[PI_OFF(ptr2index(pp))] = bp; | ||
1049 | |||
1050 | bp->next = page_dir[bits]; | ||
1051 | page_dir[bits] = bp; | ||
1052 | |||
1053 | /* MALLOC_UNLOCK */ | ||
1054 | return (1); | ||
256 | } | 1055 | } |
257 | 1056 | ||
258 | void | 1057 | /* |
259 | free(cp) | 1058 | * Allocate a fragment |
260 | void *cp; | 1059 | */ |
261 | { | 1060 | static void * |
262 | register long size; | 1061 | malloc_bytes(size_t size) |
263 | register union overhead *op; | 1062 | { |
264 | 1063 | int i, j; | |
265 | if (cp == NULL) | 1064 | size_t k; |
266 | return; | 1065 | u_long u, *lp; |
267 | op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | 1066 | struct pginfo *bp; |
268 | #ifdef DEBUG | 1067 | |
269 | ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ | 1068 | /* Don't bother with anything less than this */ |
270 | #else | 1069 | /* unless we have a malloc(0) requests */ |
271 | if (op->ov_magic != MAGIC) | 1070 | if (size != 0 && size < malloc_minsize) |
272 | return; /* sanity */ | 1071 | size = malloc_minsize; |
273 | #endif | 1072 | |
274 | #ifdef RCHECK | 1073 | /* Find the right bucket */ |
275 | ASSERT(op->ov_rmagic == RMAGIC); | 1074 | if (size == 0) |
276 | ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); | 1075 | j = 0; |
277 | #endif | 1076 | else { |
278 | size = op->ov_index; | 1077 | j = 1; |
279 | ASSERT(size < NBUCKETS); | 1078 | i = size - 1; |
280 | op->ov_next = nextf[size]; /* also clobbers ov_magic */ | 1079 | while (i >>= 1) |
281 | nextf[size] = op; | 1080 | j++; |
282 | #ifdef MSTATS | 1081 | } |
283 | nmalloc[size]--; | 1082 | |
284 | #endif | 1083 | /* If it's empty, make a page more of that size chunks */ |
1084 | if (page_dir[j] == NULL && !malloc_make_chunks(j)) | ||
1085 | return (NULL); | ||
1086 | |||
1087 | bp = page_dir[j]; | ||
1088 | |||
1089 | /* Find first word of bitmap which isn't empty */ | ||
1090 | for (lp = bp->bits; !*lp; lp++); | ||
1091 | |||
1092 | /* Find that bit, and tweak it */ | ||
1093 | u = 1; | ||
1094 | k = 0; | ||
1095 | while (!(*lp & u)) { | ||
1096 | u += u; | ||
1097 | k++; | ||
1098 | } | ||
1099 | |||
1100 | if (malloc_guard) { | ||
1101 | /* Walk to a random position. */ | ||
1102 | i = arc4random() % bp->free; | ||
1103 | while (i > 0) { | ||
1104 | u += u; | ||
1105 | k++; | ||
1106 | if (k >= MALLOC_BITS) { | ||
1107 | lp++; | ||
1108 | u = 1; | ||
1109 | k = 0; | ||
1110 | } | ||
1111 | #ifdef MALLOC_EXTRA_SANITY | ||
1112 | if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) { | ||
1113 | wrterror("chunk overflow"); | ||
1114 | errno = EFAULT; | ||
1115 | return (NULL); | ||
1116 | } | ||
1117 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1118 | if (*lp & u) | ||
1119 | i--; | ||
1120 | } | ||
1121 | } | ||
1122 | *lp ^= u; | ||
1123 | |||
1124 | /* If there are no more free, remove from free-list */ | ||
1125 | if (!--bp->free) { | ||
1126 | page_dir[j] = bp->next; | ||
1127 | bp->next = NULL; | ||
1128 | } | ||
1129 | /* Adjust to the real offset of that chunk */ | ||
1130 | k += (lp - bp->bits) * MALLOC_BITS; | ||
1131 | k <<= bp->shift; | ||
1132 | |||
1133 | if (malloc_junk && bp->size != 0) | ||
1134 | memset((char *)bp->page + k, SOME_JUNK, (size_t)bp->size); | ||
1135 | |||
1136 | return ((u_char *) bp->page + k); | ||
285 | } | 1137 | } |
286 | 1138 | ||
287 | /* | 1139 | /* |
288 | * When a program attempts "storage compaction" as mentioned in the | 1140 | * Magic so that malloc(sizeof(ptr)) is near the end of the page. |
289 | * old malloc man page, it realloc's an already freed block. Usually | ||
290 | * this is the last block it freed; occasionally it might be farther | ||
291 | * back. We have to search all the free lists for the block in order | ||
292 | * to determine its bucket: 1st we make one pass thru the lists | ||
293 | * checking only the first block in each; if that fails we search | ||
294 | * ``realloc_srchlen'' blocks in each list for a match (the variable | ||
295 | * is extern so the caller can modify it). If that fails we just copy | ||
296 | * however many bytes was given to realloc() and hope it's not huge. | ||
297 | */ | 1141 | */ |
298 | int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ | 1142 | #define PTR_GAP (malloc_pagesize - sizeof(void *)) |
1143 | #define PTR_SIZE (sizeof(void *)) | ||
1144 | #define PTR_ALIGNED(p) (((unsigned long)p & malloc_pagemask) == PTR_GAP) | ||
299 | 1145 | ||
300 | void * | 1146 | /* |
301 | realloc(cp, nbytes) | 1147 | * Allocate a piece of memory |
302 | void *cp; | 1148 | */ |
303 | size_t nbytes; | 1149 | static void * |
304 | { | 1150 | imalloc(size_t size) |
305 | register u_long onb; | 1151 | { |
306 | register long i; | 1152 | void *result; |
307 | union overhead *op; | 1153 | int ptralloc = 0; |
308 | char *res; | 1154 | |
309 | int was_alloced = 0; | 1155 | if (!malloc_started) |
310 | 1156 | malloc_init(); | |
311 | if (cp == NULL) | 1157 | |
312 | return (malloc(nbytes)); | 1158 | if (suicide) |
313 | op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | 1159 | abort(); |
314 | if (op->ov_magic == MAGIC) { | 1160 | |
315 | was_alloced++; | 1161 | /* does not matter if malloc_bytes fails */ |
316 | i = op->ov_index; | 1162 | if (px == NULL) |
1163 | px = malloc_bytes(sizeof *px); | ||
1164 | |||
1165 | if (malloc_ptrguard && size == PTR_SIZE) { | ||
1166 | ptralloc = 1; | ||
1167 | size = malloc_pagesize; | ||
1168 | } | ||
1169 | if ((size + malloc_pagesize) < size) { /* Check for overflow */ | ||
1170 | result = NULL; | ||
1171 | errno = ENOMEM; | ||
1172 | } else if (size <= malloc_maxsize) | ||
1173 | result = malloc_bytes(size); | ||
1174 | else | ||
1175 | result = malloc_pages(size); | ||
1176 | |||
1177 | if (malloc_abort == 1 && result == NULL) | ||
1178 | wrterror("allocation failed"); | ||
1179 | |||
1180 | if (malloc_zero && result != NULL) | ||
1181 | memset(result, 0, size); | ||
1182 | |||
1183 | if (result && ptralloc) | ||
1184 | return ((char *) result + PTR_GAP); | ||
1185 | return (result); | ||
1186 | } | ||
1187 | |||
1188 | /* | ||
1189 | * Change the size of an allocation. | ||
1190 | */ | ||
1191 | static void * | ||
1192 | irealloc(void *ptr, size_t size) | ||
1193 | { | ||
1194 | void *p; | ||
1195 | size_t osize; | ||
1196 | u_long index, i; | ||
1197 | struct pginfo **mp; | ||
1198 | struct pginfo **pd; | ||
1199 | struct pdinfo *pi; | ||
1200 | #ifdef MALLOC_EXTRA_SANITY | ||
1201 | u_long pidx; | ||
1202 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1203 | |||
1204 | if (suicide) | ||
1205 | abort(); | ||
1206 | |||
1207 | if (!malloc_started) { | ||
1208 | wrtwarning("malloc() has never been called"); | ||
1209 | return (NULL); | ||
1210 | } | ||
1211 | if (malloc_ptrguard && PTR_ALIGNED(ptr)) { | ||
1212 | if (size <= PTR_SIZE) | ||
1213 | return (ptr); | ||
1214 | |||
1215 | p = imalloc(size); | ||
1216 | if (p) | ||
1217 | memcpy(p, ptr, PTR_SIZE); | ||
1218 | ifree(ptr); | ||
1219 | return (p); | ||
1220 | } | ||
1221 | index = ptr2index(ptr); | ||
1222 | |||
1223 | if (index < malloc_pageshift) { | ||
1224 | wrtwarning("junk pointer, too low to make sense"); | ||
1225 | return (NULL); | ||
1226 | } | ||
1227 | if (index > last_index) { | ||
1228 | wrtwarning("junk pointer, too high to make sense"); | ||
1229 | return (NULL); | ||
1230 | } | ||
1231 | pdir_lookup(index, &pi); | ||
1232 | #ifdef MALLOC_EXTRA_SANITY | ||
1233 | pidx = PI_IDX(index); | ||
1234 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
1235 | wrterror("(ES): mapped pages not found in directory"); | ||
1236 | errno = EFAULT; | ||
1237 | return (NULL); | ||
1238 | } | ||
1239 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1240 | if (pi != last_dir) { | ||
1241 | prev_dir = last_dir; | ||
1242 | last_dir = pi; | ||
1243 | } | ||
1244 | pd = pi->base; | ||
1245 | mp = &pd[PI_OFF(index)]; | ||
1246 | |||
1247 | if (*mp == MALLOC_FIRST) { /* Page allocation */ | ||
1248 | |||
1249 | /* Check the pointer */ | ||
1250 | if ((u_long) ptr & malloc_pagemask) { | ||
1251 | wrtwarning("modified (page-) pointer"); | ||
1252 | return (NULL); | ||
1253 | } | ||
1254 | /* Find the size in bytes */ | ||
1255 | i = index; | ||
1256 | if (!PI_OFF(++i)) { | ||
1257 | pi = pi->next; | ||
1258 | if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i)) | ||
1259 | pi = NULL; | ||
1260 | if (pi != NULL) | ||
1261 | pd = pi->base; | ||
1262 | } | ||
1263 | for (osize = malloc_pagesize; | ||
1264 | pi != NULL && pd[PI_OFF(i)] == MALLOC_FOLLOW;) { | ||
1265 | osize += malloc_pagesize; | ||
1266 | if (!PI_OFF(++i)) { | ||
1267 | pi = pi->next; | ||
1268 | if (pi != NULL && PD_IDX(pi->dirnum) != PI_IDX(i)) | ||
1269 | pi = NULL; | ||
1270 | if (pi != NULL) | ||
1271 | pd = pi->base; | ||
1272 | } | ||
1273 | } | ||
1274 | |||
1275 | if (!malloc_realloc && size <= osize && | ||
1276 | size > osize - malloc_pagesize) { | ||
1277 | if (malloc_junk) | ||
1278 | memset((char *)ptr + size, SOME_JUNK, osize - size); | ||
1279 | return (ptr); /* ..don't do anything else. */ | ||
1280 | } | ||
1281 | } else if (*mp >= MALLOC_MAGIC) { /* Chunk allocation */ | ||
1282 | |||
1283 | /* Check the pointer for sane values */ | ||
1284 | if ((u_long) ptr & ((1UL << ((*mp)->shift)) - 1)) { | ||
1285 | wrtwarning("modified (chunk-) pointer"); | ||
1286 | return (NULL); | ||
1287 | } | ||
1288 | /* Find the chunk index in the page */ | ||
1289 | i = ((u_long) ptr & malloc_pagemask) >> (*mp)->shift; | ||
1290 | |||
1291 | /* Verify that it isn't a free chunk already */ | ||
1292 | if ((*mp)->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) { | ||
1293 | wrtwarning("chunk is already free"); | ||
1294 | return (NULL); | ||
1295 | } | ||
1296 | osize = (*mp)->size; | ||
1297 | |||
1298 | if (!malloc_realloc && size <= osize && | ||
1299 | (size > osize / 2 || osize == malloc_minsize)) { | ||
1300 | if (malloc_junk) | ||
1301 | memset((char *) ptr + size, SOME_JUNK, osize - size); | ||
1302 | return (ptr); /* ..don't do anything else. */ | ||
1303 | } | ||
1304 | } else { | ||
1305 | wrtwarning("irealloc: pointer to wrong page"); | ||
1306 | return (NULL); | ||
1307 | } | ||
1308 | |||
1309 | p = imalloc(size); | ||
1310 | |||
1311 | if (p != NULL) { | ||
1312 | /* copy the lesser of the two sizes, and free the old one */ | ||
1313 | /* Don't move from/to 0 sized region !!! */ | ||
1314 | if (osize != 0 && size != 0) { | ||
1315 | if (osize < size) | ||
1316 | memcpy(p, ptr, osize); | ||
1317 | else | ||
1318 | memcpy(p, ptr, size); | ||
1319 | } | ||
1320 | ifree(ptr); | ||
1321 | } | ||
1322 | return (p); | ||
1323 | } | ||
1324 | |||
1325 | /* | ||
1326 | * Free a sequence of pages | ||
1327 | */ | ||
1328 | static __inline__ void | ||
1329 | free_pages(void *ptr, u_long index, struct pginfo * info) | ||
1330 | { | ||
1331 | u_long i, pidx, lidx; | ||
1332 | size_t l, cachesize = 0; | ||
1333 | struct pginfo **pd; | ||
1334 | struct pdinfo *pi, *spi; | ||
1335 | struct pgfree *pf, *pt = NULL; | ||
1336 | caddr_t tail; | ||
1337 | |||
1338 | if (info == MALLOC_FREE) { | ||
1339 | wrtwarning("page is already free"); | ||
1340 | return; | ||
1341 | } | ||
1342 | if (info != MALLOC_FIRST) { | ||
1343 | wrtwarning("free_pages: pointer to wrong page"); | ||
1344 | return; | ||
1345 | } | ||
1346 | if ((u_long) ptr & malloc_pagemask) { | ||
1347 | wrtwarning("modified (page-) pointer"); | ||
1348 | return; | ||
1349 | } | ||
1350 | /* Count how many pages and mark them free at the same time */ | ||
1351 | pidx = PI_IDX(index); | ||
1352 | pdir_lookup(index, &pi); | ||
1353 | #ifdef MALLOC_EXTRA_SANITY | ||
1354 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
1355 | wrterror("(ES): mapped pages not found in directory"); | ||
1356 | errno = EFAULT; | ||
1357 | return; | ||
1358 | } | ||
1359 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1360 | |||
1361 | spi = pi; /* Save page index for start of region. */ | ||
1362 | |||
1363 | pd = pi->base; | ||
1364 | pd[PI_OFF(index)] = MALLOC_FREE; | ||
1365 | i = 1; | ||
1366 | if (!PI_OFF(index + i)) { | ||
1367 | pi = pi->next; | ||
1368 | if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i)) | ||
1369 | pi = NULL; | ||
1370 | else | ||
1371 | pd = pi->base; | ||
1372 | } | ||
1373 | while (pi != NULL && pd[PI_OFF(index + i)] == MALLOC_FOLLOW) { | ||
1374 | pd[PI_OFF(index + i)] = MALLOC_FREE; | ||
1375 | i++; | ||
1376 | if (!PI_OFF(index + i)) { | ||
1377 | if ((pi = pi->next) == NULL || | ||
1378 | PD_IDX(pi->dirnum) != PI_IDX(index + i)) | ||
1379 | pi = NULL; | ||
1380 | else | ||
1381 | pd = pi->base; | ||
1382 | } | ||
1383 | } | ||
1384 | |||
1385 | l = i << malloc_pageshift; | ||
1386 | |||
1387 | if (malloc_junk) | ||
1388 | memset(ptr, SOME_JUNK, l); | ||
1389 | |||
1390 | malloc_used -= l; | ||
1391 | malloc_guarded -= malloc_guard; | ||
1392 | if (malloc_guard) { | ||
1393 | #ifdef MALLOC_EXTRA_SANITY | ||
1394 | if (pi == NULL || PD_IDX(pi->dirnum) != PI_IDX(index + i)) { | ||
1395 | wrterror("(ES): hole in mapped pages directory"); | ||
1396 | errno = EFAULT; | ||
1397 | return; | ||
1398 | } | ||
1399 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1400 | pd[PI_OFF(index + i)] = MALLOC_FREE; | ||
1401 | l += malloc_guard; | ||
1402 | } | ||
1403 | tail = (caddr_t)ptr + l; | ||
1404 | |||
1405 | if (malloc_hint) | ||
1406 | madvise(ptr, l, MADV_FREE); | ||
1407 | |||
1408 | if (malloc_freeprot) | ||
1409 | mprotect(ptr, l, PROT_NONE); | ||
1410 | |||
1411 | /* Add to free-list. */ | ||
1412 | if (px == NULL && (px = malloc_bytes(sizeof *px)) == NULL) | ||
1413 | goto not_return; | ||
1414 | px->page = ptr; | ||
1415 | px->pdir = spi; | ||
1416 | px->size = l; | ||
1417 | |||
1418 | if (free_list.next == NULL) { | ||
1419 | /* Nothing on free list, put this at head. */ | ||
1420 | px->next = NULL; | ||
1421 | px->prev = &free_list; | ||
1422 | free_list.next = px; | ||
1423 | pf = px; | ||
1424 | px = NULL; | ||
317 | } else { | 1425 | } else { |
318 | /* | 1426 | /* |
319 | * Already free, doing "compaction". | 1427 | * Find the right spot, leave pf pointing to the modified |
320 | * | 1428 | * entry. |
321 | * Search for the old block of memory on the | ||
322 | * free list. First, check the most common | ||
323 | * case (last element free'd), then (this failing) | ||
324 | * the last ``realloc_srchlen'' items free'd. | ||
325 | * If all lookups fail, then assume the size of | ||
326 | * the memory block being realloc'd is the | ||
327 | * largest possible (so that all "nbytes" of new | ||
328 | * memory are copied into). Note that this could cause | ||
329 | * a memory fault if the old area was tiny, and the moon | ||
330 | * is gibbous. However, that is very unlikely. | ||
331 | */ | 1429 | */ |
332 | if ((i = findbucket(op, 1)) < 0 && | 1430 | |
333 | (i = findbucket(op, realloc_srchlen)) < 0) | 1431 | /* Race ahead here, while calculating cache size. */ |
334 | i = NBUCKETS; | 1432 | for (pf = free_list.next; |
1433 | (caddr_t)ptr > ((caddr_t)pf->page + pf->size) | ||
1434 | && pf->next != NULL; | ||
1435 | pf = pf->next) | ||
1436 | cachesize += pf->size; | ||
1437 | |||
1438 | /* Finish cache size calculation. */ | ||
1439 | pt = pf; | ||
1440 | while (pt) { | ||
1441 | cachesize += pt->size; | ||
1442 | pt = pt->next; | ||
1443 | } | ||
1444 | |||
1445 | if ((caddr_t)pf->page > tail) { | ||
1446 | /* Insert before entry */ | ||
1447 | px->next = pf; | ||
1448 | px->prev = pf->prev; | ||
1449 | pf->prev = px; | ||
1450 | px->prev->next = px; | ||
1451 | pf = px; | ||
1452 | px = NULL; | ||
1453 | } else if (((caddr_t)pf->page + pf->size) == ptr) { | ||
1454 | /* Append to the previous entry. */ | ||
1455 | cachesize -= pf->size; | ||
1456 | pf->size += l; | ||
1457 | if (pf->next != NULL && | ||
1458 | pf->next->page == ((caddr_t)pf->page + pf->size)) { | ||
1459 | /* And collapse the next too. */ | ||
1460 | pt = pf->next; | ||
1461 | pf->size += pt->size; | ||
1462 | pf->next = pt->next; | ||
1463 | if (pf->next != NULL) | ||
1464 | pf->next->prev = pf; | ||
1465 | } | ||
1466 | } else if (pf->page == tail) { | ||
1467 | /* Prepend to entry. */ | ||
1468 | cachesize -= pf->size; | ||
1469 | pf->size += l; | ||
1470 | pf->page = ptr; | ||
1471 | pf->pdir = spi; | ||
1472 | } else if (pf->next == NULL) { | ||
1473 | /* Append at tail of chain. */ | ||
1474 | px->next = NULL; | ||
1475 | px->prev = pf; | ||
1476 | pf->next = px; | ||
1477 | pf = px; | ||
1478 | px = NULL; | ||
1479 | } else { | ||
1480 | wrterror("freelist is destroyed"); | ||
1481 | errno = EFAULT; | ||
1482 | return; | ||
1483 | } | ||
335 | } | 1484 | } |
336 | onb = 1 << (i + 3); | 1485 | |
337 | if (onb < pagesz) | 1486 | if (pf->pdir != last_dir) { |
338 | onb -= sizeof (*op) + RSLOP; | 1487 | prev_dir = last_dir; |
339 | else | 1488 | last_dir = pf->pdir; |
340 | onb += pagesz - sizeof (*op) - RSLOP; | 1489 | } |
341 | /* avoid the copy if same size block */ | 1490 | |
342 | if (was_alloced) { | 1491 | /* Return something to OS ? */ |
343 | if (i) { | 1492 | if (pf->size > (malloc_cache - cachesize)) { |
344 | i = 1 << (i + 2); | 1493 | |
345 | if (i < pagesz) | 1494 | /* |
346 | i -= sizeof (*op) + RSLOP; | 1495 | * Keep the cache intact. Notice that the '>' above guarantees that |
347 | else | 1496 | * the pf will always have at least one page afterwards. |
348 | i += pagesz - sizeof (*op) - RSLOP; | 1497 | */ |
1498 | if (munmap((char *) pf->page + (malloc_cache - cachesize), | ||
1499 | pf->size - (malloc_cache - cachesize)) != 0) | ||
1500 | goto not_return; | ||
1501 | tail = (caddr_t)pf->page + pf->size; | ||
1502 | lidx = ptr2index(tail) - 1; | ||
1503 | pf->size = malloc_cache - cachesize; | ||
1504 | |||
1505 | index = ptr2index((caddr_t)pf->page + pf->size); | ||
1506 | |||
1507 | pidx = PI_IDX(index); | ||
1508 | if (prev_dir != NULL && PD_IDX(prev_dir->dirnum) >= pidx) | ||
1509 | prev_dir = NULL; /* Will be wiped out below ! */ | ||
1510 | |||
1511 | for (pi = pf->pdir; pi != NULL && PD_IDX(pi->dirnum) < pidx; | ||
1512 | pi = pi->next) | ||
1513 | ; | ||
1514 | |||
1515 | spi = pi; | ||
1516 | if (pi != NULL && PD_IDX(pi->dirnum) == pidx) { | ||
1517 | pd = pi->base; | ||
1518 | |||
1519 | for (i = index; i <= lidx;) { | ||
1520 | if (pd[PI_OFF(i)] != MALLOC_NOT_MINE) { | ||
1521 | pd[PI_OFF(i)] = MALLOC_NOT_MINE; | ||
1522 | #ifdef MALLOC_EXTRA_SANITY | ||
1523 | if (!PD_OFF(pi->dirnum)) { | ||
1524 | wrterror("(ES): pages directory underflow"); | ||
1525 | errno = EFAULT; | ||
1526 | return; | ||
1527 | } | ||
1528 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1529 | pi->dirnum--; | ||
1530 | } | ||
1531 | #ifdef MALLOC_EXTRA_SANITY | ||
1532 | else | ||
1533 | wrtwarning("(ES): page already unmapped"); | ||
1534 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1535 | i++; | ||
1536 | if (!PI_OFF(i)) { | ||
1537 | /* | ||
1538 | * If no page in that dir, free | ||
1539 | * directory page. | ||
1540 | */ | ||
1541 | if (!PD_OFF(pi->dirnum)) { | ||
1542 | /* Remove from list. */ | ||
1543 | if (spi == pi) | ||
1544 | spi = pi->prev; | ||
1545 | if (pi->prev != NULL) | ||
1546 | pi->prev->next = pi->next; | ||
1547 | if (pi->next != NULL) | ||
1548 | pi->next->prev = pi->prev; | ||
1549 | pi = pi->next; | ||
1550 | munmap(pd, malloc_pagesize); | ||
1551 | } else | ||
1552 | pi = pi->next; | ||
1553 | if (pi == NULL || | ||
1554 | PD_IDX(pi->dirnum) != PI_IDX(i)) | ||
1555 | break; | ||
1556 | pd = pi->base; | ||
1557 | } | ||
1558 | } | ||
1559 | if (pi && !PD_OFF(pi->dirnum)) { | ||
1560 | /* Resulting page dir is now empty. */ | ||
1561 | /* Remove from list. */ | ||
1562 | if (spi == pi) /* Update spi only if first. */ | ||
1563 | spi = pi->prev; | ||
1564 | if (pi->prev != NULL) | ||
1565 | pi->prev->next = pi->next; | ||
1566 | if (pi->next != NULL) | ||
1567 | pi->next->prev = pi->prev; | ||
1568 | pi = pi->next; | ||
1569 | munmap(pd, malloc_pagesize); | ||
1570 | } | ||
1571 | } | ||
1572 | if (pi == NULL && malloc_brk == tail) { | ||
1573 | /* Resize down the malloc upper boundary. */ | ||
1574 | last_index = index - 1; | ||
1575 | malloc_brk = index2ptr(index); | ||
1576 | } | ||
1577 | |||
1578 | /* XXX: We could realloc/shrink the pagedir here I guess. */ | ||
1579 | if (pf->size == 0) { /* Remove from free-list as well. */ | ||
1580 | if (px) | ||
1581 | ifree(px); | ||
1582 | if ((px = pf->prev) != &free_list) { | ||
1583 | if (pi == NULL && last_index == (index - 1)) { | ||
1584 | if (spi == NULL) { | ||
1585 | malloc_brk = NULL; | ||
1586 | i = 11; | ||
1587 | } else { | ||
1588 | pd = spi->base; | ||
1589 | if (PD_IDX(spi->dirnum) < pidx) | ||
1590 | index = | ||
1591 | ((PD_IDX(spi->dirnum) + 1) * | ||
1592 | pdi_mod) - 1; | ||
1593 | for (pi = spi, i = index; | ||
1594 | pd[PI_OFF(i)] == MALLOC_NOT_MINE; | ||
1595 | i--) | ||
1596 | #ifdef MALLOC_EXTRA_SANITY | ||
1597 | if (!PI_OFF(i)) { | ||
1598 | pi = pi->prev; | ||
1599 | if (pi == NULL || i == 0) | ||
1600 | break; | ||
1601 | pd = pi->base; | ||
1602 | i = (PD_IDX(pi->dirnum) + 1) * pdi_mod; | ||
1603 | } | ||
1604 | #else /* !MALLOC_EXTRA_SANITY */ | ||
1605 | { | ||
1606 | } | ||
1607 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1608 | malloc_brk = index2ptr(i + 1); | ||
1609 | } | ||
1610 | last_index = i; | ||
1611 | } | ||
1612 | if ((px->next = pf->next) != NULL) | ||
1613 | px->next->prev = px; | ||
1614 | } else { | ||
1615 | if ((free_list.next = pf->next) != NULL) | ||
1616 | free_list.next->prev = &free_list; | ||
1617 | } | ||
1618 | px = pf; | ||
1619 | last_dir = prev_dir; | ||
1620 | prev_dir = NULL; | ||
349 | } | 1621 | } |
350 | if (nbytes <= onb && nbytes > i) { | ||
351 | #ifdef RCHECK | ||
352 | op->ov_size = (nbytes + RSLOP - 1) & ~(RSLOP - 1); | ||
353 | *(u_short *)((caddr_t)(op + 1) + op->ov_size) = RMAGIC; | ||
354 | #endif | ||
355 | return(cp); | ||
356 | } else | ||
357 | free(cp); | ||
358 | } | 1622 | } |
359 | if ((res = malloc(nbytes)) == NULL) | 1623 | not_return: |
360 | return (NULL); | 1624 | if (pt != NULL) |
361 | if (cp != res) /* common optimization if "compacting" */ | 1625 | ifree(pt); |
362 | bcopy(cp, res, (nbytes < onb) ? nbytes : onb); | ||
363 | return (res); | ||
364 | } | 1626 | } |
365 | 1627 | ||
366 | /* | 1628 | /* |
367 | * Search ``srchlen'' elements of each free list for a block whose | 1629 | * Free a chunk, and possibly the page it's on, if the page becomes empty. |
368 | * header starts at ``freep''. If srchlen is -1 search the whole list. | ||
369 | * Return bucket number, or -1 if not found. | ||
370 | */ | 1630 | */ |
371 | static | 1631 | |
372 | findbucket(freep, srchlen) | 1632 | /* ARGSUSED */ |
373 | union overhead *freep; | 1633 | static __inline__ void |
374 | int srchlen; | 1634 | free_bytes(void *ptr, u_long index, struct pginfo * info) |
375 | { | 1635 | { |
376 | register union overhead *p; | 1636 | struct pginfo **mp, **pd; |
377 | register int i, j; | 1637 | struct pdinfo *pi; |
1638 | #ifdef MALLOC_EXTRA_SANITY | ||
1639 | u_long pidx; | ||
1640 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1641 | void *vp; | ||
1642 | long i; | ||
378 | 1643 | ||
379 | for (i = 0; i < NBUCKETS; i++) { | 1644 | /* Find the chunk number on the page */ |
380 | j = 0; | 1645 | i = ((u_long) ptr & malloc_pagemask) >> info->shift; |
381 | for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { | 1646 | |
382 | if (p == freep) | 1647 | if ((u_long) ptr & ((1UL << (info->shift)) - 1)) { |
383 | return (i); | 1648 | wrtwarning("modified (chunk-) pointer"); |
384 | j++; | 1649 | return; |
1650 | } | ||
1651 | if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) { | ||
1652 | wrtwarning("chunk is already free"); | ||
1653 | return; | ||
1654 | } | ||
1655 | if (malloc_junk && info->size != 0) | ||
1656 | memset(ptr, SOME_JUNK, (size_t)info->size); | ||
1657 | |||
1658 | info->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); | ||
1659 | info->free++; | ||
1660 | |||
1661 | if (info->size != 0) | ||
1662 | mp = page_dir + info->shift; | ||
1663 | else | ||
1664 | mp = page_dir; | ||
1665 | |||
1666 | if (info->free == 1) { | ||
1667 | /* Page became non-full */ | ||
1668 | |||
1669 | /* Insert in address order */ | ||
1670 | while (*mp != NULL && (*mp)->next != NULL && | ||
1671 | (*mp)->next->page < info->page) | ||
1672 | mp = &(*mp)->next; | ||
1673 | info->next = *mp; | ||
1674 | *mp = info; | ||
1675 | return; | ||
1676 | } | ||
1677 | if (info->free != info->total) | ||
1678 | return; | ||
1679 | |||
1680 | /* Find & remove this page in the queue */ | ||
1681 | while (*mp != info) { | ||
1682 | mp = &((*mp)->next); | ||
1683 | #ifdef MALLOC_EXTRA_SANITY | ||
1684 | if (!*mp) { | ||
1685 | wrterror("(ES): Not on queue"); | ||
1686 | errno = EFAULT; | ||
1687 | return; | ||
385 | } | 1688 | } |
1689 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1690 | } | ||
1691 | *mp = info->next; | ||
1692 | |||
1693 | /* Free the page & the info structure if need be */ | ||
1694 | pdir_lookup(ptr2index(info->page), &pi); | ||
1695 | #ifdef MALLOC_EXTRA_SANITY | ||
1696 | pidx = PI_IDX(ptr2index(info->page)); | ||
1697 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
1698 | wrterror("(ES): mapped pages not found in directory"); | ||
1699 | errno = EFAULT; | ||
1700 | return; | ||
1701 | } | ||
1702 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1703 | if (pi != last_dir) { | ||
1704 | prev_dir = last_dir; | ||
1705 | last_dir = pi; | ||
386 | } | 1706 | } |
387 | return (-1); | 1707 | pd = pi->base; |
1708 | pd[PI_OFF(ptr2index(info->page))] = MALLOC_FIRST; | ||
1709 | |||
1710 | /* If the page was mprotected, unprotect it before releasing it */ | ||
1711 | if (info->size == 0) | ||
1712 | mprotect(info->page, malloc_pagesize, PROT_READ | PROT_WRITE); | ||
1713 | |||
1714 | vp = info->page; /* Order is important ! */ | ||
1715 | if (vp != (void *) info) | ||
1716 | ifree(info); | ||
1717 | ifree(vp); | ||
1718 | } | ||
1719 | |||
1720 | static void | ||
1721 | ifree(void *ptr) | ||
1722 | { | ||
1723 | struct pginfo *info, **pd; | ||
1724 | u_long index; | ||
1725 | #ifdef MALLOC_EXTRA_SANITY | ||
1726 | u_long pidx; | ||
1727 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1728 | struct pdinfo *pi; | ||
1729 | |||
1730 | if (!malloc_started) { | ||
1731 | wrtwarning("malloc() has never been called"); | ||
1732 | return; | ||
1733 | } | ||
1734 | /* If we're already sinking, don't make matters any worse. */ | ||
1735 | if (suicide) | ||
1736 | return; | ||
1737 | |||
1738 | if (malloc_ptrguard && PTR_ALIGNED(ptr)) | ||
1739 | ptr = (char *) ptr - PTR_GAP; | ||
1740 | |||
1741 | index = ptr2index(ptr); | ||
1742 | |||
1743 | if (index < malloc_pageshift) { | ||
1744 | warnx("(%p)", ptr); | ||
1745 | wrtwarning("ifree: junk pointer, too low to make sense"); | ||
1746 | return; | ||
1747 | } | ||
1748 | if (index > last_index) { | ||
1749 | warnx("(%p)", ptr); | ||
1750 | wrtwarning("ifree: junk pointer, too high to make sense"); | ||
1751 | return; | ||
1752 | } | ||
1753 | pdir_lookup(index, &pi); | ||
1754 | #ifdef MALLOC_EXTRA_SANITY | ||
1755 | pidx = PI_IDX(index); | ||
1756 | if (pi == NULL || PD_IDX(pi->dirnum) != pidx) { | ||
1757 | wrterror("(ES): mapped pages not found in directory"); | ||
1758 | errno = EFAULT; | ||
1759 | return; | ||
1760 | } | ||
1761 | #endif /* MALLOC_EXTRA_SANITY */ | ||
1762 | if (pi != last_dir) { | ||
1763 | prev_dir = last_dir; | ||
1764 | last_dir = pi; | ||
1765 | } | ||
1766 | pd = pi->base; | ||
1767 | info = pd[PI_OFF(index)]; | ||
1768 | |||
1769 | if (info < MALLOC_MAGIC) | ||
1770 | free_pages(ptr, index, info); | ||
1771 | else | ||
1772 | free_bytes(ptr, index, info); | ||
1773 | |||
1774 | /* does not matter if malloc_bytes fails */ | ||
1775 | if (px == NULL) | ||
1776 | px = malloc_bytes(sizeof *px); | ||
1777 | |||
1778 | return; | ||
388 | } | 1779 | } |
389 | 1780 | ||
390 | #ifdef MSTATS | ||
391 | /* | 1781 | /* |
392 | * mstats - print out statistics about malloc | 1782 | * Common function for handling recursion. Only |
393 | * | 1783 | * print the error message once, to avoid making the problem |
394 | * Prints two lines of numbers, one showing the length of the free list | 1784 | * potentially worse. |
395 | * for each size category, the second showing the number of mallocs - | ||
396 | * frees for each size category. | ||
397 | */ | 1785 | */ |
398 | mstats(s) | 1786 | static void |
399 | char *s; | 1787 | malloc_recurse(void) |
400 | { | 1788 | { |
401 | register int i, j; | 1789 | static int noprint; |
402 | register union overhead *p; | 1790 | |
403 | int totfree = 0, | 1791 | if (noprint == 0) { |
404 | totused = 0; | 1792 | noprint = 1; |
405 | 1793 | wrtwarning("recursive call"); | |
406 | fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); | 1794 | } |
407 | for (i = 0; i < NBUCKETS; i++) { | 1795 | malloc_active--; |
408 | for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) | 1796 | _MALLOC_UNLOCK(); |
409 | ; | 1797 | errno = EDEADLK; |
410 | fprintf(stderr, " %d", j); | 1798 | } |
411 | totfree += j * (1 << (i + 3)); | 1799 | |
412 | } | 1800 | /* |
413 | fprintf(stderr, "\nused:\t"); | 1801 | * These are the public exported interface routines. |
414 | for (i = 0; i < NBUCKETS; i++) { | 1802 | */ |
415 | fprintf(stderr, " %d", nmalloc[i]); | 1803 | void * |
416 | totused += nmalloc[i] * (1 << (i + 3)); | 1804 | malloc(size_t size) |
417 | } | 1805 | { |
418 | fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", | 1806 | void *r; |
419 | totused, totfree); | 1807 | |
1808 | _MALLOC_LOCK(); | ||
1809 | malloc_func = " in malloc():"; | ||
1810 | if (malloc_active++) { | ||
1811 | malloc_recurse(); | ||
1812 | return (NULL); | ||
1813 | } | ||
1814 | r = imalloc(size); | ||
1815 | UTRACE(0, size, r); | ||
1816 | malloc_active--; | ||
1817 | _MALLOC_UNLOCK(); | ||
1818 | if (malloc_xmalloc && r == NULL) { | ||
1819 | wrterror("out of memory"); | ||
1820 | errno = ENOMEM; | ||
1821 | } | ||
1822 | return (r); | ||
1823 | } | ||
1824 | |||
1825 | void | ||
1826 | free(void *ptr) | ||
1827 | { | ||
1828 | /* This is legal. XXX quick path */ | ||
1829 | if (ptr == NULL) | ||
1830 | return; | ||
1831 | |||
1832 | _MALLOC_LOCK(); | ||
1833 | malloc_func = " in free():"; | ||
1834 | if (malloc_active++) { | ||
1835 | malloc_recurse(); | ||
1836 | return; | ||
1837 | } | ||
1838 | ifree(ptr); | ||
1839 | UTRACE(ptr, 0, 0); | ||
1840 | malloc_active--; | ||
1841 | _MALLOC_UNLOCK(); | ||
1842 | return; | ||
1843 | } | ||
1844 | |||
1845 | void * | ||
1846 | realloc(void *ptr, size_t size) | ||
1847 | { | ||
1848 | void *r; | ||
1849 | |||
1850 | _MALLOC_LOCK(); | ||
1851 | malloc_func = " in realloc():"; | ||
1852 | if (malloc_active++) { | ||
1853 | malloc_recurse(); | ||
1854 | return (NULL); | ||
1855 | } | ||
1856 | |||
1857 | if (ptr == NULL) | ||
1858 | r = imalloc(size); | ||
1859 | else | ||
1860 | r = irealloc(ptr, size); | ||
1861 | |||
1862 | UTRACE(ptr, size, r); | ||
1863 | malloc_active--; | ||
1864 | _MALLOC_UNLOCK(); | ||
1865 | if (malloc_xmalloc && r == NULL) { | ||
1866 | wrterror("out of memory"); | ||
1867 | errno = ENOMEM; | ||
1868 | } | ||
1869 | return (r); | ||
420 | } | 1870 | } |
421 | #endif | ||
diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c index 381fdc0830..623f652134 100644 --- a/src/lib/libc/stdlib/merge.c +++ b/src/lib/libc/stdlib/merge.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: merge.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1992, 1993 | 3 | * Copyright (c) 1992, 1993 |
3 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -13,11 +14,7 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 15 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 16 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 17 | * 3. Neither the name of the University nor the names of its contributors |
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | 18 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. | 19 | * without specific prior written permission. |
23 | * | 20 | * |
@@ -34,11 +31,6 @@ | |||
34 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
35 | */ | 32 | */ |
36 | 33 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char sccsid[] = "from: @(#)merge.c 8.2 (Berkeley) 2/14/94";*/ | ||
39 | static char *rcsid = "$Id: merge.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | /* | 34 | /* |
43 | * Hybrid exponential search/linear search merge sort with hybrid | 35 | * Hybrid exponential search/linear search merge sort with hybrid |
44 | * natural/pairwise first pass. Requires about .3% more comparisons | 36 | * natural/pairwise first pass. Requires about .3% more comparisons |
@@ -59,8 +51,8 @@ static char *rcsid = "$Id: merge.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | |||
59 | #include <stdlib.h> | 51 | #include <stdlib.h> |
60 | #include <string.h> | 52 | #include <string.h> |
61 | 53 | ||
62 | static void setup __P((u_char *, u_char *, size_t, size_t, int (*)())); | 54 | static void setup(u_char *, u_char *, size_t, size_t, int (*)()); |
63 | static void insertionsort __P((u_char *, size_t, size_t, int (*)())); | 55 | static void insertionsort(u_char *, size_t, size_t, int (*)()); |
64 | 56 | ||
65 | #define ISIZE sizeof(int) | 57 | #define ISIZE sizeof(int) |
66 | #define PSIZE sizeof(u_char *) | 58 | #define PSIZE sizeof(u_char *) |
@@ -96,15 +88,12 @@ static void insertionsort __P((u_char *, size_t, size_t, int (*)())); | |||
96 | * Arguments are as for qsort. | 88 | * Arguments are as for qsort. |
97 | */ | 89 | */ |
98 | int | 90 | int |
99 | mergesort(base, nmemb, size, cmp) | 91 | mergesort(void *base, size_t nmemb, size_t size, |
100 | void *base; | 92 | int (*cmp)(const void *, const void *)) |
101 | size_t nmemb; | ||
102 | register size_t size; | ||
103 | int (*cmp) __P((const void *, const void *)); | ||
104 | { | 93 | { |
105 | register int i, sense; | 94 | int i, sense; |
106 | int big, iflag; | 95 | int big, iflag; |
107 | register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; | 96 | u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; |
108 | u_char *list2, *list1, *p2, *p, *last, **p1; | 97 | u_char *list2, *list1, *p2, *p, *last, **p1; |
109 | 98 | ||
110 | if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ | 99 | if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ |
@@ -148,7 +137,7 @@ mergesort(base, nmemb, size, cmp) | |||
148 | sense = 0; | 137 | sense = 0; |
149 | } | 138 | } |
150 | if (!big) { /* here i = 0 */ | 139 | if (!big) { /* here i = 0 */ |
151 | LINEAR: while ((b += size) < t && cmp(q, b) >sense) | 140 | while ((b += size) < t && cmp(q, b) >sense) |
152 | if (++i == 6) { | 141 | if (++i == 6) { |
153 | big = 1; | 142 | big = 1; |
154 | goto EXPONENTIAL; | 143 | goto EXPONENTIAL; |
@@ -169,7 +158,7 @@ EXPONENTIAL: for (i = size; ; i <<= 1) | |||
169 | goto FASTCASE; | 158 | goto FASTCASE; |
170 | } else | 159 | } else |
171 | b = p; | 160 | b = p; |
172 | SLOWCASE: while (t > b+size) { | 161 | while (t > b+size) { |
173 | i = (((t - b) / size) >> 1) * size; | 162 | i = (((t - b) / size) >> 1) * size; |
174 | if ((*cmp)(q, p = b + i) <= sense) | 163 | if ((*cmp)(q, p = b + i) <= sense) |
175 | t = p; | 164 | t = p; |
@@ -256,10 +245,8 @@ COPY: b = t; | |||
256 | * is defined. Otherwise simple pairwise merging is used.) | 245 | * is defined. Otherwise simple pairwise merging is used.) |
257 | */ | 246 | */ |
258 | void | 247 | void |
259 | setup(list1, list2, n, size, cmp) | 248 | setup(u_char *list1, u_char *list2, size_t n, size_t size, |
260 | size_t n, size; | 249 | int (*cmp)(const void *, const void *)) |
261 | int (*cmp) __P((const void *, const void *)); | ||
262 | u_char *list1, *list2; | ||
263 | { | 250 | { |
264 | int i, length, size2, tmp, sense; | 251 | int i, length, size2, tmp, sense; |
265 | u_char *f1, *f2, *s, *l2, *last, *p2; | 252 | u_char *f1, *f2, *s, *l2, *last, *p2; |
@@ -330,10 +317,8 @@ setup(list1, list2, n, size, cmp) | |||
330 | * last 4 elements. | 317 | * last 4 elements. |
331 | */ | 318 | */ |
332 | static void | 319 | static void |
333 | insertionsort(a, n, size, cmp) | 320 | insertionsort(u_char *a, size_t n, size_t size, |
334 | u_char *a; | 321 | int (*cmp)(const void *, const void *)) |
335 | size_t n, size; | ||
336 | int (*cmp) __P((const void *, const void *)); | ||
337 | { | 322 | { |
338 | u_char *ai, *s, *t, *u, tmp; | 323 | u_char *ai, *s, *t, *u, tmp; |
339 | int i; | 324 | int i; |
diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c index 43356e66b3..977264aba5 100644 --- a/src/lib/libc/stdlib/mrand48.c +++ b/src/lib/libc/stdlib/mrand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: mrand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/nrand48.c b/src/lib/libc/stdlib/nrand48.c index 63f839cb05..f1f548c3af 100644 --- a/src/lib/libc/stdlib/nrand48.c +++ b/src/lib/libc/stdlib/nrand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: nrand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/putenv.c b/src/lib/libc/stdlib/putenv.c index 2194c2c608..54482f6a14 100644 --- a/src/lib/libc/stdlib/putenv.c +++ b/src/lib/libc/stdlib/putenv.c | |||
@@ -1,6 +1,7 @@ | |||
1 | /* $OpenBSD: putenv.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1988 The Regents of the University of California. | 3 | * Copyright (c) 1988, 1993 |
3 | * All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
4 | * | 5 | * |
5 | * Redistribution and use in source and binary forms, with or without | 6 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions | 7 | * modification, are permitted provided that the following conditions |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,29 +28,23 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)putenv.c 5.4 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: putenv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | #include <string.h> | 32 | #include <string.h> |
41 | 33 | ||
42 | int | 34 | int |
43 | putenv(str) | 35 | putenv(const char *str) |
44 | const char *str; | ||
45 | { | 36 | { |
46 | register char *p, *equal; | 37 | char *p, *equal; |
47 | int rval; | 38 | int rval; |
48 | 39 | ||
49 | if (!(p = strdup(str))) | 40 | if ((p = strdup(str)) == NULL) |
50 | return(1); | 41 | return (-1); |
51 | if (!(equal = strchr(p, '='))) { | 42 | if ((equal = strchr(p, '=')) == NULL) { |
52 | (void)free(p); | 43 | (void)free(p); |
53 | return(1); | 44 | return (-1); |
54 | } | 45 | } |
55 | *equal = '\0'; | 46 | *equal = '\0'; |
56 | rval = setenv(p, equal + 1, 1); | 47 | rval = setenv(p, equal + 1, 1); |
57 | (void)free(p); | 48 | (void)free(p); |
58 | return(rval); | 49 | return (rval); |
59 | } | 50 | } |
diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3 index cb1e052191..863758e1c7 100644 --- a/src/lib/libc/stdlib/qabs.3 +++ b/src/lib/libc/stdlib/qabs.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: qabs.3,v 1.10 2006/01/15 17:31:06 jmc Exp $ |
37 | .\" $Id: qabs.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt QABS 3 | 35 | .Dt QABS 3 |
@@ -49,14 +44,14 @@ | |||
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn qabs | 46 | .Fn qabs |
52 | function | 47 | function returns the absolute value of the quad integer |
53 | returns the absolute value of the quad integer | 48 | .Fa j . |
54 | .Ar j . | ||
55 | .Sh SEE ALSO | 49 | .Sh SEE ALSO |
56 | .Xr abs 3 , | 50 | .Xr abs 3 , |
57 | .Xr labs 3 , | ||
58 | .Xr floor 3 , | ||
59 | .Xr cabs 3 , | 51 | .Xr cabs 3 , |
52 | .Xr floor 3 , | ||
53 | .Xr imaxabs 3 , | ||
54 | .Xr labs 3 , | ||
60 | .Xr math 3 | 55 | .Xr math 3 |
61 | .Sh BUGS | 56 | .Sh BUGS |
62 | The absolute value of the most negative integer remains negative. | 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 index 9c51a8baa9..656b93c822 100644 --- a/src/lib/libc/stdlib/qabs.c +++ b/src/lib/libc/stdlib/qabs.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: qabs.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,16 +28,10 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)labs.c 5.2 (Berkeley) 5/17/90";*/ | ||
36 | static char *rcsid = "$Id: qabs.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | 32 | ||
41 | quad_t | 33 | quad_t |
42 | qabs(j) | 34 | qabs(quad_t j) |
43 | quad_t j; | ||
44 | { | 35 | { |
45 | return(j < 0 ? -j : j); | 36 | return(j < 0 ? -j : j); |
46 | } | 37 | } |
diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3 index 0efcfc96ef..643672ea5b 100644 --- a/src/lib/libc/stdlib/qdiv.3 +++ b/src/lib/libc/stdlib/qdiv.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)qdiv.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: qdiv.3,v 1.7 2006/01/13 17:58:09 millert Exp $ |
37 | .\" $Id: qdiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt QDIV 3 | 35 | .Dt QDIV 3 |
@@ -49,18 +44,19 @@ | |||
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn qdiv | 46 | .Fn qdiv |
52 | function | 47 | function computes the value |
53 | computes the value | 48 | .Fa num Ns / Ns Fa denom |
54 | .Ar num/denom | ||
55 | and returns the quotient and remainder in a structure named | 49 | and returns the quotient and remainder in a structure named |
56 | .Ar qdiv_t | 50 | .Li qdiv_t |
57 | that contains two | 51 | that contains two |
58 | .Em quad integer | 52 | .Li quad integer |
59 | members named | 53 | members named |
60 | .Ar quot | 54 | .Fa quot |
61 | and | 55 | and |
62 | .Ar rem . | 56 | .Fa rem . |
63 | .Sh SEE ALSO | 57 | .Sh SEE ALSO |
64 | .Xr div 3 , | 58 | .Xr div 3 , |
59 | .Xr imaxdiv 3 , | ||
65 | .Xr ldiv 3 , | 60 | .Xr ldiv 3 , |
61 | .Xr lldiv 3 , | ||
66 | .Xr math 3 | 62 | .Xr math 3 |
diff --git a/src/lib/libc/stdlib/qdiv.c b/src/lib/libc/stdlib/qdiv.c index 8f8e3f89c4..f3db0915ed 100644 --- a/src/lib/libc/stdlib/qdiv.c +++ b/src/lib/libc/stdlib/qdiv.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: qdiv.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1990 Regents of the University of California. | 3 | * Copyright (c) 1990 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -13,11 +14,7 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 15 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 16 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 17 | * 3. Neither the name of the University nor the names of its contributors |
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | 18 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. | 19 | * without specific prior written permission. |
23 | * | 20 | * |
@@ -34,16 +31,10 @@ | |||
34 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
35 | */ | 32 | */ |
36 | 33 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char *sccsid = "from: @(#)ldiv.c 5.2 (Berkeley) 4/16/91";*/ | ||
39 | static char *rcsid = "$Id: qdiv.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | #include <stdlib.h> /* qdiv_t */ | 34 | #include <stdlib.h> /* qdiv_t */ |
43 | 35 | ||
44 | qdiv_t | 36 | qdiv_t |
45 | qdiv(num, denom) | 37 | qdiv(quad_t num, quad_t denom) |
46 | quad_t num, denom; | ||
47 | { | 38 | { |
48 | qdiv_t r; | 39 | qdiv_t r; |
49 | 40 | ||
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 index eb122cde12..668ea90243 100644 --- a/src/lib/libc/stdlib/qsort.3 +++ b/src/lib/libc/stdlib/qsort.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,14 +29,15 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93 | 32 | .\" $OpenBSD: qsort.3,v 1.14 2003/10/01 08:11:58 jmc Exp $ |
37 | .\" $Id: qsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 4, 1993 | 34 | .Dd June 4, 1993 |
40 | .Dt QSORT 3 | 35 | .Dt QSORT 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
43 | .Nm qsort, heapsort, mergesort | 38 | .Nm qsort , |
39 | .Nm heapsort , | ||
40 | .Nm mergesort | ||
44 | .Nd sort functions | 41 | .Nd sort functions |
45 | .Sh SYNOPSIS | 42 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 43 | .Fd #include <stdlib.h> |
@@ -72,7 +69,7 @@ objects, the initial member of which is pointed to by | |||
72 | .Fa base . | 69 | .Fa base . |
73 | The size of each object is specified by | 70 | The size of each object is specified by |
74 | .Fa size . | 71 | .Fa size . |
75 | .Fn Mergesort | 72 | .Fn mergesort |
76 | behaves similarly, but | 73 | behaves similarly, but |
77 | .Em requires | 74 | .Em requires |
78 | that | 75 | that |
@@ -106,51 +103,49 @@ is stable. | |||
106 | .Pp | 103 | .Pp |
107 | The | 104 | The |
108 | .Fn qsort | 105 | .Fn qsort |
109 | function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, | 106 | function is an implementation of C.A.R. Hoare's |
107 | .Dq quicksort | ||
108 | algorithm, | ||
110 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's | 109 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's |
111 | Algorithm Q. | 110 | Algorithm Q. |
112 | .Fn Qsort | 111 | .Fn qsort |
113 | takes O N lg N average time. | 112 | takes O N lg N average time. |
114 | This implementation uses median selection to avoid its | 113 | This implementation uses median selection to avoid its |
115 | O N**2 worst-case behavior. | 114 | O N**2 worst-case behavior. |
116 | .Pp | 115 | .Pp |
117 | The | 116 | The |
118 | .Fn heapsort | 117 | .Fn heapsort |
119 | function is an implementation of J.W.J. William's ``heapsort'' algorithm, | 118 | function is an implementation of J.W.J. William's |
119 | .Dq heapsort | ||
120 | algorithm, | ||
120 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. | 121 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. |
121 | .Fn Heapsort | 122 | .Fn heapsort |
122 | takes O N lg N worst-case time. | 123 | takes O N lg N worst-case time. |
123 | Its | 124 | This implementation of |
124 | .Em only | 125 | .Fn heapsort |
125 | advantage over | 126 | is implemented without recursive function calls. |
126 | .Fn qsort | ||
127 | is that it uses almost no additional memory; while | ||
128 | .Fn qsort | ||
129 | does not allocate memory, it is implemented using recursion. | ||
130 | .Pp | 127 | .Pp |
131 | The function | 128 | The function |
132 | .Fn mergesort | 129 | .Fn mergesort |
133 | requires additional memory of size | 130 | requires additional memory of size |
134 | .Fa nmemb * | 131 | .Fa nmemb * |
135 | .Fa size | 132 | .Fa size |
136 | bytes; it should be used only when space is not at a premium. | 133 | bytes; it should be used only when space is not at a premium. |
137 | .Fn Mergesort | 134 | .Fn mergesort |
138 | is optimized for data with pre-existing order; its worst case | 135 | is optimized for data with pre-existing order; its worst case |
139 | time is O N lg N; its best case is O N. | 136 | time is O N lg N; its best case is O N. |
140 | .Pp | 137 | .Pp |
141 | Normally, | 138 | Normally, |
142 | .Fn qsort | 139 | .Fn qsort |
143 | is faster than | 140 | is faster than |
144 | .Fn mergesort | 141 | .Fn mergesort , |
145 | is faster than | 142 | which is faster than |
146 | .Fn heapsort . | 143 | .Fn heapsort . |
147 | Memory availability and pre-existing order in the data can make this | 144 | Memory availability and pre-existing order in the data can make this untrue. |
148 | untrue. | ||
149 | .Sh RETURN VALUES | 145 | .Sh RETURN VALUES |
150 | The | 146 | The |
151 | .Fn qsort | 147 | .Fn qsort |
152 | function | 148 | function returns no value. |
153 | returns no value. | ||
154 | .Pp | 149 | .Pp |
155 | Upon successful completion, | 150 | Upon successful completion, |
156 | .Fn heapsort | 151 | .Fn heapsort |
@@ -163,30 +158,25 @@ is set to indicate the error. | |||
163 | .Sh ERRORS | 158 | .Sh ERRORS |
164 | The | 159 | The |
165 | .Fn heapsort | 160 | .Fn heapsort |
166 | function succeeds unless: | 161 | and |
162 | .Fn mergesort | ||
163 | functions succeed unless: | ||
167 | .Bl -tag -width Er | 164 | .Bl -tag -width Er |
168 | .It Bq Er EINVAL | 165 | .It Bq Er EINVAL |
169 | The | 166 | The |
170 | .Fa size | 167 | .Fa size |
171 | argument is zero, or, | 168 | argument is zero, or the |
172 | the | ||
173 | .Fa size | 169 | .Fa size |
174 | argument to | 170 | argument to |
175 | .Fn mergesort | 171 | .Fn mergesort |
176 | is less than | 172 | is less than |
177 | .Dq "sizeof(void *) / 2" . | 173 | .Dq "sizeof(void *) / 2" . |
178 | .It Bq Er ENOMEM | 174 | .It Bq Er ENOMEM |
179 | .Fn Heapsort | 175 | .Fn heapsort |
180 | or | 176 | or |
181 | .Fn mergesort | 177 | .Fn mergesort |
182 | were unable to allocate memory. | 178 | were unable to allocate memory. |
183 | .El | 179 | .El |
184 | .Sh COMPATIBILITY | ||
185 | Previous versions of | ||
186 | .Fn qsort | ||
187 | did not permit the comparison routine itself to call | ||
188 | .Fn qsort 3 . | ||
189 | This is no longer true. | ||
190 | .Sh SEE ALSO | 180 | .Sh SEE ALSO |
191 | .Xr sort 1 , | 181 | .Xr sort 1 , |
192 | .Xr radixsort 3 | 182 | .Xr radixsort 3 |
@@ -204,7 +194,7 @@ This is no longer true. | |||
204 | .%T "Heapsort" | 194 | .%T "Heapsort" |
205 | .%J "Communications of the ACM" | 195 | .%J "Communications of the ACM" |
206 | .%V 7:1 | 196 | .%V 7:1 |
207 | .%P pp. 347-348 | 197 | .%P pp. 347\-348 |
208 | .Re | 198 | .Re |
209 | .Rs | 199 | .Rs |
210 | .%A Knuth, D.E. | 200 | .%A Knuth, D.E. |
@@ -212,23 +202,32 @@ This is no longer true. | |||
212 | .%B "The Art of Computer Programming" | 202 | .%B "The Art of Computer Programming" |
213 | .%V Vol. 3 | 203 | .%V Vol. 3 |
214 | .%T "Sorting and Searching" | 204 | .%T "Sorting and Searching" |
215 | .%P pp. 114-123, 145-149 | 205 | .%P pp. 114\-123, 145\-149 |
216 | .Re | 206 | .Re |
217 | .Rs | 207 | .Rs |
218 | .%A Mcilroy, P.M. | 208 | .%A McIlroy, P.M. |
219 | .%T "Optimistic Sorting and Information Theoretic Complexity" | 209 | .%T "Optimistic Sorting and Information Theoretic Complexity" |
220 | .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" | 210 | .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" |
221 | .%V January 1992 | 211 | .%P pp. 467\-464 |
212 | .%D January 1993 | ||
222 | .Re | 213 | .Re |
223 | .Rs | 214 | .Rs |
224 | .%A Bentley, J.L. | 215 | .%A Bentley, J.L. |
216 | .%A McIlroy, M.D. | ||
225 | .%T "Engineering a Sort Function" | 217 | .%T "Engineering a Sort Function" |
226 | .%J "bentley@research.att.com" | 218 | .%J "Software \- Practice and Experience" |
227 | .%V January 1992 | 219 | .%V Vol. 23(11) |
220 | .%P pp. 1249\-1265 | ||
221 | .%D November 1993 | ||
228 | .Re | 222 | .Re |
229 | .Sh STANDARDS | 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 | 230 | The |
231 | .Fn qsort | 231 | .Fn qsort |
232 | function | 232 | function conforms to |
233 | conforms to | ||
234 | .St -ansiC . | 233 | .St -ansiC . |
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c index c06bd54054..bb4a9a11f2 100644 --- a/src/lib/libc/stdlib/qsort.c +++ b/src/lib/libc/stdlib/qsort.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1992, 1993 | 3 | * Copyright (c) 1992, 1993 |
3 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,16 +28,11 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char sccsid[] = "from: @(#)qsort.c 8.1 (Berkeley) 6/4/93";*/ | ||
36 | static char *rcsid = "$Id: qsort.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <sys/types.h> | 31 | #include <sys/types.h> |
40 | #include <stdlib.h> | 32 | #include <stdlib.h> |
41 | 33 | ||
42 | static inline char *med3 __P((char *, char *, char *, int (*)())); | 34 | static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *)); |
43 | static inline void swapfunc __P((char *, char *, int, int)); | 35 | static __inline void swapfunc(char *, char *, int, int); |
44 | 36 | ||
45 | #define min(a, b) (a) < (b) ? a : b | 37 | #define min(a, b) (a) < (b) ? a : b |
46 | 38 | ||
@@ -49,10 +41,10 @@ static inline void swapfunc __P((char *, char *, int, int)); | |||
49 | */ | 41 | */ |
50 | #define swapcode(TYPE, parmi, parmj, n) { \ | 42 | #define swapcode(TYPE, parmi, parmj, n) { \ |
51 | long i = (n) / sizeof (TYPE); \ | 43 | long i = (n) / sizeof (TYPE); \ |
52 | register TYPE *pi = (TYPE *) (parmi); \ | 44 | TYPE *pi = (TYPE *) (parmi); \ |
53 | register TYPE *pj = (TYPE *) (parmj); \ | 45 | TYPE *pj = (TYPE *) (parmj); \ |
54 | do { \ | 46 | do { \ |
55 | register TYPE t = *pi; \ | 47 | TYPE t = *pi; \ |
56 | *pi++ = *pj; \ | 48 | *pi++ = *pj; \ |
57 | *pj++ = t; \ | 49 | *pj++ = t; \ |
58 | } while (--i > 0); \ | 50 | } while (--i > 0); \ |
@@ -61,12 +53,10 @@ static inline void swapfunc __P((char *, char *, int, int)); | |||
61 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ | 53 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ |
62 | es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; | 54 | es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; |
63 | 55 | ||
64 | static inline void | 56 | static __inline void |
65 | swapfunc(a, b, n, swaptype) | 57 | swapfunc(char *a, char *b, int n, int swaptype) |
66 | char *a, *b; | ||
67 | int n, swaptype; | ||
68 | { | 58 | { |
69 | if(swaptype <= 1) | 59 | if (swaptype <= 1) |
70 | swapcode(long, a, b, n) | 60 | swapcode(long, a, b, n) |
71 | else | 61 | else |
72 | swapcode(char, a, b, n) | 62 | swapcode(char, a, b, n) |
@@ -82,10 +72,8 @@ swapfunc(a, b, n, swaptype) | |||
82 | 72 | ||
83 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) | 73 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) |
84 | 74 | ||
85 | static inline char * | 75 | static __inline char * |
86 | med3(a, b, c, cmp) | 76 | med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) |
87 | char *a, *b, *c; | ||
88 | int (*cmp)(); | ||
89 | { | 77 | { |
90 | return cmp(a, b) < 0 ? | 78 | return cmp(a, b) < 0 ? |
91 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) | 79 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) |
@@ -93,27 +81,25 @@ med3(a, b, c, cmp) | |||
93 | } | 81 | } |
94 | 82 | ||
95 | void | 83 | void |
96 | qsort(a, n, es, cmp) | 84 | qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) |
97 | void *a; | ||
98 | size_t n, es; | ||
99 | int (*cmp)(); | ||
100 | { | 85 | { |
101 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | 86 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; |
102 | int d, r, swaptype, swap_cnt; | 87 | int d, r, swaptype, swap_cnt; |
88 | char *a = aa; | ||
103 | 89 | ||
104 | loop: SWAPINIT(a, es); | 90 | loop: SWAPINIT(a, es); |
105 | swap_cnt = 0; | 91 | swap_cnt = 0; |
106 | if (n < 7) { | 92 | if (n < 7) { |
107 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | 93 | for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) |
108 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | 94 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
109 | pl -= es) | 95 | pl -= es) |
110 | swap(pl, pl - es); | 96 | swap(pl, pl - es); |
111 | return; | 97 | return; |
112 | } | 98 | } |
113 | pm = a + (n / 2) * es; | 99 | pm = (char *)a + (n / 2) * es; |
114 | if (n > 7) { | 100 | if (n > 7) { |
115 | pl = a; | 101 | pl = (char *)a; |
116 | pn = a + (n - 1) * es; | 102 | pn = (char *)a + (n - 1) * es; |
117 | if (n > 40) { | 103 | if (n > 40) { |
118 | d = (n / 8) * es; | 104 | d = (n / 8) * es; |
119 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | 105 | pl = med3(pl, pl + d, pl + 2 * d, cmp); |
@@ -123,9 +109,9 @@ loop: SWAPINIT(a, es); | |||
123 | pm = med3(pl, pm, pn, cmp); | 109 | pm = med3(pl, pm, pn, cmp); |
124 | } | 110 | } |
125 | swap(a, pm); | 111 | swap(a, pm); |
126 | pa = pb = a + es; | 112 | pa = pb = (char *)a + es; |
127 | 113 | ||
128 | pc = pd = a + (n - 1) * es; | 114 | pc = pd = (char *)a + (n - 1) * es; |
129 | for (;;) { | 115 | for (;;) { |
130 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { | 116 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { |
131 | if (r == 0) { | 117 | if (r == 0) { |
@@ -151,14 +137,14 @@ loop: SWAPINIT(a, es); | |||
151 | pc -= es; | 137 | pc -= es; |
152 | } | 138 | } |
153 | if (swap_cnt == 0) { /* Switch to insertion sort */ | 139 | if (swap_cnt == 0) { /* Switch to insertion sort */ |
154 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | 140 | for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) |
155 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | 141 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
156 | pl -= es) | 142 | pl -= es) |
157 | swap(pl, pl - es); | 143 | swap(pl, pl - es); |
158 | return; | 144 | return; |
159 | } | 145 | } |
160 | 146 | ||
161 | pn = a + n * es; | 147 | pn = (char *)a + n * es; |
162 | r = min(pa - (char *)a, pb - pa); | 148 | r = min(pa - (char *)a, pb - pa); |
163 | vecswap(a, pb - r, r); | 149 | vecswap(a, pb - r, r); |
164 | r = min(pd - pc, pn - pd - es); | 150 | r = min(pd - pc, pn - pd - es); |
diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3 index a2af9f17a4..8a4f71641f 100644 --- a/src/lib/libc/stdlib/radixsort.3 +++ b/src/lib/libc/stdlib/radixsort.3 | |||
@@ -9,11 +9,7 @@ | |||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer in the | 10 | .\" notice, this list of conditions and the following disclaimer in the |
11 | .\" documentation and/or other materials provided with the distribution. | 11 | .\" documentation and/or other materials provided with the distribution. |
12 | .\" 3. All advertising materials mentioning features or use of this software | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
13 | .\" must display the following acknowledgement: | ||
14 | .\" This product includes software developed by the University of | ||
15 | .\" California, Berkeley and its contributors. | ||
16 | .\" 4. 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 | 13 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | 14 | .\" without specific prior written permission. |
19 | .\" | 15 | .\" |
@@ -29,32 +25,33 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
31 | .\" | 27 | .\" |
32 | .\" from: @(#)radixsort.3 8.2 (Berkeley) 1/27/94 | 28 | .\" $OpenBSD: radixsort.3,v 1.10 2005/07/26 04:20:23 jaredy Exp $ |
33 | .\" $Id: radixsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
34 | .\" | 29 | .\" |
35 | .Dd January 27, 1994 | 30 | .Dd January 27, 1994 |
36 | .Dt RADIXSORT 3 | 31 | .Dt RADIXSORT 3 |
37 | .Os | 32 | .Os |
38 | .Sh NAME | 33 | .Sh NAME |
39 | .Nm radixsort | 34 | .Nm radixsort , |
35 | .Nm sradixsort | ||
40 | .Nd radix sort | 36 | .Nd radix sort |
41 | .Sh SYNOPSIS | 37 | .Sh SYNOPSIS |
42 | .Fd #include <limits.h> | 38 | .Fd #include <limits.h> |
43 | .Fd #include <stdlib.h> | 39 | .Fd #include <stdlib.h> |
44 | .Ft int | 40 | .Ft int |
45 | .Fn radixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" | 41 | .Fn radixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte" |
46 | .Ft int | 42 | .Ft int |
47 | .Fn sradixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" | 43 | .Fn sradixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte" |
48 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
49 | The | 45 | The |
50 | .Fn radixsort | 46 | .Fn radixsort |
51 | and | 47 | and |
52 | .Fn sradixsort | 48 | .Fn sradixsort |
53 | functions | 49 | functions are implementations of radix sort. |
54 | are implementations of radix sort. | ||
55 | .Pp | 50 | .Pp |
56 | These functions sort an array of pointers to byte strings, the initial | 51 | These functions sort an array of |
57 | member of which is referenced by | 52 | .Fa nmemb |
53 | pointers to byte strings. | ||
54 | The initial member is referenced by | ||
58 | .Fa base . | 55 | .Fa base . |
59 | The byte strings may contain any values; the end of each string | 56 | The byte strings may contain any values; the end of each string |
60 | is denoted by the user-specified value | 57 | is denoted by the user-specified value |
@@ -63,26 +60,24 @@ is denoted by the user-specified value | |||
63 | Applications may specify a sort order by providing the | 60 | Applications may specify a sort order by providing the |
64 | .Fa table | 61 | .Fa table |
65 | argument. | 62 | argument. |
66 | If | 63 | If non-null, |
67 | .Pf non- Dv NULL , | ||
68 | .Fa table | 64 | .Fa table |
69 | must reference an array of | 65 | must reference an array of |
70 | .Dv UCHAR_MAX | 66 | .Dv UCHAR_MAX |
71 | + 1 bytes which contains the sort | 67 | + 1 bytes which contains the sort weight of each possible byte value. |
72 | weight of each possible byte value. | ||
73 | The end-of-string byte must have a sort weight of 0 or 255 | 68 | The end-of-string byte must have a sort weight of 0 or 255 |
74 | (for sorting in reverse order). | 69 | (for sorting in reverse order). |
75 | More than one byte may have the same sort weight. | 70 | More than one byte may have the same sort weight. |
76 | The | 71 | The |
77 | .Fa table | 72 | .Fa table |
78 | argument | 73 | argument is useful for applications which wish to sort different characters |
79 | is useful for applications which wish to sort different characters | 74 | equally; for example, providing a table with the same weights |
80 | 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. |
81 | for A-Z as for a-z will result in a case-insensitive sort. | ||
82 | If | 76 | If |
83 | .Fa table | 77 | .Fa table |
84 | is NULL, the contents of the array are sorted in ascending order | 78 | is |
85 | according to the | 79 | .Dv NULL , |
80 | the contents of the array are sorted in ascending order according to the | ||
86 | .Tn ASCII | 81 | .Tn ASCII |
87 | order of the byte strings they reference and | 82 | order of the byte strings they reference and |
88 | .Fa endbyte | 83 | .Fa endbyte |
@@ -90,7 +85,7 @@ has a sorting weight of 0. | |||
90 | .Pp | 85 | .Pp |
91 | The | 86 | The |
92 | .Fn sradixsort | 87 | .Fn sradixsort |
93 | function is stable, that is, if two elements compare as equal, their | 88 | function is stable; that is, if two elements compare as equal, their |
94 | order in the sorted array is unchanged. | 89 | order in the sorted array is unchanged. |
95 | The | 90 | The |
96 | .Fn sradixsort | 91 | .Fn sradixsort |
@@ -107,7 +102,7 @@ particular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10. | |||
107 | They take linear time relative to the number of bytes in the strings. | 102 | They take linear time relative to the number of bytes in the strings. |
108 | .Sh RETURN VALUES | 103 | .Sh RETURN VALUES |
109 | Upon successful completion 0 is returned. | 104 | Upon successful completion 0 is returned. |
110 | Otherwise, \-1 is returned and the global variable | 105 | Otherwise, \-1 is returned and the global variable |
111 | .Va errno | 106 | .Va errno |
112 | is set to indicate the error. | 107 | is set to indicate the error. |
113 | .Sh ERRORS | 108 | .Sh ERRORS |
@@ -122,15 +117,13 @@ is not 0 or 255. | |||
122 | .Pp | 117 | .Pp |
123 | Additionally, the | 118 | Additionally, the |
124 | .Fn sradixsort | 119 | .Fn sradixsort |
125 | function | 120 | function may fail and set |
126 | may fail and set | ||
127 | .Va errno | 121 | .Va errno |
128 | for any of the errors specified for the library routine | 122 | for any of the errors specified for the library routine |
129 | .Xr malloc 3 . | 123 | .Xr malloc 3 . |
130 | .Sh SEE ALSO | 124 | .Sh SEE ALSO |
131 | .Xr sort 1 , | 125 | .Xr sort 1 , |
132 | .Xr qsort 3 | 126 | .Xr qsort 3 |
133 | .Pp | ||
134 | .Rs | 127 | .Rs |
135 | .%A Knuth, D.E. | 128 | .%A Knuth, D.E. |
136 | .%D 1968 | 129 | .%D 1968 |
@@ -158,4 +151,5 @@ for any of the errors specified for the library routine | |||
158 | .Sh HISTORY | 151 | .Sh HISTORY |
159 | The | 152 | The |
160 | .Fn radixsort | 153 | .Fn radixsort |
161 | function first appeared in 4.4BSD. | 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 index dd51013c94..0b2ff27044 100644 --- a/src/lib/libc/stdlib/radixsort.c +++ b/src/lib/libc/stdlib/radixsort.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: radixsort.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1990, 1993 | 3 | * Copyright (c) 1990, 1993 |
3 | * The Regents of the University of California. All rights reserved. | 4 | * The Regents of the University of California. All rights reserved. |
@@ -13,11 +14,7 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 15 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 16 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 17 | * 3. Neither the name of the University nor the names of its contributors |
17 | * must display the following acknowledgement: | ||
18 | * This product includes software developed by the University of | ||
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | 18 | * may be used to endorse or promote products derived from this software |
22 | * without specific prior written permission. | 19 | * without specific prior written permission. |
23 | * | 20 | * |
@@ -34,11 +31,6 @@ | |||
34 | * SUCH DAMAGE. | 31 | * SUCH DAMAGE. |
35 | */ | 32 | */ |
36 | 33 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char sccsid[] = "from: @(#)radixsort.c 8.1 (Berkeley) 6/4/93";*/ | ||
39 | static char *rcsid = "$Id: radixsort.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | /* | 34 | /* |
43 | * Radixsort routines. | 35 | * Radixsort routines. |
44 | * | 36 | * |
@@ -61,11 +53,11 @@ typedef struct { | |||
61 | int sn, si; | 53 | int sn, si; |
62 | } stack; | 54 | } stack; |
63 | 55 | ||
64 | static inline void simplesort | 56 | static __inline void simplesort |
65 | __P((const u_char **, int, int, const u_char *, u_int)); | 57 | (const u_char **, int, int, const u_char *, u_int); |
66 | static void r_sort_a __P((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); |
67 | static void r_sort_b __P((const u_char **, | 59 | static void r_sort_b(const u_char **, |
68 | const u_char **, int, int, const u_char *, u_int)); | 60 | const u_char **, int, int, const u_char *, u_int); |
69 | 61 | ||
70 | #define THRESHOLD 20 /* Divert to simplesort(). */ | 62 | #define THRESHOLD 20 /* Divert to simplesort(). */ |
71 | #define SIZE 512 /* Default stack size. */ | 63 | #define SIZE 512 /* Default stack size. */ |
@@ -90,10 +82,7 @@ static void r_sort_b __P((const u_char **, | |||
90 | } | 82 | } |
91 | 83 | ||
92 | int | 84 | int |
93 | radixsort(a, n, tab, endch) | 85 | radixsort(const u_char **a, int n, const u_char *tab, u_int endch) |
94 | const u_char **a, *tab; | ||
95 | int n; | ||
96 | u_int endch; | ||
97 | { | 86 | { |
98 | const u_char *tr; | 87 | const u_char *tr; |
99 | int c; | 88 | int c; |
@@ -105,10 +94,7 @@ radixsort(a, n, tab, endch) | |||
105 | } | 94 | } |
106 | 95 | ||
107 | int | 96 | int |
108 | sradixsort(a, n, tab, endch) | 97 | sradixsort(const u_char **a, int n, const u_char *tab, u_int endch) |
109 | const u_char **a, *tab; | ||
110 | int n; | ||
111 | u_int endch; | ||
112 | { | 98 | { |
113 | const u_char *tr, **ta; | 99 | const u_char *tr, **ta; |
114 | int c; | 100 | int c; |
@@ -133,15 +119,11 @@ sradixsort(a, n, tab, endch) | |||
133 | 119 | ||
134 | /* Unstable, in-place sort. */ | 120 | /* Unstable, in-place sort. */ |
135 | void | 121 | void |
136 | r_sort_a(a, n, i, tr, endch) | 122 | r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch) |
137 | const u_char **a; | ||
138 | int n, i; | ||
139 | const u_char *tr; | ||
140 | u_int endch; | ||
141 | { | 123 | { |
142 | static int count[256], nc, bmin; | 124 | static int count[256], nc, bmin; |
143 | register int c; | 125 | int c; |
144 | register const u_char **ak, *r; | 126 | const u_char **ak, *r; |
145 | stack s[SIZE], *sp, *sp0, *sp1, temp; | 127 | stack s[SIZE], *sp, *sp0, *sp1, temp; |
146 | int *cp, bigc; | 128 | int *cp, bigc; |
147 | const u_char **an, *t, **aj, **top[256]; | 129 | const u_char **an, *t, **aj, **top[256]; |
@@ -224,15 +206,12 @@ r_sort_a(a, n, i, tr, endch) | |||
224 | 206 | ||
225 | /* Stable sort, requiring additional memory. */ | 207 | /* Stable sort, requiring additional memory. */ |
226 | void | 208 | void |
227 | r_sort_b(a, ta, n, i, tr, endch) | 209 | r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr, |
228 | const u_char **a, **ta; | 210 | u_int endch) |
229 | int n, i; | ||
230 | const u_char *tr; | ||
231 | u_int endch; | ||
232 | { | 211 | { |
233 | static int count[256], nc, bmin; | 212 | static int count[256], nc, bmin; |
234 | register int c; | 213 | int c; |
235 | register const u_char **ak, **ai; | 214 | const u_char **ak, **ai; |
236 | stack s[512], *sp, *sp0, *sp1, temp; | 215 | stack s[512], *sp, *sp0, *sp1, temp; |
237 | const u_char **top[256]; | 216 | const u_char **top[256]; |
238 | int *cp, bigc; | 217 | int *cp, bigc; |
@@ -295,14 +274,11 @@ r_sort_b(a, ta, n, i, tr, endch) | |||
295 | } | 274 | } |
296 | } | 275 | } |
297 | 276 | ||
298 | static inline void | 277 | static __inline void |
299 | simplesort(a, n, b, tr, endch) /* insertion sort */ | 278 | simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch) |
300 | register const u_char **a; | 279 | /* insertion sort */ |
301 | int n, b; | ||
302 | register const u_char *tr; | ||
303 | u_int endch; | ||
304 | { | 280 | { |
305 | register u_char ch; | 281 | u_char ch; |
306 | const u_char **ak, **ai, *s, *t; | 282 | const u_char **ak, **ai, *s, *t; |
307 | 283 | ||
308 | for (ak = a+1; --n >= 1; ak++) | 284 | for (ak = a+1; --n >= 1; ak++) |
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3 index a0e7740e66..0d9c52a3b9 100644 --- a/src/lib/libc/stdlib/rand.3 +++ b/src/lib/libc/stdlib/rand.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)rand.3 6.7 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: rand.3,v 1.9 2003/06/02 20:18:38 millert Exp $ |
37 | .\" $Id: rand.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt RAND 3 | 35 | .Dt RAND 3 |
@@ -46,12 +41,15 @@ | |||
46 | .Sh SYNOPSIS | 41 | .Sh SYNOPSIS |
47 | .Fd #include <stdlib.h> | 42 | .Fd #include <stdlib.h> |
48 | .Ft void | 43 | .Ft void |
49 | .Fn srand "unsigned seed" | 44 | .Fn srand "unsigned int seed" |
50 | .Ft int | 45 | .Ft int |
51 | .Fn rand void | 46 | .Fn rand void |
47 | .Ft int | ||
48 | .Fn rand_r "unsigned int *seed" | ||
52 | .Sh DESCRIPTION | 49 | .Sh DESCRIPTION |
53 | .Bf -symbolic | 50 | .Bf -symbolic |
54 | These interfaces are obsoleted by random(3). | 51 | These interfaces are obsoleted by |
52 | .Xr random 3 . | ||
55 | .Ef | 53 | .Ef |
56 | .Pp | 54 | .Pp |
57 | The | 55 | The |
@@ -73,13 +71,28 @@ with the same seed value. | |||
73 | .Pp | 71 | .Pp |
74 | If no seed value is provided, the functions are automatically | 72 | If no seed value is provided, the functions are automatically |
75 | seeded with a value of 1. | 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. | ||
76 | .Sh SEE ALSO | 82 | .Sh SEE ALSO |
83 | .Xr arc4random 3 , | ||
84 | .Xr rand48 3 , | ||
77 | .Xr random 3 | 85 | .Xr random 3 |
78 | .Sh STANDARDS | 86 | .Sh STANDARDS |
79 | The | 87 | The |
80 | .Fn rand | 88 | .Fn rand |
81 | and | 89 | and |
82 | .Fn srand | 90 | .Fn srand |
83 | functions | 91 | functions conform to |
84 | conform to | ||
85 | .St -ansiC . | 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 index 361d473448..0f9c100807 100644 --- a/src/lib/libc/stdlib/rand.c +++ b/src/lib/libc/stdlib/rand.c | |||
@@ -10,11 +10,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 12 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 13 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 14 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 15 | * without specific prior written permission. |
20 | * | 16 | * |
@@ -31,25 +27,26 @@ | |||
31 | * SUCH DAMAGE. | 27 | * SUCH DAMAGE. |
32 | */ | 28 | */ |
33 | 29 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)rand.c 5.6 (Berkeley) 6/24/91";*/ | ||
36 | static char *rcsid = "$Id: rand.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <sys/types.h> | 30 | #include <sys/types.h> |
40 | #include <stdlib.h> | 31 | #include <stdlib.h> |
41 | 32 | ||
42 | static u_long next = 1; | 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 | } | ||
43 | 41 | ||
44 | int | 42 | int |
45 | rand() | 43 | rand(void) |
46 | { | 44 | { |
47 | return ((next = next * 1103515245 + 12345) % ((u_int)RAND_MAX + 1)); | 45 | return (rand_r(&next)); |
48 | } | 46 | } |
49 | 47 | ||
50 | void | 48 | void |
51 | srand(seed) | 49 | srand(u_int seed) |
52 | u_int seed; | ||
53 | { | 50 | { |
54 | next = seed; | 51 | next = seed; |
55 | } | 52 | } |
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 index 5a772c9a8c..78cd0a7cd5 100644 --- a/src/lib/libc/stdlib/rand48.3 +++ b/src/lib/libc/stdlib/rand48.3 | |||
@@ -1,4 +1,4 @@ | |||
1 | \" Copyright (c) 1993 Martin Birgmeier | 1 | .\" Copyright (c) 1993 Martin Birgmeier |
2 | .\" All rights reserved. | 2 | .\" All rights reserved. |
3 | .\" | 3 | .\" |
4 | .\" You may redistribute unmodified or modified versions of this source | 4 | .\" You may redistribute unmodified or modified versions of this source |
@@ -9,7 +9,7 @@ | |||
9 | .\" of any kind. I shall in no event be liable for anything that happens | 9 | .\" of any kind. I shall in no event be liable for anything that happens |
10 | .\" to anyone/anything when using this software. | 10 | .\" to anyone/anything when using this software. |
11 | .\" | 11 | .\" |
12 | .\" $Id: rand48.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | 12 | .\" $OpenBSD: rand48.3,v 1.10 2003/06/01 19:27:27 jmc Exp $ |
13 | .\" | 13 | .\" |
14 | .Dd October 8, 1993 | 14 | .Dd October 8, 1993 |
15 | .Dt RAND48 3 | 15 | .Dt RAND48 3 |
@@ -24,10 +24,10 @@ | |||
24 | .Nm srand48 , | 24 | .Nm srand48 , |
25 | .Nm seed48 , | 25 | .Nm seed48 , |
26 | .Nm lcong48 | 26 | .Nm lcong48 |
27 | .Nd pseudo random number generators and initialization routines | 27 | .Nd pseudo-random number generators and initialization routines |
28 | .Sh SYNOPSIS | 28 | .Sh SYNOPSIS |
29 | .Fd #include <stdlib.h> | 29 | .Fd #include <stdlib.h> |
30 | .Ft double | 30 | .Ft double |
31 | .Fn drand48 void | 31 | .Fn drand48 void |
32 | .Ft double | 32 | .Ft double |
33 | .Fn erand48 "unsigned short xseed[3]" | 33 | .Fn erand48 "unsigned short xseed[3]" |
@@ -49,12 +49,13 @@ | |||
49 | The | 49 | The |
50 | .Fn rand48 | 50 | .Fn rand48 |
51 | family of functions generates pseudo-random numbers using a linear | 51 | family of functions generates pseudo-random numbers using a linear |
52 | congruential algorithm working on integers 48 bits in size. The | 52 | congruential algorithm working on integers 48 bits in size. |
53 | particular formula employed is | 53 | The particular formula employed is |
54 | r(n+1) = (a * r(n) + c) mod m | 54 | r(n+1) = (a * r(n) + c) mod m |
55 | where the default values are | 55 | where the default values are |
56 | for the multiplicand a = 0xfdeece66d = 25214903917 and | 56 | for the multiplicand a = 0xfdeece66d = 25214903917 and |
57 | the addend c = 0xb = 11. The modulus is always fixed at m = 2 ** 48. | 57 | the addend c = 0xb = 11. |
58 | The modulus is always fixed at m = 2 ** 48. | ||
58 | r(n) is called the seed of the random number generator. | 59 | r(n) is called the seed of the random number generator. |
59 | .Pp | 60 | .Pp |
60 | For all the six generator routines described next, the first | 61 | For all the six generator routines described next, the first |
@@ -63,15 +64,17 @@ computational step is to perform a single iteration of the algorithm. | |||
63 | .Fn drand48 | 64 | .Fn drand48 |
64 | and | 65 | and |
65 | .Fn erand48 | 66 | .Fn erand48 |
66 | return values of type double. The full 48 bits of r(n+1) are | 67 | return values of type double. |
68 | The full 48 bits of r(n+1) are | ||
67 | loaded into the mantissa of the returned value, with the exponent set | 69 | loaded into the mantissa of the returned value, with the exponent set |
68 | such that the values produced lie in the interval [0.0, 1.0). | 70 | such that the values produced lie in the interval [0.0, 1.0]. |
69 | .Pp | 71 | .Pp |
70 | .Fn lrand48 | 72 | .Fn lrand48 |
71 | and | 73 | and |
72 | .Fn nrand48 | 74 | .Fn nrand48 |
73 | return values of type long in the range | 75 | return values of type long in the range |
74 | [0, 2**31-1]. The high-order (31) bits of | 76 | [0, 2**31-1]. |
77 | The high-order (31) bits of | ||
75 | r(n+1) are loaded into the lower bits of the returned value, with | 78 | r(n+1) are loaded into the lower bits of the returned value, with |
76 | the topmost (sign) bit set to zero. | 79 | the topmost (sign) bit set to zero. |
77 | .Pp | 80 | .Pp |
@@ -79,14 +82,15 @@ the topmost (sign) bit set to zero. | |||
79 | and | 82 | and |
80 | .Fn jrand48 | 83 | .Fn jrand48 |
81 | return values of type long in the range | 84 | return values of type long in the range |
82 | [-2**31, 2**31-1]. The high-order (32) bits of | 85 | [-2**31, 2**31-1]. |
83 | r(n+1) are loaded into the returned value. | 86 | The high-order (32) bits of r(n+1) are loaded into the returned value. |
84 | .Pp | 87 | .Pp |
85 | .Fn drand48 , | 88 | .Fn drand48 , |
86 | .Fn lrand48 , | 89 | .Fn lrand48 , |
87 | and | 90 | and |
88 | .Fn mrand48 | 91 | .Fn mrand48 |
89 | use an internal buffer to store r(n). For these functions | 92 | use an internal buffer to store r(n). |
93 | For these functions | ||
90 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. | 94 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. |
91 | .Pp | 95 | .Pp |
92 | On the other hand, | 96 | On the other hand, |
@@ -118,12 +122,12 @@ also initializes the internal buffer r(n) of | |||
118 | and | 122 | and |
119 | .Fn mrand48 , | 123 | .Fn mrand48 , |
120 | but here all 48 bits of the seed can be specified in an array of 3 shorts, | 124 | but here all 48 bits of the seed can be specified in an array of 3 shorts, |
121 | where the zeroth member specifies the lowest bits. Again, | 125 | where the zeroth member specifies the lowest bits. |
122 | the constant multiplicand and addend of the algorithm are | 126 | Again, the constant multiplicand and addend of the algorithm are |
123 | reset to the default values given above. | 127 | reset to the default values given above. |
124 | .Fn seed48 | 128 | .Fn seed48 |
125 | returns a pointer to an array of 3 shorts which contains the old seed. | 129 | returns a pointer to an array of 3 shorts which contains the old seed. |
126 | This array is statically allocated, thus its contents are lost after | 130 | This array is statically allocated, so its contents are lost after |
127 | each new call to | 131 | each new call to |
128 | .Fn seed48 . | 132 | .Fn seed48 . |
129 | .Pp | 133 | .Pp |
@@ -152,9 +156,10 @@ always also set the multiplicand and addend for any of the six | |||
152 | generator calls. | 156 | generator calls. |
153 | .Pp | 157 | .Pp |
154 | For a more powerful random number generator, see | 158 | For a more powerful random number generator, see |
155 | .Xr random 3 | 159 | .Xr random 3 . |
156 | .Sh AUTHOR | ||
157 | Martin Birgmeier | ||
158 | .Sh SEE ALSO | 160 | .Sh SEE ALSO |
161 | .Xr arc4random 3 , | ||
159 | .Xr rand 3 , | 162 | .Xr rand 3 , |
160 | .Xr random 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 index 12496d1c8c..afa49f65f3 100644 --- a/src/lib/libc/stdlib/rand48.h +++ b/src/lib/libc/stdlib/rand48.h | |||
@@ -9,6 +9,8 @@ | |||
9 | * This software is provided ``as is'', and comes with no warranties | 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 | 10 | * of any kind. I shall in no event be liable for anything that happens |
11 | * to anyone/anything when using this software. | 11 | * to anyone/anything when using this software. |
12 | * | ||
13 | * $OpenBSD: rand48.h,v 1.3 2002/02/16 21:27:24 millert Exp $ | ||
12 | */ | 14 | */ |
13 | 15 | ||
14 | #ifndef _RAND48_H_ | 16 | #ifndef _RAND48_H_ |
@@ -17,7 +19,7 @@ | |||
17 | #include <math.h> | 19 | #include <math.h> |
18 | #include <stdlib.h> | 20 | #include <stdlib.h> |
19 | 21 | ||
20 | void __dorand48 __P((unsigned short[3])); | 22 | void __dorand48(unsigned short[3]); |
21 | 23 | ||
22 | #define RAND48_SEED_0 (0x330e) | 24 | #define RAND48_SEED_0 (0x330e) |
23 | #define RAND48_SEED_1 (0xabcd) | 25 | #define RAND48_SEED_1 (0xabcd) |
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3 index 38c15a9803..626b040b50 100644 --- a/src/lib/libc/stdlib/random.3 +++ b/src/lib/libc/stdlib/random.3 | |||
@@ -9,11 +9,7 @@ | |||
9 | .\" 2. Redistributions in binary form must reproduce the above copyright | 9 | .\" 2. Redistributions in binary form must reproduce the above copyright |
10 | .\" notice, this list of conditions and the following disclaimer in the | 10 | .\" notice, this list of conditions and the following disclaimer in the |
11 | .\" documentation and/or other materials provided with the distribution. | 11 | .\" documentation and/or other materials provided with the distribution. |
12 | .\" 3. All advertising materials mentioning features or use of this software | 12 | .\" 3. Neither the name of the University nor the names of its contributors |
13 | .\" must display the following acknowledgement: | ||
14 | .\" This product includes software developed by the University of | ||
15 | .\" California, Berkeley and its contributors. | ||
16 | .\" 4. 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 | 13 | .\" may be used to endorse or promote products derived from this software |
18 | .\" without specific prior written permission. | 14 | .\" without specific prior written permission. |
19 | .\" | 15 | .\" |
@@ -29,97 +25,105 @@ | |||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 25 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
30 | .\" SUCH DAMAGE. | 26 | .\" SUCH DAMAGE. |
31 | .\" | 27 | .\" |
32 | .\" from: @(#)random.3 6.5 (Berkeley) 4/19/91 | 28 | .\" $OpenBSD: random.3,v 1.18 2005/11/30 07:51:02 otto Exp $ |
33 | .\" $Id: random.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
34 | .\" | 29 | .\" |
35 | .Dd April 19, 1991 | 30 | .Dd April 19, 1991 |
36 | .Dt RANDOM 3 | 31 | .Dt RANDOM 3 |
37 | .Os BSD 4.2 | 32 | .Os |
38 | .Sh NAME | 33 | .Sh NAME |
39 | .Nm random , | 34 | .Nm random , |
40 | .Nm srandom , | 35 | .Nm srandom , |
36 | .Nm srandomdev , | ||
41 | .Nm initstate , | 37 | .Nm initstate , |
42 | .Nm setstate | 38 | .Nm setstate |
43 | .Nd better random number generator; routines for changing generators | 39 | .Nd better random number generator; routines for changing generators |
44 | .Sh SYNOPSIS | 40 | .Sh SYNOPSIS |
45 | .Fd #include <stdlib.h> | 41 | .Fd #include <stdlib.h> |
46 | .Ft long | 42 | .Ft long |
47 | .Fn random void | 43 | .Fn random void |
48 | .Ft void | 44 | .Ft void |
49 | .Fn srandom "unsigned seed" | 45 | .Fn srandom "unsigned int seed" |
46 | .Ft void | ||
47 | .Fn srandomdev void | ||
50 | .Ft char * | 48 | .Ft char * |
51 | .Fn initstate "unsigned seed" "char *state" "int n" | 49 | .Fn initstate "unsigned int seed" "char *state" "size_t n" |
52 | .Ft char * | 50 | .Ft char * |
53 | .Fn setstate "char *state" | 51 | .Fn setstate "const char *state" |
54 | .Sh DESCRIPTION | 52 | .Sh DESCRIPTION |
55 | The | 53 | The |
56 | .Fn random | 54 | .Fn random |
57 | function | 55 | function uses a non-linear additive feedback random number generator employing |
58 | uses a non-linear additive feedback random number generator employing a | 56 | a default table of size 31 long integers to return successive pseudo-random |
59 | default table of size 31 long integers to return successive pseudo-random | 57 | numbers in the range from 0 to (2**31)\-1. |
60 | numbers in the range from 0 to | ||
61 | .if t 2\u\s731\s10\d\(mi1. | ||
62 | .if n (2**31)\(mi1. | ||
63 | The period of this random number generator is very large, approximately | 58 | The period of this random number generator is very large, approximately |
64 | .if t 16\(mu(2\u\s731\s10\d\(mi1). | 59 | 16*((2**31)\-1). |
65 | .if n 16*((2**31)\(mi1). | ||
66 | .Pp | 60 | .Pp |
67 | The | 61 | The |
68 | .Fn random Ns / Fn srandom | 62 | .Fn random |
69 | have (almost) the same calling sequence and initialization properties as | 63 | and |
70 | .Xr rand 3 Ns / Xr srand 3 . | 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 . | ||
71 | The difference is that | 68 | The difference is that |
72 | .Xr rand | 69 | .Xr rand |
73 | produces a much less random sequence \(em in fact, the low dozen bits | 70 | produces a much less random sequence \(em in fact, the low dozen bits |
74 | generated by rand go through a cyclic pattern. All the bits generated by | 71 | generated by rand go through a cyclic pattern. |
72 | All the bits generated by | ||
75 | .Fn random | 73 | .Fn random |
76 | are usable. For example, | 74 | are usable. |
75 | For example, | ||
77 | .Sq Li random()&01 | 76 | .Sq Li random()&01 |
78 | will produce a random binary | 77 | will produce a random binary |
79 | value. | 78 | value. |
80 | .Pp | 79 | .Pp |
81 | Unlike | 80 | Like |
82 | .Xr srand , | ||
83 | .Fn srandom | ||
84 | does not return the old seed; the reason for this is that the amount of | ||
85 | state information used is much more than a single word. (Two other | ||
86 | routines are provided to deal with restarting/changing random | ||
87 | number generators). Like | ||
88 | .Xr rand 3 , | 81 | .Xr rand 3 , |
89 | however, | ||
90 | .Fn random | 82 | .Fn random |
91 | will by default produce a sequence of numbers that can be duplicated | 83 | will by default produce a sequence of numbers that can be duplicated |
92 | by calling | 84 | by calling |
93 | .Fn srandom | 85 | .Fn srandom |
94 | with | 86 | with |
95 | .Ql 1 | 87 | .Ql 1 |
96 | as the seed. | 88 | as the seed. |
97 | .Pp | 89 | .Pp |
98 | The | 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 | ||
99 | .Fn initstate | 103 | .Fn initstate |
100 | routine allows a state array, passed in as an argument, to be initialized | 104 | routine allows a state array, passed in as an argument, to be initialized |
101 | for future use. The size of the state array (in bytes) is used by | 105 | for future use. |
106 | The size of the state array (in bytes) is used by | ||
102 | .Fn initstate | 107 | .Fn initstate |
103 | to decide how sophisticated a random number generator it should use \(em the | 108 | to decide how sophisticated a random number generator it should use \(em the |
104 | more state, the better the random numbers will be. | 109 | more state, the better the random numbers will be. |
105 | (Current "optimal" values for the amount of state information are | 110 | (Current "optimal" values for the amount of state information are |
106 | 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to | 111 | 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to |
107 | the nearest known amount. Using less than 8 bytes will cause an error.) | 112 | the nearest known amount. |
113 | Using less than 8 bytes will cause an error.) | ||
108 | The seed for the initialization (which specifies a starting point for | 114 | The seed for the initialization (which specifies a starting point for |
109 | the random number sequence, and provides for restarting at the same | 115 | the random number sequence, and provides for restarting at the same |
110 | point) is also an argument. | 116 | point) is also an argument. |
111 | The | 117 | The |
112 | .Fn initstate | 118 | .Fn initstate |
113 | function | 119 | function returns a pointer to the previous state information array. |
114 | returns a pointer to the previous state information array. | ||
115 | .Pp | 120 | .Pp |
116 | Once a state has been initialized, the | 121 | Once a state has been initialized, the |
117 | .Fn setstate | 122 | .Fn setstate |
118 | routine provides for rapid switching between states. | 123 | routine provides for rapid switching between states. |
119 | The | 124 | The |
120 | .Fn setstate | 125 | .Fn setstate |
121 | function | 126 | function returns a pointer to the previous state array; its |
122 | returns a pointer to the previous state array; its | ||
123 | argument state array is used for further random number generation | 127 | argument state array is used for further random number generation |
124 | until the next call to | 128 | until the next call to |
125 | .Fn initstate | 129 | .Fn initstate |
@@ -143,12 +147,8 @@ is that the size of the state array does not have to be remembered after | |||
143 | it is initialized. | 147 | it is initialized. |
144 | .Pp | 148 | .Pp |
145 | With 256 bytes of state information, the period of the random number | 149 | With 256 bytes of state information, the period of the random number |
146 | generator is greater than | 150 | generator is greater than 2**69 |
147 | .if t 2\u\s769\s10\d, | ||
148 | .if n 2**69 | ||
149 | which should be sufficient for most purposes. | 151 | which should be sufficient for most purposes. |
150 | .Sh AUTHOR | ||
151 | Earl T. Cohen | ||
152 | .Sh DIAGNOSTICS | 152 | .Sh DIAGNOSTICS |
153 | If | 153 | If |
154 | .Fn initstate | 154 | .Fn initstate |
@@ -157,11 +157,29 @@ is called with less than 8 bytes of state information, or if | |||
157 | detects that the state information has been garbled, error | 157 | detects that the state information has been garbled, error |
158 | messages are printed on the standard error output. | 158 | messages are printed on the standard error output. |
159 | .Sh SEE ALSO | 159 | .Sh SEE ALSO |
160 | .Xr rand 3 | 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. | ||
161 | .Sh HISTORY | 177 | .Sh HISTORY |
162 | These | 178 | These |
163 | functions appeared in | 179 | functions appeared in |
164 | .Bx 4.2 . | 180 | .Bx 4.2 . |
181 | .Sh AUTHORS | ||
182 | .An Earl T. Cohen | ||
165 | .Sh BUGS | 183 | .Sh BUGS |
166 | About 2/3 the speed of | 184 | About 2/3 the speed of |
167 | .Xr rand 3 . | 185 | .Xr rand 3 . |
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c index 469b6d976a..48e892042b 100644 --- a/src/lib/libc/stdlib/random.c +++ b/src/lib/libc/stdlib/random.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: random.c,v 1.15 2005/11/30 07:51:02 otto Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | 3 | * Copyright (c) 1983 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,13 +28,13 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 31 | #include <sys/param.h> |
35 | /*static char *sccsid = "from: @(#)random.c 5.9 (Berkeley) 2/23/91";*/ | 32 | #include <sys/sysctl.h> |
36 | static char *rcsid = "$Id: random.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | 33 | #include <sys/time.h> |
37 | #endif /* LIBC_SCCS and not lint */ | 34 | #include <fcntl.h> |
38 | |||
39 | #include <stdio.h> | 35 | #include <stdio.h> |
40 | #include <stdlib.h> | 36 | #include <stdlib.h> |
37 | #include <unistd.h> | ||
41 | 38 | ||
42 | /* | 39 | /* |
43 | * random.c: | 40 | * random.c: |
@@ -55,10 +52,10 @@ static char *rcsid = "$Id: random.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $" | |||
55 | * congruential generator. If the amount of state information is less than | 52 | * congruential generator. If the amount of state information is less than |
56 | * 32 bytes, a simple linear congruential R.N.G. is used. | 53 | * 32 bytes, a simple linear congruential R.N.G. is used. |
57 | * | 54 | * |
58 | * Internally, the state information is treated as an array of longs; the | 55 | * Internally, the state information is treated as an array of int32_t; the |
59 | * zeroeth element of the array is the type of R.N.G. being used (small | 56 | * zeroeth element of the array is the type of R.N.G. being used (small |
60 | * integer); the remainder of the array is the state information for the | 57 | * integer); the remainder of the array is the state information for the |
61 | * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of | 58 | * R.N.G. Thus, 32 bytes of state information will give 7 int32_ts worth of |
62 | * state information, which will allow a degree seven polynomial. (Note: | 59 | * state information, which will allow a degree seven polynomial. (Note: |
63 | * the zeroeth word of state information also has some other information | 60 | * the zeroeth word of state information also has some other information |
64 | * stored in it -- see setstate() for details). | 61 | * stored in it -- see setstate() for details). |
@@ -134,14 +131,14 @@ static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; | |||
134 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. | 131 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. |
135 | */ | 132 | */ |
136 | 133 | ||
137 | static long randtbl[DEG_3 + 1] = { | 134 | static int32_t randtbl[DEG_3 + 1] = { |
138 | TYPE_3, | 135 | TYPE_3, |
139 | 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, | 136 | 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, |
140 | 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, | 137 | 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, |
141 | 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, | 138 | 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471, |
142 | 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, | 139 | 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1, |
143 | 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, | 140 | 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41, |
144 | 0x27fb47b9, | 141 | 0xf3bec5da, |
145 | }; | 142 | }; |
146 | 143 | ||
147 | /* | 144 | /* |
@@ -158,8 +155,8 @@ static long randtbl[DEG_3 + 1] = { | |||
158 | * in the initialization of randtbl) because the state table pointer is set | 155 | * in the initialization of randtbl) because the state table pointer is set |
159 | * to point to randtbl[1] (as explained below). | 156 | * to point to randtbl[1] (as explained below). |
160 | */ | 157 | */ |
161 | static long *fptr = &randtbl[SEP_3 + 1]; | 158 | static int32_t *fptr = &randtbl[SEP_3 + 1]; |
162 | static long *rptr = &randtbl[1]; | 159 | static int32_t *rptr = &randtbl[1]; |
163 | 160 | ||
164 | /* | 161 | /* |
165 | * The following things are the pointer to the state information table, the | 162 | * The following things are the pointer to the state information table, the |
@@ -171,11 +168,11 @@ static long *rptr = &randtbl[1]; | |||
171 | * this is more efficient than indexing every time to find the address of | 168 | * this is more efficient than indexing every time to find the address of |
172 | * the last element to see if the front and rear pointers have wrapped. | 169 | * the last element to see if the front and rear pointers have wrapped. |
173 | */ | 170 | */ |
174 | static long *state = &randtbl[1]; | 171 | static int32_t *state = &randtbl[1]; |
172 | static int32_t *end_ptr = &randtbl[DEG_3 + 1]; | ||
175 | static int rand_type = TYPE_3; | 173 | static int rand_type = TYPE_3; |
176 | static int rand_deg = DEG_3; | 174 | static int rand_deg = DEG_3; |
177 | static int rand_sep = SEP_3; | 175 | static int rand_sep = SEP_3; |
178 | static long *end_ptr = &randtbl[DEG_3 + 1]; | ||
179 | 176 | ||
180 | /* | 177 | /* |
181 | * srandom: | 178 | * srandom: |
@@ -190,18 +187,28 @@ static long *end_ptr = &randtbl[DEG_3 + 1]; | |||
190 | * for default usage relies on values produced by this routine. | 187 | * for default usage relies on values produced by this routine. |
191 | */ | 188 | */ |
192 | void | 189 | void |
193 | srandom(x) | 190 | srandom(unsigned int x) |
194 | u_int x; | ||
195 | { | 191 | { |
196 | register int i, j; | 192 | int i; |
193 | int32_t test; | ||
194 | div_t val; | ||
197 | 195 | ||
198 | if (rand_type == TYPE_0) | 196 | if (rand_type == TYPE_0) |
199 | state[0] = x; | 197 | state[0] = x; |
200 | else { | 198 | else { |
201 | j = 1; | ||
202 | state[0] = x; | 199 | state[0] = x; |
203 | for (i = 1; i < rand_deg; i++) | 200 | for (i = 1; i < rand_deg; i++) { |
204 | state[i] = 1103515245 * state[i - 1] + 12345; | 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 | } | ||
205 | fptr = &state[rand_sep]; | 212 | fptr = &state[rand_sep]; |
206 | rptr = &state[0]; | 213 | rptr = &state[0]; |
207 | for (i = 0; i < 10 * rand_deg; i++) | 214 | for (i = 0; i < 10 * rand_deg; i++) |
@@ -210,6 +217,38 @@ srandom(x) | |||
210 | } | 217 | } |
211 | 218 | ||
212 | /* | 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 | /* | ||
213 | * initstate: | 252 | * initstate: |
214 | * | 253 | * |
215 | * Initialize the state information in the given array of n bytes for future | 254 | * Initialize the state information in the given array of n bytes for future |
@@ -229,22 +268,16 @@ srandom(x) | |||
229 | * Returns a pointer to the old state. | 268 | * Returns a pointer to the old state. |
230 | */ | 269 | */ |
231 | char * | 270 | char * |
232 | initstate(seed, arg_state, n) | 271 | initstate(u_int seed, char *arg_state, size_t n) |
233 | u_int seed; /* seed for R.N.G. */ | ||
234 | char *arg_state; /* pointer to state array */ | ||
235 | int n; /* # bytes of state info */ | ||
236 | { | 272 | { |
237 | register char *ostate = (char *)(&state[-1]); | 273 | char *ostate = (char *)(&state[-1]); |
238 | 274 | ||
239 | if (rand_type == TYPE_0) | 275 | if (rand_type == TYPE_0) |
240 | state[-1] = rand_type; | 276 | state[-1] = rand_type; |
241 | else | 277 | else |
242 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | 278 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; |
243 | if (n < BREAK_0) { | 279 | if (n < BREAK_0) |
244 | (void)fprintf(stderr, | 280 | return(NULL); |
245 | "random: not enough state (%d bytes); ignored.\n", n); | ||
246 | return(0); | ||
247 | } | ||
248 | if (n < BREAK_1) { | 281 | if (n < BREAK_1) { |
249 | rand_type = TYPE_0; | 282 | rand_type = TYPE_0; |
250 | rand_deg = DEG_0; | 283 | rand_deg = DEG_0; |
@@ -266,7 +299,7 @@ initstate(seed, arg_state, n) | |||
266 | rand_deg = DEG_4; | 299 | rand_deg = DEG_4; |
267 | rand_sep = SEP_4; | 300 | rand_sep = SEP_4; |
268 | } | 301 | } |
269 | state = &(((long *)arg_state)[1]); /* first location */ | 302 | state = &(((int32_t *)arg_state)[1]); /* first location */ |
270 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ | 303 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ |
271 | srandom(seed); | 304 | srandom(seed); |
272 | if (rand_type == TYPE_0) | 305 | if (rand_type == TYPE_0) |
@@ -292,12 +325,11 @@ initstate(seed, arg_state, n) | |||
292 | * Returns a pointer to the old state information. | 325 | * Returns a pointer to the old state information. |
293 | */ | 326 | */ |
294 | char * | 327 | char * |
295 | setstate(arg_state) | 328 | setstate(const char *arg_state) |
296 | char *arg_state; | ||
297 | { | 329 | { |
298 | register long *new_state = (long *)arg_state; | 330 | int32_t *new_state = (int32_t *)arg_state; |
299 | register int type = new_state[0] % MAX_TYPES; | 331 | int32_t type = new_state[0] % MAX_TYPES; |
300 | register int rear = new_state[0] / MAX_TYPES; | 332 | int32_t rear = new_state[0] / MAX_TYPES; |
301 | char *ostate = (char *)(&state[-1]); | 333 | char *ostate = (char *)(&state[-1]); |
302 | 334 | ||
303 | if (rand_type == TYPE_0) | 335 | if (rand_type == TYPE_0) |
@@ -315,8 +347,7 @@ setstate(arg_state) | |||
315 | rand_sep = seps[type]; | 347 | rand_sep = seps[type]; |
316 | break; | 348 | break; |
317 | default: | 349 | default: |
318 | (void)fprintf(stderr, | 350 | return(NULL); |
319 | "random: state info corrupted; not changed.\n"); | ||
320 | } | 351 | } |
321 | state = &new_state[1]; | 352 | state = &new_state[1]; |
322 | if (rand_type != TYPE_0) { | 353 | if (rand_type != TYPE_0) { |
@@ -345,9 +376,9 @@ setstate(arg_state) | |||
345 | * Returns a 31-bit random number. | 376 | * Returns a 31-bit random number. |
346 | */ | 377 | */ |
347 | long | 378 | long |
348 | random() | 379 | random(void) |
349 | { | 380 | { |
350 | long i; | 381 | int32_t i; |
351 | 382 | ||
352 | if (rand_type == TYPE_0) | 383 | if (rand_type == TYPE_0) |
353 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; | 384 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; |
@@ -360,5 +391,5 @@ random() | |||
360 | } else if (++rptr >= end_ptr) | 391 | } else if (++rptr >= end_ptr) |
361 | rptr = state; | 392 | rptr = state; |
362 | } | 393 | } |
363 | return(i); | 394 | return((long)i); |
364 | } | 395 | } |
diff --git a/src/lib/libc/stdlib/realloc.3 b/src/lib/libc/stdlib/realloc.3 deleted file mode 100644 index 66f09b2081..0000000000 --- a/src/lib/libc/stdlib/realloc.3 +++ /dev/null | |||
@@ -1,100 +0,0 @@ | |||
1 | .\" Copyright (c) 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. All advertising materials mentioning features or use of this software | ||
13 | .\" must display the following acknowledgement: | ||
14 | .\" This product includes software developed by the University of | ||
15 | .\" California, Berkeley and its contributors. | ||
16 | .\" 4. Neither the name of the University nor the names of its contributors | ||
17 | .\" may be used to endorse or promote products derived from this software | ||
18 | .\" without specific prior written permission. | ||
19 | .\" | ||
20 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
21 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
22 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
23 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
24 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
25 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
26 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
27 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
28 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
30 | .\" SUCH DAMAGE. | ||
31 | .\" | ||
32 | .\" from: @(#)realloc.3 5.1 (Berkeley) 5/2/91 | ||
33 | .\" $Id: realloc.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
34 | .\" | ||
35 | .Dd May 2, 1991 | ||
36 | .Dt REALLOC 3 | ||
37 | .Os | ||
38 | .Sh NAME | ||
39 | .Nm realloc | ||
40 | .Nd reallocation of memory function | ||
41 | .Sh SYNOPSIS | ||
42 | .Fd #include <stdlib.h> | ||
43 | .Ft void * | ||
44 | .Fn realloc "void *ptr" "size_t size" | ||
45 | .Sh DESCRIPTION | ||
46 | The | ||
47 | .Fn realloc | ||
48 | function changes the size of the object pointed to by | ||
49 | .Fa ptr | ||
50 | to the size specified by | ||
51 | .Fa size . | ||
52 | The contents of the object are unchanged up to the lesser | ||
53 | of the new and old sizes. | ||
54 | If the new size is larger, the value of the newly allocated portion | ||
55 | of the object is indeterminate. | ||
56 | If | ||
57 | .Fa ptr | ||
58 | is a null pointer, the | ||
59 | .Fn realloc | ||
60 | function behaves like the | ||
61 | .Xr malloc 3 | ||
62 | function for the specified size. | ||
63 | Otherwise, if | ||
64 | .Fa ptr | ||
65 | does not match a pointer earlier returned by the | ||
66 | .Xr calloc 3 , | ||
67 | .Xr malloc 3 , | ||
68 | or | ||
69 | .Fn realloc | ||
70 | function, or if the space has been deallocated | ||
71 | by a call to the | ||
72 | .Xr free | ||
73 | or | ||
74 | .Fn realloc | ||
75 | function, unpredictable and usually detrimental | ||
76 | behavior will occur. | ||
77 | If the space cannot be allocated, the object | ||
78 | pointed to by | ||
79 | .Fa ptr | ||
80 | is unchanged. | ||
81 | If | ||
82 | .Fa size | ||
83 | is zero and | ||
84 | .Fa ptr | ||
85 | is not a null pointer, the object it points to is freed. | ||
86 | .Pp | ||
87 | The | ||
88 | .Fn realloc | ||
89 | function returns either a null pointer or a pointer | ||
90 | to the possibly moved allocated space. | ||
91 | .Sh SEE ALSO | ||
92 | .Xr alloca 3 , | ||
93 | .Xr calloc 3 , | ||
94 | .Xr free 3 , | ||
95 | .Xr malloc 3 , | ||
96 | .Sh STANDARDS | ||
97 | The | ||
98 | .Fn realloc | ||
99 | function conforms to | ||
100 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3 index 9d8b1ff2ce..e1e0b10605 100644 --- a/src/lib/libc/stdlib/realpath.3 +++ b/src/lib/libc/stdlib/realpath.3 | |||
@@ -12,11 +12,7 @@ | |||
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | 12 | .\" 2. Redistributions in binary form must reproduce the above copyright |
13 | .\" notice, this list of conditions and the following disclaimer in the | 13 | .\" notice, this list of conditions and the following disclaimer in the |
14 | .\" documentation and/or other materials provided with the distribution. | 14 | .\" documentation and/or other materials provided with the distribution. |
15 | .\" 3. All advertising materials mentioning features or use of this software | 15 | .\" 3. Neither the name of the University nor the names of its contributors |
16 | .\" must display the following acknowledgement: | ||
17 | .\" This product includes software developed by the University of | ||
18 | .\" California, Berkeley and its contributors. | ||
19 | .\" 4. Neither the name of the University nor the names of its contributors | ||
20 | .\" may be used to endorse or promote products derived from this software | 16 | .\" may be used to endorse or promote products derived from this software |
21 | .\" without specific prior written permission. | 17 | .\" without specific prior written permission. |
22 | .\" | 18 | .\" |
@@ -32,10 +28,9 @@ | |||
32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 | .\" SUCH DAMAGE. | 29 | .\" SUCH DAMAGE. |
34 | .\" | 30 | .\" |
35 | .\" from: @(#)realpath.3 8.2 (Berkeley) 2/16/94 | 31 | .\" $OpenBSD: realpath.3,v 1.13 2005/04/03 18:59:15 otto Exp $ |
36 | .\" $Id: realpath.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
37 | .\" | 32 | .\" |
38 | .Dd "February 16, 1994" | 33 | .Dd February 16, 1994 |
39 | .Dt REALPATH 3 | 34 | .Dt REALPATH 3 |
40 | .Os | 35 | .Os |
41 | .Sh NAME | 36 | .Sh NAME |
@@ -45,7 +40,7 @@ | |||
45 | .Fd #include <sys/param.h> | 40 | .Fd #include <sys/param.h> |
46 | .Fd #include <stdlib.h> | 41 | .Fd #include <stdlib.h> |
47 | .Ft "char *" | 42 | .Ft "char *" |
48 | .Fn realpath "const char *pathname" "char resolvedname[MAXPATHLEN]" | 43 | .Fn realpath "const char *pathname" "char resolved[PATH_MAX]" |
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn realpath | 46 | .Fn realpath |
@@ -57,18 +52,17 @@ and | |||
57 | .Pa /../ | 52 | .Pa /../ |
58 | in | 53 | in |
59 | .Fa pathname , | 54 | .Fa pathname , |
60 | and copies the resulting absolute pathname into | 55 | and copies the resulting absolute pathname into the memory referenced by |
61 | the memory referenced by | 56 | .Fa resolved . |
62 | .Fa resolvedname . | ||
63 | The | 57 | The |
64 | .Fa resolvedname | 58 | .Fa resolved |
65 | argument | 59 | argument |
66 | .Em must | 60 | .Em must |
67 | refer to a buffer capable of storing at least | 61 | refer to a buffer capable of storing at least |
68 | .Dv MAXPATHLEN | 62 | .Dv PATH_MAX |
69 | characters. | 63 | characters. |
70 | .Pp | 64 | .Pp |
71 | The | 65 | The |
72 | .Fn realpath | 66 | .Fn realpath |
73 | function will resolve both absolute and relative paths | 67 | function will resolve both absolute and relative paths |
74 | and return the absolute pathname corresponding to | 68 | and return the absolute pathname corresponding to |
@@ -78,33 +72,37 @@ All but the last component of | |||
78 | must exist when | 72 | must exist when |
79 | .Fn realpath | 73 | .Fn realpath |
80 | is called. | 74 | is called. |
81 | .Sh "RETURN VALUES" | 75 | .Sh RETURN VALUES |
82 | The | 76 | The |
83 | .Fn realpath | 77 | .Fn realpath |
84 | function returns | 78 | function returns |
85 | .Fa resolved_name | 79 | .Fa resolved |
86 | on success. | 80 | on success. |
87 | If an error occurs, | 81 | If an error occurs, |
88 | .Fn realpath | 82 | .Fn realpath |
89 | returns | 83 | returns |
90 | .Dv NULL , | 84 | .Dv NULL |
91 | and | 85 | and the contents of |
92 | .Fa resolved_name | 86 | .Fa resolved |
93 | contains the pathname which caused the problem. | 87 | are undefined. |
94 | .Sh ERRORS | 88 | .Sh ERRORS |
95 | The function | 89 | The function |
96 | .Fn realpath | 90 | .Fn realpath |
97 | may fail and set the external variable | 91 | may fail and set the external variable |
98 | .Va errno | 92 | .Va errno |
99 | for any of the errors specified for the library functions | 93 | for any of the errors specified for the library functions |
100 | .Xr chdir 2 , | ||
101 | .Xr close 2 , | ||
102 | .Xr fchdir 2 , | ||
103 | .Xr lstat 2 , | 94 | .Xr lstat 2 , |
104 | .Xr open 2 , | 95 | .Xr readlink 2 , |
105 | .Xr readlink 2 | ||
106 | and | 96 | and |
107 | .Xr getcwd 3 . | 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 . | ||
108 | .Sh CAVEATS | 106 | .Sh CAVEATS |
109 | This implementation of | 107 | This implementation of |
110 | .Fn realpath | 108 | .Fn realpath |
@@ -114,13 +112,6 @@ The | |||
114 | version always returns absolute pathnames, | 112 | version always returns absolute pathnames, |
115 | whereas the Solaris implementation will, | 113 | whereas the Solaris implementation will, |
116 | under certain circumstances, return a relative | 114 | under certain circumstances, return a relative |
117 | .Fa resolved_path | 115 | .Fa resolved |
118 | when given a relative | 116 | when given a relative |
119 | .Fa pathname . | 117 | .Fa pathname . |
120 | .Sh "SEE ALSO" | ||
121 | .Xr getcwd 3 | ||
122 | .Sh HISTORY | ||
123 | The | ||
124 | .Fn realpath | ||
125 | function call first appeared in | ||
126 | .Bx 4.4 . | ||
diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c index e349b7e068..4cb847b313 100644 --- a/src/lib/libc/stdlib/realpath.c +++ b/src/lib/libc/stdlib/realpath.c | |||
@@ -1,9 +1,6 @@ | |||
1 | /* $OpenBSD: realpath.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1994 | 3 | * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru> |
3 | * The Regents of the University of California. All rights reserved. | ||
4 | * | ||
5 | * This code is derived from software contributed to Berkeley by | ||
6 | * Jan-Simon Pendry. | ||
7 | * | 4 | * |
8 | * Redistribution and use in source and binary forms, with or without | 5 | * Redistribution and use in source and binary forms, with or without |
9 | * modification, are permitted provided that the following conditions | 6 | * modification, are permitted provided that the following conditions |
@@ -13,18 +10,14 @@ | |||
13 | * 2. Redistributions in binary form must reproduce the above copyright | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
14 | * notice, this list of conditions and the following disclaimer in the | 11 | * notice, this list of conditions and the following disclaimer in the |
15 | * documentation and/or other materials provided with the distribution. | 12 | * documentation and/or other materials provided with the distribution. |
16 | * 3. All advertising materials mentioning features or use of this software | 13 | * 3. The names of the authors may not be used to endorse or promote |
17 | * must display the following acknowledgement: | 14 | * products derived from this software without specific prior written |
18 | * This product includes software developed by the University of | 15 | * permission. |
19 | * California, Berkeley and its contributors. | ||
20 | * 4. Neither the name of the University nor the names of its contributors | ||
21 | * may be used to endorse or promote products derived from this software | ||
22 | * without specific prior written permission. | ||
23 | * | 16 | * |
24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
@@ -34,126 +27,164 @@ | |||
34 | * SUCH DAMAGE. | 27 | * SUCH DAMAGE. |
35 | */ | 28 | */ |
36 | 29 | ||
37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
38 | /*static char sccsid[] = "from: @(#)realpath.c 8.1 (Berkeley) 2/16/94";*/ | ||
39 | static char *rcsid = "$Id: realpath.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
40 | #endif /* LIBC_SCCS and not lint */ | ||
41 | |||
42 | #include <sys/param.h> | 30 | #include <sys/param.h> |
43 | #include <sys/stat.h> | 31 | #include <sys/stat.h> |
44 | 32 | ||
45 | #include <errno.h> | 33 | #include <errno.h> |
46 | #include <fcntl.h> | ||
47 | #include <stdlib.h> | 34 | #include <stdlib.h> |
48 | #include <string.h> | 35 | #include <string.h> |
49 | #include <unistd.h> | 36 | #include <unistd.h> |
50 | 37 | ||
51 | /* | 38 | /* |
52 | * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); | 39 | * char *realpath(const char *path, char resolved[PATH_MAX]); |
53 | * | 40 | * |
54 | * Find the real name of path, by removing all ".", ".." and symlink | 41 | * Find the real name of path, by removing all ".", ".." and symlink |
55 | * components. Returns (resolved) on success, or (NULL) on failure, | 42 | * components. Returns (resolved) on success, or (NULL) on failure, |
56 | * in which case the path which caused trouble is left in (resolved). | 43 | * in which case the path which caused trouble is left in (resolved). |
57 | */ | 44 | */ |
58 | char * | 45 | char * |
59 | realpath(path, resolved) | 46 | realpath(const char *path, char resolved[PATH_MAX]) |
60 | const char *path; | ||
61 | char *resolved; | ||
62 | { | 47 | { |
63 | struct stat sb; | 48 | struct stat sb; |
64 | int fd, n, rootd, serrno; | 49 | char *p, *q, *s; |
65 | char *p, *q, wbuf[MAXPATHLEN]; | 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]; | ||
66 | 54 | ||
67 | /* Save the starting point. */ | 55 | serrno = errno; |
68 | if ((fd = open(".", O_RDONLY)) < 0) { | 56 | symlinks = 0; |
69 | (void)strcpy(resolved, "."); | 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; | ||
70 | return (NULL); | 74 | return (NULL); |
71 | } | 75 | } |
72 | 76 | ||
73 | /* | 77 | /* |
74 | * Find the dirname and basename from the path to be resolved. | 78 | * Iterate over path components in `left'. |
75 | * Change directory to the dirname component. | ||
76 | * lstat the basename part. | ||
77 | * if it is a symlink, read in the value and loop. | ||
78 | * if it is a directory, then change to that directory. | ||
79 | * get the current directory name and append the basename. | ||
80 | */ | 79 | */ |
81 | (void)strncpy(resolved, path, MAXPATHLEN - 1); | 80 | while (left_len != 0) { |
82 | resolved[MAXPATHLEN - 1] = '\0'; | 81 | /* |
83 | loop: | 82 | * Extract the next path component and adjust `left' |
84 | q = strrchr(resolved, '/'); | 83 | * and its length. |
85 | if (q != NULL) { | 84 | */ |
86 | p = q + 1; | 85 | p = strchr(left, '/'); |
87 | if (q == resolved) | 86 | s = p ? p : left + left_len; |
88 | q = "/"; | 87 | if (s - left >= sizeof(next_token)) { |
89 | else { | 88 | errno = ENAMETOOLONG; |
90 | do { | 89 | return (NULL); |
91 | --q; | ||
92 | } while (q > resolved && *q == '/'); | ||
93 | q[1] = '\0'; | ||
94 | q = resolved; | ||
95 | } | 90 | } |
96 | if (chdir(q) < 0) | 91 | memcpy(next_token, left, s - left); |
97 | goto err1; | 92 | next_token[s - left] = '\0'; |
98 | } else | 93 | left_len -= s - left; |
99 | p = resolved; | 94 | if (p != NULL) |
100 | 95 | memmove(left, s + 1, left_len + 1); | |
101 | /* Deal with the last component. */ | 96 | if (resolved[resolved_len - 1] != '/') { |
102 | if (lstat(p, &sb) == 0) { | 97 | if (resolved_len + 1 >= PATH_MAX) { |
103 | if (S_ISLNK(sb.st_mode)) { | 98 | errno = ENAMETOOLONG; |
104 | n = readlink(p, resolved, MAXPATHLEN); | 99 | return (NULL); |
105 | if (n < 0) | 100 | } |
106 | goto err1; | 101 | resolved[resolved_len++] = '/'; |
107 | resolved[n] = '\0'; | 102 | resolved[resolved_len] = '\0'; |
108 | goto loop; | ||
109 | } | 103 | } |
110 | if (S_ISDIR(sb.st_mode)) { | 104 | if (next_token[0] == '\0') |
111 | if (chdir(p) < 0) | 105 | continue; |
112 | goto err1; | 106 | else if (strcmp(next_token, ".") == 0) |
113 | p = ""; | 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; | ||
114 | } | 120 | } |
115 | } | ||
116 | |||
117 | /* | ||
118 | * Save the last component name and get the full pathname of | ||
119 | * the current directory. | ||
120 | */ | ||
121 | (void)strcpy(wbuf, p); | ||
122 | if (getcwd(resolved, MAXPATHLEN) == 0) | ||
123 | goto err1; | ||
124 | 121 | ||
125 | /* | 122 | /* |
126 | * Join the two strings together, ensuring that the right thing | 123 | * Append the next path component and lstat() it. If |
127 | * happens if the last component is empty, or the dirname is root. | 124 | * lstat() fails we still can return successfully if |
128 | */ | 125 | * there are no more path components left. |
129 | if (resolved[0] == '/' && resolved[1] == '\0') | 126 | */ |
130 | rootd = 1; | 127 | resolved_len = strlcat(resolved, next_token, PATH_MAX); |
131 | else | 128 | if (resolved_len >= PATH_MAX) { |
132 | rootd = 0; | ||
133 | |||
134 | if (*wbuf) { | ||
135 | if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { | ||
136 | errno = ENAMETOOLONG; | 129 | errno = ENAMETOOLONG; |
137 | goto err1; | 130 | return (NULL); |
138 | } | 131 | } |
139 | if (rootd == 0) | 132 | if (lstat(resolved, &sb) != 0) { |
140 | (void)strcat(resolved, "/"); | 133 | if (errno == ENOENT && p == NULL) { |
141 | (void)strcat(resolved, wbuf); | 134 | errno = serrno; |
142 | } | 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 | } | ||
143 | 158 | ||
144 | /* Go back to where we came from. */ | 159 | /* |
145 | if (fchdir(fd) < 0) { | 160 | * If there are any path components left, then |
146 | serrno = errno; | 161 | * append them to symlink. The result is placed |
147 | goto err2; | 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 | } | ||
148 | } | 181 | } |
149 | 182 | ||
150 | /* It's okay if the close fails, what's an fd more or less? */ | 183 | /* |
151 | (void)close(fd); | 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'; | ||
152 | return (resolved); | 189 | return (resolved); |
153 | |||
154 | err1: serrno = errno; | ||
155 | (void)fchdir(fd); | ||
156 | err2: (void)close(fd); | ||
157 | errno = serrno; | ||
158 | return (NULL); | ||
159 | } | 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 index e3d31901dd..583262f2d5 100644 --- a/src/lib/libc/stdlib/seed48.c +++ b/src/lib/libc/stdlib/seed48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: seed48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c index a36669888d..36540ebb0c 100644 --- a/src/lib/libc/stdlib/setenv.c +++ b/src/lib/libc/stdlib/setenv.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | 3 | * Copyright (c) 1987 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,30 +28,24 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)setenv.c 5.6 (Berkeley) 6/4/91";*/ | ||
36 | static char *rcsid = "$Id: setenv.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
40 | #include <string.h> | 32 | #include <string.h> |
41 | 33 | ||
34 | char *__findenv(const char *name, int *offset); | ||
35 | |||
36 | extern char **environ; | ||
37 | |||
42 | /* | 38 | /* |
43 | * setenv -- | 39 | * setenv -- |
44 | * Set the value of the environmental variable "name" to be | 40 | * Set the value of the environmental variable "name" to be |
45 | * "value". If rewrite is set, replace any current value. | 41 | * "value". If rewrite is set, replace any current value. |
46 | */ | 42 | */ |
47 | int | 43 | int |
48 | setenv(name, value, rewrite) | 44 | setenv(const char *name, const char *value, int rewrite) |
49 | register const char *name; | ||
50 | register const char *value; | ||
51 | int rewrite; | ||
52 | { | 45 | { |
53 | extern char **environ; | 46 | static char **lastenv; /* last value of environ */ |
54 | static int alloced; /* if allocated space before */ | 47 | char *C; |
55 | register char *C; | ||
56 | int l_value, offset; | 48 | int l_value, offset; |
57 | char *__findenv(); | ||
58 | 49 | ||
59 | if (*value == '=') /* no `=' in value */ | 50 | if (*value == '=') /* no `=' in value */ |
60 | ++value; | 51 | ++value; |
@@ -63,39 +54,34 @@ setenv(name, value, rewrite) | |||
63 | if (!rewrite) | 54 | if (!rewrite) |
64 | return (0); | 55 | return (0); |
65 | if (strlen(C) >= l_value) { /* old larger; copy over */ | 56 | if (strlen(C) >= l_value) { /* old larger; copy over */ |
66 | while (*C++ = *value++); | 57 | while ((*C++ = *value++)) |
58 | ; | ||
67 | return (0); | 59 | return (0); |
68 | } | 60 | } |
69 | } else { /* create new slot */ | 61 | } else { /* create new slot */ |
70 | register int cnt; | 62 | size_t cnt; |
71 | register char **P; | 63 | char **P; |
72 | 64 | ||
73 | for (P = environ, cnt = 0; *P; ++P, ++cnt); | 65 | for (P = environ; *P != NULL; P++) |
74 | if (alloced) { /* just increase size */ | 66 | ; |
75 | environ = (char **)realloc((char *)environ, | 67 | cnt = P - environ; |
76 | (size_t)(sizeof(char *) * (cnt + 2))); | 68 | P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2)); |
77 | if (!environ) | 69 | if (!P) |
78 | return (-1); | 70 | return (-1); |
79 | } | 71 | if (lastenv != environ) |
80 | else { /* get new space */ | 72 | memcpy(P, environ, cnt * sizeof(char *)); |
81 | alloced = 1; /* copy old entries into it */ | 73 | lastenv = environ = P; |
82 | P = (char **)malloc((size_t)(sizeof(char *) * | ||
83 | (cnt + 2))); | ||
84 | if (!P) | ||
85 | return (-1); | ||
86 | bcopy(environ, P, cnt * sizeof(char *)); | ||
87 | environ = P; | ||
88 | } | ||
89 | environ[cnt + 1] = NULL; | ||
90 | offset = cnt; | 74 | offset = cnt; |
75 | environ[cnt + 1] = NULL; | ||
91 | } | 76 | } |
92 | for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ | 77 | for (C = (char *)name; *C && *C != '='; ++C) |
78 | ; /* no `=' in name */ | ||
93 | if (!(environ[offset] = /* name + `=' + value */ | 79 | if (!(environ[offset] = /* name + `=' + value */ |
94 | malloc((size_t)((int)(C - name) + l_value + 2)))) | 80 | malloc((size_t)((int)(C - name) + l_value + 2)))) |
95 | return (-1); | 81 | return (-1); |
96 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) | 82 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) |
97 | ; | 83 | ; |
98 | for (*C++ = '='; *C++ = *value++; ) | 84 | for (*C++ = '='; (*C++ = *value++); ) |
99 | ; | 85 | ; |
100 | return (0); | 86 | return (0); |
101 | } | 87 | } |
@@ -105,15 +91,12 @@ setenv(name, value, rewrite) | |||
105 | * Delete environmental variable "name". | 91 | * Delete environmental variable "name". |
106 | */ | 92 | */ |
107 | void | 93 | void |
108 | unsetenv(name) | 94 | unsetenv(const char *name) |
109 | const char *name; | ||
110 | { | 95 | { |
111 | extern char **environ; | 96 | char **P; |
112 | register char **P; | ||
113 | int offset; | 97 | int offset; |
114 | char *__findenv(); | ||
115 | 98 | ||
116 | while (__findenv(name, &offset)) /* if set multiple times */ | 99 | while (__findenv(name, &offset)) /* if set multiple times */ |
117 | for (P = &environ[offset];; ++P) | 100 | for (P = &environ[offset];; ++P) |
118 | if (!(*P = *(P + 1))) | 101 | if (!(*P = *(P + 1))) |
119 | break; | 102 | break; |
diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c index daf733f93e..f76b6cca86 100644 --- a/src/lib/libc/stdlib/srand48.c +++ b/src/lib/libc/stdlib/srand48.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: srand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1993 Martin Birgmeier | 3 | * Copyright (c) 1993 Martin Birgmeier |
3 | * All rights reserved. | 4 | * All rights reserved. |
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3 index 0b7f973857..e25b0a5511 100644 --- a/src/lib/libc/stdlib/strtod.3 +++ b/src/lib/libc/stdlib/strtod.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)strtod.3 5.3 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: strtod.3,v 1.8 2003/06/02 20:18:38 millert Exp $ |
37 | .\" $Id: strtod.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt STRTOD 3 | 35 | .Dt STRTOD 3 |
@@ -51,20 +46,25 @@ string to double | |||
51 | .Sh DESCRIPTION | 46 | .Sh DESCRIPTION |
52 | The | 47 | The |
53 | .Fn strtod | 48 | .Fn strtod |
54 | function converts the initial portion of the string | 49 | function converts the initial portion of the string pointed to by |
55 | pointed to by | ||
56 | .Fa nptr | 50 | .Fa nptr |
57 | to | 51 | to |
58 | .Em double | 52 | .Li double |
59 | representation. | 53 | representation. |
60 | .Pp | 54 | .Pp |
61 | The expected form of the string is an optional plus (``+'') or minus | 55 | The expected form of the string is an optional plus |
62 | sign (``-'') followed by a sequence of digits optionally containing | 56 | .Pq Ql + |
57 | or minus sign | ||
58 | .Pq Ql - | ||
59 | followed by a sequence of digits optionally containing | ||
63 | a decimal-point character, optionally followed by an exponent. | 60 | a decimal-point character, optionally followed by an exponent. |
64 | An exponent consists of an ``E'' or ``e'', followed by an optional plus | 61 | An exponent consists of an |
65 | or minus sign, followed by a sequence of digits. | 62 | .Sq E |
63 | or | ||
64 | .Sq e , | ||
65 | followed by an optional plus or minus sign, followed by a sequence of digits. | ||
66 | .Pp | 66 | .Pp |
67 | Leading white-space characters in the string (as defined by the | 67 | Leading whitespace characters in the string (as defined by the |
68 | .Xr isspace 3 | 68 | .Xr isspace 3 |
69 | function) are skipped. | 69 | function) are skipped. |
70 | .Sh RETURN VALUES | 70 | .Sh RETURN VALUES |
@@ -88,18 +88,18 @@ is stored in the location referenced by | |||
88 | If the correct value would cause overflow, plus or minus | 88 | If the correct value would cause overflow, plus or minus |
89 | .Dv HUGE_VAL | 89 | .Dv HUGE_VAL |
90 | is returned (according to the sign of the value), and | 90 | is returned (according to the sign of the value), and |
91 | .Dv ERANGE | 91 | .Er ERANGE |
92 | is stored in | 92 | is stored in |
93 | .Va errno . | 93 | .Va errno . |
94 | If the correct value would cause underflow, zero is | 94 | If the correct value would cause underflow, zero is returned and |
95 | returned and | 95 | .Er ERANGE |
96 | .Dv ERANGE | 96 | is stored in |
97 | is stored in | ||
98 | .Va errno . | 97 | .Va errno . |
99 | .Sh ERRORS | 98 | .Sh ERRORS |
100 | .Bl -tag -width Er | 99 | .Bl -tag -width Er |
101 | .It Bq Er ERANGE | 100 | .It Bq Er ERANGE |
102 | Overflow or underflow occurred. | 101 | Overflow or underflow occurred. |
102 | .El | ||
103 | .Sh SEE ALSO | 103 | .Sh SEE ALSO |
104 | .Xr atof 3 , | 104 | .Xr atof 3 , |
105 | .Xr atoi 3 , | 105 | .Xr atoi 3 , |
@@ -109,6 +109,5 @@ Overflow or underflow occurred. | |||
109 | .Sh STANDARDS | 109 | .Sh STANDARDS |
110 | The | 110 | The |
111 | .Fn strtod | 111 | .Fn strtod |
112 | function | 112 | function conforms to |
113 | conforms to | ||
114 | .St -ansiC . | 113 | .St -ansiC . |
diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c index b13fa128f5..d01158e10c 100644 --- a/src/lib/libc/stdlib/strtod.c +++ b/src/lib/libc/stdlib/strtod.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: strtod.c,v 1.22 2006/05/19 14:15:27 thib Exp $ */ | ||
1 | /**************************************************************** | 2 | /**************************************************************** |
2 | * | 3 | * |
3 | * The author of this software is David M. Gay. | 4 | * The author of this software is David M. Gay. |
@@ -79,7 +80,6 @@ | |||
79 | * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision | 80 | * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision |
80 | * integer arithmetic. Whether this speeds things up or slows things | 81 | * integer arithmetic. Whether this speeds things up or slows things |
81 | * down depends on the machine and the number being converted. | 82 | * down depends on the machine and the number being converted. |
82 | * #define KR_headers for old-style C function headers. | ||
83 | * #define Bad_float_h if your system lacks a float.h or if it does not | 83 | * #define Bad_float_h if your system lacks a float.h or if it does not |
84 | * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, | 84 | * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, |
85 | * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. | 85 | * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. |
@@ -89,13 +89,11 @@ | |||
89 | * directly -- and assumed always to succeed. | 89 | * directly -- and assumed always to succeed. |
90 | */ | 90 | */ |
91 | 91 | ||
92 | #if defined(LIBC_SCCS) && !defined(lint) | ||
93 | static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
94 | #endif /* LIBC_SCCS and not lint */ | ||
95 | |||
96 | #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ | 92 | #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ |
97 | defined(__mips__) || defined(__ns32k__) || defined(__alpha__) | 93 | defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \ |
98 | #include <machine/endian.h> | 94 | defined(__powerpc__) || defined(__m88k__) || defined(__hppa__) || \ |
95 | defined(__x86_64__) || (defined(__arm__) && defined(__VFP_FP__)) | ||
96 | #include <sys/types.h> | ||
99 | #if BYTE_ORDER == BIG_ENDIAN | 97 | #if BYTE_ORDER == BIG_ENDIAN |
100 | #define IEEE_BIG_ENDIAN | 98 | #define IEEE_BIG_ENDIAN |
101 | #else | 99 | #else |
@@ -103,7 +101,16 @@ static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $" | |||
103 | #endif | 101 | #endif |
104 | #endif | 102 | #endif |
105 | 103 | ||
106 | #ifdef vax | 104 | #if defined(__arm__) && !defined(__VFP_FP__) |
105 | /* | ||
106 | * Although the CPU is little endian the FP has different | ||
107 | * byte and word endianness. The byte order is still little endian | ||
108 | * but the word order is big endian. | ||
109 | */ | ||
110 | #define IEEE_BIG_ENDIAN | ||
111 | #endif | ||
112 | |||
113 | #ifdef __vax__ | ||
107 | #define VAX | 114 | #define VAX |
108 | #endif | 115 | #endif |
109 | 116 | ||
@@ -119,22 +126,13 @@ static char *rcsid = "$Id: strtod.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $" | |||
119 | #include "malloc.h" | 126 | #include "malloc.h" |
120 | #include "memory.h" | 127 | #include "memory.h" |
121 | #else | 128 | #else |
122 | #ifndef KR_headers | ||
123 | #include "stdlib.h" | 129 | #include "stdlib.h" |
124 | #include "string.h" | 130 | #include "string.h" |
125 | #include "locale.h" | 131 | #include "locale.h" |
126 | #else | ||
127 | #include "malloc.h" | ||
128 | #include "memory.h" | ||
129 | #endif | ||
130 | #endif | 132 | #endif |
131 | 133 | ||
132 | #ifdef MALLOC | 134 | #ifdef MALLOC |
133 | #ifdef KR_headers | ||
134 | extern char *MALLOC(); | ||
135 | #else | ||
136 | extern void *MALLOC(size_t); | 135 | extern void *MALLOC(size_t); |
137 | #endif | ||
138 | #else | 136 | #else |
139 | #define MALLOC malloc | 137 | #define MALLOC malloc |
140 | #endif | 138 | #endif |
@@ -143,7 +141,6 @@ extern void *MALLOC(size_t); | |||
143 | #include "errno.h" | 141 | #include "errno.h" |
144 | 142 | ||
145 | #ifdef Bad_float_h | 143 | #ifdef Bad_float_h |
146 | #undef __STDC__ | ||
147 | #ifdef IEEE_BIG_ENDIAN | 144 | #ifdef IEEE_BIG_ENDIAN |
148 | #define IEEE_ARITHMETIC | 145 | #define IEEE_ARITHMETIC |
149 | #endif | 146 | #endif |
@@ -193,12 +190,8 @@ extern "C" { | |||
193 | #endif | 190 | #endif |
194 | 191 | ||
195 | #ifndef CONST | 192 | #ifndef CONST |
196 | #ifdef KR_headers | ||
197 | #define CONST /* blank */ | ||
198 | #else | ||
199 | #define CONST const | 193 | #define CONST const |
200 | #endif | 194 | #endif |
201 | #endif | ||
202 | 195 | ||
203 | #ifdef Unsigned_Shifts | 196 | #ifdef Unsigned_Shifts |
204 | #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; | 197 | #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; |
@@ -212,19 +205,24 @@ Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or | |||
212 | IBM should be defined. | 205 | IBM should be defined. |
213 | #endif | 206 | #endif |
214 | 207 | ||
208 | typedef union { | ||
209 | double d; | ||
210 | ULong ul[2]; | ||
211 | } _double; | ||
212 | #define value(x) ((x).d) | ||
215 | #ifdef IEEE_LITTLE_ENDIAN | 213 | #ifdef IEEE_LITTLE_ENDIAN |
216 | #define word0(x) ((ULong *)&x)[1] | 214 | #define word0(x) ((x).ul[1]) |
217 | #define word1(x) ((ULong *)&x)[0] | 215 | #define word1(x) ((x).ul[0]) |
218 | #else | 216 | #else |
219 | #define word0(x) ((ULong *)&x)[0] | 217 | #define word0(x) ((x).ul[0]) |
220 | #define word1(x) ((ULong *)&x)[1] | 218 | #define word1(x) ((x).ul[1]) |
221 | #endif | 219 | #endif |
222 | 220 | ||
223 | /* The following definition of Storeinc is appropriate for MIPS processors. | 221 | /* The following definition of Storeinc is appropriate for MIPS processors. |
224 | * An alternative that might be better on some machines is | 222 | * An alternative that might be better on some machines is |
225 | * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) | 223 | * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) |
226 | */ | 224 | */ |
227 | #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) | 225 | #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__) |
228 | #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ | 226 | #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ |
229 | ((unsigned short *)a)[0] = (unsigned short)c, a++) | 227 | ((unsigned short *)a)[0] = (unsigned short)c, a++) |
230 | #else | 228 | #else |
@@ -326,11 +324,7 @@ IBM should be defined. | |||
326 | #ifdef RND_PRODQUOT | 324 | #ifdef RND_PRODQUOT |
327 | #define rounded_product(a,b) a = rnd_prod(a, b) | 325 | #define rounded_product(a,b) a = rnd_prod(a, b) |
328 | #define rounded_quotient(a,b) a = rnd_quot(a, b) | 326 | #define rounded_quotient(a,b) a = rnd_quot(a, b) |
329 | #ifdef KR_headers | ||
330 | extern double rnd_prod(), rnd_quot(); | ||
331 | #else | ||
332 | extern double rnd_prod(double, double), rnd_quot(double, double); | 327 | extern double rnd_prod(double, double), rnd_quot(double, double); |
333 | #endif | ||
334 | #else | 328 | #else |
335 | #define rounded_product(a,b) a *= b | 329 | #define rounded_product(a,b) a *= b |
336 | #define rounded_quotient(a,b) a /= b | 330 | #define rounded_quotient(a,b) a /= b |
@@ -370,17 +364,12 @@ Bigint { | |||
370 | static Bigint *freelist[Kmax+1]; | 364 | static Bigint *freelist[Kmax+1]; |
371 | 365 | ||
372 | static Bigint * | 366 | static Bigint * |
373 | Balloc | 367 | Balloc(int k) |
374 | #ifdef KR_headers | ||
375 | (k) int k; | ||
376 | #else | ||
377 | (int k) | ||
378 | #endif | ||
379 | { | 368 | { |
380 | int x; | 369 | int x; |
381 | Bigint *rv; | 370 | Bigint *rv; |
382 | 371 | ||
383 | if (rv = freelist[k]) { | 372 | if ((rv = freelist[k])) { |
384 | freelist[k] = rv->next; | 373 | freelist[k] = rv->next; |
385 | } | 374 | } |
386 | else { | 375 | else { |
@@ -394,12 +383,7 @@ Balloc | |||
394 | } | 383 | } |
395 | 384 | ||
396 | static void | 385 | static void |
397 | Bfree | 386 | Bfree(Bigint *v) |
398 | #ifdef KR_headers | ||
399 | (v) Bigint *v; | ||
400 | #else | ||
401 | (Bigint *v) | ||
402 | #endif | ||
403 | { | 387 | { |
404 | if (v) { | 388 | if (v) { |
405 | v->next = freelist[v->k]; | 389 | v->next = freelist[v->k]; |
@@ -411,12 +395,7 @@ Bfree | |||
411 | y->wds*sizeof(Long) + 2*sizeof(int)) | 395 | y->wds*sizeof(Long) + 2*sizeof(int)) |
412 | 396 | ||
413 | static Bigint * | 397 | static Bigint * |
414 | multadd | 398 | multadd(Bigint *b, int m, int a) /* multiply by m and add a */ |
415 | #ifdef KR_headers | ||
416 | (b, m, a) Bigint *b; int m, a; | ||
417 | #else | ||
418 | (Bigint *b, int m, int a) /* multiply by m and add a */ | ||
419 | #endif | ||
420 | { | 399 | { |
421 | int i, wds; | 400 | int i, wds; |
422 | ULong *x, y; | 401 | ULong *x, y; |
@@ -456,12 +435,7 @@ multadd | |||
456 | } | 435 | } |
457 | 436 | ||
458 | static Bigint * | 437 | static Bigint * |
459 | s2b | 438 | s2b(CONST char *s, int nd0, int nd, ULong y9) |
460 | #ifdef KR_headers | ||
461 | (s, nd0, nd, y9) CONST char *s; int nd0, nd; ULong y9; | ||
462 | #else | ||
463 | (CONST char *s, int nd0, int nd, ULong y9) | ||
464 | #endif | ||
465 | { | 439 | { |
466 | Bigint *b; | 440 | Bigint *b; |
467 | int i, k; | 441 | int i, k; |
@@ -494,14 +468,9 @@ s2b | |||
494 | } | 468 | } |
495 | 469 | ||
496 | static int | 470 | static int |
497 | hi0bits | 471 | hi0bits(ULong x) |
498 | #ifdef KR_headers | ||
499 | (x) register ULong x; | ||
500 | #else | ||
501 | (register ULong x) | ||
502 | #endif | ||
503 | { | 472 | { |
504 | register int k = 0; | 473 | int k = 0; |
505 | 474 | ||
506 | if (!(x & 0xffff0000)) { | 475 | if (!(x & 0xffff0000)) { |
507 | k = 16; | 476 | k = 16; |
@@ -528,15 +497,10 @@ hi0bits | |||
528 | } | 497 | } |
529 | 498 | ||
530 | static int | 499 | static int |
531 | lo0bits | 500 | lo0bits(ULong *y) |
532 | #ifdef KR_headers | ||
533 | (y) ULong *y; | ||
534 | #else | ||
535 | (ULong *y) | ||
536 | #endif | ||
537 | { | 501 | { |
538 | register int k; | 502 | int k; |
539 | register ULong x = *y; | 503 | ULong x = *y; |
540 | 504 | ||
541 | if (x & 7) { | 505 | if (x & 7) { |
542 | if (x & 1) | 506 | if (x & 1) |
@@ -568,7 +532,7 @@ lo0bits | |||
568 | if (!(x & 1)) { | 532 | if (!(x & 1)) { |
569 | k++; | 533 | k++; |
570 | x >>= 1; | 534 | x >>= 1; |
571 | if (!x & 1) | 535 | if (!(x & 1)) |
572 | return 32; | 536 | return 32; |
573 | } | 537 | } |
574 | *y = x; | 538 | *y = x; |
@@ -576,12 +540,7 @@ lo0bits | |||
576 | } | 540 | } |
577 | 541 | ||
578 | static Bigint * | 542 | static Bigint * |
579 | i2b | 543 | i2b(int i) |
580 | #ifdef KR_headers | ||
581 | (i) int i; | ||
582 | #else | ||
583 | (int i) | ||
584 | #endif | ||
585 | { | 544 | { |
586 | Bigint *b; | 545 | Bigint *b; |
587 | 546 | ||
@@ -592,12 +551,7 @@ i2b | |||
592 | } | 551 | } |
593 | 552 | ||
594 | static Bigint * | 553 | static Bigint * |
595 | mult | 554 | mult(Bigint *a, Bigint *b) |
596 | #ifdef KR_headers | ||
597 | (a, b) Bigint *a, *b; | ||
598 | #else | ||
599 | (Bigint *a, Bigint *b) | ||
600 | #endif | ||
601 | { | 555 | { |
602 | Bigint *c; | 556 | Bigint *c; |
603 | int k, wa, wb, wc; | 557 | int k, wa, wb, wc; |
@@ -628,7 +582,7 @@ mult | |||
628 | xc0 = c->x; | 582 | xc0 = c->x; |
629 | #ifdef Pack_32 | 583 | #ifdef Pack_32 |
630 | for(; xb < xbe; xb++, xc0++) { | 584 | for(; xb < xbe; xb++, xc0++) { |
631 | if (y = *xb & 0xffff) { | 585 | if ((y = *xb & 0xffff)) { |
632 | x = xa; | 586 | x = xa; |
633 | xc = xc0; | 587 | xc = xc0; |
634 | carry = 0; | 588 | carry = 0; |
@@ -642,7 +596,7 @@ mult | |||
642 | while(x < xae); | 596 | while(x < xae); |
643 | *xc = carry; | 597 | *xc = carry; |
644 | } | 598 | } |
645 | if (y = *xb >> 16) { | 599 | if ((y = *xb >> 16)) { |
646 | x = xa; | 600 | x = xa; |
647 | xc = xc0; | 601 | xc = xc0; |
648 | carry = 0; | 602 | carry = 0; |
@@ -682,18 +636,13 @@ mult | |||
682 | static Bigint *p5s; | 636 | static Bigint *p5s; |
683 | 637 | ||
684 | static Bigint * | 638 | static Bigint * |
685 | pow5mult | 639 | pow5mult(Bigint *b, int k) |
686 | #ifdef KR_headers | ||
687 | (b, k) Bigint *b; int k; | ||
688 | #else | ||
689 | (Bigint *b, int k) | ||
690 | #endif | ||
691 | { | 640 | { |
692 | Bigint *b1, *p5, *p51; | 641 | Bigint *b1, *p5, *p51; |
693 | int i; | 642 | int i; |
694 | static int p05[3] = { 5, 25, 125 }; | 643 | static int p05[3] = { 5, 25, 125 }; |
695 | 644 | ||
696 | if (i = k & 3) | 645 | if ((i = k & 3)) |
697 | b = multadd(b, p05[i-1], 0); | 646 | b = multadd(b, p05[i-1], 0); |
698 | 647 | ||
699 | if (!(k >>= 2)) | 648 | if (!(k >>= 2)) |
@@ -721,12 +670,7 @@ pow5mult | |||
721 | } | 670 | } |
722 | 671 | ||
723 | static Bigint * | 672 | static Bigint * |
724 | lshift | 673 | lshift(Bigint *b, int k) |
725 | #ifdef KR_headers | ||
726 | (b, k) Bigint *b; int k; | ||
727 | #else | ||
728 | (Bigint *b, int k) | ||
729 | #endif | ||
730 | { | 674 | { |
731 | int i, k1, n, n1; | 675 | int i, k1, n, n1; |
732 | Bigint *b1; | 676 | Bigint *b1; |
@@ -756,7 +700,7 @@ lshift | |||
756 | z = *x++ >> k1; | 700 | z = *x++ >> k1; |
757 | } | 701 | } |
758 | while(x < xe); | 702 | while(x < xe); |
759 | if (*x1 = z) | 703 | if ((*x1 = z)) |
760 | ++n1; | 704 | ++n1; |
761 | } | 705 | } |
762 | #else | 706 | #else |
@@ -781,12 +725,7 @@ lshift | |||
781 | } | 725 | } |
782 | 726 | ||
783 | static int | 727 | static int |
784 | cmp | 728 | cmp(Bigint *a, Bigint *b) |
785 | #ifdef KR_headers | ||
786 | (a, b) Bigint *a, *b; | ||
787 | #else | ||
788 | (Bigint *a, Bigint *b) | ||
789 | #endif | ||
790 | { | 729 | { |
791 | ULong *xa, *xa0, *xb, *xb0; | 730 | ULong *xa, *xa0, *xb, *xb0; |
792 | int i, j; | 731 | int i, j; |
@@ -815,12 +754,7 @@ cmp | |||
815 | } | 754 | } |
816 | 755 | ||
817 | static Bigint * | 756 | static Bigint * |
818 | diff | 757 | diff(Bigint *a, Bigint *b) |
819 | #ifdef KR_headers | ||
820 | (a, b) Bigint *a, *b; | ||
821 | #else | ||
822 | (Bigint *a, Bigint *b) | ||
823 | #endif | ||
824 | { | 758 | { |
825 | Bigint *c; | 759 | Bigint *c; |
826 | int i, wa, wb; | 760 | int i, wa, wb; |
@@ -897,16 +831,13 @@ diff | |||
897 | } | 831 | } |
898 | 832 | ||
899 | static double | 833 | static double |
900 | ulp | 834 | ulp(double _x) |
901 | #ifdef KR_headers | ||
902 | (x) double x; | ||
903 | #else | ||
904 | (double x) | ||
905 | #endif | ||
906 | { | 835 | { |
907 | register Long L; | 836 | _double x; |
908 | double a; | 837 | Long L; |
838 | _double a; | ||
909 | 839 | ||
840 | value(x) = _x; | ||
910 | L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; | 841 | L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; |
911 | #ifndef Sudden_Underflow | 842 | #ifndef Sudden_Underflow |
912 | if (L > 0) { | 843 | if (L > 0) { |
@@ -931,20 +862,15 @@ ulp | |||
931 | } | 862 | } |
932 | } | 863 | } |
933 | #endif | 864 | #endif |
934 | return a; | 865 | return value(a); |
935 | } | 866 | } |
936 | 867 | ||
937 | static double | 868 | static double |
938 | b2d | 869 | b2d(Bigint *a, int *e) |
939 | #ifdef KR_headers | ||
940 | (a, e) Bigint *a; int *e; | ||
941 | #else | ||
942 | (Bigint *a, int *e) | ||
943 | #endif | ||
944 | { | 870 | { |
945 | ULong *xa, *xa0, w, y, z; | 871 | ULong *xa, *xa0, w, y, z; |
946 | int k; | 872 | int k; |
947 | double d; | 873 | _double d; |
948 | #ifdef VAX | 874 | #ifdef VAX |
949 | ULong d0, d1; | 875 | ULong d0, d1; |
950 | #else | 876 | #else |
@@ -1001,22 +927,22 @@ b2d | |||
1001 | #undef d0 | 927 | #undef d0 |
1002 | #undef d1 | 928 | #undef d1 |
1003 | #endif | 929 | #endif |
1004 | return d; | 930 | return value(d); |
1005 | } | 931 | } |
1006 | 932 | ||
1007 | static Bigint * | 933 | static Bigint * |
1008 | d2b | 934 | d2b(double _d, int *e, int *bits) |
1009 | #ifdef KR_headers | ||
1010 | (d, e, bits) double d; int *e, *bits; | ||
1011 | #else | ||
1012 | (double d, int *e, int *bits) | ||
1013 | #endif | ||
1014 | { | 935 | { |
1015 | Bigint *b; | 936 | Bigint *b; |
1016 | int de, i, k; | 937 | int de, i, k; |
1017 | ULong *x, y, z; | 938 | ULong *x, y, z; |
939 | _double d; | ||
1018 | #ifdef VAX | 940 | #ifdef VAX |
1019 | ULong d0, d1; | 941 | ULong d0, d1; |
942 | #endif | ||
943 | |||
944 | value(d) = _d; | ||
945 | #ifdef VAX | ||
1020 | d0 = word0(d) >> 16 | word0(d) << 16; | 946 | d0 = word0(d) >> 16 | word0(d) << 16; |
1021 | d1 = word1(d) >> 16 | word1(d) << 16; | 947 | d1 = word1(d) >> 16 | word1(d) << 16; |
1022 | #else | 948 | #else |
@@ -1134,18 +1060,13 @@ d2b | |||
1134 | #undef d1 | 1060 | #undef d1 |
1135 | 1061 | ||
1136 | static double | 1062 | static double |
1137 | ratio | 1063 | ratio(Bigint *a, Bigint *b) |
1138 | #ifdef KR_headers | ||
1139 | (a, b) Bigint *a, *b; | ||
1140 | #else | ||
1141 | (Bigint *a, Bigint *b) | ||
1142 | #endif | ||
1143 | { | 1064 | { |
1144 | double da, db; | 1065 | _double da, db; |
1145 | int k, ka, kb; | 1066 | int k, ka, kb; |
1146 | 1067 | ||
1147 | da = b2d(a, &ka); | 1068 | value(da) = b2d(a, &ka); |
1148 | db = b2d(b, &kb); | 1069 | value(db) = b2d(b, &kb); |
1149 | #ifdef Pack_32 | 1070 | #ifdef Pack_32 |
1150 | k = ka - kb + 32*(a->wds - b->wds); | 1071 | k = ka - kb + 32*(a->wds - b->wds); |
1151 | #else | 1072 | #else |
@@ -1171,7 +1092,7 @@ ratio | |||
1171 | word0(db) += k*Exp_msk1; | 1092 | word0(db) += k*Exp_msk1; |
1172 | } | 1093 | } |
1173 | #endif | 1094 | #endif |
1174 | return da / db; | 1095 | return value(da) / value(db); |
1175 | } | 1096 | } |
1176 | 1097 | ||
1177 | static CONST double | 1098 | static CONST double |
@@ -1201,32 +1122,24 @@ static CONST double tinytens[] = { 1e-16, 1e-32 }; | |||
1201 | #endif | 1122 | #endif |
1202 | 1123 | ||
1203 | double | 1124 | double |
1204 | strtod | 1125 | strtod(CONST char *s00, char **se) |
1205 | #ifdef KR_headers | ||
1206 | (s00, se) CONST char *s00; char **se; | ||
1207 | #else | ||
1208 | (CONST char *s00, char **se) | ||
1209 | #endif | ||
1210 | { | 1126 | { |
1211 | int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, | 1127 | int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, |
1212 | e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; | 1128 | e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; |
1213 | CONST char *s, *s0, *s1; | 1129 | CONST char *s, *s0, *s1; |
1214 | double aadj, aadj1, adj, rv, rv0; | 1130 | double aadj, aadj1, adj; |
1131 | _double rv, rv0; | ||
1215 | Long L; | 1132 | Long L; |
1216 | ULong y, z; | 1133 | ULong y, z; |
1217 | Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; | 1134 | Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; |
1218 | 1135 | ||
1219 | #ifndef KR_headers | ||
1220 | CONST char decimal_point = localeconv()->decimal_point[0]; | 1136 | CONST char decimal_point = localeconv()->decimal_point[0]; |
1221 | #else | ||
1222 | CONST char decimal_point = '.'; | ||
1223 | #endif | ||
1224 | 1137 | ||
1225 | sign = nz0 = nz = 0; | 1138 | sign = nz0 = nz = 0; |
1226 | rv = 0.; | 1139 | value(rv) = 0.; |
1227 | 1140 | ||
1228 | 1141 | ||
1229 | for(s = s00; isspace(*s); s++) | 1142 | for(s = s00; isspace((unsigned char) *s); s++) |
1230 | ; | 1143 | ; |
1231 | 1144 | ||
1232 | if (*s == '-') { | 1145 | if (*s == '-') { |
@@ -1340,9 +1253,9 @@ strtod | |||
1340 | if (!nd0) | 1253 | if (!nd0) |
1341 | nd0 = nd; | 1254 | nd0 = nd; |
1342 | k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; | 1255 | k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; |
1343 | rv = y; | 1256 | value(rv) = y; |
1344 | if (k > 9) | 1257 | if (k > 9) |
1345 | rv = tens[k - 9] * rv + z; | 1258 | value(rv) = tens[k - 9] * value(rv) + z; |
1346 | bd0 = 0; | 1259 | bd0 = 0; |
1347 | if (nd <= DBL_DIG | 1260 | if (nd <= DBL_DIG |
1348 | #ifndef RND_PRODQUOT | 1261 | #ifndef RND_PRODQUOT |
@@ -1356,7 +1269,8 @@ strtod | |||
1356 | #ifdef VAX | 1269 | #ifdef VAX |
1357 | goto vax_ovfl_check; | 1270 | goto vax_ovfl_check; |
1358 | #else | 1271 | #else |
1359 | /* rv = */ rounded_product(rv, tens[e]); | 1272 | /* value(rv) = */ rounded_product(value(rv), |
1273 | tens[e]); | ||
1360 | goto ret; | 1274 | goto ret; |
1361 | #endif | 1275 | #endif |
1362 | } | 1276 | } |
@@ -1366,27 +1280,30 @@ strtod | |||
1366 | * this for larger i values. | 1280 | * this for larger i values. |
1367 | */ | 1281 | */ |
1368 | e -= i; | 1282 | e -= i; |
1369 | rv *= tens[i]; | 1283 | value(rv) *= tens[i]; |
1370 | #ifdef VAX | 1284 | #ifdef VAX |
1371 | /* VAX exponent range is so narrow we must | 1285 | /* VAX exponent range is so narrow we must |
1372 | * worry about overflow here... | 1286 | * worry about overflow here... |
1373 | */ | 1287 | */ |
1374 | vax_ovfl_check: | 1288 | vax_ovfl_check: |
1375 | word0(rv) -= P*Exp_msk1; | 1289 | word0(rv) -= P*Exp_msk1; |
1376 | /* rv = */ rounded_product(rv, tens[e]); | 1290 | /* value(rv) = */ rounded_product(value(rv), |
1291 | tens[e]); | ||
1377 | if ((word0(rv) & Exp_mask) | 1292 | if ((word0(rv) & Exp_mask) |
1378 | > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) | 1293 | > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) |
1379 | goto ovfl; | 1294 | goto ovfl; |
1380 | word0(rv) += P*Exp_msk1; | 1295 | word0(rv) += P*Exp_msk1; |
1381 | #else | 1296 | #else |
1382 | /* rv = */ rounded_product(rv, tens[e]); | 1297 | /* value(rv) = */ rounded_product(value(rv), |
1298 | tens[e]); | ||
1383 | #endif | 1299 | #endif |
1384 | goto ret; | 1300 | goto ret; |
1385 | } | 1301 | } |
1386 | } | 1302 | } |
1387 | #ifndef Inaccurate_Divide | 1303 | #ifndef Inaccurate_Divide |
1388 | else if (e >= -Ten_pmax) { | 1304 | else if (e >= -Ten_pmax) { |
1389 | /* rv = */ rounded_quotient(rv, tens[-e]); | 1305 | /* value(rv) = */ rounded_quotient(value(rv), |
1306 | tens[-e]); | ||
1390 | goto ret; | 1307 | goto ret; |
1391 | } | 1308 | } |
1392 | #endif | 1309 | #endif |
@@ -1397,13 +1314,13 @@ strtod | |||
1397 | 1314 | ||
1398 | if (e1 > 0) { | 1315 | if (e1 > 0) { |
1399 | if (i = e1 & 15) | 1316 | if (i = e1 & 15) |
1400 | rv *= tens[i]; | 1317 | value(rv) *= tens[i]; |
1401 | if (e1 &= ~15) { | 1318 | if (e1 &= ~15) { |
1402 | if (e1 > DBL_MAX_10_EXP) { | 1319 | if (e1 > DBL_MAX_10_EXP) { |
1403 | ovfl: | 1320 | ovfl: |
1404 | errno = ERANGE; | 1321 | errno = ERANGE; |
1405 | #ifdef __STDC__ | 1322 | #ifndef Bad_float_h |
1406 | rv = HUGE_VAL; | 1323 | value(rv) = HUGE_VAL; |
1407 | #else | 1324 | #else |
1408 | /* Can't trust HUGE_VAL */ | 1325 | /* Can't trust HUGE_VAL */ |
1409 | #ifdef IEEE_Arith | 1326 | #ifdef IEEE_Arith |
@@ -1421,10 +1338,10 @@ strtod | |||
1421 | if (e1 >>= 4) { | 1338 | if (e1 >>= 4) { |
1422 | for(j = 0; e1 > 1; j++, e1 >>= 1) | 1339 | for(j = 0; e1 > 1; j++, e1 >>= 1) |
1423 | if (e1 & 1) | 1340 | if (e1 & 1) |
1424 | rv *= bigtens[j]; | 1341 | value(rv) *= bigtens[j]; |
1425 | /* The last multiplication could overflow. */ | 1342 | /* The last multiplication could overflow. */ |
1426 | word0(rv) -= P*Exp_msk1; | 1343 | word0(rv) -= P*Exp_msk1; |
1427 | rv *= bigtens[j]; | 1344 | value(rv) *= bigtens[j]; |
1428 | if ((z = word0(rv) & Exp_mask) | 1345 | if ((z = word0(rv) & Exp_mask) |
1429 | > Exp_msk1*(DBL_MAX_EXP+Bias-P)) | 1346 | > Exp_msk1*(DBL_MAX_EXP+Bias-P)) |
1430 | goto ovfl; | 1347 | goto ovfl; |
@@ -1443,23 +1360,23 @@ strtod | |||
1443 | else if (e1 < 0) { | 1360 | else if (e1 < 0) { |
1444 | e1 = -e1; | 1361 | e1 = -e1; |
1445 | if (i = e1 & 15) | 1362 | if (i = e1 & 15) |
1446 | rv /= tens[i]; | 1363 | value(rv) /= tens[i]; |
1447 | if (e1 &= ~15) { | 1364 | if (e1 &= ~15) { |
1448 | e1 >>= 4; | 1365 | e1 >>= 4; |
1449 | if (e1 >= 1 << n_bigtens) | 1366 | if (e1 >= 1 << n_bigtens) |
1450 | goto undfl; | 1367 | goto undfl; |
1451 | for(j = 0; e1 > 1; j++, e1 >>= 1) | 1368 | for(j = 0; e1 > 1; j++, e1 >>= 1) |
1452 | if (e1 & 1) | 1369 | if (e1 & 1) |
1453 | rv *= tinytens[j]; | 1370 | value(rv) *= tinytens[j]; |
1454 | /* The last multiplication could underflow. */ | 1371 | /* The last multiplication could underflow. */ |
1455 | rv0 = rv; | 1372 | value(rv0) = value(rv); |
1456 | rv *= tinytens[j]; | 1373 | value(rv) *= tinytens[j]; |
1457 | if (!rv) { | 1374 | if (!value(rv)) { |
1458 | rv = 2.*rv0; | 1375 | value(rv) = 2.*value(rv0); |
1459 | rv *= tinytens[j]; | 1376 | value(rv) *= tinytens[j]; |
1460 | if (!rv) { | 1377 | if (!value(rv)) { |
1461 | undfl: | 1378 | undfl: |
1462 | rv = 0.; | 1379 | value(rv) = 0.; |
1463 | errno = ERANGE; | 1380 | errno = ERANGE; |
1464 | if (bd0) | 1381 | if (bd0) |
1465 | goto retfree; | 1382 | goto retfree; |
@@ -1483,7 +1400,7 @@ strtod | |||
1483 | for(;;) { | 1400 | for(;;) { |
1484 | bd = Balloc(bd0->k); | 1401 | bd = Balloc(bd0->k); |
1485 | Bcopy(bd, bd0); | 1402 | Bcopy(bd, bd0); |
1486 | bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ | 1403 | bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */ |
1487 | bs = i2b(1); | 1404 | bs = i2b(1); |
1488 | 1405 | ||
1489 | if (e >= 0) { | 1406 | if (e >= 0) { |
@@ -1595,12 +1512,12 @@ strtod | |||
1595 | break; | 1512 | break; |
1596 | #endif | 1513 | #endif |
1597 | if (dsign) | 1514 | if (dsign) |
1598 | rv += ulp(rv); | 1515 | value(rv) += ulp(value(rv)); |
1599 | #ifndef ROUND_BIASED | 1516 | #ifndef ROUND_BIASED |
1600 | else { | 1517 | else { |
1601 | rv -= ulp(rv); | 1518 | value(rv) -= ulp(value(rv)); |
1602 | #ifndef Sudden_Underflow | 1519 | #ifndef Sudden_Underflow |
1603 | if (!rv) | 1520 | if (!value(rv)) |
1604 | goto undfl; | 1521 | goto undfl; |
1605 | #endif | 1522 | #endif |
1606 | } | 1523 | } |
@@ -1651,10 +1568,10 @@ strtod | |||
1651 | /* Check for overflow */ | 1568 | /* Check for overflow */ |
1652 | 1569 | ||
1653 | if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { | 1570 | if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { |
1654 | rv0 = rv; | 1571 | value(rv0) = value(rv); |
1655 | word0(rv) -= P*Exp_msk1; | 1572 | word0(rv) -= P*Exp_msk1; |
1656 | adj = aadj1 * ulp(rv); | 1573 | adj = aadj1 * ulp(value(rv)); |
1657 | rv += adj; | 1574 | value(rv) += adj; |
1658 | if ((word0(rv) & Exp_mask) >= | 1575 | if ((word0(rv) & Exp_mask) >= |
1659 | Exp_msk1*(DBL_MAX_EXP+Bias-P)) { | 1576 | Exp_msk1*(DBL_MAX_EXP+Bias-P)) { |
1660 | if (word0(rv0) == Big0 && word1(rv0) == Big1) | 1577 | if (word0(rv0) == Big0 && word1(rv0) == Big1) |
@@ -1669,10 +1586,10 @@ strtod | |||
1669 | else { | 1586 | else { |
1670 | #ifdef Sudden_Underflow | 1587 | #ifdef Sudden_Underflow |
1671 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { | 1588 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { |
1672 | rv0 = rv; | 1589 | value(rv0) = value(rv); |
1673 | word0(rv) += P*Exp_msk1; | 1590 | word0(rv) += P*Exp_msk1; |
1674 | adj = aadj1 * ulp(rv); | 1591 | adj = aadj1 * ulp(value(rv)); |
1675 | rv += adj; | 1592 | value(rv) += adj; |
1676 | #ifdef IBM | 1593 | #ifdef IBM |
1677 | if ((word0(rv) & Exp_mask) < P*Exp_msk1) | 1594 | if ((word0(rv) & Exp_mask) < P*Exp_msk1) |
1678 | #else | 1595 | #else |
@@ -1690,8 +1607,8 @@ strtod | |||
1690 | word0(rv) -= P*Exp_msk1; | 1607 | word0(rv) -= P*Exp_msk1; |
1691 | } | 1608 | } |
1692 | else { | 1609 | else { |
1693 | adj = aadj1 * ulp(rv); | 1610 | adj = aadj1 * ulp(value(rv)); |
1694 | rv += adj; | 1611 | value(rv) += adj; |
1695 | } | 1612 | } |
1696 | #else | 1613 | #else |
1697 | /* Compute adj so that the IEEE rounding rules will | 1614 | /* Compute adj so that the IEEE rounding rules will |
@@ -1706,8 +1623,8 @@ strtod | |||
1706 | if (!dsign) | 1623 | if (!dsign) |
1707 | aadj1 = -aadj1; | 1624 | aadj1 = -aadj1; |
1708 | } | 1625 | } |
1709 | adj = aadj1 * ulp(rv); | 1626 | adj = aadj1 * ulp(value(rv)); |
1710 | rv += adj; | 1627 | value(rv) += adj; |
1711 | #endif | 1628 | #endif |
1712 | } | 1629 | } |
1713 | z = word0(rv) & Exp_mask; | 1630 | z = word0(rv) & Exp_mask; |
@@ -1738,16 +1655,11 @@ strtod | |||
1738 | ret: | 1655 | ret: |
1739 | if (se) | 1656 | if (se) |
1740 | *se = (char *)s; | 1657 | *se = (char *)s; |
1741 | return sign ? -rv : rv; | 1658 | return sign ? -value(rv) : value(rv); |
1742 | } | 1659 | } |
1743 | 1660 | ||
1744 | static int | 1661 | static int |
1745 | quorem | 1662 | quorem(Bigint *b, Bigint *S) |
1746 | #ifdef KR_headers | ||
1747 | (b, S) Bigint *b, *S; | ||
1748 | #else | ||
1749 | (Bigint *b, Bigint *S) | ||
1750 | #endif | ||
1751 | { | 1663 | { |
1752 | int n; | 1664 | int n; |
1753 | Long borrow, y; | 1665 | Long borrow, y; |
@@ -1882,13 +1794,7 @@ quorem | |||
1882 | */ | 1794 | */ |
1883 | 1795 | ||
1884 | char * | 1796 | char * |
1885 | __dtoa | 1797 | __dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve) |
1886 | #ifdef KR_headers | ||
1887 | (d, mode, ndigits, decpt, sign, rve) | ||
1888 | double d; int mode, ndigits, *decpt, *sign; char **rve; | ||
1889 | #else | ||
1890 | (double d, int mode, int ndigits, int *decpt, int *sign, char **rve) | ||
1891 | #endif | ||
1892 | { | 1798 | { |
1893 | /* Arguments ndigits, decpt, sign are similar to those | 1799 | /* Arguments ndigits, decpt, sign are similar to those |
1894 | of ecvt and fcvt; trailing zeros are suppressed from | 1800 | of ecvt and fcvt; trailing zeros are suppressed from |
@@ -1933,11 +1839,13 @@ __dtoa | |||
1933 | ULong x; | 1839 | ULong x; |
1934 | #endif | 1840 | #endif |
1935 | Bigint *b, *b1, *delta, *mlo, *mhi, *S; | 1841 | Bigint *b, *b1, *delta, *mlo, *mhi, *S; |
1936 | double d2, ds, eps; | 1842 | double ds; |
1937 | char *s, *s0; | 1843 | char *s, *s0; |
1938 | static Bigint *result; | 1844 | static Bigint *result; |
1939 | static int result_k; | 1845 | static int result_k; |
1846 | _double d, d2, eps; | ||
1940 | 1847 | ||
1848 | value(d) = _d; | ||
1941 | if (result) { | 1849 | if (result) { |
1942 | result->k = result_k; | 1850 | result->k = result_k; |
1943 | result->maxwds = 1 << result_k; | 1851 | result->maxwds = 1 << result_k; |
@@ -1964,7 +1872,7 @@ __dtoa | |||
1964 | *decpt = 9999; | 1872 | *decpt = 9999; |
1965 | s = | 1873 | s = |
1966 | #ifdef IEEE_Arith | 1874 | #ifdef IEEE_Arith |
1967 | !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" : | 1875 | !word1(d) && !(word0(d) & 0xfffff) ? ndigits < 8 ? "Inf" : "Infinity" : |
1968 | #endif | 1876 | #endif |
1969 | "NaN"; | 1877 | "NaN"; |
1970 | if (rve) | 1878 | if (rve) |
@@ -1977,9 +1885,9 @@ __dtoa | |||
1977 | } | 1885 | } |
1978 | #endif | 1886 | #endif |
1979 | #ifdef IBM | 1887 | #ifdef IBM |
1980 | d += 0; /* normalize */ | 1888 | value(d) += 0; /* normalize */ |
1981 | #endif | 1889 | #endif |
1982 | if (!d) { | 1890 | if (!value(d)) { |
1983 | *decpt = 1; | 1891 | *decpt = 1; |
1984 | s = "0"; | 1892 | s = "0"; |
1985 | if (rve) | 1893 | if (rve) |
@@ -1987,18 +1895,18 @@ __dtoa | |||
1987 | return s; | 1895 | return s; |
1988 | } | 1896 | } |
1989 | 1897 | ||
1990 | b = d2b(d, &be, &bbits); | 1898 | b = d2b(value(d), &be, &bbits); |
1991 | #ifdef Sudden_Underflow | 1899 | #ifdef Sudden_Underflow |
1992 | i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); | 1900 | i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); |
1993 | #else | 1901 | #else |
1994 | if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { | 1902 | if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { |
1995 | #endif | 1903 | #endif |
1996 | d2 = d; | 1904 | value(d2) = value(d); |
1997 | word0(d2) &= Frac_mask1; | 1905 | word0(d2) &= Frac_mask1; |
1998 | word0(d2) |= Exp_11; | 1906 | word0(d2) |= Exp_11; |
1999 | #ifdef IBM | 1907 | #ifdef IBM |
2000 | if (j = 11 - hi0bits(word0(d2) & Frac_mask)) | 1908 | if (j = 11 - hi0bits(word0(d2) & Frac_mask)) |
2001 | d2 /= 1 << j; | 1909 | value(d2) /= 1 << j; |
2002 | #endif | 1910 | #endif |
2003 | 1911 | ||
2004 | /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 | 1912 | /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 |
@@ -2037,19 +1945,20 @@ __dtoa | |||
2037 | i = bbits + be + (Bias + (P-1) - 1); | 1945 | i = bbits + be + (Bias + (P-1) - 1); |
2038 | x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 | 1946 | x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 |
2039 | : word1(d) << 32 - i; | 1947 | : word1(d) << 32 - i; |
2040 | d2 = x; | 1948 | value(d2) = x; |
2041 | word0(d2) -= 31*Exp_msk1; /* adjust exponent */ | 1949 | word0(d2) -= 31*Exp_msk1; /* adjust exponent */ |
2042 | i -= (Bias + (P-1) - 1) + 1; | 1950 | i -= (Bias + (P-1) - 1) + 1; |
2043 | denorm = 1; | 1951 | denorm = 1; |
2044 | } | 1952 | } |
2045 | #endif | 1953 | #endif |
2046 | ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; | 1954 | ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + |
1955 | i*0.301029995663981; | ||
2047 | k = (int)ds; | 1956 | k = (int)ds; |
2048 | if (ds < 0. && ds != k) | 1957 | if (ds < 0. && ds != k) |
2049 | k--; /* want k = floor(ds) */ | 1958 | k--; /* want k = floor(ds) */ |
2050 | k_check = 1; | 1959 | k_check = 1; |
2051 | if (k >= 0 && k <= Ten_pmax) { | 1960 | if (k >= 0 && k <= Ten_pmax) { |
2052 | if (d < tens[k]) | 1961 | if (value(d) < tens[k]) |
2053 | k--; | 1962 | k--; |
2054 | k_check = 0; | 1963 | k_check = 0; |
2055 | } | 1964 | } |
@@ -2116,7 +2025,7 @@ __dtoa | |||
2116 | /* Try to get by with floating-point arithmetic. */ | 2025 | /* Try to get by with floating-point arithmetic. */ |
2117 | 2026 | ||
2118 | i = 0; | 2027 | i = 0; |
2119 | d2 = d; | 2028 | value(d2) = value(d); |
2120 | k0 = k; | 2029 | k0 = k; |
2121 | ilim0 = ilim; | 2030 | ilim0 = ilim; |
2122 | ieps = 2; /* conservative */ | 2031 | ieps = 2; /* conservative */ |
@@ -2126,7 +2035,7 @@ __dtoa | |||
2126 | if (j & Bletch) { | 2035 | if (j & Bletch) { |
2127 | /* prevent overflows */ | 2036 | /* prevent overflows */ |
2128 | j &= Bletch - 1; | 2037 | j &= Bletch - 1; |
2129 | d /= bigtens[n_bigtens-1]; | 2038 | value(d) /= bigtens[n_bigtens-1]; |
2130 | ieps++; | 2039 | ieps++; |
2131 | } | 2040 | } |
2132 | for(; j; j >>= 1, i++) | 2041 | for(; j; j >>= 1, i++) |
@@ -2134,32 +2043,32 @@ __dtoa | |||
2134 | ieps++; | 2043 | ieps++; |
2135 | ds *= bigtens[i]; | 2044 | ds *= bigtens[i]; |
2136 | } | 2045 | } |
2137 | d /= ds; | 2046 | value(d) /= ds; |
2138 | } | 2047 | } |
2139 | else if (j1 = -k) { | 2048 | else if (j1 = -k) { |
2140 | d *= tens[j1 & 0xf]; | 2049 | value(d) *= tens[j1 & 0xf]; |
2141 | for(j = j1 >> 4; j; j >>= 1, i++) | 2050 | for(j = j1 >> 4; j; j >>= 1, i++) |
2142 | if (j & 1) { | 2051 | if (j & 1) { |
2143 | ieps++; | 2052 | ieps++; |
2144 | d *= bigtens[i]; | 2053 | value(d) *= bigtens[i]; |
2145 | } | 2054 | } |
2146 | } | 2055 | } |
2147 | if (k_check && d < 1. && ilim > 0) { | 2056 | if (k_check && value(d) < 1. && ilim > 0) { |
2148 | if (ilim1 <= 0) | 2057 | if (ilim1 <= 0) |
2149 | goto fast_failed; | 2058 | goto fast_failed; |
2150 | ilim = ilim1; | 2059 | ilim = ilim1; |
2151 | k--; | 2060 | k--; |
2152 | d *= 10.; | 2061 | value(d) *= 10.; |
2153 | ieps++; | 2062 | ieps++; |
2154 | } | 2063 | } |
2155 | eps = ieps*d + 7.; | 2064 | value(eps) = ieps*value(d) + 7.; |
2156 | word0(eps) -= (P-1)*Exp_msk1; | 2065 | word0(eps) -= (P-1)*Exp_msk1; |
2157 | if (ilim == 0) { | 2066 | if (ilim == 0) { |
2158 | S = mhi = 0; | 2067 | S = mhi = 0; |
2159 | d -= 5.; | 2068 | value(d) -= 5.; |
2160 | if (d > eps) | 2069 | if (value(d) > value(eps)) |
2161 | goto one_digit; | 2070 | goto one_digit; |
2162 | if (d < -eps) | 2071 | if (value(d) < -value(eps)) |
2163 | goto no_digits; | 2072 | goto no_digits; |
2164 | goto fast_failed; | 2073 | goto fast_failed; |
2165 | } | 2074 | } |
@@ -2168,33 +2077,33 @@ __dtoa | |||
2168 | /* Use Steele & White method of only | 2077 | /* Use Steele & White method of only |
2169 | * generating digits needed. | 2078 | * generating digits needed. |
2170 | */ | 2079 | */ |
2171 | eps = 0.5/tens[ilim-1] - eps; | 2080 | value(eps) = 0.5/tens[ilim-1] - value(eps); |
2172 | for(i = 0;;) { | 2081 | for(i = 0;;) { |
2173 | L = d; | 2082 | L = value(d); |
2174 | d -= L; | 2083 | value(d) -= L; |
2175 | *s++ = '0' + (int)L; | 2084 | *s++ = '0' + (int)L; |
2176 | if (d < eps) | 2085 | if (value(d) < value(eps)) |
2177 | goto ret1; | 2086 | goto ret1; |
2178 | if (1. - d < eps) | 2087 | if (1. - value(d) < value(eps)) |
2179 | goto bump_up; | 2088 | goto bump_up; |
2180 | if (++i >= ilim) | 2089 | if (++i >= ilim) |
2181 | break; | 2090 | break; |
2182 | eps *= 10.; | 2091 | value(eps) *= 10.; |
2183 | d *= 10.; | 2092 | value(d) *= 10.; |
2184 | } | 2093 | } |
2185 | } | 2094 | } |
2186 | else { | 2095 | else { |
2187 | #endif | 2096 | #endif |
2188 | /* Generate ilim digits, then fix them up. */ | 2097 | /* Generate ilim digits, then fix them up. */ |
2189 | eps *= tens[ilim-1]; | 2098 | value(eps) *= tens[ilim-1]; |
2190 | for(i = 1;; i++, d *= 10.) { | 2099 | for(i = 1;; i++, value(d) *= 10.) { |
2191 | L = d; | 2100 | L = value(d); |
2192 | d -= L; | 2101 | value(d) -= L; |
2193 | *s++ = '0' + (int)L; | 2102 | *s++ = '0' + (int)L; |
2194 | if (i == ilim) { | 2103 | if (i == ilim) { |
2195 | if (d > 0.5 + eps) | 2104 | if (value(d) > 0.5 + value(eps)) |
2196 | goto bump_up; | 2105 | goto bump_up; |
2197 | else if (d < 0.5 - eps) { | 2106 | else if (value(d) < 0.5 - value(eps)) { |
2198 | while(*--s == '0'); | 2107 | while(*--s == '0'); |
2199 | s++; | 2108 | s++; |
2200 | goto ret1; | 2109 | goto ret1; |
@@ -2207,7 +2116,7 @@ __dtoa | |||
2207 | #endif | 2116 | #endif |
2208 | fast_failed: | 2117 | fast_failed: |
2209 | s = s0; | 2118 | s = s0; |
2210 | d = d2; | 2119 | value(d) = value(d2); |
2211 | k = k0; | 2120 | k = k0; |
2212 | ilim = ilim0; | 2121 | ilim = ilim0; |
2213 | } | 2122 | } |
@@ -2219,24 +2128,24 @@ __dtoa | |||
2219 | ds = tens[k]; | 2128 | ds = tens[k]; |
2220 | if (ndigits < 0 && ilim <= 0) { | 2129 | if (ndigits < 0 && ilim <= 0) { |
2221 | S = mhi = 0; | 2130 | S = mhi = 0; |
2222 | if (ilim < 0 || d <= 5*ds) | 2131 | if (ilim < 0 || value(d) <= 5*ds) |
2223 | goto no_digits; | 2132 | goto no_digits; |
2224 | goto one_digit; | 2133 | goto one_digit; |
2225 | } | 2134 | } |
2226 | for(i = 1;; i++) { | 2135 | for(i = 1;; i++) { |
2227 | L = d / ds; | 2136 | L = value(d) / ds; |
2228 | d -= L*ds; | 2137 | value(d) -= L*ds; |
2229 | #ifdef Check_FLT_ROUNDS | 2138 | #ifdef Check_FLT_ROUNDS |
2230 | /* If FLT_ROUNDS == 2, L will usually be high by 1 */ | 2139 | /* If FLT_ROUNDS == 2, L will usually be high by 1 */ |
2231 | if (d < 0) { | 2140 | if (value(d) < 0) { |
2232 | L--; | 2141 | L--; |
2233 | d += ds; | 2142 | value(d) += ds; |
2234 | } | 2143 | } |
2235 | #endif | 2144 | #endif |
2236 | *s++ = '0' + (int)L; | 2145 | *s++ = '0' + (int)L; |
2237 | if (i == ilim) { | 2146 | if (i == ilim) { |
2238 | d += d; | 2147 | value(d) += value(d); |
2239 | if (d > ds || d == ds && L & 1) { | 2148 | if (value(d) > ds || value(d) == ds && L & 1) { |
2240 | bump_up: | 2149 | bump_up: |
2241 | while(*--s == '9') | 2150 | while(*--s == '9') |
2242 | if (s == s0) { | 2151 | if (s == s0) { |
@@ -2248,7 +2157,7 @@ __dtoa | |||
2248 | } | 2157 | } |
2249 | break; | 2158 | break; |
2250 | } | 2159 | } |
2251 | if (!(d *= 10.)) | 2160 | if (!(value(d) *= 10.)) |
2252 | break; | 2161 | break; |
2253 | } | 2162 | } |
2254 | goto ret1; | 2163 | goto ret1; |
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 index 808ba90165..e45941e07b 100644 --- a/src/lib/libc/stdlib/strtol.3 +++ b/src/lib/libc/stdlib/strtol.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,49 +29,67 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)strtol.3 5.4 (Berkeley) 6/25/92 | 32 | .\" $OpenBSD: strtol.3,v 1.15 2006/03/29 20:10:52 grunk Exp $ |
37 | .\" $Id: strtol.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 25, 1992 | 34 | .Dd January 3, 2006 |
40 | .Dt STRTOL 3 | 35 | .Dt STRTOL 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
43 | .Nm strtol, strtoq | 38 | .Nm strtol , |
44 | .Nd convert string value to a long or quad_t integer | 39 | .Nm strtoll , |
40 | .Nm strtoimax , | ||
41 | .Nm strtoq , | ||
42 | .Nd "convert string value to a long, long long or intmax_t integer" | ||
45 | .Sh SYNOPSIS | 43 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 44 | .Fd #include <stdlib.h> |
47 | .Fd #include <limits.h> | 45 | .Fd #include <limits.h> |
48 | .Ft long | 46 | .Ft long |
49 | .Fn strtol "char *nptr" "char **endptr" "int base" | 47 | .Fn strtol "const char *nptr" "char **endptr" "int base" |
50 | 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 | ||
51 | .Fd #include <sys/types.h> | 56 | .Fd #include <sys/types.h> |
52 | .Fd #include <stdlib.h> | 57 | .Fd #include <stdlib.h> |
53 | .Fd #include <limits.h> | 58 | .Fd #include <limits.h> |
54 | .Ft quad_t | 59 | .Ft quad_t |
55 | .Fn strtoq "char *nptr" "char **endptr" "int base" | 60 | .Fn strtoq "const char *nptr" "char **endptr" "int base" |
56 | .Sh DESCRIPTION | 61 | .Sh DESCRIPTION |
57 | The | 62 | The |
58 | .Fn strtol | 63 | .Fn strtol |
59 | function | 64 | function converts the string in |
60 | converts the string in | ||
61 | .Fa nptr | 65 | .Fa nptr |
62 | to a | 66 | to a |
63 | .Em long | 67 | .Li long |
64 | value. | 68 | value. |
65 | The | 69 | The |
66 | .Fn strtoq | 70 | .Fn strtoll |
67 | function | 71 | function converts the string in |
68 | converts the string in | ||
69 | .Fa nptr | 72 | .Fa nptr |
70 | to a | 73 | to a |
71 | .Em quad_t | 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 | ||
72 | value. | 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. | ||
73 | The conversion is done according to the given | 88 | The conversion is done according to the given |
74 | .Fa base , | 89 | .Fa base , |
75 | which must be between 2 and 36 inclusive, | 90 | which must be a number between 2 and 36 inclusive or the special value 0. |
76 | or be the special value 0. | ||
77 | .Pp | 91 | .Pp |
78 | The string may begin with an arbitrary amount of white space | 92 | The string may begin with an arbitrary amount of whitespace |
79 | (as determined by | 93 | (as determined by |
80 | .Xr isspace 3 ) | 94 | .Xr isspace 3 ) |
81 | followed by a single optional | 95 | followed by a single optional |
@@ -85,25 +99,25 @@ or | |||
85 | sign. | 99 | sign. |
86 | If | 100 | If |
87 | .Fa base | 101 | .Fa base |
88 | is zero or 16, | 102 | is zero or 16, the string may then include a |
89 | the string may then include a | ||
90 | .Ql 0x | 103 | .Ql 0x |
91 | prefix, | 104 | prefix, and the number will be read in base 16; otherwise, a zero |
92 | and the number will be read in base 16; otherwise, a zero | ||
93 | .Fa base | 105 | .Fa base |
94 | is taken as 10 (decimal) unless the next character is | 106 | is taken as 10 (decimal) unless the next character is |
95 | .Ql 0 , | 107 | .Ql 0 , |
96 | in which case it is taken as 8 (octal). | 108 | in which case it is taken as 8 (octal). |
97 | .Pp | 109 | .Pp |
98 | The remainder of the string is converted to a | 110 | The remainder of the string is converted to a |
99 | .Em long | 111 | .Li long , |
112 | .Li long long , | ||
113 | or | ||
114 | .Li intmax_t , | ||
100 | value in the obvious manner, | 115 | value in the obvious manner, |
101 | stopping at the first character which is not a valid digit | 116 | stopping at the first character which is not a valid digit |
102 | in the given base. | 117 | in the given base. |
103 | (In bases above 10, the letter | 118 | (In bases above 10, the letter |
104 | .Ql A | 119 | .Ql A |
105 | in either upper or lower case | 120 | in either upper or lower case represents 10, |
106 | represents 10, | ||
107 | .Ql B | 121 | .Ql B |
108 | represents 11, and so forth, with | 122 | represents 11, and so forth, with |
109 | .Ql Z | 123 | .Ql Z |
@@ -111,7 +125,7 @@ representing 35.) | |||
111 | .Pp | 125 | .Pp |
112 | If | 126 | If |
113 | .Fa endptr | 127 | .Fa endptr |
114 | is non nil, | 128 | is non-null, |
115 | .Fn strtol | 129 | .Fn strtol |
116 | stores the address of the first invalid character in | 130 | stores the address of the first invalid character in |
117 | .Fa *endptr . | 131 | .Fa *endptr . |
@@ -132,22 +146,87 @@ is | |||
132 | on return, the entire string was valid.) | 146 | on return, the entire string was valid.) |
133 | .Sh RETURN VALUES | 147 | .Sh RETURN VALUES |
134 | The | 148 | The |
135 | .Fn strtol | 149 | .Fn strtol , |
136 | function | 150 | .Fn strtoll , |
137 | returns the result of the conversion, | 151 | .Fn strtoimax , |
152 | and | ||
153 | .Fn strtoq | ||
154 | functions returns the result of the conversion, | ||
138 | unless the value would underflow or overflow. | 155 | unless the value would underflow or overflow. |
139 | If an underflow occurs, | 156 | If overflow or underflow occurs, |
140 | .Fn strtol | ||
141 | returns | ||
142 | .Dv LONG_MIN . | ||
143 | If an overflow occurs, | ||
144 | .Fn strtol | ||
145 | returns | ||
146 | .Dv LONG_MAX . | ||
147 | In both cases, | ||
148 | .Va errno | 157 | .Va errno |
149 | is set to | 158 | is set to |
150 | .Er ERANGE . | 159 | .Er ERANGE |
160 | and the function return value is as follows: | ||
161 | .Bl -column -offset indent "strtoimax" "overflow" "underflow" | ||
162 | .It Sy Function Ta Sy overflow Ta Sy underflow | ||
163 | .It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX | ||
164 | .It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX | ||
165 | .It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX | ||
166 | .It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX | ||
167 | .El | ||
168 | .Sh EXAMPLES | ||
169 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
170 | trailing characters) requires clearing | ||
171 | .Va errno | ||
172 | beforehand explicitly since | ||
173 | .Va errno | ||
174 | is not changed on a successful call to | ||
175 | .Fn strtol , | ||
176 | and the return value of | ||
177 | .Fn strtol | ||
178 | cannot be used unambiguously to signal an error: | ||
179 | .Bd -literal -offset indent | ||
180 | char *ep; | ||
181 | long lval; | ||
182 | |||
183 | \&... | ||
184 | |||
185 | errno = 0; | ||
186 | lval = strtol(buf, &ep, 10); | ||
187 | if (buf[0] == '\e0' || *ep != '\e0') | ||
188 | goto not_a_number; | ||
189 | if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) | ||
190 | goto out_of_range; | ||
191 | .Ed | ||
192 | .Pp | ||
193 | This example will accept | ||
194 | .Dq 12 | ||
195 | but not | ||
196 | .Dq 12foo | ||
197 | or | ||
198 | .Dq 12\en . | ||
199 | If trailing whitespace is acceptable, further checks must be done on | ||
200 | .Va *ep ; | ||
201 | alternately, use | ||
202 | .Xr sscanf 3 . | ||
203 | .Pp | ||
204 | If | ||
205 | .Fn strtol | ||
206 | is being used instead of | ||
207 | .Xr atoi 3 , | ||
208 | error checking is further complicated because the desired return value is an | ||
209 | .Li int | ||
210 | rather than a | ||
211 | .Li long ; | ||
212 | however, on some architectures integers and long integers are the same size. | ||
213 | Thus the following is necessary: | ||
214 | .Bd -literal -offset indent | ||
215 | char *ep; | ||
216 | int ival; | ||
217 | long lval; | ||
218 | |||
219 | \&... | ||
220 | |||
221 | errno = 0; | ||
222 | lval = strtol(buf, &ep, 10); | ||
223 | if (buf[0] == '\e0' || *ep != '\e0') | ||
224 | goto not_a_number; | ||
225 | if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || | ||
226 | (lval > INT_MAX || lval < INT_MIN)) | ||
227 | goto out_of_range; | ||
228 | ival = lval; | ||
229 | .Ed | ||
151 | .Sh ERRORS | 230 | .Sh ERRORS |
152 | .Bl -tag -width Er | 231 | .Bl -tag -width Er |
153 | .It Bq Er ERANGE | 232 | .It Bq Er ERANGE |
@@ -157,13 +236,23 @@ The given string was out of range; the value converted has been clamped. | |||
157 | .Xr atof 3 , | 236 | .Xr atof 3 , |
158 | .Xr atoi 3 , | 237 | .Xr atoi 3 , |
159 | .Xr atol 3 , | 238 | .Xr atol 3 , |
239 | .Xr atoll 3 , | ||
240 | .Xr sscanf 3 , | ||
160 | .Xr strtod 3 , | 241 | .Xr strtod 3 , |
242 | .Xr strtonum 3 , | ||
161 | .Xr strtoul 3 | 243 | .Xr strtoul 3 |
162 | .Sh STANDARDS | 244 | .Sh STANDARDS |
163 | The | 245 | The |
164 | .Fn strtol | 246 | .Fn strtol , |
165 | function | 247 | .Fn strtoll , |
166 | conforms to | 248 | and |
167 | .St -ansiC . | 249 | .Fn strtoimax |
250 | functions conform to | ||
251 | .St -ansiC-99 . | ||
252 | The | ||
253 | .Fn strtoq | ||
254 | function is a | ||
255 | .Bx | ||
256 | extension and is provided for backwards compatibility with legacy programs. | ||
168 | .Sh BUGS | 257 | .Sh BUGS |
169 | Ignores the current locale. | 258 | Ignores the current locale. |
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c index 6f374abd5f..5a244766db 100644 --- a/src/lib/libc/stdlib/strtol.c +++ b/src/lib/libc/stdlib/strtol.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: strtol.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,14 +28,9 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)strtol.c 5.4 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <limits.h> | ||
40 | #include <ctype.h> | 31 | #include <ctype.h> |
41 | #include <errno.h> | 32 | #include <errno.h> |
33 | #include <limits.h> | ||
42 | #include <stdlib.h> | 34 | #include <stdlib.h> |
43 | 35 | ||
44 | 36 | ||
@@ -49,30 +41,30 @@ static char *rcsid = "$Id: strtol.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $" | |||
49 | * alphabets and digits are each contiguous. | 41 | * alphabets and digits are each contiguous. |
50 | */ | 42 | */ |
51 | long | 43 | long |
52 | strtol(nptr, endptr, base) | 44 | strtol(const char *nptr, char **endptr, int base) |
53 | const char *nptr; | ||
54 | char **endptr; | ||
55 | register int base; | ||
56 | { | 45 | { |
57 | register const char *s = nptr; | 46 | const char *s; |
58 | register unsigned long acc; | 47 | long acc, cutoff; |
59 | register int c; | 48 | int c; |
60 | register unsigned long cutoff; | 49 | int neg, any, cutlim; |
61 | register int neg = 0, any, cutlim; | ||
62 | 50 | ||
63 | /* | 51 | /* |
64 | * Skip white space and pick up leading +/- sign if any. | 52 | * Skip white space and pick up leading +/- sign if any. |
65 | * If base is 0, allow 0x for hex and 0 for octal, else | 53 | * If base is 0, allow 0x for hex and 0 for octal, else |
66 | * assume decimal; if base is already 16, allow 0x. | 54 | * assume decimal; if base is already 16, allow 0x. |
67 | */ | 55 | */ |
56 | s = nptr; | ||
68 | do { | 57 | do { |
69 | c = *s++; | 58 | c = (unsigned char) *s++; |
70 | } while (isspace(c)); | 59 | } while (isspace(c)); |
71 | if (c == '-') { | 60 | if (c == '-') { |
72 | neg = 1; | 61 | neg = 1; |
73 | c = *s++; | 62 | c = *s++; |
74 | } else if (c == '+') | 63 | } else { |
75 | c = *s++; | 64 | neg = 0; |
65 | if (c == '+') | ||
66 | c = *s++; | ||
67 | } | ||
76 | if ((base == 0 || base == 16) && | 68 | if ((base == 0 || base == 16) && |
77 | c == '0' && (*s == 'x' || *s == 'X')) { | 69 | c == '0' && (*s == 'x' || *s == 'X')) { |
78 | c = s[1]; | 70 | c = s[1]; |
@@ -99,10 +91,17 @@ strtol(nptr, endptr, base) | |||
99 | * Set any if any `digits' consumed; make it negative to indicate | 91 | * Set any if any `digits' consumed; make it negative to indicate |
100 | * overflow. | 92 | * overflow. |
101 | */ | 93 | */ |
102 | cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; | 94 | cutoff = neg ? LONG_MIN : LONG_MAX; |
103 | cutlim = cutoff % (unsigned long)base; | 95 | cutlim = cutoff % base; |
104 | cutoff /= (unsigned long)base; | 96 | cutoff /= base; |
105 | for (acc = 0, any = 0;; c = *s++) { | 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++) { | ||
106 | if (isdigit(c)) | 105 | if (isdigit(c)) |
107 | c -= '0'; | 106 | c -= '0'; |
108 | else if (isalpha(c)) | 107 | else if (isalpha(c)) |
@@ -111,19 +110,30 @@ strtol(nptr, endptr, base) | |||
111 | break; | 110 | break; |
112 | if (c >= base) | 111 | if (c >= base) |
113 | break; | 112 | break; |
114 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | 113 | if (any < 0) |
115 | any = -1; | 114 | continue; |
116 | else { | 115 | if (neg) { |
117 | any = 1; | 116 | if (acc < cutoff || acc == cutoff && c > cutlim) { |
118 | acc *= base; | 117 | any = -1; |
119 | acc += c; | 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 | } | ||
120 | } | 135 | } |
121 | } | 136 | } |
122 | if (any < 0) { | ||
123 | acc = neg ? LONG_MIN : LONG_MAX; | ||
124 | errno = ERANGE; | ||
125 | } else if (neg) | ||
126 | acc = -acc; | ||
127 | if (endptr != 0) | 137 | if (endptr != 0) |
128 | *endptr = (char *) (any ? s - 1 : nptr); | 138 | *endptr = (char *) (any ? s - 1 : nptr); |
129 | return (acc); | 139 | return (acc); |
diff --git a/src/lib/libc/stdlib/strtoq.c b/src/lib/libc/stdlib/strtoll.c index fc559e9d7f..37379252e3 100644 --- a/src/lib/libc/stdlib/strtoq.c +++ b/src/lib/libc/stdlib/strtoll.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: strtoll.c,v 1.6 2005/11/10 10:00:17 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1992 The Regents of the University of California. | 3 | * Copyright (c) 1992 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,34 +28,26 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | static char sccsid[] = "@(#)strtoq.c 5.1 (Berkeley) 6/26/92"; | ||
36 | #endif /* LIBC_SCCS and not lint */ | ||
37 | |||
38 | #include <sys/types.h> | 31 | #include <sys/types.h> |
39 | 32 | ||
40 | #include <limits.h> | ||
41 | #include <errno.h> | ||
42 | #include <ctype.h> | 33 | #include <ctype.h> |
34 | #include <errno.h> | ||
35 | #include <limits.h> | ||
43 | #include <stdlib.h> | 36 | #include <stdlib.h> |
44 | 37 | ||
45 | /* | 38 | /* |
46 | * Convert a string to a quad integer. | 39 | * Convert a string to a long long. |
47 | * | 40 | * |
48 | * Ignores `locale' stuff. Assumes that the upper and lower case | 41 | * Ignores `locale' stuff. Assumes that the upper and lower case |
49 | * alphabets and digits are each contiguous. | 42 | * alphabets and digits are each contiguous. |
50 | */ | 43 | */ |
51 | quad_t | 44 | long long |
52 | strtoq(nptr, endptr, base) | 45 | strtoll(const char *nptr, char **endptr, int base) |
53 | const char *nptr; | ||
54 | char **endptr; | ||
55 | register int base; | ||
56 | { | 46 | { |
57 | register const char *s; | 47 | const char *s; |
58 | register u_quad_t acc; | 48 | long long acc, cutoff; |
59 | register int c; | 49 | int c; |
60 | register u_quad_t qbase, cutoff; | 50 | int neg, any, cutlim; |
61 | register int neg, any, cutlim; | ||
62 | 51 | ||
63 | /* | 52 | /* |
64 | * Skip white space and pick up leading +/- sign if any. | 53 | * Skip white space and pick up leading +/- sign if any. |
@@ -67,7 +56,7 @@ strtoq(nptr, endptr, base) | |||
67 | */ | 56 | */ |
68 | s = nptr; | 57 | s = nptr; |
69 | do { | 58 | do { |
70 | c = *s++; | 59 | c = (unsigned char) *s++; |
71 | } while (isspace(c)); | 60 | } while (isspace(c)); |
72 | if (c == '-') { | 61 | if (c == '-') { |
73 | neg = 1; | 62 | neg = 1; |
@@ -93,7 +82,7 @@ strtoq(nptr, endptr, base) | |||
93 | * followed by a legal input character, is too big. One that | 82 | * followed by a legal input character, is too big. One that |
94 | * is equal to this value may be valid or not; the limit | 83 | * is equal to this value may be valid or not; the limit |
95 | * between valid and invalid numbers is then based on the last | 84 | * between valid and invalid numbers is then based on the last |
96 | * digit. For instance, if the range for quads is | 85 | * digit. For instance, if the range for long longs is |
97 | * [-9223372036854775808..9223372036854775807] and the input base | 86 | * [-9223372036854775808..9223372036854775807] and the input base |
98 | * is 10, cutoff will be set to 922337203685477580 and cutlim to | 87 | * is 10, cutoff will be set to 922337203685477580 and cutlim to |
99 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have | 88 | * either 7 (neg==0) or 8 (neg==1), meaning that if we have |
@@ -104,11 +93,17 @@ strtoq(nptr, endptr, base) | |||
104 | * Set any if any `digits' consumed; make it negative to indicate | 93 | * Set any if any `digits' consumed; make it negative to indicate |
105 | * overflow. | 94 | * overflow. |
106 | */ | 95 | */ |
107 | qbase = (unsigned)base; | 96 | cutoff = neg ? LLONG_MIN : LLONG_MAX; |
108 | cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX; | 97 | cutlim = cutoff % base; |
109 | cutlim = cutoff % qbase; | 98 | cutoff /= base; |
110 | cutoff /= qbase; | 99 | if (neg) { |
111 | for (acc = 0, any = 0;; c = *s++) { | 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++) { | ||
112 | if (isdigit(c)) | 107 | if (isdigit(c)) |
113 | c -= '0'; | 108 | c -= '0'; |
114 | else if (isalpha(c)) | 109 | else if (isalpha(c)) |
@@ -117,20 +112,42 @@ strtoq(nptr, endptr, base) | |||
117 | break; | 112 | break; |
118 | if (c >= base) | 113 | if (c >= base) |
119 | break; | 114 | break; |
120 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | 115 | if (any < 0) |
121 | any = -1; | 116 | continue; |
122 | else { | 117 | if (neg) { |
123 | any = 1; | 118 | if (acc < cutoff || (acc == cutoff && c > cutlim)) { |
124 | acc *= qbase; | 119 | any = -1; |
125 | acc += c; | 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 | } | ||
126 | } | 137 | } |
127 | } | 138 | } |
128 | if (any < 0) { | ||
129 | acc = neg ? QUAD_MIN : QUAD_MAX; | ||
130 | errno = ERANGE; | ||
131 | } else if (neg) | ||
132 | acc = -acc; | ||
133 | if (endptr != 0) | 139 | if (endptr != 0) |
134 | *endptr = (char *) (any ? s - 1 : nptr); | 140 | *endptr = (char *) (any ? s - 1 : nptr); |
135 | return (acc); | 141 | return (acc); |
136 | } | 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..6b3c9031d6 --- /dev/null +++ b/src/lib/libc/stdlib/strtonum.3 | |||
@@ -0,0 +1,152 @@ | |||
1 | .\" $OpenBSD: strtonum.3,v 1.13 2006/04/25 05:15:42 tedu 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 April 29, 2004 | ||
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 index db551b0141..2ea5e83297 100644 --- a/src/lib/libc/stdlib/strtoul.3 +++ b/src/lib/libc/stdlib/strtoul.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,21 +29,30 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 | 32 | .\" $OpenBSD: strtoul.3,v 1.16 2006/01/13 17:58:09 millert Exp $ |
37 | .\" $Id: strtoul.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 25, 1992 | 34 | .Dd January 3, 2006 |
40 | .Dt STRTOUL 3 | 35 | .Dt STRTOUL 3 |
41 | .Os | 36 | .Os |
42 | .Sh NAME | 37 | .Sh NAME |
43 | .Nm strtoul, strtouq | 38 | .Nm strtoul , |
44 | .Nd convert a string to an unsigned long or uquad_t integer | 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" | ||
45 | .Sh SYNOPSIS | 43 | .Sh SYNOPSIS |
46 | .Fd #include <stdlib.h> | 44 | .Fd #include <stdlib.h> |
47 | .Fd #include <limits.h> | 45 | .Fd #include <limits.h> |
48 | .Ft unsigned long | 46 | .Ft unsigned long |
49 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | 47 | .Fn strtoul "const char *nptr" "char **endptr" "int base" |
50 | 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 | ||
51 | .Fd #include <sys/types.h> | 56 | .Fd #include <sys/types.h> |
52 | .Fd #include <stdlib.h> | 57 | .Fd #include <stdlib.h> |
53 | .Fd #include <limits.h> | 58 | .Fd #include <limits.h> |
@@ -56,26 +61,41 @@ | |||
56 | .Sh DESCRIPTION | 61 | .Sh DESCRIPTION |
57 | The | 62 | The |
58 | .Fn strtoul | 63 | .Fn strtoul |
59 | function | 64 | function converts the string in |
60 | converts the string in | ||
61 | .Fa nptr | 65 | .Fa nptr |
62 | to an | 66 | to an |
63 | .Em unsigned long | 67 | .Li unsigned long |
64 | value. | 68 | value. |
65 | The | 69 | The |
66 | .Fn strtouq | 70 | .Fn strtoull |
67 | function | 71 | function converts the string in |
68 | 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 | ||
69 | .Fa nptr | 79 | .Fa nptr |
70 | to a | 80 | to a |
71 | .Em u_quad_t | 81 | .Li umaxint_t |
72 | value. | 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. | ||
73 | The conversion is done according to the given | 88 | The conversion is done according to the given |
74 | .Fa base , | 89 | .Fa base , |
75 | which must be between 2 and 36 inclusive, | 90 | which must be a number between 2 and 36 inclusive |
76 | or be the special value 0. | 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. | ||
77 | .Pp | 97 | .Pp |
78 | The string may begin with an arbitrary amount of white space | 98 | The string may begin with an arbitrary amount of whitespace |
79 | (as determined by | 99 | (as determined by |
80 | .Xr isspace 3 ) | 100 | .Xr isspace 3 ) |
81 | followed by a single optional | 101 | followed by a single optional |
@@ -85,26 +105,22 @@ or | |||
85 | sign. | 105 | sign. |
86 | If | 106 | If |
87 | .Fa base | 107 | .Fa base |
88 | is zero or 16, | 108 | is zero or 16, the string may then include a |
89 | the string may then include a | ||
90 | .Ql 0x | 109 | .Ql 0x |
91 | prefix, | 110 | prefix, and the number will be read in base 16; otherwise, a zero |
92 | and the number will be read in base 16; otherwise, a zero | ||
93 | .Fa base | 111 | .Fa base |
94 | is taken as 10 (decimal) unless the next character is | 112 | is taken as 10 (decimal) unless the next character is |
95 | .Ql 0 , | 113 | .Ql 0 , |
96 | in which case it is taken as 8 (octal). | 114 | in which case it is taken as 8 (octal). |
97 | .Pp | 115 | .Pp |
98 | The remainder of the string is converted to an | 116 | The remainder of the string is converted to an |
99 | .Em unsigned long | 117 | .Li unsigned long |
100 | value in the obvious manner, | 118 | value in the obvious manner, stopping at the end of the string |
101 | stopping at the end of the string | ||
102 | or at the first character that does not produce a valid digit | 119 | or at the first character that does not produce a valid digit |
103 | in the given base. | 120 | in the given base. |
104 | (In bases above 10, the letter | 121 | (In bases above 10, the letter |
105 | .Ql A | 122 | .Ql A |
106 | in either upper or lower case | 123 | in either upper or lower case represents 10, |
107 | represents 10, | ||
108 | .Ql B | 124 | .Ql B |
109 | represents 11, and so forth, with | 125 | represents 11, and so forth, with |
110 | .Ql Z | 126 | .Ql Z |
@@ -112,7 +128,7 @@ representing 35.) | |||
112 | .Pp | 128 | .Pp |
113 | If | 129 | If |
114 | .Fa endptr | 130 | .Fa endptr |
115 | is non nil, | 131 | is non-null, |
116 | .Fn strtoul | 132 | .Fn strtoul |
117 | stores the address of the first invalid character in | 133 | stores the address of the first invalid character in |
118 | .Fa *endptr . | 134 | .Fa *endptr . |
@@ -133,32 +149,94 @@ is | |||
133 | on return, the entire string was valid.) | 149 | on return, the entire string was valid.) |
134 | .Sh RETURN VALUES | 150 | .Sh RETURN VALUES |
135 | The | 151 | The |
136 | .Fn strtoul | 152 | .Fn strtoul , |
137 | function | 153 | .Fn strtoull , |
138 | returns either the result of the conversion | 154 | .Fn strtoumax |
139 | or, if there was a leading minus sign, | 155 | and |
156 | .Fn strtouq | ||
157 | functions return either the result of the conversion or, | ||
158 | if there was a leading minus sign, | ||
140 | the negation of the result of the conversion, | 159 | the negation of the result of the conversion, |
141 | unless the original (non-negated) value would overflow; | 160 | unless the original (non-negated) value would overflow. |
142 | in the latter case, | 161 | If overflow occurs, |
143 | .Fn strtoul | 162 | .Fn strtoul |
144 | returns | 163 | returns |
145 | .Dv ULONG_MAX | 164 | .Dv ULONG_MAX , |
146 | and sets the global variable | 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 | ||
147 | .Va errno | 175 | .Va errno |
148 | to | 176 | is set to |
149 | .Er ERANGE . | 177 | .Er ERANGE . |
178 | .Pp | ||
179 | There is no way to determine if | ||
180 | .Fn strtoul | ||
181 | has processed a negative number (and returned an unsigned value) short of | ||
182 | examining the string in | ||
183 | .Fa nptr | ||
184 | directly. | ||
185 | .Sh EXAMPLES | ||
186 | Ensuring that a string is a valid number (i.e., in range and containing no | ||
187 | trailing characters) requires clearing | ||
188 | .Va errno | ||
189 | beforehand explicitly since | ||
190 | .Va errno | ||
191 | is not changed on a successful call to | ||
192 | .Fn strtoul , | ||
193 | and the return value of | ||
194 | .Fn strtoul | ||
195 | cannot be used unambiguously to signal an error: | ||
196 | .Bd -literal -offset indent | ||
197 | char *ep; | ||
198 | unsigned long ulval; | ||
199 | |||
200 | \&... | ||
201 | |||
202 | errno = 0; | ||
203 | ulval = strtoul(buf, &ep, 10); | ||
204 | if (buf[0] == '\e0' || *ep != '\e0') | ||
205 | goto not_a_number; | ||
206 | if (errno == ERANGE && ulval == ULONG_MAX) | ||
207 | goto out_of_range; | ||
208 | .Ed | ||
209 | .Pp | ||
210 | This example will accept | ||
211 | .Dq 12 | ||
212 | but not | ||
213 | .Dq 12foo | ||
214 | or | ||
215 | .Dq 12\en . | ||
216 | If trailing whitespace is acceptable, further checks must be done on | ||
217 | .Va *ep ; | ||
218 | alternately, use | ||
219 | .Xr sscanf 3 . | ||
150 | .Sh ERRORS | 220 | .Sh ERRORS |
151 | .Bl -tag -width Er | 221 | .Bl -tag -width Er |
152 | .It Bq Er ERANGE | 222 | .It Bq Er ERANGE |
153 | The given string was out of range; the value converted has been clamped. | 223 | The given string was out of range; the value converted has been clamped. |
154 | .El | 224 | .El |
155 | .Sh SEE ALSO | 225 | .Sh SEE ALSO |
226 | .Xr sscanf 3 , | ||
156 | .Xr strtol 3 | 227 | .Xr strtol 3 |
157 | .Sh STANDARDS | 228 | .Sh STANDARDS |
158 | The | 229 | The |
159 | .Fn strtoul | 230 | .Fn strtoul , |
160 | function | 231 | .Fn strtoull , |
161 | conforms to | 232 | and |
162 | .St -ansiC . | 233 | .Fn strtoumax |
234 | functions conform to | ||
235 | .St -ansiC-99 . | ||
236 | The | ||
237 | .Fn strtouq | ||
238 | function is a | ||
239 | .Bx | ||
240 | extension and is provided for backwards compatibility with legacy programs. | ||
163 | .Sh BUGS | 241 | .Sh BUGS |
164 | Ignores the current locale. | 242 | Ignores the current locale. |
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c index 00f7210fa1..d7dddab778 100644 --- a/src/lib/libc/stdlib/strtoul.c +++ b/src/lib/libc/stdlib/strtoul.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: strtoul.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1990 Regents of the University of California. | 3 | * Copyright (c) 1990 Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,14 +28,9 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)strtoul.c 5.3 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: strtoul.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <limits.h> | ||
40 | #include <ctype.h> | 31 | #include <ctype.h> |
41 | #include <errno.h> | 32 | #include <errno.h> |
33 | #include <limits.h> | ||
42 | #include <stdlib.h> | 34 | #include <stdlib.h> |
43 | 35 | ||
44 | /* | 36 | /* |
@@ -48,28 +40,28 @@ static char *rcsid = "$Id: strtoul.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | |||
48 | * alphabets and digits are each contiguous. | 40 | * alphabets and digits are each contiguous. |
49 | */ | 41 | */ |
50 | unsigned long | 42 | unsigned long |
51 | strtoul(nptr, endptr, base) | 43 | strtoul(const char *nptr, char **endptr, int base) |
52 | const char *nptr; | ||
53 | char **endptr; | ||
54 | register int base; | ||
55 | { | 44 | { |
56 | register const char *s = nptr; | 45 | const char *s; |
57 | register unsigned long acc; | 46 | unsigned long acc, cutoff; |
58 | register int c; | 47 | int c; |
59 | register unsigned long cutoff; | 48 | int neg, any, cutlim; |
60 | register int neg = 0, any, cutlim; | ||
61 | 49 | ||
62 | /* | 50 | /* |
63 | * See strtol for comments as to the logic used. | 51 | * See strtol for comments as to the logic used. |
64 | */ | 52 | */ |
53 | s = nptr; | ||
65 | do { | 54 | do { |
66 | c = *s++; | 55 | c = (unsigned char) *s++; |
67 | } while (isspace(c)); | 56 | } while (isspace(c)); |
68 | if (c == '-') { | 57 | if (c == '-') { |
69 | neg = 1; | 58 | neg = 1; |
70 | c = *s++; | 59 | c = *s++; |
71 | } else if (c == '+') | 60 | } else { |
72 | c = *s++; | 61 | neg = 0; |
62 | if (c == '+') | ||
63 | c = *s++; | ||
64 | } | ||
73 | if ((base == 0 || base == 16) && | 65 | if ((base == 0 || base == 16) && |
74 | c == '0' && (*s == 'x' || *s == 'X')) { | 66 | c == '0' && (*s == 'x' || *s == 'X')) { |
75 | c = s[1]; | 67 | c = s[1]; |
@@ -78,9 +70,10 @@ strtoul(nptr, endptr, base) | |||
78 | } | 70 | } |
79 | if (base == 0) | 71 | if (base == 0) |
80 | base = c == '0' ? 8 : 10; | 72 | base = c == '0' ? 8 : 10; |
81 | cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; | 73 | |
82 | cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; | 74 | cutoff = ULONG_MAX / (unsigned long)base; |
83 | for (acc = 0, any = 0;; c = *s++) { | 75 | cutlim = ULONG_MAX % (unsigned long)base; |
76 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { | ||
84 | if (isdigit(c)) | 77 | if (isdigit(c)) |
85 | c -= '0'; | 78 | c -= '0'; |
86 | else if (isalpha(c)) | 79 | else if (isalpha(c)) |
@@ -89,18 +82,19 @@ strtoul(nptr, endptr, base) | |||
89 | break; | 82 | break; |
90 | if (c >= base) | 83 | if (c >= base) |
91 | break; | 84 | break; |
92 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | 85 | if (any < 0) |
86 | continue; | ||
87 | if (acc > cutoff || acc == cutoff && c > cutlim) { | ||
93 | any = -1; | 88 | any = -1; |
94 | else { | 89 | acc = ULONG_MAX; |
90 | errno = ERANGE; | ||
91 | } else { | ||
95 | any = 1; | 92 | any = 1; |
96 | acc *= base; | 93 | acc *= (unsigned long)base; |
97 | acc += c; | 94 | acc += c; |
98 | } | 95 | } |
99 | } | 96 | } |
100 | if (any < 0) { | 97 | if (neg && any > 0) |
101 | acc = ULONG_MAX; | ||
102 | errno = ERANGE; | ||
103 | } else if (neg) | ||
104 | acc = -acc; | 98 | acc = -acc; |
105 | if (endptr != 0) | 99 | if (endptr != 0) |
106 | *endptr = (char *) (any ? s - 1 : nptr); | 100 | *endptr = (char *) (any ? s - 1 : nptr); |
diff --git a/src/lib/libc/stdlib/strtouq.c b/src/lib/libc/stdlib/strtoull.c index cc647d8d28..37859776f9 100644 --- a/src/lib/libc/stdlib/strtouq.c +++ b/src/lib/libc/stdlib/strtoull.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: strtoull.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /*- | 2 | /*- |
2 | * Copyright (c) 1992 The Regents of the University of California. | 3 | * Copyright (c) 1992 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,41 +28,33 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | static char sccsid[] = "@(#)strtouq.c 5.1 (Berkeley) 6/26/92"; | ||
36 | #endif /* LIBC_SCCS and not lint */ | ||
37 | |||
38 | #include <sys/types.h> | 31 | #include <sys/types.h> |
39 | 32 | ||
40 | #include <limits.h> | ||
41 | #include <errno.h> | ||
42 | #include <ctype.h> | 33 | #include <ctype.h> |
34 | #include <errno.h> | ||
35 | #include <limits.h> | ||
43 | #include <stdlib.h> | 36 | #include <stdlib.h> |
44 | 37 | ||
45 | /* | 38 | /* |
46 | * Convert a string to an unsigned quad integer. | 39 | * Convert a string to an unsigned long long. |
47 | * | 40 | * |
48 | * Ignores `locale' stuff. Assumes that the upper and lower case | 41 | * Ignores `locale' stuff. Assumes that the upper and lower case |
49 | * alphabets and digits are each contiguous. | 42 | * alphabets and digits are each contiguous. |
50 | */ | 43 | */ |
51 | u_quad_t | 44 | unsigned long long |
52 | strtouq(nptr, endptr, base) | 45 | strtoull(const char *nptr, char **endptr, int base) |
53 | const char *nptr; | ||
54 | char **endptr; | ||
55 | register int base; | ||
56 | { | 46 | { |
57 | register const char *s = nptr; | 47 | const char *s; |
58 | register u_quad_t acc; | 48 | unsigned long long acc, cutoff; |
59 | register int c; | 49 | int c; |
60 | register u_quad_t qbase, cutoff; | 50 | int neg, any, cutlim; |
61 | register int neg, any, cutlim; | ||
62 | 51 | ||
63 | /* | 52 | /* |
64 | * See strtoq for comments as to the logic used. | 53 | * See strtoq for comments as to the logic used. |
65 | */ | 54 | */ |
66 | s = nptr; | 55 | s = nptr; |
67 | do { | 56 | do { |
68 | c = *s++; | 57 | c = (unsigned char) *s++; |
69 | } while (isspace(c)); | 58 | } while (isspace(c)); |
70 | if (c == '-') { | 59 | if (c == '-') { |
71 | neg = 1; | 60 | neg = 1; |
@@ -83,10 +72,10 @@ strtouq(nptr, endptr, base) | |||
83 | } | 72 | } |
84 | if (base == 0) | 73 | if (base == 0) |
85 | base = c == '0' ? 8 : 10; | 74 | base = c == '0' ? 8 : 10; |
86 | qbase = (unsigned)base; | 75 | |
87 | cutoff = (u_quad_t)UQUAD_MAX / qbase; | 76 | cutoff = ULLONG_MAX / (unsigned long long)base; |
88 | cutlim = (u_quad_t)UQUAD_MAX % qbase; | 77 | cutlim = ULLONG_MAX % (unsigned long long)base; |
89 | for (acc = 0, any = 0;; c = *s++) { | 78 | for (acc = 0, any = 0;; c = (unsigned char) *s++) { |
90 | if (isdigit(c)) | 79 | if (isdigit(c)) |
91 | c -= '0'; | 80 | c -= '0'; |
92 | else if (isalpha(c)) | 81 | else if (isalpha(c)) |
@@ -95,20 +84,32 @@ strtouq(nptr, endptr, base) | |||
95 | break; | 84 | break; |
96 | if (c >= base) | 85 | if (c >= base) |
97 | break; | 86 | break; |
98 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | 87 | if (any < 0) |
88 | continue; | ||
89 | if (acc > cutoff || (acc == cutoff && c > cutlim)) { | ||
99 | any = -1; | 90 | any = -1; |
100 | else { | 91 | acc = ULLONG_MAX; |
92 | errno = ERANGE; | ||
93 | } else { | ||
101 | any = 1; | 94 | any = 1; |
102 | acc *= qbase; | 95 | acc *= (unsigned long long)base; |
103 | acc += c; | 96 | acc += c; |
104 | } | 97 | } |
105 | } | 98 | } |
106 | if (any < 0) { | 99 | if (neg && any > 0) |
107 | acc = UQUAD_MAX; | ||
108 | errno = ERANGE; | ||
109 | } else if (neg) | ||
110 | acc = -acc; | 100 | acc = -acc; |
111 | if (endptr != 0) | 101 | if (endptr != 0) |
112 | *endptr = (char *) (any ? s - 1 : nptr); | 102 | *endptr = (char *) (any ? s - 1 : nptr); |
113 | return (acc); | 103 | return (acc); |
114 | } | 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 index 520f51db0a..1034b01066 100644 --- a/src/lib/libc/stdlib/system.3 +++ b/src/lib/libc/stdlib/system.3 | |||
@@ -13,11 +13,7 @@ | |||
13 | .\" 2. Redistributions in binary form must reproduce the above copyright | 13 | .\" 2. Redistributions in binary form must reproduce the above copyright |
14 | .\" notice, this list of conditions and the following disclaimer in the | 14 | .\" notice, this list of conditions and the following disclaimer in the |
15 | .\" documentation and/or other materials provided with the distribution. | 15 | .\" documentation and/or other materials provided with the distribution. |
16 | .\" 3. All advertising materials mentioning features or use of this software | 16 | .\" 3. Neither the name of the University nor the names of its contributors |
17 | .\" must display the following acknowledgement: | ||
18 | .\" This product includes software developed by the University of | ||
19 | .\" California, Berkeley and its contributors. | ||
20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
21 | .\" may be used to endorse or promote products derived from this software | 17 | .\" may be used to endorse or promote products derived from this software |
22 | .\" without specific prior written permission. | 18 | .\" without specific prior written permission. |
23 | .\" | 19 | .\" |
@@ -33,8 +29,7 @@ | |||
33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 29 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
34 | .\" SUCH DAMAGE. | 30 | .\" SUCH DAMAGE. |
35 | .\" | 31 | .\" |
36 | .\" from: @(#)system.3 6.5 (Berkeley) 6/29/91 | 32 | .\" $OpenBSD: system.3,v 1.9 2003/06/02 20:18:38 millert Exp $ |
37 | .\" $Id: system.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
38 | .\" | 33 | .\" |
39 | .Dd June 29, 1991 | 34 | .Dd June 29, 1991 |
40 | .Dt SYSTEM 3 | 35 | .Dt SYSTEM 3 |
@@ -49,8 +44,7 @@ | |||
49 | .Sh DESCRIPTION | 44 | .Sh DESCRIPTION |
50 | The | 45 | The |
51 | .Fn system | 46 | .Fn system |
52 | function | 47 | function hands the argument |
53 | hands the argument | ||
54 | .Fa string | 48 | .Fa string |
55 | to the command interpreter | 49 | to the command interpreter |
56 | .Xr sh 1 . | 50 | .Xr sh 1 . |
@@ -64,36 +58,42 @@ and blocking | |||
64 | .Pp | 58 | .Pp |
65 | If | 59 | If |
66 | .Fa string | 60 | .Fa string |
67 | is a | 61 | is |
68 | .Dv NULL | 62 | .Dv NULL , |
69 | pointer, | ||
70 | .Fn system | 63 | .Fn system |
71 | will return non-zero. | 64 | will return non-zero. |
72 | Otherwise, | 65 | Otherwise, |
73 | .Fn system | 66 | .Fn system |
74 | returns the termination status of the shell in the format specified by | 67 | returns the termination status of the shell in the format specified by |
75 | .Xr waitpid 3 . | 68 | .Xr waitpid 2 . |
76 | .Sh RETURN VALUES | 69 | .Sh RETURN VALUES |
77 | If a child process cannot be created, or the termination status of | 70 | If a child process cannot be created, or the termination status of |
78 | the shell cannot be obtained, | 71 | the shell cannot be obtained, |
79 | .Fn system | 72 | .Fn system |
80 | returns -1 and sets | 73 | returns \-1 and sets |
81 | .Va errno | 74 | .Va errno |
82 | to indicate the error. | 75 | to indicate the error. |
83 | If execution of the shell fails, | 76 | If execution of the shell fails, |
84 | .Fn system | 77 | .Fn system |
85 | returns the termination status for a program that terminates with a call of | 78 | returns the termination status for a program that terminates with a call of |
86 | .Fn exit 127 . | 79 | .Fn exit 127 . |
87 | .Sh SEE ALSO | 80 | .Sh SEE ALSO |
88 | .Xr sh 1 , | 81 | .Xr sh 1 , |
89 | .Xr execve 2 , | 82 | .Xr execve 2 , |
90 | .Xr popen 3 , | 83 | .Xr waitpid 2 , |
91 | .Xr waitpid 3 , | 84 | .Xr popen 3 |
92 | .Sh STANDARDS | 85 | .Sh STANDARDS |
93 | The | 86 | The |
94 | .Fn system | 87 | .Fn system |
95 | function | 88 | function conforms to |
96 | conforms to | 89 | .St -ansiC |
97 | .St -ansiC | ||
98 | and | 90 | and |
99 | .St -1003.2-92 . | 91 | .St -p1003.2-92 . |
92 | .Sh CAVEATS | ||
93 | Never supply the | ||
94 | .Fn system | ||
95 | function with a command containing any part of an unsanitized user-supplied | ||
96 | string. | ||
97 | Shell meta-characters present will be honored by the | ||
98 | .Xr sh 1 | ||
99 | command interpreter. | ||
diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c index c2f39325f6..14ddcae8d3 100644 --- a/src/lib/libc/stdlib/system.c +++ b/src/lib/libc/stdlib/system.c | |||
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: system.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 1988 The Regents of the University of California. | 3 | * Copyright (c) 1988 The Regents of the University of California. |
3 | * All rights reserved. | 4 | * All rights reserved. |
@@ -10,11 +11,7 @@ | |||
10 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the | 12 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. | 13 | * documentation and/or other materials provided with the distribution. |
13 | * 3. All advertising materials mentioning features or use of this software | 14 | * 3. Neither the name of the University nor the names of its contributors |
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. 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 | 15 | * may be used to endorse or promote products derived from this software |
19 | * without specific prior written permission. | 16 | * without specific prior written permission. |
20 | * | 17 | * |
@@ -31,11 +28,6 @@ | |||
31 | * SUCH DAMAGE. | 28 | * SUCH DAMAGE. |
32 | */ | 29 | */ |
33 | 30 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | ||
35 | /*static char *sccsid = "from: @(#)system.c 5.10 (Berkeley) 2/23/91";*/ | ||
36 | static char *rcsid = "$Id: system.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | ||
38 | |||
39 | #include <sys/types.h> | 31 | #include <sys/types.h> |
40 | #include <sys/wait.h> | 32 | #include <sys/wait.h> |
41 | #include <signal.h> | 33 | #include <signal.h> |
@@ -46,25 +38,28 @@ static char *rcsid = "$Id: system.c,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $" | |||
46 | extern char **environ; | 38 | extern char **environ; |
47 | 39 | ||
48 | int | 40 | int |
49 | system(command) | 41 | system(const char *command) |
50 | const char *command; | ||
51 | { | 42 | { |
52 | pid_t pid; | 43 | pid_t pid; |
53 | sig_t intsave, quitsave; | 44 | sig_t intsave, quitsave; |
54 | int omask; | 45 | sigset_t mask, omask; |
55 | int pstat; | 46 | int pstat; |
56 | char *argp[] = {"sh", "-c", (char *) command, NULL}; | 47 | char *argp[] = {"sh", "-c", NULL, NULL}; |
57 | 48 | ||
58 | if (!command) /* just checking... */ | 49 | if (!command) /* just checking... */ |
59 | return(1); | 50 | return(1); |
60 | 51 | ||
61 | omask = sigblock(sigmask(SIGCHLD)); | 52 | argp[2] = (char *)command; |
62 | switch(pid = vfork()) { | 53 | |
54 | sigemptyset(&mask); | ||
55 | sigaddset(&mask, SIGCHLD); | ||
56 | sigprocmask(SIG_BLOCK, &mask, &omask); | ||
57 | switch (pid = vfork()) { | ||
63 | case -1: /* error */ | 58 | case -1: /* error */ |
64 | (void)sigsetmask(omask); | 59 | sigprocmask(SIG_SETMASK, &omask, NULL); |
65 | return(-1); | 60 | return(-1); |
66 | case 0: /* child */ | 61 | case 0: /* child */ |
67 | (void)sigsetmask(omask); | 62 | sigprocmask(SIG_SETMASK, &omask, NULL); |
68 | execve(_PATH_BSHELL, argp, environ); | 63 | execve(_PATH_BSHELL, argp, environ); |
69 | _exit(127); | 64 | _exit(127); |
70 | } | 65 | } |
@@ -72,8 +67,8 @@ system(command) | |||
72 | intsave = signal(SIGINT, SIG_IGN); | 67 | intsave = signal(SIGINT, SIG_IGN); |
73 | quitsave = signal(SIGQUIT, SIG_IGN); | 68 | quitsave = signal(SIGQUIT, SIG_IGN); |
74 | pid = waitpid(pid, (int *)&pstat, 0); | 69 | pid = waitpid(pid, (int *)&pstat, 0); |
75 | (void)sigsetmask(omask); | 70 | sigprocmask(SIG_SETMASK, &omask, NULL); |
76 | (void)signal(SIGINT, intsave); | 71 | (void)signal(SIGINT, intsave); |
77 | (void)signal(SIGQUIT, quitsave); | 72 | (void)signal(SIGQUIT, quitsave); |
78 | return(pid == -1 ? -1 : pstat); | 73 | return (pid == -1 ? -1 : pstat); |
79 | } | 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..0dc5c0c374 --- /dev/null +++ b/src/lib/libc/stdlib/tsearch.3 | |||
@@ -0,0 +1,127 @@ | |||
1 | .\" $OpenBSD: tsearch.3,v 1.15 2006/04/04 19:07:23 otto Exp $ | ||
2 | .\" | ||
3 | .\" Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> | ||
4 | .\" | ||
5 | .\" Permission to use, copy, modify, and distribute this software for any | ||
6 | .\" purpose with or without fee is hereby granted, provided that the above | ||
7 | .\" copyright notice and this permission notice appear in all copies. | ||
8 | .\" | ||
9 | .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
10 | .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
11 | .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
12 | .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
13 | .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
14 | .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
15 | .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
16 | .\" | ||
17 | .Dd June 15, 1997 | ||
18 | .Dt TSEARCH 3 | ||
19 | .Os | ||
20 | .Sh NAME | ||
21 | .Nm tsearch , | ||
22 | .Nm tfind , | ||
23 | .Nm tdelete , | ||
24 | .Nm twalk | ||
25 | .Nd manipulate binary search trees | ||
26 | .Sh SYNOPSIS | ||
27 | .Fd #include <search.h> | ||
28 | .Ft void * | ||
29 | .Fn tdelete "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" | ||
30 | .Ft void * | ||
31 | .Fn tfind "const void *key" "void * const *rootp" "int (*compar) (const void *, const void *)" | ||
32 | .Ft void * | ||
33 | .Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" | ||
34 | .Ft void | ||
35 | .Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)" | ||
36 | .Sh DESCRIPTION | ||
37 | The | ||
38 | .Fn tdelete , | ||
39 | .Fn tfind , | ||
40 | .Fn tsearch , | ||
41 | and | ||
42 | .Fn twalk | ||
43 | functions manage binary search trees based on algorithms T and D | ||
44 | from Knuth (6.2.2). | ||
45 | The comparison function passed in by | ||
46 | the user has the same style of return values as | ||
47 | .Xr strcmp 3 . | ||
48 | .Pp | ||
49 | .Fn tfind | ||
50 | searches for the datum matched by the argument | ||
51 | .Fa key | ||
52 | in the binary tree rooted at | ||
53 | .Fa rootp , | ||
54 | returning a pointer to the datum if it is found and | ||
55 | .Dv NULL | ||
56 | if it is not. | ||
57 | .Pp | ||
58 | .Fn tsearch | ||
59 | is identical to | ||
60 | .Fn tfind | ||
61 | except that if no match is found, | ||
62 | .Fa key | ||
63 | is inserted into the tree and a pointer to it is returned. | ||
64 | If | ||
65 | .Fa rootp | ||
66 | points to a null value a new binary search tree is created. | ||
67 | .Pp | ||
68 | .Fn tdelete | ||
69 | deletes a node from the specified binary search tree and returns | ||
70 | a pointer to the parent of the node to be deleted. | ||
71 | It takes the same arguments as | ||
72 | .Fn tfind | ||
73 | and | ||
74 | .Fn tsearch . | ||
75 | If the node to be deleted is the root of the binary search tree, | ||
76 | .Fa rootp | ||
77 | will be adjusted 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 | } | ||