diff options
Diffstat (limited to '')
79 files changed, 10218 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc new file mode 100644 index 0000000000..782a4ab022 --- /dev/null +++ b/src/lib/libc/stdlib/Makefile.inc | |||
| @@ -0,0 +1,45 @@ | |||
| 1 | # from: @(#)Makefile.inc 5.6 (Berkeley) 6/4/91 | ||
| 2 | # $Id: Makefile.inc,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 3 | |||
| 4 | # stdlib sources | ||
| 5 | .PATH: ${.CURDIR}/arch/${MACHINE_ARCH}/stdlib ${.CURDIR}/stdlib | ||
| 6 | |||
| 7 | SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c bsearch.c calloc.c \ | ||
| 8 | exit.c getenv.c getopt.c heapsort.c l64a.c malloc.c merge.c \ | ||
| 9 | multibyte.c putenv.c qsort.c radixsort.c rand.c random.c realpath.c \ | ||
| 10 | setenv.c strtod.c strtol.c strtoq.c strtoul.c strtouq.c system.c \ | ||
| 11 | _rand48.c drand48.c erand48.c jrand48.c lcong48.c lrand48.c \ | ||
| 12 | mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c | ||
| 13 | |||
| 14 | .if (${MACHINE_ARCH} == "m68k") | ||
| 15 | SRCS+= abs.S div.c labs.c ldiv.c | ||
| 16 | .elif (${MACHINE_ARCH} == "i386") | ||
| 17 | SRCS+= abs.S div.S labs.S ldiv.S | ||
| 18 | .elif (${MACHINE_ARCH} == "ns32k") | ||
| 19 | SRCS+= abs.S div.c labs.c ldiv.c | ||
| 20 | .elif (${MACHINE_ARCH} == "tahoe") | ||
| 21 | SRCS+= abs.S div.c labs.c ldiv.c | ||
| 22 | .elif (${MACHINE_ARCH} == "vax") | ||
| 23 | SRCS+= abs.c div.c labs.c ldiv.c | ||
| 24 | .elif (${MACHINE_ARCH} == "alpha") | ||
| 25 | # XXX should be .S's | ||
| 26 | SRCS+= abs.c div.c labs.c ldiv.c | ||
| 27 | .else | ||
| 28 | SRCS+= abs.c div.c labs.c ldiv.c | ||
| 29 | .endif | ||
| 30 | |||
| 31 | MAN+= abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 bsearch.3 \ | ||
| 32 | calloc.3 div.3 exit.3 free.3 getenv.3 getopt.3 labs.3 ldiv.3 \ | ||
| 33 | malloc.3 memory.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ | ||
| 34 | rand.3 random.3 realloc.3 realpath.3 strtod.3 strtol.3 strtoul.3 \ | ||
| 35 | system.3 | ||
| 36 | |||
| 37 | MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 | ||
| 38 | MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 | ||
| 39 | MLINKS+=rand.3 srand.3 | ||
| 40 | MLINKS+=strtol.3 strtoq.3 | ||
| 41 | MLINKS+=strtoul.3 strtouq.3 | ||
| 42 | MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 | ||
| 43 | 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 | ||
| 45 | MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 | ||
diff --git a/src/lib/libc/stdlib/_rand48.c b/src/lib/libc/stdlib/_rand48.c new file mode 100644 index 0000000000..83ade4645a --- /dev/null +++ b/src/lib/libc/stdlib/_rand48.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | unsigned short __rand48_seed[3] = { | ||
| 17 | RAND48_SEED_0, | ||
| 18 | RAND48_SEED_1, | ||
| 19 | RAND48_SEED_2 | ||
| 20 | }; | ||
| 21 | unsigned short __rand48_mult[3] = { | ||
| 22 | RAND48_MULT_0, | ||
| 23 | RAND48_MULT_1, | ||
| 24 | RAND48_MULT_2 | ||
| 25 | }; | ||
| 26 | unsigned short __rand48_add = RAND48_ADD; | ||
| 27 | |||
| 28 | void | ||
| 29 | __dorand48(unsigned short xseed[3]) | ||
| 30 | { | ||
| 31 | unsigned long accu; | ||
| 32 | unsigned short temp[2]; | ||
| 33 | |||
| 34 | accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] + | ||
| 35 | (unsigned long) __rand48_add; | ||
| 36 | temp[0] = (unsigned short) accu; /* lower 16 bits */ | ||
| 37 | accu >>= sizeof(unsigned short) * 8; | ||
| 38 | accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] + | ||
| 39 | (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0]; | ||
| 40 | temp[1] = (unsigned short) accu; /* middle 16 bits */ | ||
| 41 | accu >>= sizeof(unsigned short) * 8; | ||
| 42 | accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0]; | ||
| 43 | xseed[0] = temp[0]; | ||
| 44 | xseed[1] = temp[1]; | ||
| 45 | xseed[2] = (unsigned short) accu; | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/a64l.c b/src/lib/libc/stdlib/a64l.c new file mode 100644 index 0000000000..03fc77e034 --- /dev/null +++ b/src/lib/libc/stdlib/a64l.c | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | /* | ||
| 2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 3 | * Public domain. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 7 | static char *rcsid = "$NetBSD: a64l.c,v 1.3 1995/05/11 23:04:47 jtc Exp $"; | ||
| 8 | #endif | ||
| 9 | |||
| 10 | long | ||
| 11 | a64l(s) | ||
| 12 | const char *s; | ||
| 13 | { | ||
| 14 | long value, digit, shift; | ||
| 15 | int i; | ||
| 16 | |||
| 17 | value = 0; | ||
| 18 | shift = 0; | ||
| 19 | for (i = 0; *s && i < 6; i++, s++) { | ||
| 20 | if (*s <= '/') | ||
| 21 | digit = *s - '.'; | ||
| 22 | else if (*s <= '9') | ||
| 23 | digit = *s - '0' + 2; | ||
| 24 | else if (*s <= 'Z') | ||
| 25 | digit = *s - 'A' + 12; | ||
| 26 | else | ||
| 27 | digit = *s - 'a' + 38; | ||
| 28 | |||
| 29 | value |= digit << shift; | ||
| 30 | shift += 6; | ||
| 31 | } | ||
| 32 | |||
| 33 | return (long) value; | ||
| 34 | } | ||
diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3 new file mode 100644 index 0000000000..ab57327585 --- /dev/null +++ b/src/lib/libc/stdlib/abort.3 | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)abort.3 6.7 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: abort.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ABORT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm abort | ||
| 44 | .Nd cause abnormal program termination | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn abort void | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn abort | ||
| 52 | function causes abnormal program termination to occur, unless the | ||
| 53 | signal | ||
| 54 | .Dv SIGABRT | ||
| 55 | is being caught and the signal handler does not return. | ||
| 56 | .Pp | ||
| 57 | No open streams are closed or flushed. | ||
| 58 | .Sh RETURN VALUES | ||
| 59 | The | ||
| 60 | .Nm abort | ||
| 61 | function | ||
| 62 | never returns. | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr sigaction 2 , | ||
| 65 | .Xr exit 2 | ||
| 66 | .Sh STANDARDS | ||
| 67 | The | ||
| 68 | .Fn abort | ||
| 69 | function | ||
| 70 | conforms to | ||
| 71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c new file mode 100644 index 0000000000..c298e016b4 --- /dev/null +++ b/src/lib/libc/stdlib/abort.c | |||
| @@ -0,0 +1,65 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1985 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <stdlib.h> | ||
| 41 | #include <unistd.h> | ||
| 42 | |||
| 43 | void | ||
| 44 | abort() | ||
| 45 | { | ||
| 46 | sigset_t mask; | ||
| 47 | |||
| 48 | sigfillset(&mask); | ||
| 49 | /* | ||
| 50 | * don't block SIGABRT to give any handler a chance; we ignore | ||
| 51 | * any errors -- X311J doesn't allow abort to return anyway. | ||
| 52 | */ | ||
| 53 | sigdelset(&mask, SIGABRT); | ||
| 54 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
| 55 | (void)kill(getpid(), SIGABRT); | ||
| 56 | |||
| 57 | /* | ||
| 58 | * if SIGABRT ignored, or caught and the handler returns, do | ||
| 59 | * it again, only harder. | ||
| 60 | */ | ||
| 61 | (void)signal(SIGABRT, SIG_DFL); | ||
| 62 | (void)sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); | ||
| 63 | (void)kill(getpid(), SIGABRT); | ||
| 64 | exit(1); | ||
| 65 | } | ||
diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3 new file mode 100644 index 0000000000..4748d89e77 --- /dev/null +++ b/src/lib/libc/stdlib/abs.3 | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)abs.3 6.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: abs.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ABS 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm abs | ||
| 44 | .Nd integer absolute value function | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn abs "int j" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn abs | ||
| 52 | function | ||
| 53 | computes | ||
| 54 | the absolute value of the integer | ||
| 55 | .Ar j . | ||
| 56 | .Sh RETURN VALUES | ||
| 57 | The | ||
| 58 | .Fn abs | ||
| 59 | function | ||
| 60 | returns | ||
| 61 | the absolute value. | ||
| 62 | .Sh SEE ALSO | ||
| 63 | .Xr floor 3 , | ||
| 64 | .Xr labs 3 , | ||
| 65 | .Xr cabs 3 , | ||
| 66 | .Xr hypot 3 , | ||
| 67 | .Xr math 3 | ||
| 68 | .Sh STANDARDS | ||
| 69 | The | ||
| 70 | .Fn abs | ||
| 71 | function conforms to | ||
| 72 | .St -ansiC . | ||
| 73 | .Sh BUGS | ||
| 74 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/abs.c b/src/lib/libc/stdlib/abs.c new file mode 100644 index 0000000000..64468e0224 --- /dev/null +++ b/src/lib/libc/stdlib/abs.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | int | ||
| 42 | abs(j) | ||
| 43 | int j; | ||
| 44 | { | ||
| 45 | return(j < 0 ? -j : j); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3 new file mode 100644 index 0000000000..dcb97ab11c --- /dev/null +++ b/src/lib/libc/stdlib/alloca.3 | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. 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: @(#)alloca.3 5.1 (Berkeley) 5/2/91 | ||
| 33 | .\" $Id: alloca.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd May 2, 1991 | ||
| 36 | .Dt ALLOCA 3 | ||
| 37 | .Os BSD 4 | ||
| 38 | .Sh NAME | ||
| 39 | .Nm alloca | ||
| 40 | .Nd memory allocator | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <stdlib.h> | ||
| 43 | .Ft void * | ||
| 44 | .Fn alloca "size_t size" | ||
| 45 | .Sh DESCRIPTION | ||
| 46 | The | ||
| 47 | .Fn alloca | ||
| 48 | function | ||
| 49 | allocates | ||
| 50 | .Fa size | ||
| 51 | bytes of space in the stack frame of the caller. | ||
| 52 | This temporary space is automatically freed on | ||
| 53 | return. | ||
| 54 | .Sh RETURN VALUES | ||
| 55 | The | ||
| 56 | .Fn alloca | ||
| 57 | 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 | ||
| 62 | .Xr brk 2 , | ||
| 63 | .Xr pagesize 2 | ||
| 64 | .Xr calloc 3 , | ||
| 65 | .Xr malloc 3 , | ||
| 66 | .Xr realloc 3 , | ||
| 67 | .Sh BUGS | ||
| 68 | The | ||
| 69 | .Fn alloca | ||
| 70 | function | ||
| 71 | is machine dependent; its use is discouraged. | ||
| 72 | .\" .Sh HISTORY | ||
| 73 | .\" The | ||
| 74 | .\" .Fn alloca | ||
| 75 | .\" function appeared in | ||
| 76 | .\" .Bx ?? . | ||
| 77 | .\" 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 | ||
| 79 | .\" moment is 4.3... | ||
diff --git a/src/lib/libc/stdlib/atexit.3 b/src/lib/libc/stdlib/atexit.3 new file mode 100644 index 0000000000..07de054d3c --- /dev/null +++ b/src/lib/libc/stdlib/atexit.3 | |||
| @@ -0,0 +1,78 @@ | |||
| 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. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atexit.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atexit.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATEXIT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atexit | ||
| 44 | .Nd register a function to be called on exit | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn atexit "void (*function)(void)" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn atexit | ||
| 52 | function | ||
| 53 | registers the given | ||
| 54 | .Ar function | ||
| 55 | to be called at program exit, whether via | ||
| 56 | .Xr exit 3 | ||
| 57 | or via return from the program's | ||
| 58 | .Em main . | ||
| 59 | Functions so registered are called in reverse order; | ||
| 60 | no arguments are passed. | ||
| 61 | At least 32 functions can always be registered, | ||
| 62 | and more are allowed as long as sufficient memory can be allocated. | ||
| 63 | .Sh RETURN VALUES | ||
| 64 | .Rv -std atexit | ||
| 65 | .Sh ERRORS | ||
| 66 | .Bl -tag -width Er | ||
| 67 | .It Bq Er ENOMEM | ||
| 68 | No memory was available to add the function to the list. | ||
| 69 | The existing list of functions is unmodified. | ||
| 70 | .El | ||
| 71 | .Sh SEE ALSO | ||
| 72 | .Xr exit 3 | ||
| 73 | .Sh STANDARDS | ||
| 74 | The | ||
| 75 | .Fn atexit | ||
| 76 | function | ||
| 77 | conforms to | ||
| 78 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c new file mode 100644 index 0000000000..4da1eb0d9c --- /dev/null +++ b/src/lib/libc/stdlib/atexit.c | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 38 | /*static char *sccsid = "from: @(#)atexit.c 5.2 (Berkeley) 11/14/90";*/ | ||
| 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> | ||
| 43 | #include "atexit.h" | ||
| 44 | |||
| 45 | struct atexit *__atexit; | ||
| 46 | |||
| 47 | /* | ||
| 48 | * Register a function to be performed at exit. | ||
| 49 | */ | ||
| 50 | int | ||
| 51 | atexit(fn) | ||
| 52 | void (*fn)(); | ||
| 53 | { | ||
| 54 | static struct atexit __atexit0; /* one guaranteed table */ | ||
| 55 | register struct atexit *p; | ||
| 56 | |||
| 57 | if ((p = __atexit) == NULL) | ||
| 58 | __atexit = p = &__atexit0; | ||
| 59 | else if (p->ind >= ATEXIT_SIZE) { | ||
| 60 | if ((p = malloc(sizeof(*p))) == NULL) | ||
| 61 | return (-1); | ||
| 62 | p->ind = 0; | ||
| 63 | p->next = __atexit; | ||
| 64 | __atexit = p; | ||
| 65 | } | ||
| 66 | p->fns[p->ind++] = fn; | ||
| 67 | return (0); | ||
| 68 | } | ||
diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h new file mode 100644 index 0000000000..8b756e8fe2 --- /dev/null +++ b/src/lib/libc/stdlib/atexit.h | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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 | * 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 | */ | ||
| 36 | |||
| 37 | /* must be at least 32 to guarantee ANSI conformance */ | ||
| 38 | #define ATEXIT_SIZE 32 | ||
| 39 | |||
| 40 | struct atexit { | ||
| 41 | struct atexit *next; /* next in list */ | ||
| 42 | int ind; /* next index in this table */ | ||
| 43 | void (*fns[ATEXIT_SIZE])(); /* the table itself */ | ||
| 44 | }; | ||
| 45 | |||
| 46 | extern struct atexit *__atexit; /* points to head of LIFO stack */ | ||
diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3 new file mode 100644 index 0000000000..53e04f71c5 --- /dev/null +++ b/src/lib/libc/stdlib/atof.3 | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atof.3 6.4 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atof.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATOF 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atof | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to double | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft double | ||
| 50 | .Fn atof "const char *nptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn atof | ||
| 54 | function converts the initial portion of the string pointed to by | ||
| 55 | .Ar nptr | ||
| 56 | to | ||
| 57 | .Ar double | ||
| 58 | representation. | ||
| 59 | .Pp | ||
| 60 | It is equivalent to: | ||
| 61 | .Bd -literal -offset indent | ||
| 62 | strtod(nptr, (char **)NULL); | ||
| 63 | .Ed | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr atoi 3 , | ||
| 66 | .Xr atol 3 , | ||
| 67 | .Xr strtod 3 , | ||
| 68 | .Xr strtol 3 , | ||
| 69 | .Xr strtoul 3 | ||
| 70 | .Sh STANDARDS | ||
| 71 | The | ||
| 72 | .Fn atof | ||
| 73 | function conforms to | ||
| 74 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atof.c b/src/lib/libc/stdlib/atof.c new file mode 100644 index 0000000000..9202de50bb --- /dev/null +++ b/src/lib/libc/stdlib/atof.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | double | ||
| 42 | atof(ascii) | ||
| 43 | const char *ascii; | ||
| 44 | { | ||
| 45 | return(strtod(ascii, (char **)NULL)); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3 new file mode 100644 index 0000000000..219ba73c00 --- /dev/null +++ b/src/lib/libc/stdlib/atoi.3 | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atoi.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atoi.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATOI 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atoi | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to integer | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft int | ||
| 50 | .Fn atoi "const char *nptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn atoi | ||
| 54 | function converts the initial portion of the string pointed to by | ||
| 55 | .Em nptr | ||
| 56 | to | ||
| 57 | .Em integer | ||
| 58 | representation. | ||
| 59 | .Pp | ||
| 60 | It is equivalent to: | ||
| 61 | .Pp | ||
| 62 | .Bd -literal -offset indent | ||
| 63 | (int)strtol(nptr, (char **)NULL, 10); | ||
| 64 | .Ed | ||
| 65 | .Sh SEE ALSO | ||
| 66 | .Xr atof 3 , | ||
| 67 | .Xr atol 3 , | ||
| 68 | .Xr strtod 3 , | ||
| 69 | .Xr strtol 3 , | ||
| 70 | .Xr strtoul 3 | ||
| 71 | .Sh STANDARDS | ||
| 72 | The | ||
| 73 | .Fn atoi | ||
| 74 | function conforms to | ||
| 75 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atoi.c b/src/lib/libc/stdlib/atoi.c new file mode 100644 index 0000000000..df7845f90c --- /dev/null +++ b/src/lib/libc/stdlib/atoi.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | int | ||
| 42 | atoi(str) | ||
| 43 | const char *str; | ||
| 44 | { | ||
| 45 | return((int)strtol(str, (char **)NULL, 10)); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3 new file mode 100644 index 0000000000..86e3d324a3 --- /dev/null +++ b/src/lib/libc/stdlib/atol.3 | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)atol.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: atol.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt ATOL 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm atol | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to long integer | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft long | ||
| 50 | .Fn atol "const char *nptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn atol | ||
| 54 | function converts the initial portion of the string pointed to by | ||
| 55 | .Ar nptr | ||
| 56 | to | ||
| 57 | .Em long integer | ||
| 58 | representation. | ||
| 59 | .Pp | ||
| 60 | It is equivalent to: | ||
| 61 | .Bd -literal -offset indent | ||
| 62 | strtol(nptr, (char **)NULL, 10); | ||
| 63 | .Ed | ||
| 64 | .Sh SEE ALSO | ||
| 65 | .Xr atof 3 , | ||
| 66 | .Xr atoi 3 , | ||
| 67 | .Xr strtod 3 , | ||
| 68 | .Xr strtol 3 , | ||
| 69 | .Xr strtoul 3 | ||
| 70 | .Sh STANDARDS | ||
| 71 | The | ||
| 72 | .Fn atol | ||
| 73 | function | ||
| 74 | conforms to | ||
| 75 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/atol.c b/src/lib/libc/stdlib/atol.c new file mode 100644 index 0000000000..31ed06298b --- /dev/null +++ b/src/lib/libc/stdlib/atol.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | long | ||
| 42 | atol(str) | ||
| 43 | const char *str; | ||
| 44 | { | ||
| 45 | return(strtol(str, (char **)NULL, 10)); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/bsearch.3 b/src/lib/libc/stdlib/bsearch.3 new file mode 100644 index 0000000000..1622c96c6b --- /dev/null +++ b/src/lib/libc/stdlib/bsearch.3 | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)bsearch.3 5.6 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: bsearch.3,v 1.1.1.1 1995/10/18 08:42:16 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt BSEARCH 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm bsearch | ||
| 44 | .Nd binary search of a sorted table | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn bsearch "const void *key" "const void *base" "size_t nmemb" "size_t size" "int (*compar) (const void *, const void *)" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn bsearch | ||
| 52 | function searches an array of | ||
| 53 | .Fa nmemb | ||
| 54 | objects, the initial member of which is | ||
| 55 | pointed to by | ||
| 56 | .Fa base , | ||
| 57 | for a member that matches the object pointed to by | ||
| 58 | .Fa key . | ||
| 59 | The size of each member of the array is specified by | ||
| 60 | .Fa size . | ||
| 61 | .Pp | ||
| 62 | The contents of the array should be in ascending sorted order according | ||
| 63 | to the comparison function referenced by | ||
| 64 | .Fa compar . | ||
| 65 | The | ||
| 66 | .Fa compar | ||
| 67 | routine | ||
| 68 | is expected to have two | ||
| 69 | two arguments which point to the | ||
| 70 | .Fa key | ||
| 71 | 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 | ||
| 73 | .Fa key | ||
| 74 | object is found, respectively, to be less than, to match, or be | ||
| 75 | greater than the array member. | ||
| 76 | .Sh RETURN VALUES | ||
| 77 | The | ||
| 78 | .Fn bsearch | ||
| 79 | function returns a pointer to a matching member of the array, or a null | ||
| 80 | pointer if no match is found. | ||
| 81 | If two members compare as equal, which member is matched is unspecified. | ||
| 82 | .Sh SEE ALSO | ||
| 83 | .Xr db 3 , | ||
| 84 | .Xr lsearch 3 , | ||
| 85 | .Xr qsort 3 , | ||
| 86 | .\" .Xr tsearch 3 | ||
| 87 | .Sh STANDARDS | ||
| 88 | The | ||
| 89 | .Fn bsearch | ||
| 90 | function conforms to | ||
| 91 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/bsearch.c b/src/lib/libc/stdlib/bsearch.c new file mode 100644 index 0000000000..fac03f694f --- /dev/null +++ b/src/lib/libc/stdlib/bsearch.c | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Perform a binary search. | ||
| 43 | * | ||
| 44 | * The code below is a bit sneaky. After a comparison fails, we | ||
| 45 | * divide the work in half by moving either left or right. If lim | ||
| 46 | * is odd, moving left simply involves halving lim: e.g., when lim | ||
| 47 | * is 5 we look at item 2, so we change lim to 2 so that we will | ||
| 48 | * look at items 0 & 1. If lim is even, the same applies. If lim | ||
| 49 | * is odd, moving right again involes halving lim, this time moving | ||
| 50 | * the base up one item past p: e.g., when lim is 5 we change base | ||
| 51 | * to item 3 and make lim 2 so that we will look at items 3 and 4. | ||
| 52 | * If lim is even, however, we have to shrink it by one before | ||
| 53 | * halving: e.g., when lim is 4, we still looked at item 2, so we | ||
| 54 | * have to make lim 3, then halve, obtaining 1, so that we will only | ||
| 55 | * look at item 3. | ||
| 56 | */ | ||
| 57 | void * | ||
| 58 | bsearch(key, base0, nmemb, size, compar) | ||
| 59 | register const void *key; | ||
| 60 | const void *base0; | ||
| 61 | size_t nmemb; | ||
| 62 | register size_t size; | ||
| 63 | register int (*compar) __P((const void *, const void *)); | ||
| 64 | { | ||
| 65 | register const char *base = base0; | ||
| 66 | register int lim, cmp; | ||
| 67 | register const void *p; | ||
| 68 | |||
| 69 | for (lim = nmemb; lim != 0; lim >>= 1) { | ||
| 70 | p = base + (lim >> 1) * size; | ||
| 71 | cmp = (*compar)(key, p); | ||
| 72 | if (cmp == 0) | ||
| 73 | return ((void *)p); | ||
| 74 | if (cmp > 0) { /* key > p: move right */ | ||
| 75 | base = (char *)p + size; | ||
| 76 | lim--; | ||
| 77 | } /* else move left */ | ||
| 78 | } | ||
| 79 | return (NULL); | ||
| 80 | } | ||
diff --git a/src/lib/libc/stdlib/calloc.3 b/src/lib/libc/stdlib/calloc.3 new file mode 100644 index 0000000000..d0754b46a0 --- /dev/null +++ b/src/lib/libc/stdlib/calloc.3 | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)calloc.3 5.2 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: calloc.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt CALLOC 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm calloc | ||
| 44 | .Nd allocate clean memory (zero initialized space) | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn calloc "size_t nmemb" "size_t size" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn calloc | ||
| 52 | function allocates space for an array of | ||
| 53 | .Fa nmemb | ||
| 54 | objects, each of whose size is | ||
| 55 | .Fa size . | ||
| 56 | The space is initialized to all bits zero. | ||
| 57 | .Sh RETURN VALUES | ||
| 58 | The | ||
| 59 | .Fn calloc | ||
| 60 | function returns | ||
| 61 | a pointer to the | ||
| 62 | the allocated space if successful; otherwise a null pointer is returned. | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr malloc 3 , | ||
| 65 | .Xr realloc 3 , | ||
| 66 | .Xr free 3 | ||
| 67 | .Sh STANDARDS | ||
| 68 | The | ||
| 69 | .Fn calloc | ||
| 70 | function conforms to | ||
| 71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/calloc.c b/src/lib/libc/stdlib/calloc.c new file mode 100644 index 0000000000..3353fab052 --- /dev/null +++ b/src/lib/libc/stdlib/calloc.c | |||
| @@ -0,0 +1,53 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | void * | ||
| 43 | calloc(num, size) | ||
| 44 | size_t num; | ||
| 45 | register size_t size; | ||
| 46 | { | ||
| 47 | register void *p; | ||
| 48 | |||
| 49 | size *= num; | ||
| 50 | if (p = malloc(size)) | ||
| 51 | memset(p, '\0', size); | ||
| 52 | return(p); | ||
| 53 | } | ||
diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3 new file mode 100644 index 0000000000..a4730694a5 --- /dev/null +++ b/src/lib/libc/stdlib/div.3 | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek. | ||
| 6 | .\" Redistribution and use in source and binary forms, with or without | ||
| 7 | .\" modification, are permitted provided that the following conditions | ||
| 8 | .\" are met: | ||
| 9 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer. | ||
| 11 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 13 | .\" documentation and/or other materials provided with the distribution. | ||
| 14 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 15 | .\" must display the following acknowledgement: | ||
| 16 | .\" This product includes software developed by the University of | ||
| 17 | .\" California, Berkeley and its contributors. | ||
| 18 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 19 | .\" may be used to endorse or promote products derived from this software | ||
| 20 | .\" without specific prior written permission. | ||
| 21 | .\" | ||
| 22 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 23 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 24 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 25 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 26 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 27 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 28 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 29 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 30 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 31 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 32 | .\" SUCH DAMAGE. | ||
| 33 | .\" | ||
| 34 | .\" from: @(#)div.3 5.2 (Berkeley) 4/19/91 | ||
| 35 | .\" $Id: div.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 36 | .\" | ||
| 37 | .Dd April 19, 1991 | ||
| 38 | .Dt DIV 3 | ||
| 39 | .Os | ||
| 40 | .Sh NAME | ||
| 41 | .Nm div | ||
| 42 | .Nd return quotient and remainder from division | ||
| 43 | .Sh SYNOPSIS | ||
| 44 | .Fd #include <stdlib.h> | ||
| 45 | .Ft div_t | ||
| 46 | .Fn div "int num" "int denom" | ||
| 47 | .Sh DESCRIPTION | ||
| 48 | The | ||
| 49 | .Fn div | ||
| 50 | function | ||
| 51 | computes the value | ||
| 52 | .Fa num/denom | ||
| 53 | and returns the quotient and remainder in a structure named | ||
| 54 | .Fa div_t | ||
| 55 | that contains two | ||
| 56 | .Em int | ||
| 57 | members named | ||
| 58 | .Fa quot | ||
| 59 | and | ||
| 60 | .Fa rem . | ||
| 61 | .Sh SEE ALSO | ||
| 62 | .Xr ldiv 3 , | ||
| 63 | .Xr qdiv 3 , | ||
| 64 | .Xr math 3 | ||
| 65 | .Sh STANDARDS | ||
| 66 | The | ||
| 67 | .Fn div | ||
| 68 | function | ||
| 69 | conforms to | ||
| 70 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c new file mode 100644 index 0000000000..122ac0deec --- /dev/null +++ b/src/lib/libc/stdlib/div.c | |||
| @@ -0,0 +1,80 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #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 */ | ||
| 43 | |||
| 44 | div_t | ||
| 45 | div(num, denom) | ||
| 46 | int num, denom; | ||
| 47 | { | ||
| 48 | div_t r; | ||
| 49 | |||
| 50 | r.quot = num / denom; | ||
| 51 | r.rem = num % denom; | ||
| 52 | /* | ||
| 53 | * The ANSI standard says that |r.quot| <= |n/d|, where | ||
| 54 | * n/d is to be computed in infinite precision. In other | ||
| 55 | * words, we should always truncate the quotient towards | ||
| 56 | * 0, never -infinity. | ||
| 57 | * | ||
| 58 | * Machine division and remainer may work either way when | ||
| 59 | * one or both of n or d is negative. If only one is | ||
| 60 | * negative and r.quot has been truncated towards -inf, | ||
| 61 | * r.rem will have the same sign as denom and the opposite | ||
| 62 | * sign of num; if both are negative and r.quot has been | ||
| 63 | * truncated towards -inf, r.rem will be positive (will | ||
| 64 | * have the opposite sign of num). These are considered | ||
| 65 | * `wrong'. | ||
| 66 | * | ||
| 67 | * If both are num and denom are positive, r will always | ||
| 68 | * be positive. | ||
| 69 | * | ||
| 70 | * This all boils down to: | ||
| 71 | * if num >= 0, but r.rem < 0, we got the wrong answer. | ||
| 72 | * In that case, to get the right answer, add 1 to r.quot and | ||
| 73 | * subtract denom from r.rem. | ||
| 74 | */ | ||
| 75 | if (num >= 0 && r.rem < 0) { | ||
| 76 | r.quot++; | ||
| 77 | r.rem -= denom; | ||
| 78 | } | ||
| 79 | return (r); | ||
| 80 | } | ||
diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c new file mode 100644 index 0000000000..ae1a8634dc --- /dev/null +++ b/src/lib/libc/stdlib/drand48.c | |||
| @@ -0,0 +1,22 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | |||
| 18 | double | ||
| 19 | drand48(void) | ||
| 20 | { | ||
| 21 | return erand48(__rand48_seed); | ||
| 22 | } | ||
diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c new file mode 100644 index 0000000000..cc9fbf770c --- /dev/null +++ b/src/lib/libc/stdlib/erand48.c | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | double | ||
| 17 | erand48(unsigned short xseed[3]) | ||
| 18 | { | ||
| 19 | __dorand48(xseed); | ||
| 20 | return ldexp((double) xseed[0], -48) + | ||
| 21 | ldexp((double) xseed[1], -32) + | ||
| 22 | ldexp((double) xseed[2], -16); | ||
| 23 | } | ||
diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3 new file mode 100644 index 0000000000..adb81ffcb4 --- /dev/null +++ b/src/lib/libc/stdlib/exit.3 | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)exit.3 6.6 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: exit.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt EXIT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm exit | ||
| 44 | .Nd perform normal program termination | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn exit "int status" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | .Fn Exit | ||
| 51 | terminates a process. | ||
| 52 | .Pp | ||
| 53 | Before termination it performs the following functions in the | ||
| 54 | order listed: | ||
| 55 | .Bl -enum -offset indent | ||
| 56 | .It | ||
| 57 | Call the functions registered with the | ||
| 58 | .Xr atexit 3 | ||
| 59 | function, in the reverse order of their registration. | ||
| 60 | .It | ||
| 61 | Flush all open output streams. | ||
| 62 | .It | ||
| 63 | Close all open streams. | ||
| 64 | .It | ||
| 65 | Unlink all files created with the | ||
| 66 | .Xr tmpfile 3 | ||
| 67 | function. | ||
| 68 | .El | ||
| 69 | .Sh RETURN VALUES | ||
| 70 | The | ||
| 71 | .Fn exit | ||
| 72 | function | ||
| 73 | never returns. | ||
| 74 | .Sh SEE ALSO | ||
| 75 | .Xr _exit 2 , | ||
| 76 | .Xr atexit 3 , | ||
| 77 | .Xr intro 3 , | ||
| 78 | .Xr tmpfile 3 | ||
| 79 | .Sh STANDARDS | ||
| 80 | The | ||
| 81 | .Fn exit | ||
| 82 | function | ||
| 83 | conforms to | ||
| 84 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c new file mode 100644 index 0000000000..b1412f42bb --- /dev/null +++ b/src/lib/libc/stdlib/exit.c | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)exit.c 5.4 (Berkeley) 2/23/91";*/ | ||
| 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> | ||
| 40 | #include <unistd.h> | ||
| 41 | #include "atexit.h" | ||
| 42 | |||
| 43 | void (*__cleanup)(); | ||
| 44 | |||
| 45 | /* | ||
| 46 | * Exit, flushing stdio buffers if necessary. | ||
| 47 | */ | ||
| 48 | void | ||
| 49 | exit(status) | ||
| 50 | int status; | ||
| 51 | { | ||
| 52 | register struct atexit *p; | ||
| 53 | register int n; | ||
| 54 | |||
| 55 | for (p = __atexit; p; p = p->next) | ||
| 56 | for (n = p->ind; --n >= 0;) | ||
| 57 | (*p->fns[n])(); | ||
| 58 | if (__cleanup) | ||
| 59 | (*__cleanup)(); | ||
| 60 | _exit(status); | ||
| 61 | } | ||
diff --git a/src/lib/libc/stdlib/free.3 b/src/lib/libc/stdlib/free.3 new file mode 100644 index 0000000000..3d0131d7de --- /dev/null +++ b/src/lib/libc/stdlib/free.3 | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | .\" Copyright (c) 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)free.3 5.2 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: free.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt FREE 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm free | ||
| 44 | .Nd free up memory allocated with malloc, calloc or realloc | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn free "void *ptr" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn free | ||
| 52 | function causes the space pointed to by | ||
| 53 | .Fa ptr | ||
| 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 | ||
| 71 | The | ||
| 72 | .Fn free | ||
| 73 | function returns no value. | ||
| 74 | .Sh SEE ALSO | ||
| 75 | .Xr calloc 3 , | ||
| 76 | .Xr malloc 3 , | ||
| 77 | .Xr realloc 3 | ||
| 78 | .Sh STANDARDS | ||
| 79 | The | ||
| 80 | .Fn free | ||
| 81 | function conforms to | ||
| 82 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3 new file mode 100644 index 0000000000..411eb35da4 --- /dev/null +++ b/src/lib/libc/stdlib/getenv.3 | |||
| @@ -0,0 +1,152 @@ | |||
| 1 | .\" Copyright (c) 1988, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)getenv.3 6.11 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: getenv.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt GETENV 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm getenv , | ||
| 44 | .Nm putenv , | ||
| 45 | .Nm setenv , | ||
| 46 | .Nm unsetenv | ||
| 47 | .Nd environment variable functions | ||
| 48 | .Sh SYNOPSIS | ||
| 49 | .Fd #include <stdlib.h> | ||
| 50 | .Ft char * | ||
| 51 | .Fn getenv "const char *name" | ||
| 52 | .Ft int | ||
| 53 | .Fn setenv "const char *name" "const char *value" "int overwrite" | ||
| 54 | .Ft int | ||
| 55 | .Fn putenv "const char *string" | ||
| 56 | .Ft void | ||
| 57 | .Fn unsetenv "const char *name" | ||
| 58 | .Sh DESCRIPTION | ||
| 59 | These functions set, unset and fetch environment variables from the | ||
| 60 | host | ||
| 61 | .Em environment list . | ||
| 62 | For compatibility with differing environment conventions, | ||
| 63 | the given arguments | ||
| 64 | .Ar name | ||
| 65 | and | ||
| 66 | .Ar value | ||
| 67 | may be appended and prepended, | ||
| 68 | respectively, | ||
| 69 | with an equal sign | ||
| 70 | .Dq Li \&= . | ||
| 71 | .Pp | ||
| 72 | The | ||
| 73 | .Fn getenv | ||
| 74 | function obtains the current value of the environment variable, | ||
| 75 | .Ar name . | ||
| 76 | If the variable | ||
| 77 | .Ar name | ||
| 78 | is not in the current environment , | ||
| 79 | a null pointer is returned. | ||
| 80 | .Pp | ||
| 81 | The | ||
| 82 | .Fn setenv | ||
| 83 | function inserts or resets the environment variable | ||
| 84 | .Ar name | ||
| 85 | in the current environment list. | ||
| 86 | If the variable | ||
| 87 | .Ar name | ||
| 88 | does not exist in the list, | ||
| 89 | it is inserted with the given | ||
| 90 | .Ar value. | ||
| 91 | If the variable does exist, the argument | ||
| 92 | .Ar overwrite | ||
| 93 | is tested; if | ||
| 94 | .Ar overwrite is | ||
| 95 | zero, the | ||
| 96 | variable is not reset, otherwise it is reset | ||
| 97 | to the given | ||
| 98 | .Ar value . | ||
| 99 | .Pp | ||
| 100 | The | ||
| 101 | .Fn putenv | ||
| 102 | function takes an argument of the form ``name=value'' and is | ||
| 103 | equivalent to: | ||
| 104 | .Bd -literal -offset indent | ||
| 105 | setenv(name, value, 1); | ||
| 106 | .Ed | ||
| 107 | .Pp | ||
| 108 | The | ||
| 109 | .Fn unsetenv | ||
| 110 | function | ||
| 111 | deletes all instances of the variable name pointed to by | ||
| 112 | .Fa name | ||
| 113 | from the list. | ||
| 114 | .Sh RETURN VALUES | ||
| 115 | The functions | ||
| 116 | .Fn setenv | ||
| 117 | and | ||
| 118 | .Fn putenv | ||
| 119 | return zero if successful; otherwise the global variable | ||
| 120 | .Va errno | ||
| 121 | is set to indicate the error and a | ||
| 122 | \-1 is returned. | ||
| 123 | .Sh ERRORS | ||
| 124 | .Bl -tag -width Er | ||
| 125 | .It Bq Er ENOMEM | ||
| 126 | The function | ||
| 127 | .Fn setenv | ||
| 128 | or | ||
| 129 | .Fn putenv | ||
| 130 | failed because they were unable to allocate memory for the environment. | ||
| 131 | .El | ||
| 132 | .Sh SEE ALSO | ||
| 133 | .Xr csh 1 , | ||
| 134 | .Xr sh 1 , | ||
| 135 | .Xr execve 2 , | ||
| 136 | .Xr environ 7 | ||
| 137 | .Sh STANDARDS | ||
| 138 | The | ||
| 139 | .Fn getenv | ||
| 140 | function conforms to | ||
| 141 | .St -ansiC . | ||
| 142 | .Sh HISTORY | ||
| 143 | The functions | ||
| 144 | .Fn setenv | ||
| 145 | and | ||
| 146 | .Fn unsetenv | ||
| 147 | appeared in | ||
| 148 | .At v7 . | ||
| 149 | The | ||
| 150 | .Fn putenv | ||
| 151 | function appeared in | ||
| 152 | .Bx 4.3 Reno . | ||
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c new file mode 100644 index 0000000000..09d47f2149 --- /dev/null +++ b/src/lib/libc/stdlib/getenv.c | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | /* | ||
| 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 | |||
| 56 | /* | ||
| 57 | * __findenv -- | ||
| 58 | * Returns pointer to value associated with name, if any, else NULL. | ||
| 59 | * Sets offset to be the offset of the name/value combination in the | ||
| 60 | * environmental array, for use by setenv(3) and unsetenv(3). | ||
| 61 | * Explicitly removes '=' in argument name. | ||
| 62 | * | ||
| 63 | * This routine *should* be a static; don't use it. | ||
| 64 | */ | ||
| 65 | char * | ||
| 66 | __findenv(name, offset) | ||
| 67 | register char *name; | ||
| 68 | int *offset; | ||
| 69 | { | ||
| 70 | extern char **environ; | ||
| 71 | register int len; | ||
| 72 | register char **P, *C; | ||
| 73 | |||
| 74 | for (C = name, len = 0; *C && *C != '='; ++C, ++len); | ||
| 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 | } | ||
diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 new file mode 100644 index 0000000000..f843881afd --- /dev/null +++ b/src/lib/libc/stdlib/getopt.3 | |||
| @@ -0,0 +1,260 @@ | |||
| 1 | .\" Copyright (c) 1988, 1991, 1993 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. 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 | .\" @(#)getopt.3 8.4 (Berkeley) 4/19/94 | ||
| 33 | .\" | ||
| 34 | .Dd April 19, 1994 | ||
| 35 | .Dt GETOPT 3 | ||
| 36 | .Os BSD 4.3 | ||
| 37 | .Sh NAME | ||
| 38 | .Nm getopt | ||
| 39 | .Nd get option character from command line argument list | ||
| 40 | .Sh SYNOPSIS | ||
| 41 | .Fd #include <unistd.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 "int argc" "char * const *argv" "const char *optstring" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn getopt | ||
| 52 | function incrementally parses a command line argument list | ||
| 53 | .Fa argv | ||
| 54 | and returns the next | ||
| 55 | .Em known | ||
| 56 | option character. | ||
| 57 | An option character is | ||
| 58 | .Em known | ||
| 59 | if it has been specified in the string of accepted option characters, | ||
| 60 | .Fa optstring . | ||
| 61 | .Pp | ||
| 62 | The option string | ||
| 63 | .Fa optstring | ||
| 64 | may contain the following elements: individual characters, and | ||
| 65 | characters followed by a colon to indicate an option argument | ||
| 66 | is to follow. | ||
| 67 | For example, an option string | ||
| 68 | .Li "\&""x"" | ||
| 69 | recognizes an option | ||
| 70 | .Dq Fl x , | ||
| 71 | and an option string | ||
| 72 | .Li "\&""x:"" | ||
| 73 | recognizes an option and argument | ||
| 74 | .Dq Fl x Ar argument . | ||
| 75 | It does not matter to | ||
| 76 | .Fn getopt | ||
| 77 | if a following argument has leading white space. | ||
| 78 | .Pp | ||
| 79 | On return from | ||
| 80 | .Fn getopt , | ||
| 81 | .Va optarg | ||
| 82 | points to an option argument, if it is anticipated, | ||
| 83 | and the variable | ||
| 84 | .Va optind | ||
| 85 | contains the index to the next | ||
| 86 | .Fa argv | ||
| 87 | argument for a subsequent call | ||
| 88 | to | ||
| 89 | .Fn getopt . | ||
| 90 | The variable | ||
| 91 | .Va optopt | ||
| 92 | saves the last | ||
| 93 | .Em known | ||
| 94 | option character returned by | ||
| 95 | .Fn getopt . | ||
| 96 | .Pp | ||
| 97 | The variable | ||
| 98 | .Va opterr | ||
| 99 | and | ||
| 100 | .Va optind | ||
| 101 | are both initialized to 1. | ||
| 102 | The | ||
| 103 | .Va optind | ||
| 104 | variable may be set to another value before a set of calls to | ||
| 105 | .Fn getopt | ||
| 106 | in order to skip over more or less argv entries. | ||
| 107 | .Pp | ||
| 108 | In order to use | ||
| 109 | .Fn getopt | ||
| 110 | to evaluate multiple sets of arguments, or to evaluate a single set of | ||
| 111 | arguments multiple times, | ||
| 112 | the variable | ||
| 113 | .Va optreset | ||
| 114 | must be set to 1 before the second and each additional set of calls to | ||
| 115 | .Fn getopt , | ||
| 116 | and the variable | ||
| 117 | .Va optind | ||
| 118 | must be reinitialized. | ||
| 119 | .Pp | ||
| 120 | The | ||
| 121 | .Fn getopt | ||
| 122 | function | ||
| 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 | ||
| 127 | by the option | ||
| 128 | .Ql -- | ||
| 129 | (double dash) which causes | ||
| 130 | .Fn getopt | ||
| 131 | to signal the end of argument processing and returns \-1. | ||
| 132 | When all options have been processed (i.e., up to the first non-option | ||
| 133 | argument), | ||
| 134 | .Fn getopt | ||
| 135 | returns \-1. | ||
| 136 | .Sh DIAGNOSTICS | ||
| 137 | If the | ||
| 138 | .Fn getopt | ||
| 139 | function encounters a character not found in the string | ||
| 140 | .Va optarg | ||
| 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 | ||
| 150 | .Va optstring | ||
| 151 | has a leading | ||
| 152 | .Ql \&: | ||
| 153 | then a missing option argument causes a | ||
| 154 | .Ql \&: | ||
| 155 | to be returned in addition to suppressing any error messages. | ||
| 156 | .Pp | ||
| 157 | Option arguments are allowed to begin with | ||
| 158 | .Dq Li \- ; | ||
| 159 | this is reasonable but | ||
| 160 | reduces the amount of error checking possible. | ||
| 161 | .Sh EXTENSIONS | ||
| 162 | The | ||
| 163 | .Va optreset | ||
| 164 | variable was added to make it possible to call the | ||
| 165 | .Fn getopt | ||
| 166 | function multiple times. | ||
| 167 | This is an extension to the | ||
| 168 | .St -p1003.2 | ||
| 169 | specification. | ||
| 170 | .Sh EXAMPLE | ||
| 171 | .Bd -literal -compact | ||
| 172 | extern char *optarg; | ||
| 173 | extern int optind; | ||
| 174 | int bflag, ch, fd; | ||
| 175 | |||
| 176 | bflag = 0; | ||
| 177 | while ((ch = getopt(argc, argv, "bf:")) != -1) | ||
| 178 | switch(ch) { | ||
| 179 | case 'b': | ||
| 180 | bflag = 1; | ||
| 181 | break; | ||
| 182 | case 'f': | ||
| 183 | if ((fd = open(optarg, O_RDONLY, 0)) < 0) { | ||
| 184 | (void)fprintf(stderr, | ||
| 185 | "myname: %s: %s\en", optarg, strerror(errno)); | ||
| 186 | exit(1); | ||
| 187 | } | ||
| 188 | break; | ||
| 189 | case '?': | ||
| 190 | default: | ||
| 191 | usage(); | ||
| 192 | } | ||
| 193 | argc -= optind; | ||
| 194 | argv += optind; | ||
| 195 | .Ed | ||
| 196 | .Sh HISTORY | ||
| 197 | The | ||
| 198 | .Fn getopt | ||
| 199 | function appeared | ||
| 200 | .Bx 4.3 . | ||
| 201 | .Sh BUGS | ||
| 202 | The | ||
| 203 | .Fn getopt | ||
| 204 | function was once specified to return | ||
| 205 | .Dv EOF | ||
| 206 | instead of \-1. | ||
| 207 | This was changed by | ||
| 208 | .St -p1003.2-92 | ||
| 209 | to decouple | ||
| 210 | .Fn getopt | ||
| 211 | from | ||
| 212 | .Pa <stdio.h> . | ||
| 213 | .Pp | ||
| 214 | A single dash | ||
| 215 | .Dq Li - | ||
| 216 | may be specified as an character in | ||
| 217 | .Fa optstring , | ||
| 218 | however it should | ||
| 219 | .Em never | ||
| 220 | have an argument associated with it. | ||
| 221 | This allows | ||
| 222 | .Fn getopt | ||
| 223 | to be used with programs that expect | ||
| 224 | .Dq Li - | ||
| 225 | as an option flag. | ||
| 226 | This practice is wrong, and should not be used in any current development. | ||
| 227 | It is provided for backward compatibility | ||
| 228 | .Em only . | ||
| 229 | By default, a single dash causes | ||
| 230 | .Fn getopt | ||
| 231 | to return \-1. | ||
| 232 | This is, we believe, compatible with System V. | ||
| 233 | .Pp | ||
| 234 | It is also possible to handle digits as option letters. | ||
| 235 | This allows | ||
| 236 | .Fn getopt | ||
| 237 | to be used with programs that expect a number | ||
| 238 | .Pq Dq Li \&-\&3 | ||
| 239 | as an option. | ||
| 240 | This practice is wrong, and should not be used in any current development. | ||
| 241 | It is provided for backward compatibility | ||
| 242 | .Em only . | ||
| 243 | The following code fragment works in most cases. | ||
| 244 | .Bd -literal -offset indent | ||
| 245 | int length; | ||
| 246 | char *p; | ||
| 247 | |||
| 248 | while ((c = getopt(argc, argv, "0123456789")) != -1) | ||
| 249 | switch (c) { | ||
| 250 | case '0': case '1': case '2': case '3': case '4': | ||
| 251 | case '5': case '6': case '7': case '8': case '9': | ||
| 252 | p = argv[optind - 1]; | ||
| 253 | if (p[0] == '-' && p[1] == ch && !p[2]) | ||
| 254 | length = atoi(++p); | ||
| 255 | else | ||
| 256 | length = atoi(argv[optind] + 1); | ||
| 257 | break; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | .Ed | ||
diff --git a/src/lib/libc/stdlib/getopt.c b/src/lib/libc/stdlib/getopt.c new file mode 100644 index 0000000000..63c5e6a479 --- /dev/null +++ b/src/lib/libc/stdlib/getopt.c | |||
| @@ -0,0 +1,118 @@ | |||
| 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/heapsort.c b/src/lib/libc/stdlib/heapsort.c new file mode 100644 index 0000000000..bd998fa357 --- /dev/null +++ b/src/lib/libc/stdlib/heapsort.c | |||
| @@ -0,0 +1,184 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1991, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #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> | ||
| 43 | #include <errno.h> | ||
| 44 | #include <stdlib.h> | ||
| 45 | |||
| 46 | /* | ||
| 47 | * Swap two areas of size number of bytes. Although qsort(3) permits random | ||
| 48 | * blocks of memory to be sorted, sorting pointers is almost certainly the | ||
| 49 | * common case (and, were it not, could easily be made so). Regardless, it | ||
| 50 | * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer | ||
| 51 | * arithmetic gets lost in the time required for comparison function calls. | ||
| 52 | */ | ||
| 53 | #define SWAP(a, b, count, size, tmp) { \ | ||
| 54 | count = size; \ | ||
| 55 | do { \ | ||
| 56 | tmp = *a; \ | ||
| 57 | *a++ = *b; \ | ||
| 58 | *b++ = tmp; \ | ||
| 59 | } while (--count); \ | ||
| 60 | } | ||
| 61 | |||
| 62 | /* Copy one block of size size to another. */ | ||
| 63 | #define COPY(a, b, count, size, tmp1, tmp2) { \ | ||
| 64 | count = size; \ | ||
| 65 | tmp1 = a; \ | ||
| 66 | tmp2 = b; \ | ||
| 67 | do { \ | ||
| 68 | *tmp1++ = *tmp2++; \ | ||
| 69 | } while (--count); \ | ||
| 70 | } | ||
| 71 | |||
| 72 | /* | ||
| 73 | * 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. | ||
| 75 | * | ||
| 76 | * There two cases. If j == nmemb, select largest of Ki and Kj. If | ||
| 77 | * j < nmemb, select largest of Ki, Kj and Kj+1. | ||
| 78 | */ | ||
| 79 | #define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ | ||
| 80 | for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ | ||
| 81 | par_i = child_i) { \ | ||
| 82 | child = base + child_i * size; \ | ||
| 83 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
| 84 | child += size; \ | ||
| 85 | ++child_i; \ | ||
| 86 | } \ | ||
| 87 | par = base + par_i * size; \ | ||
| 88 | if (compar(child, par) <= 0) \ | ||
| 89 | break; \ | ||
| 90 | SWAP(par, child, count, size, tmp); \ | ||
| 91 | } \ | ||
| 92 | } | ||
| 93 | |||
| 94 | /* | ||
| 95 | * 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 | ||
| 97 | * 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 | ||
| 99 | * heapify, always maintaining the invariant that the larger child is copied | ||
| 100 | * over its parent's record. | ||
| 101 | * | ||
| 102 | * 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 | ||
| 104 | * is 'lost' when k is assigned its correct place in the heap. | ||
| 105 | * | ||
| 106 | * The time savings from this optimization are on the order of 15-20% for the | ||
| 107 | * average case. See Knuth, Vol. 3, page 158, problem 18. | ||
| 108 | * | ||
| 109 | * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset. | ||
| 110 | */ | ||
| 111 | #define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \ | ||
| 112 | for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \ | ||
| 113 | child = base + child_i * size; \ | ||
| 114 | if (child_i < nmemb && compar(child, child + size) < 0) { \ | ||
| 115 | child += size; \ | ||
| 116 | ++child_i; \ | ||
| 117 | } \ | ||
| 118 | par = base + par_i * size; \ | ||
| 119 | COPY(par, child, count, size, tmp1, tmp2); \ | ||
| 120 | } \ | ||
| 121 | for (;;) { \ | ||
| 122 | child_i = par_i; \ | ||
| 123 | par_i = child_i / 2; \ | ||
| 124 | child = base + child_i * size; \ | ||
| 125 | par = base + par_i * size; \ | ||
| 126 | if (child_i == 1 || compar(k, par) < 0) { \ | ||
| 127 | COPY(child, k, count, size, tmp1, tmp2); \ | ||
| 128 | break; \ | ||
| 129 | } \ | ||
| 130 | COPY(child, par, count, size, tmp1, tmp2); \ | ||
| 131 | } \ | ||
| 132 | } | ||
| 133 | |||
| 134 | /* | ||
| 135 | * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average | ||
| 136 | * and worst. While heapsort is faster than the worst case of quicksort, | ||
| 137 | * the BSD quicksort does median selection so that the chance of finding | ||
| 138 | * a data set that will trigger the worst case is nonexistent. Heapsort's | ||
| 139 | * only advantage over quicksort is that it requires little additional memory. | ||
| 140 | */ | ||
| 141 | int | ||
| 142 | heapsort(vbase, nmemb, size, compar) | ||
| 143 | void *vbase; | ||
| 144 | size_t nmemb, size; | ||
| 145 | int (*compar) __P((const void *, const void *)); | ||
| 146 | { | ||
| 147 | register int cnt, i, j, l; | ||
| 148 | register char tmp, *tmp1, *tmp2; | ||
| 149 | char *base, *k, *p, *t; | ||
| 150 | |||
| 151 | if (nmemb <= 1) | ||
| 152 | return (0); | ||
| 153 | |||
| 154 | if (!size) { | ||
| 155 | errno = EINVAL; | ||
| 156 | return (-1); | ||
| 157 | } | ||
| 158 | |||
| 159 | if ((k = malloc(size)) == NULL) | ||
| 160 | return (-1); | ||
| 161 | |||
| 162 | /* | ||
| 163 | * Items are numbered from 1 to nmemb, so offset from size bytes | ||
| 164 | * below the starting address. | ||
| 165 | */ | ||
| 166 | base = (char *)vbase - size; | ||
| 167 | |||
| 168 | for (l = nmemb / 2 + 1; --l;) | ||
| 169 | CREATE(l, nmemb, i, j, t, p, size, cnt, tmp); | ||
| 170 | |||
| 171 | /* | ||
| 172 | * For each element of the heap, save the largest element into its | ||
| 173 | * final slot, save the displaced element (k), then recreate the | ||
| 174 | * heap. | ||
| 175 | */ | ||
| 176 | while (nmemb > 1) { | ||
| 177 | COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2); | ||
| 178 | COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2); | ||
| 179 | --nmemb; | ||
| 180 | SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2); | ||
| 181 | } | ||
| 182 | free(k); | ||
| 183 | return (0); | ||
| 184 | } | ||
diff --git a/src/lib/libc/stdlib/jrand48.c b/src/lib/libc/stdlib/jrand48.c new file mode 100644 index 0000000000..205781e0ee --- /dev/null +++ b/src/lib/libc/stdlib/jrand48.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | long | ||
| 17 | jrand48(unsigned short xseed[3]) | ||
| 18 | { | ||
| 19 | __dorand48(xseed); | ||
| 20 | return ((long) xseed[2] << 16) + (long) xseed[1]; | ||
| 21 | } | ||
diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c new file mode 100644 index 0000000000..3069b31bf6 --- /dev/null +++ b/src/lib/libc/stdlib/l64a.c | |||
| @@ -0,0 +1,43 @@ | |||
| 1 | /* | ||
| 2 | * Written by J.T. Conklin <jtc@netbsd.org>. | ||
| 3 | * Public domain. | ||
| 4 | */ | ||
| 5 | |||
| 6 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 7 | static char *rcsid = "$NetBSD: l64a.c,v 1.4 1995/05/11 23:04:52 jtc Exp $"; | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #include <stdlib.h> | ||
| 11 | |||
| 12 | char * | ||
| 13 | l64a (value) | ||
| 14 | long value; | ||
| 15 | { | ||
| 16 | static char buf[8]; | ||
| 17 | char *s = buf; | ||
| 18 | int digit; | ||
| 19 | int i; | ||
| 20 | |||
| 21 | if (!value) | ||
| 22 | return NULL; | ||
| 23 | |||
| 24 | for (i = 0; value != 0 && i < 6; i++) { | ||
| 25 | digit = value & 0x3f; | ||
| 26 | |||
| 27 | if (digit < 2) | ||
| 28 | *s = digit + '.'; | ||
| 29 | else if (digit < 12) | ||
| 30 | *s = digit + '0' - 2; | ||
| 31 | else if (digit < 38) | ||
| 32 | *s = digit + 'A' - 12; | ||
| 33 | else | ||
| 34 | *s = digit + 'a' - 38; | ||
| 35 | |||
| 36 | value >>= 6; | ||
| 37 | s++; | ||
| 38 | } | ||
| 39 | |||
| 40 | *s = '\0'; | ||
| 41 | |||
| 42 | return buf; | ||
| 43 | } | ||
diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3 new file mode 100644 index 0000000000..28e4d2053c --- /dev/null +++ b/src/lib/libc/stdlib/labs.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 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: labs.3,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt LABS 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm labs | ||
| 44 | .Nd return the absolute value of a long integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft long | ||
| 48 | .Fn labs "long j" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn labs | ||
| 52 | function | ||
| 53 | returns the absolute value of the long integer | ||
| 54 | .Ar j . | ||
| 55 | .Sh SEE ALSO | ||
| 56 | .Xr abs 3 , | ||
| 57 | .Xr floor 3 , | ||
| 58 | .Xr cabs 3 , | ||
| 59 | .Xr math 3 | ||
| 60 | .Sh STANDARDS | ||
| 61 | The | ||
| 62 | .Fn labs | ||
| 63 | function | ||
| 64 | conforms to | ||
| 65 | .St -ansiC . | ||
| 66 | .Sh BUGS | ||
| 67 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/labs.c b/src/lib/libc/stdlib/labs.c new file mode 100644 index 0000000000..ccf1415792 --- /dev/null +++ b/src/lib/libc/stdlib/labs.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | long | ||
| 42 | labs(j) | ||
| 43 | long j; | ||
| 44 | { | ||
| 45 | return(j < 0 ? -j : j); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c new file mode 100644 index 0000000000..965d46b17a --- /dev/null +++ b/src/lib/libc/stdlib/lcong48.c | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | extern unsigned short __rand48_mult[3]; | ||
| 18 | extern unsigned short __rand48_add; | ||
| 19 | |||
| 20 | void | ||
| 21 | lcong48(unsigned short p[7]) | ||
| 22 | { | ||
| 23 | __rand48_seed[0] = p[0]; | ||
| 24 | __rand48_seed[1] = p[1]; | ||
| 25 | __rand48_seed[2] = p[2]; | ||
| 26 | __rand48_mult[0] = p[3]; | ||
| 27 | __rand48_mult[1] = p[4]; | ||
| 28 | __rand48_mult[2] = p[5]; | ||
| 29 | __rand48_add = p[6]; | ||
| 30 | } | ||
diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3 new file mode 100644 index 0000000000..a7b5ccf878 --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.3 | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek and the American National Standards Committee X3, | ||
| 6 | .\" on Information Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)ldiv.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: ldiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt LDIV 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm ldiv | ||
| 44 | .Nd return quotient and remainder from division | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft ldiv_t | ||
| 48 | .Fn ldiv "long num" "long denom" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn ldiv | ||
| 52 | function | ||
| 53 | computes the value | ||
| 54 | .Ar num/denom | ||
| 55 | and returns the quotient and remainder in a structure named | ||
| 56 | .Ar ldiv_t | ||
| 57 | that contains two | ||
| 58 | .Em long integer | ||
| 59 | members named | ||
| 60 | .Ar quot | ||
| 61 | and | ||
| 62 | .Ar rem . | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr div 3 , | ||
| 65 | .Xr qdiv 3 , | ||
| 66 | .Xr math 3 | ||
| 67 | .Sh STANDARDS | ||
| 68 | The | ||
| 69 | .Fn ldiv | ||
| 70 | function | ||
| 71 | conforms to | ||
| 72 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c new file mode 100644 index 0000000000..f7074507e5 --- /dev/null +++ b/src/lib/libc/stdlib/ldiv.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #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 */ | ||
| 43 | |||
| 44 | ldiv_t | ||
| 45 | ldiv(num, denom) | ||
| 46 | long num, denom; | ||
| 47 | { | ||
| 48 | ldiv_t r; | ||
| 49 | |||
| 50 | /* see div.c for comments */ | ||
| 51 | |||
| 52 | r.quot = num / denom; | ||
| 53 | r.rem = num % denom; | ||
| 54 | if (num >= 0 && r.rem < 0) { | ||
| 55 | r.quot++; | ||
| 56 | r.rem -= denom; | ||
| 57 | } | ||
| 58 | return (r); | ||
| 59 | } | ||
diff --git a/src/lib/libc/stdlib/lrand48.c b/src/lib/libc/stdlib/lrand48.c new file mode 100644 index 0000000000..8e7f26237f --- /dev/null +++ b/src/lib/libc/stdlib/lrand48.c | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | |||
| 18 | long | ||
| 19 | lrand48(void) | ||
| 20 | { | ||
| 21 | __dorand48(__rand48_seed); | ||
| 22 | return ((long) __rand48_seed[2] << 15) + ((long) __rand48_seed[1] >> 1); | ||
| 23 | } | ||
diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 new file mode 100644 index 0000000000..3bbf2bf65e --- /dev/null +++ b/src/lib/libc/stdlib/malloc.3 | |||
| @@ -0,0 +1,91 @@ | |||
| 1 | .\" Copyright (c) 1980, 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)malloc.3 6.7 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: malloc.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt MALLOC 3 | ||
| 41 | .Os BSD 4 | ||
| 42 | .Sh NAME | ||
| 43 | .Nm malloc | ||
| 44 | .Nd general memory allocation function | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn malloc "size_t size" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn malloc | ||
| 52 | function allocates uninitialized space for an object whose | ||
| 53 | size is specified by | ||
| 54 | .Fa size . | ||
| 55 | The | ||
| 56 | .Fn malloc | ||
| 57 | function maintains multiple lists of free blocks according to size, allocating | ||
| 58 | space from the appropriate list. | ||
| 59 | .Pp | ||
| 60 | The allocated space is | ||
| 61 | suitably aligned (after possible pointer | ||
| 62 | coercion) for storage of any type of object. If the space is of | ||
| 63 | .Em pagesize | ||
| 64 | or larger, the memory returned will be page-aligned. | ||
| 65 | .Sh RETURN VALUES | ||
| 66 | The | ||
| 67 | .Fn malloc | ||
| 68 | function returns | ||
| 69 | a pointer to the allocated space if successful; otherwise | ||
| 70 | a null pointer is returned. | ||
| 71 | .Sh SEE ALSO | ||
| 72 | .Xr brk 2 , | ||
| 73 | .Xr getpagesize 2 , | ||
| 74 | .Xr free 3 , | ||
| 75 | .Xr calloc 3 , | ||
| 76 | .Xr alloca 3 , | ||
| 77 | .Xr realloc 3 , | ||
| 78 | .Xr memory 3 | ||
| 79 | .Sh STANDARDS | ||
| 80 | The | ||
| 81 | .Fn malloc | ||
| 82 | function conforms to | ||
| 83 | .St -ansiC . | ||
| 84 | .Sh BUGS | ||
| 85 | The current implementation of | ||
| 86 | .Xr malloc | ||
| 87 | does not always fail gracefully when system | ||
| 88 | memory limits are approached. | ||
| 89 | It may fail to allocate memory when larger free blocks could be broken | ||
| 90 | up, or when limits are exceeded because the size is rounded up. | ||
| 91 | It is optimized for sizes that are powers of two. | ||
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c new file mode 100644 index 0000000000..3c57fad024 --- /dev/null +++ b/src/lib/libc/stdlib/malloc.c | |||
| @@ -0,0 +1,421 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1983 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)malloc.c 5.11 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: malloc.c,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | /* | ||
| 40 | * malloc.c (Caltech) 2/21/82 | ||
| 41 | * Chris Kingsley, kingsley@cit-20. | ||
| 42 | * | ||
| 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 | */ | ||
| 49 | |||
| 50 | #include <sys/types.h> | ||
| 51 | #include <stdlib.h> | ||
| 52 | #include <string.h> | ||
| 53 | #include <unistd.h> | ||
| 54 | |||
| 55 | #define NULL 0 | ||
| 56 | |||
| 57 | static void morecore(); | ||
| 58 | static int findbucket(); | ||
| 59 | |||
| 60 | /* | ||
| 61 | * The overhead on a block is at least 4 bytes. When free, this space | ||
| 62 | * contains a pointer to the next free block, and the bottom two bits must | ||
| 63 | * be zero. When in use, the first byte is set to MAGIC, and the second | ||
| 64 | * byte is the size index. The remaining bytes are for alignment. | ||
| 65 | * If range checking is enabled then a second word holds the size of the | ||
| 66 | * requested block, less 1, rounded up to a multiple of sizeof(RMAGIC). | ||
| 67 | * The order of elements is critical: ov_magic must overlay the low order | ||
| 68 | * bits of ov_next, and ov_magic can not be a valid ov_next bit pattern. | ||
| 69 | */ | ||
| 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 | |||
| 86 | #define MAGIC 0xef /* magic # on accounting info */ | ||
| 87 | #define RMAGIC 0x5555 /* magic # on range info */ | ||
| 88 | |||
| 89 | #ifdef RCHECK | ||
| 90 | #define RSLOP sizeof (u_short) | ||
| 91 | #else | ||
| 92 | #define RSLOP 0 | ||
| 93 | #endif | ||
| 94 | |||
| 95 | /* | ||
| 96 | * nextf[i] is the pointer to the next free block of size 2^(i+3). The | ||
| 97 | * smallest allocatable block is 8 bytes. The overhead information | ||
| 98 | * precedes the data area returned to the user. | ||
| 99 | */ | ||
| 100 | #define NBUCKETS 30 | ||
| 101 | static union overhead *nextf[NBUCKETS]; | ||
| 102 | extern char *sbrk(); | ||
| 103 | |||
| 104 | static int pagesz; /* page size */ | ||
| 105 | static int pagebucket; /* page size bucket */ | ||
| 106 | |||
| 107 | #ifdef MSTATS | ||
| 108 | /* | ||
| 109 | * nmalloc[i] is the difference between the number of mallocs and frees | ||
| 110 | * for a given block size. | ||
| 111 | */ | ||
| 112 | static u_int nmalloc[NBUCKETS]; | ||
| 113 | #include <stdio.h> | ||
| 114 | #endif | ||
| 115 | |||
| 116 | #if defined(DEBUG) || defined(RCHECK) | ||
| 117 | #define ASSERT(p) if (!(p)) botch("p") | ||
| 118 | #include <stdio.h> | ||
| 119 | static | ||
| 120 | botch(s) | ||
| 121 | char *s; | ||
| 122 | { | ||
| 123 | fprintf(stderr, "\r\nassertion botched: %s\r\n", s); | ||
| 124 | (void) fflush(stderr); /* just in case user buffered it */ | ||
| 125 | abort(); | ||
| 126 | } | ||
| 127 | #else | ||
| 128 | #define ASSERT(p) | ||
| 129 | #endif | ||
| 130 | |||
| 131 | void * | ||
| 132 | malloc(nbytes) | ||
| 133 | size_t nbytes; | ||
| 134 | { | ||
| 135 | register union overhead *op; | ||
| 136 | register long bucket, n; | ||
| 137 | register unsigned amt; | ||
| 138 | |||
| 139 | /* | ||
| 140 | * First time malloc is called, setup page size and | ||
| 141 | * align break pointer so all data will be page aligned. | ||
| 142 | */ | ||
| 143 | if (pagesz == 0) { | ||
| 144 | pagesz = n = getpagesize(); | ||
| 145 | op = (union overhead *)sbrk(0); | ||
| 146 | n = n - sizeof (*op) - ((long)op & (n - 1)); | ||
| 147 | if (n < 0) | ||
| 148 | n += pagesz; | ||
| 149 | if (n) { | ||
| 150 | if (sbrk(n) == (char *)-1) | ||
| 151 | return (NULL); | ||
| 152 | } | ||
| 153 | bucket = 0; | ||
| 154 | amt = 8; | ||
| 155 | while (pagesz > amt) { | ||
| 156 | amt <<= 1; | ||
| 157 | bucket++; | ||
| 158 | } | ||
| 159 | pagebucket = bucket; | ||
| 160 | } | ||
| 161 | /* | ||
| 162 | * Convert amount of memory requested into closest block size | ||
| 163 | * stored in hash buckets which satisfies request. | ||
| 164 | * Account for space used per block for accounting. | ||
| 165 | */ | ||
| 166 | if (nbytes <= (n = pagesz - sizeof (*op) - RSLOP)) { | ||
| 167 | #ifndef RCHECK | ||
| 168 | amt = 8; /* size of first bucket */ | ||
| 169 | bucket = 0; | ||
| 170 | #else | ||
| 171 | amt = 16; /* size of first bucket */ | ||
| 172 | bucket = 1; | ||
| 173 | #endif | ||
| 174 | n = -((long)sizeof (*op) + RSLOP); | ||
| 175 | } else { | ||
| 176 | amt = pagesz; | ||
| 177 | bucket = pagebucket; | ||
| 178 | } | ||
| 179 | while (nbytes > amt + n) { | ||
| 180 | amt <<= 1; | ||
| 181 | if (amt == 0) | ||
| 182 | return (NULL); | ||
| 183 | bucket++; | ||
| 184 | } | ||
| 185 | /* | ||
| 186 | * If nothing in hash bucket right now, | ||
| 187 | * request more memory from the system. | ||
| 188 | */ | ||
| 189 | if ((op = nextf[bucket]) == NULL) { | ||
| 190 | morecore(bucket); | ||
| 191 | if ((op = nextf[bucket]) == NULL) | ||
| 192 | return (NULL); | ||
| 193 | } | ||
| 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 | } | ||
| 212 | |||
| 213 | /* | ||
| 214 | * Allocate more memory to the indicated bucket. | ||
| 215 | */ | ||
| 216 | static void | ||
| 217 | morecore(bucket) | ||
| 218 | int bucket; | ||
| 219 | { | ||
| 220 | register union overhead *op; | ||
| 221 | register long sz; /* size of desired block */ | ||
| 222 | long amt; /* amount to allocate */ | ||
| 223 | int nblks; /* how many blocks we get */ | ||
| 224 | |||
| 225 | /* | ||
| 226 | * sbrk_size <= 0 only for big, FLUFFY, requests (about | ||
| 227 | * 2^30 bytes on a VAX, I think) or for a negative arg. | ||
| 228 | */ | ||
| 229 | sz = 1 << (bucket + 3); | ||
| 230 | #ifdef DEBUG | ||
| 231 | ASSERT(sz > 0); | ||
| 232 | #else | ||
| 233 | if (sz <= 0) | ||
| 234 | return; | ||
| 235 | #endif | ||
| 236 | if (sz < pagesz) { | ||
| 237 | amt = pagesz; | ||
| 238 | nblks = amt / sz; | ||
| 239 | } else { | ||
| 240 | amt = sz + pagesz; | ||
| 241 | nblks = 1; | ||
| 242 | } | ||
| 243 | op = (union overhead *)sbrk(amt); | ||
| 244 | /* no more room! */ | ||
| 245 | if ((long)op == -1) | ||
| 246 | return; | ||
| 247 | /* | ||
| 248 | * Add new memory allocated to that on | ||
| 249 | * free list for this hash bucket. | ||
| 250 | */ | ||
| 251 | nextf[bucket] = op; | ||
| 252 | while (--nblks > 0) { | ||
| 253 | op->ov_next = (union overhead *)((caddr_t)op + sz); | ||
| 254 | op = (union overhead *)((caddr_t)op + sz); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | void | ||
| 259 | free(cp) | ||
| 260 | void *cp; | ||
| 261 | { | ||
| 262 | register long size; | ||
| 263 | register union overhead *op; | ||
| 264 | |||
| 265 | if (cp == NULL) | ||
| 266 | return; | ||
| 267 | op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | ||
| 268 | #ifdef DEBUG | ||
| 269 | ASSERT(op->ov_magic == MAGIC); /* make sure it was in use */ | ||
| 270 | #else | ||
| 271 | if (op->ov_magic != MAGIC) | ||
| 272 | return; /* sanity */ | ||
| 273 | #endif | ||
| 274 | #ifdef RCHECK | ||
| 275 | ASSERT(op->ov_rmagic == RMAGIC); | ||
| 276 | ASSERT(*(u_short *)((caddr_t)(op + 1) + op->ov_size) == RMAGIC); | ||
| 277 | #endif | ||
| 278 | size = op->ov_index; | ||
| 279 | ASSERT(size < NBUCKETS); | ||
| 280 | op->ov_next = nextf[size]; /* also clobbers ov_magic */ | ||
| 281 | nextf[size] = op; | ||
| 282 | #ifdef MSTATS | ||
| 283 | nmalloc[size]--; | ||
| 284 | #endif | ||
| 285 | } | ||
| 286 | |||
| 287 | /* | ||
| 288 | * When a program attempts "storage compaction" as mentioned in the | ||
| 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 | */ | ||
| 298 | int realloc_srchlen = 4; /* 4 should be plenty, -1 =>'s whole list */ | ||
| 299 | |||
| 300 | void * | ||
| 301 | realloc(cp, nbytes) | ||
| 302 | void *cp; | ||
| 303 | size_t nbytes; | ||
| 304 | { | ||
| 305 | register u_long onb; | ||
| 306 | register long i; | ||
| 307 | union overhead *op; | ||
| 308 | char *res; | ||
| 309 | int was_alloced = 0; | ||
| 310 | |||
| 311 | if (cp == NULL) | ||
| 312 | return (malloc(nbytes)); | ||
| 313 | op = (union overhead *)((caddr_t)cp - sizeof (union overhead)); | ||
| 314 | if (op->ov_magic == MAGIC) { | ||
| 315 | was_alloced++; | ||
| 316 | i = op->ov_index; | ||
| 317 | } else { | ||
| 318 | /* | ||
| 319 | * Already free, doing "compaction". | ||
| 320 | * | ||
| 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 | */ | ||
| 332 | if ((i = findbucket(op, 1)) < 0 && | ||
| 333 | (i = findbucket(op, realloc_srchlen)) < 0) | ||
| 334 | i = NBUCKETS; | ||
| 335 | } | ||
| 336 | onb = 1 << (i + 3); | ||
| 337 | if (onb < pagesz) | ||
| 338 | onb -= sizeof (*op) + RSLOP; | ||
| 339 | else | ||
| 340 | onb += pagesz - sizeof (*op) - RSLOP; | ||
| 341 | /* avoid the copy if same size block */ | ||
| 342 | if (was_alloced) { | ||
| 343 | if (i) { | ||
| 344 | i = 1 << (i + 2); | ||
| 345 | if (i < pagesz) | ||
| 346 | i -= sizeof (*op) + RSLOP; | ||
| 347 | else | ||
| 348 | i += pagesz - sizeof (*op) - RSLOP; | ||
| 349 | } | ||
| 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 | } | ||
| 359 | if ((res = malloc(nbytes)) == NULL) | ||
| 360 | return (NULL); | ||
| 361 | if (cp != res) /* common optimization if "compacting" */ | ||
| 362 | bcopy(cp, res, (nbytes < onb) ? nbytes : onb); | ||
| 363 | return (res); | ||
| 364 | } | ||
| 365 | |||
| 366 | /* | ||
| 367 | * Search ``srchlen'' elements of each free list for a block whose | ||
| 368 | * header starts at ``freep''. If srchlen is -1 search the whole list. | ||
| 369 | * Return bucket number, or -1 if not found. | ||
| 370 | */ | ||
| 371 | static | ||
| 372 | findbucket(freep, srchlen) | ||
| 373 | union overhead *freep; | ||
| 374 | int srchlen; | ||
| 375 | { | ||
| 376 | register union overhead *p; | ||
| 377 | register int i, j; | ||
| 378 | |||
| 379 | for (i = 0; i < NBUCKETS; i++) { | ||
| 380 | j = 0; | ||
| 381 | for (p = nextf[i]; p && j != srchlen; p = p->ov_next) { | ||
| 382 | if (p == freep) | ||
| 383 | return (i); | ||
| 384 | j++; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | return (-1); | ||
| 388 | } | ||
| 389 | |||
| 390 | #ifdef MSTATS | ||
| 391 | /* | ||
| 392 | * mstats - print out statistics about malloc | ||
| 393 | * | ||
| 394 | * Prints two lines of numbers, one showing the length of the free list | ||
| 395 | * for each size category, the second showing the number of mallocs - | ||
| 396 | * frees for each size category. | ||
| 397 | */ | ||
| 398 | mstats(s) | ||
| 399 | char *s; | ||
| 400 | { | ||
| 401 | register int i, j; | ||
| 402 | register union overhead *p; | ||
| 403 | int totfree = 0, | ||
| 404 | totused = 0; | ||
| 405 | |||
| 406 | fprintf(stderr, "Memory allocation statistics %s\nfree:\t", s); | ||
| 407 | for (i = 0; i < NBUCKETS; i++) { | ||
| 408 | for (j = 0, p = nextf[i]; p; p = p->ov_next, j++) | ||
| 409 | ; | ||
| 410 | fprintf(stderr, " %d", j); | ||
| 411 | totfree += j * (1 << (i + 3)); | ||
| 412 | } | ||
| 413 | fprintf(stderr, "\nused:\t"); | ||
| 414 | for (i = 0; i < NBUCKETS; i++) { | ||
| 415 | fprintf(stderr, " %d", nmalloc[i]); | ||
| 416 | totused += nmalloc[i] * (1 << (i + 3)); | ||
| 417 | } | ||
| 418 | fprintf(stderr, "\n\tTotal in use: %d, total free: %d\n", | ||
| 419 | totused, totfree); | ||
| 420 | } | ||
| 421 | #endif | ||
diff --git a/src/lib/libc/stdlib/memory.3 b/src/lib/libc/stdlib/memory.3 new file mode 100644 index 0000000000..735252c837 --- /dev/null +++ b/src/lib/libc/stdlib/memory.3 | |||
| @@ -0,0 +1,71 @@ | |||
| 1 | .\" Copyright (c) 1991 Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. 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: @(#)memory.3 5.1 (Berkeley) 5/2/91 | ||
| 33 | .\" $Id: memory.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd May 2, 1991 | ||
| 36 | .Dt MEMORY 3 | ||
| 37 | .Os BSD 4 | ||
| 38 | .Sh NAME | ||
| 39 | .Nm malloc , | ||
| 40 | .Nm free , | ||
| 41 | .Nm realloc , | ||
| 42 | .Nm calloc , | ||
| 43 | .Nm alloca | ||
| 44 | .Nd general memory allocation operations | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void * | ||
| 48 | .Fn malloc "size_t size" | ||
| 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 | ||
| 58 | These functions allocate and free memory for the calling process. | ||
| 59 | They are described in the | ||
| 60 | individual manual pages. | ||
| 61 | .Sh SEE ALSO | ||
| 62 | .Xr calloc 3 , | ||
| 63 | .Xr free 3 , | ||
| 64 | .Xr malloc 3 , | ||
| 65 | .Xr realloc 3 , | ||
| 66 | .Xr alloca 3 , | ||
| 67 | .Sh STANDARDS | ||
| 68 | These functions, with the exception of | ||
| 69 | .Fn alloca | ||
| 70 | conform to | ||
| 71 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c new file mode 100644 index 0000000000..381fdc0830 --- /dev/null +++ b/src/lib/libc/stdlib/merge.c | |||
| @@ -0,0 +1,348 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Peter McIlroy. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #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 | /* | ||
| 43 | * Hybrid exponential search/linear search merge sort with hybrid | ||
| 44 | * natural/pairwise first pass. Requires about .3% more comparisons | ||
| 45 | * for random data than LSMS with pairwise first pass alone. | ||
| 46 | * It works for objects as small as two bytes. | ||
| 47 | */ | ||
| 48 | |||
| 49 | #define NATURAL | ||
| 50 | #define THRESHOLD 16 /* Best choice for natural merge cut-off. */ | ||
| 51 | |||
| 52 | /* #define NATURAL to get hybrid natural merge. | ||
| 53 | * (The default is pairwise merging.) | ||
| 54 | */ | ||
| 55 | |||
| 56 | #include <sys/types.h> | ||
| 57 | |||
| 58 | #include <errno.h> | ||
| 59 | #include <stdlib.h> | ||
| 60 | #include <string.h> | ||
| 61 | |||
| 62 | static void setup __P((u_char *, u_char *, size_t, size_t, int (*)())); | ||
| 63 | static void insertionsort __P((u_char *, size_t, size_t, int (*)())); | ||
| 64 | |||
| 65 | #define ISIZE sizeof(int) | ||
| 66 | #define PSIZE sizeof(u_char *) | ||
| 67 | #define ICOPY_LIST(src, dst, last) \ | ||
| 68 | do \ | ||
| 69 | *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ | ||
| 70 | while(src < last) | ||
| 71 | #define ICOPY_ELT(src, dst, i) \ | ||
| 72 | do \ | ||
| 73 | *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ | ||
| 74 | while (i -= ISIZE) | ||
| 75 | |||
| 76 | #define CCOPY_LIST(src, dst, last) \ | ||
| 77 | do \ | ||
| 78 | *dst++ = *src++; \ | ||
| 79 | while (src < last) | ||
| 80 | #define CCOPY_ELT(src, dst, i) \ | ||
| 81 | do \ | ||
| 82 | *dst++ = *src++; \ | ||
| 83 | while (i -= 1) | ||
| 84 | |||
| 85 | /* | ||
| 86 | * Find the next possible pointer head. (Trickery for forcing an array | ||
| 87 | * to do double duty as a linked list when objects do not align with word | ||
| 88 | * boundaries. | ||
| 89 | */ | ||
| 90 | /* Assumption: PSIZE is a power of 2. */ | ||
| 91 | #define EVAL(p) (u_char **) \ | ||
| 92 | ((u_char *)0 + \ | ||
| 93 | (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1))) | ||
| 94 | |||
| 95 | /* | ||
| 96 | * Arguments are as for qsort. | ||
| 97 | */ | ||
| 98 | int | ||
| 99 | mergesort(base, nmemb, size, cmp) | ||
| 100 | void *base; | ||
| 101 | size_t nmemb; | ||
| 102 | register size_t size; | ||
| 103 | int (*cmp) __P((const void *, const void *)); | ||
| 104 | { | ||
| 105 | register int i, sense; | ||
| 106 | int big, iflag; | ||
| 107 | register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; | ||
| 108 | u_char *list2, *list1, *p2, *p, *last, **p1; | ||
| 109 | |||
| 110 | if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ | ||
| 111 | errno = EINVAL; | ||
| 112 | return (-1); | ||
| 113 | } | ||
| 114 | |||
| 115 | /* | ||
| 116 | * XXX | ||
| 117 | * Stupid subtraction for the Cray. | ||
| 118 | */ | ||
| 119 | iflag = 0; | ||
| 120 | if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) | ||
| 121 | iflag = 1; | ||
| 122 | |||
| 123 | if ((list2 = malloc(nmemb * size + PSIZE)) == NULL) | ||
| 124 | return (-1); | ||
| 125 | |||
| 126 | list1 = base; | ||
| 127 | setup(list1, list2, nmemb, size, cmp); | ||
| 128 | last = list2 + nmemb * size; | ||
| 129 | i = big = 0; | ||
| 130 | while (*EVAL(list2) != last) { | ||
| 131 | l2 = list1; | ||
| 132 | p1 = EVAL(list1); | ||
| 133 | for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { | ||
| 134 | p2 = *EVAL(p2); | ||
| 135 | f1 = l2; | ||
| 136 | f2 = l1 = list1 + (p2 - list2); | ||
| 137 | if (p2 != last) | ||
| 138 | p2 = *EVAL(p2); | ||
| 139 | l2 = list1 + (p2 - list2); | ||
| 140 | while (f1 < l1 && f2 < l2) { | ||
| 141 | if ((*cmp)(f1, f2) <= 0) { | ||
| 142 | q = f2; | ||
| 143 | b = f1, t = l1; | ||
| 144 | sense = -1; | ||
| 145 | } else { | ||
| 146 | q = f1; | ||
| 147 | b = f2, t = l2; | ||
| 148 | sense = 0; | ||
| 149 | } | ||
| 150 | if (!big) { /* here i = 0 */ | ||
| 151 | LINEAR: while ((b += size) < t && cmp(q, b) >sense) | ||
| 152 | if (++i == 6) { | ||
| 153 | big = 1; | ||
| 154 | goto EXPONENTIAL; | ||
| 155 | } | ||
| 156 | } else { | ||
| 157 | EXPONENTIAL: for (i = size; ; i <<= 1) | ||
| 158 | if ((p = (b + i)) >= t) { | ||
| 159 | if ((p = t - size) > b && | ||
| 160 | (*cmp)(q, p) <= sense) | ||
| 161 | t = p; | ||
| 162 | else | ||
| 163 | b = p; | ||
| 164 | break; | ||
| 165 | } else if ((*cmp)(q, p) <= sense) { | ||
| 166 | t = p; | ||
| 167 | if (i == size) | ||
| 168 | big = 0; | ||
| 169 | goto FASTCASE; | ||
| 170 | } else | ||
| 171 | b = p; | ||
| 172 | SLOWCASE: while (t > b+size) { | ||
| 173 | i = (((t - b) / size) >> 1) * size; | ||
| 174 | if ((*cmp)(q, p = b + i) <= sense) | ||
| 175 | t = p; | ||
| 176 | else | ||
| 177 | b = p; | ||
| 178 | } | ||
| 179 | goto COPY; | ||
| 180 | FASTCASE: while (i > size) | ||
| 181 | if ((*cmp)(q, | ||
| 182 | p = b + (i >>= 1)) <= sense) | ||
| 183 | t = p; | ||
| 184 | else | ||
| 185 | b = p; | ||
| 186 | COPY: b = t; | ||
| 187 | } | ||
| 188 | i = size; | ||
| 189 | if (q == f1) { | ||
| 190 | if (iflag) { | ||
| 191 | ICOPY_LIST(f2, tp2, b); | ||
| 192 | ICOPY_ELT(f1, tp2, i); | ||
| 193 | } else { | ||
| 194 | CCOPY_LIST(f2, tp2, b); | ||
| 195 | CCOPY_ELT(f1, tp2, i); | ||
| 196 | } | ||
| 197 | } else { | ||
| 198 | if (iflag) { | ||
| 199 | ICOPY_LIST(f1, tp2, b); | ||
| 200 | ICOPY_ELT(f2, tp2, i); | ||
| 201 | } else { | ||
| 202 | CCOPY_LIST(f1, tp2, b); | ||
| 203 | CCOPY_ELT(f2, tp2, i); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | } | ||
| 207 | if (f2 < l2) { | ||
| 208 | if (iflag) | ||
| 209 | ICOPY_LIST(f2, tp2, l2); | ||
| 210 | else | ||
| 211 | CCOPY_LIST(f2, tp2, l2); | ||
| 212 | } else if (f1 < l1) { | ||
| 213 | if (iflag) | ||
| 214 | ICOPY_LIST(f1, tp2, l1); | ||
| 215 | else | ||
| 216 | CCOPY_LIST(f1, tp2, l1); | ||
| 217 | } | ||
| 218 | *p1 = l2; | ||
| 219 | } | ||
| 220 | tp2 = list1; /* swap list1, list2 */ | ||
| 221 | list1 = list2; | ||
| 222 | list2 = tp2; | ||
| 223 | last = list2 + nmemb*size; | ||
| 224 | } | ||
| 225 | if (base == list2) { | ||
| 226 | memmove(list2, list1, nmemb*size); | ||
| 227 | list2 = list1; | ||
| 228 | } | ||
| 229 | free(list2); | ||
| 230 | return (0); | ||
| 231 | } | ||
| 232 | |||
| 233 | #define swap(a, b) { \ | ||
| 234 | s = b; \ | ||
| 235 | i = size; \ | ||
| 236 | do { \ | ||
| 237 | tmp = *a; *a++ = *s; *s++ = tmp; \ | ||
| 238 | } while (--i); \ | ||
| 239 | a -= size; \ | ||
| 240 | } | ||
| 241 | #define reverse(bot, top) { \ | ||
| 242 | s = top; \ | ||
| 243 | do { \ | ||
| 244 | i = size; \ | ||
| 245 | do { \ | ||
| 246 | tmp = *bot; *bot++ = *s; *s++ = tmp; \ | ||
| 247 | } while (--i); \ | ||
| 248 | s -= size2; \ | ||
| 249 | } while(bot < s); \ | ||
| 250 | } | ||
| 251 | |||
| 252 | /* | ||
| 253 | * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of | ||
| 254 | * increasing order, list2 in a corresponding linked list. Checks for runs | ||
| 255 | * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL | ||
| 256 | * is defined. Otherwise simple pairwise merging is used.) | ||
| 257 | */ | ||
| 258 | void | ||
| 259 | setup(list1, list2, n, size, cmp) | ||
| 260 | size_t n, size; | ||
| 261 | int (*cmp) __P((const void *, const void *)); | ||
| 262 | u_char *list1, *list2; | ||
| 263 | { | ||
| 264 | int i, length, size2, tmp, sense; | ||
| 265 | u_char *f1, *f2, *s, *l2, *last, *p2; | ||
| 266 | |||
| 267 | size2 = size*2; | ||
| 268 | if (n <= 5) { | ||
| 269 | insertionsort(list1, n, size, cmp); | ||
| 270 | *EVAL(list2) = (u_char*) list2 + n*size; | ||
| 271 | return; | ||
| 272 | } | ||
| 273 | /* | ||
| 274 | * Avoid running pointers out of bounds; limit n to evens | ||
| 275 | * for simplicity. | ||
| 276 | */ | ||
| 277 | i = 4 + (n & 1); | ||
| 278 | insertionsort(list1 + (n - i) * size, i, size, cmp); | ||
| 279 | last = list1 + size * (n - i); | ||
| 280 | *EVAL(list2 + (last - list1)) = list2 + n * size; | ||
| 281 | |||
| 282 | #ifdef NATURAL | ||
| 283 | p2 = list2; | ||
| 284 | f1 = list1; | ||
| 285 | sense = (cmp(f1, f1 + size) > 0); | ||
| 286 | for (; f1 < last; sense = !sense) { | ||
| 287 | length = 2; | ||
| 288 | /* Find pairs with same sense. */ | ||
| 289 | for (f2 = f1 + size2; f2 < last; f2 += size2) { | ||
| 290 | if ((cmp(f2, f2+ size) > 0) != sense) | ||
| 291 | break; | ||
| 292 | length += 2; | ||
| 293 | } | ||
| 294 | if (length < THRESHOLD) { /* Pairwise merge */ | ||
| 295 | do { | ||
| 296 | p2 = *EVAL(p2) = f1 + size2 - list1 + list2; | ||
| 297 | if (sense > 0) | ||
| 298 | swap (f1, f1 + size); | ||
| 299 | } while ((f1 += size2) < f2); | ||
| 300 | } else { /* Natural merge */ | ||
| 301 | l2 = f2; | ||
| 302 | for (f2 = f1 + size2; f2 < l2; f2 += size2) { | ||
| 303 | if ((cmp(f2-size, f2) > 0) != sense) { | ||
| 304 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
| 305 | if (sense > 0) | ||
| 306 | reverse(f1, f2-size); | ||
| 307 | f1 = f2; | ||
| 308 | } | ||
| 309 | } | ||
| 310 | if (sense > 0) | ||
| 311 | reverse (f1, f2-size); | ||
| 312 | f1 = f2; | ||
| 313 | if (f2 < last || cmp(f2 - size, f2) > 0) | ||
| 314 | p2 = *EVAL(p2) = f2 - list1 + list2; | ||
| 315 | else | ||
| 316 | p2 = *EVAL(p2) = list2 + n*size; | ||
| 317 | } | ||
| 318 | } | ||
| 319 | #else /* pairwise merge only. */ | ||
| 320 | for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { | ||
| 321 | p2 = *EVAL(p2) = p2 + size2; | ||
| 322 | if (cmp (f1, f1 + size) > 0) | ||
| 323 | swap(f1, f1 + size); | ||
| 324 | } | ||
| 325 | #endif /* NATURAL */ | ||
| 326 | } | ||
| 327 | |||
| 328 | /* | ||
| 329 | * This is to avoid out-of-bounds addresses in sorting the | ||
| 330 | * last 4 elements. | ||
| 331 | */ | ||
| 332 | static void | ||
| 333 | insertionsort(a, n, size, cmp) | ||
| 334 | u_char *a; | ||
| 335 | size_t n, size; | ||
| 336 | int (*cmp) __P((const void *, const void *)); | ||
| 337 | { | ||
| 338 | u_char *ai, *s, *t, *u, tmp; | ||
| 339 | int i; | ||
| 340 | |||
| 341 | for (ai = a+size; --n >= 1; ai += size) | ||
| 342 | for (t = ai; t > a; t -= size) { | ||
| 343 | u = t - size; | ||
| 344 | if (cmp(u, t) <= 0) | ||
| 345 | break; | ||
| 346 | swap(u, t); | ||
| 347 | } | ||
| 348 | } | ||
diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c new file mode 100644 index 0000000000..43356e66b3 --- /dev/null +++ b/src/lib/libc/stdlib/mrand48.c | |||
| @@ -0,0 +1,23 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | |||
| 18 | long | ||
| 19 | mrand48(void) | ||
| 20 | { | ||
| 21 | __dorand48(__rand48_seed); | ||
| 22 | return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1]; | ||
| 23 | } | ||
diff --git a/src/lib/libc/stdlib/multibyte.c b/src/lib/libc/stdlib/multibyte.c new file mode 100644 index 0000000000..fe1cd5781b --- /dev/null +++ b/src/lib/libc/stdlib/multibyte.c | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1991 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | /* | ||
| 42 | * Stub multibyte character functions. | ||
| 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 | { | ||
| 52 | if (s == NULL || *s == '\0') | ||
| 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 | } | ||
| 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/nrand48.c b/src/lib/libc/stdlib/nrand48.c new file mode 100644 index 0000000000..63f839cb05 --- /dev/null +++ b/src/lib/libc/stdlib/nrand48.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | long | ||
| 17 | nrand48(unsigned short xseed[3]) | ||
| 18 | { | ||
| 19 | __dorand48(xseed); | ||
| 20 | return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); | ||
| 21 | } | ||
diff --git a/src/lib/libc/stdlib/putenv.c b/src/lib/libc/stdlib/putenv.c new file mode 100644 index 0000000000..2194c2c608 --- /dev/null +++ b/src/lib/libc/stdlib/putenv.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1988 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | int | ||
| 43 | putenv(str) | ||
| 44 | const char *str; | ||
| 45 | { | ||
| 46 | register char *p, *equal; | ||
| 47 | int rval; | ||
| 48 | |||
| 49 | if (!(p = strdup(str))) | ||
| 50 | return(1); | ||
| 51 | if (!(equal = strchr(p, '='))) { | ||
| 52 | (void)free(p); | ||
| 53 | return(1); | ||
| 54 | } | ||
| 55 | *equal = '\0'; | ||
| 56 | rval = setenv(p, equal + 1, 1); | ||
| 57 | (void)free(p); | ||
| 58 | return(rval); | ||
| 59 | } | ||
diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3 new file mode 100644 index 0000000000..cb1e052191 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.3 | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)labs.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: qabs.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt QABS 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm qabs | ||
| 44 | .Nd return the absolute value of a quad integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft quad_t | ||
| 48 | .Fn qabs "quad_t j" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn qabs | ||
| 52 | function | ||
| 53 | returns the absolute value of the quad integer | ||
| 54 | .Ar j . | ||
| 55 | .Sh SEE ALSO | ||
| 56 | .Xr abs 3 , | ||
| 57 | .Xr labs 3 , | ||
| 58 | .Xr floor 3 , | ||
| 59 | .Xr cabs 3 , | ||
| 60 | .Xr math 3 | ||
| 61 | .Sh BUGS | ||
| 62 | The absolute value of the most negative integer remains negative. | ||
diff --git a/src/lib/libc/stdlib/qabs.c b/src/lib/libc/stdlib/qabs.c new file mode 100644 index 0000000000..9c51a8baa9 --- /dev/null +++ b/src/lib/libc/stdlib/qabs.c | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | |||
| 41 | quad_t | ||
| 42 | qabs(j) | ||
| 43 | quad_t j; | ||
| 44 | { | ||
| 45 | return(j < 0 ? -j : j); | ||
| 46 | } | ||
diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3 new file mode 100644 index 0000000000..0efcfc96ef --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.3 | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Chris Torek 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. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)qdiv.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: qdiv.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt QDIV 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm qdiv | ||
| 44 | .Nd return quotient and remainder from division | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft qdiv_t | ||
| 48 | .Fn qdiv "quad_t num" "quad_t denom" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn qdiv | ||
| 52 | function | ||
| 53 | computes the value | ||
| 54 | .Ar num/denom | ||
| 55 | and returns the quotient and remainder in a structure named | ||
| 56 | .Ar qdiv_t | ||
| 57 | that contains two | ||
| 58 | .Em quad integer | ||
| 59 | members named | ||
| 60 | .Ar quot | ||
| 61 | and | ||
| 62 | .Ar rem . | ||
| 63 | .Sh SEE ALSO | ||
| 64 | .Xr div 3 , | ||
| 65 | .Xr ldiv 3 , | ||
| 66 | .Xr math 3 | ||
diff --git a/src/lib/libc/stdlib/qdiv.c b/src/lib/libc/stdlib/qdiv.c new file mode 100644 index 0000000000..8f8e3f89c4 --- /dev/null +++ b/src/lib/libc/stdlib/qdiv.c | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Chris Torek. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #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 */ | ||
| 43 | |||
| 44 | qdiv_t | ||
| 45 | qdiv(num, denom) | ||
| 46 | quad_t num, denom; | ||
| 47 | { | ||
| 48 | qdiv_t r; | ||
| 49 | |||
| 50 | /* see div.c for comments */ | ||
| 51 | |||
| 52 | r.quot = num / denom; | ||
| 53 | r.rem = num % denom; | ||
| 54 | if (num >= 0 && r.rem < 0) { | ||
| 55 | r.quot++; | ||
| 56 | r.rem -= denom; | ||
| 57 | } | ||
| 58 | return (r); | ||
| 59 | } | ||
diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 new file mode 100644 index 0000000000..eb122cde12 --- /dev/null +++ b/src/lib/libc/stdlib/qsort.3 | |||
| @@ -0,0 +1,234 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991, 1993 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)qsort.3 8.1 (Berkeley) 6/4/93 | ||
| 37 | .\" $Id: qsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 4, 1993 | ||
| 40 | .Dt QSORT 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm qsort, heapsort, mergesort | ||
| 44 | .Nd sort functions | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft void | ||
| 48 | .Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
| 49 | .Ft int | ||
| 50 | .Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
| 51 | .Ft int | ||
| 52 | .Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" | ||
| 53 | .Sh DESCRIPTION | ||
| 54 | The | ||
| 55 | .Fn qsort | ||
| 56 | function is a modified partition-exchange sort, or quicksort. | ||
| 57 | The | ||
| 58 | .Fn heapsort | ||
| 59 | function is a modified selection sort. | ||
| 60 | The | ||
| 61 | .Fn mergesort | ||
| 62 | function is a modified merge sort with exponential search | ||
| 63 | intended for sorting data with pre-existing order. | ||
| 64 | .Pp | ||
| 65 | The | ||
| 66 | .Fn qsort | ||
| 67 | and | ||
| 68 | .Fn heapsort | ||
| 69 | functions sort an array of | ||
| 70 | .Fa nmemb | ||
| 71 | objects, the initial member of which is pointed to by | ||
| 72 | .Fa base . | ||
| 73 | The size of each object is specified by | ||
| 74 | .Fa size . | ||
| 75 | .Fn Mergesort | ||
| 76 | behaves similarly, but | ||
| 77 | .Em requires | ||
| 78 | that | ||
| 79 | .Fa size | ||
| 80 | be greater than | ||
| 81 | .Dq "sizeof(void *) / 2" . | ||
| 82 | .Pp | ||
| 83 | The contents of the array | ||
| 84 | .Fa base | ||
| 85 | are sorted in ascending order according to | ||
| 86 | a comparison function pointed to by | ||
| 87 | .Fa compar , | ||
| 88 | which requires two arguments pointing to the objects being | ||
| 89 | compared. | ||
| 90 | .Pp | ||
| 91 | The comparison function must return an integer less than, equal to, or | ||
| 92 | greater than zero if the first argument is considered to be respectively | ||
| 93 | less than, equal to, or greater than the second. | ||
| 94 | .Pp | ||
| 95 | The functions | ||
| 96 | .Fn qsort | ||
| 97 | and | ||
| 98 | .Fn heapsort | ||
| 99 | are | ||
| 100 | .Em not | ||
| 101 | stable, that is, if two members compare as equal, their order in | ||
| 102 | the sorted array is undefined. | ||
| 103 | The function | ||
| 104 | .Fn mergesort | ||
| 105 | is stable. | ||
| 106 | .Pp | ||
| 107 | The | ||
| 108 | .Fn qsort | ||
| 109 | function is an implementation of C.A.R. Hoare's ``quicksort'' algorithm, | ||
| 110 | a variant of partition-exchange sorting; in particular, see D.E. Knuth's | ||
| 111 | Algorithm Q. | ||
| 112 | .Fn Qsort | ||
| 113 | takes O N lg N average time. | ||
| 114 | This implementation uses median selection to avoid its | ||
| 115 | O N**2 worst-case behavior. | ||
| 116 | .Pp | ||
| 117 | The | ||
| 118 | .Fn heapsort | ||
| 119 | function is an implementation of J.W.J. William's ``heapsort'' algorithm, | ||
| 120 | a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. | ||
| 121 | .Fn Heapsort | ||
| 122 | takes O N lg N worst-case time. | ||
| 123 | Its | ||
| 124 | .Em only | ||
| 125 | advantage over | ||
| 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 | ||
| 131 | The function | ||
| 132 | .Fn mergesort | ||
| 133 | requires additional memory of size | ||
| 134 | .Fa nmemb * | ||
| 135 | .Fa size | ||
| 136 | bytes; it should be used only when space is not at a premium. | ||
| 137 | .Fn Mergesort | ||
| 138 | is optimized for data with pre-existing order; its worst case | ||
| 139 | time is O N lg N; its best case is O N. | ||
| 140 | .Pp | ||
| 141 | Normally, | ||
| 142 | .Fn qsort | ||
| 143 | is faster than | ||
| 144 | .Fn mergesort | ||
| 145 | is faster than | ||
| 146 | .Fn heapsort . | ||
| 147 | Memory availability and pre-existing order in the data can make this | ||
| 148 | untrue. | ||
| 149 | .Sh RETURN VALUES | ||
| 150 | The | ||
| 151 | .Fn qsort | ||
| 152 | function | ||
| 153 | returns no value. | ||
| 154 | .Pp | ||
| 155 | Upon successful completion, | ||
| 156 | .Fn heapsort | ||
| 157 | and | ||
| 158 | .Fn mergesort | ||
| 159 | return 0. | ||
| 160 | Otherwise, they return \-1 and the global variable | ||
| 161 | .Va errno | ||
| 162 | is set to indicate the error. | ||
| 163 | .Sh ERRORS | ||
| 164 | The | ||
| 165 | .Fn heapsort | ||
| 166 | function succeeds unless: | ||
| 167 | .Bl -tag -width Er | ||
| 168 | .It Bq Er EINVAL | ||
| 169 | The | ||
| 170 | .Fa size | ||
| 171 | argument is zero, or, | ||
| 172 | the | ||
| 173 | .Fa size | ||
| 174 | argument to | ||
| 175 | .Fn mergesort | ||
| 176 | is less than | ||
| 177 | .Dq "sizeof(void *) / 2" . | ||
| 178 | .It Bq Er ENOMEM | ||
| 179 | .Fn Heapsort | ||
| 180 | or | ||
| 181 | .Fn mergesort | ||
| 182 | were unable to allocate memory. | ||
| 183 | .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 | ||
| 191 | .Xr sort 1 , | ||
| 192 | .Xr radixsort 3 | ||
| 193 | .Rs | ||
| 194 | .%A Hoare, C.A.R. | ||
| 195 | .%D 1962 | ||
| 196 | .%T "Quicksort" | ||
| 197 | .%J "The Computer Journal" | ||
| 198 | .%V 5:1 | ||
| 199 | .%P pp. 10-15 | ||
| 200 | .Re | ||
| 201 | .Rs | ||
| 202 | .%A Williams, J.W.J | ||
| 203 | .%D 1964 | ||
| 204 | .%T "Heapsort" | ||
| 205 | .%J "Communications of the ACM" | ||
| 206 | .%V 7:1 | ||
| 207 | .%P pp. 347-348 | ||
| 208 | .Re | ||
| 209 | .Rs | ||
| 210 | .%A Knuth, D.E. | ||
| 211 | .%D 1968 | ||
| 212 | .%B "The Art of Computer Programming" | ||
| 213 | .%V Vol. 3 | ||
| 214 | .%T "Sorting and Searching" | ||
| 215 | .%P pp. 114-123, 145-149 | ||
| 216 | .Re | ||
| 217 | .Rs | ||
| 218 | .%A Mcilroy, P.M. | ||
| 219 | .%T "Optimistic Sorting and Information Theoretic Complexity" | ||
| 220 | .%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" | ||
| 221 | .%V January 1992 | ||
| 222 | .Re | ||
| 223 | .Rs | ||
| 224 | .%A Bentley, J.L. | ||
| 225 | .%T "Engineering a Sort Function" | ||
| 226 | .%J "bentley@research.att.com" | ||
| 227 | .%V January 1992 | ||
| 228 | .Re | ||
| 229 | .Sh STANDARDS | ||
| 230 | The | ||
| 231 | .Fn qsort | ||
| 232 | function | ||
| 233 | conforms to | ||
| 234 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c new file mode 100644 index 0000000000..c06bd54054 --- /dev/null +++ b/src/lib/libc/stdlib/qsort.c | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <stdlib.h> | ||
| 41 | |||
| 42 | static inline char *med3 __P((char *, char *, char *, int (*)())); | ||
| 43 | static inline void swapfunc __P((char *, char *, int, int)); | ||
| 44 | |||
| 45 | #define min(a, b) (a) < (b) ? a : b | ||
| 46 | |||
| 47 | /* | ||
| 48 | * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". | ||
| 49 | */ | ||
| 50 | #define swapcode(TYPE, parmi, parmj, n) { \ | ||
| 51 | long i = (n) / sizeof (TYPE); \ | ||
| 52 | register TYPE *pi = (TYPE *) (parmi); \ | ||
| 53 | register TYPE *pj = (TYPE *) (parmj); \ | ||
| 54 | do { \ | ||
| 55 | register TYPE t = *pi; \ | ||
| 56 | *pi++ = *pj; \ | ||
| 57 | *pj++ = t; \ | ||
| 58 | } while (--i > 0); \ | ||
| 59 | } | ||
| 60 | |||
| 61 | #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ | ||
| 62 | es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; | ||
| 63 | |||
| 64 | static inline void | ||
| 65 | swapfunc(a, b, n, swaptype) | ||
| 66 | char *a, *b; | ||
| 67 | int n, swaptype; | ||
| 68 | { | ||
| 69 | if(swaptype <= 1) | ||
| 70 | swapcode(long, a, b, n) | ||
| 71 | else | ||
| 72 | swapcode(char, a, b, n) | ||
| 73 | } | ||
| 74 | |||
| 75 | #define swap(a, b) \ | ||
| 76 | if (swaptype == 0) { \ | ||
| 77 | long t = *(long *)(a); \ | ||
| 78 | *(long *)(a) = *(long *)(b); \ | ||
| 79 | *(long *)(b) = t; \ | ||
| 80 | } else \ | ||
| 81 | swapfunc(a, b, es, swaptype) | ||
| 82 | |||
| 83 | #define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) | ||
| 84 | |||
| 85 | static inline char * | ||
| 86 | med3(a, b, c, cmp) | ||
| 87 | char *a, *b, *c; | ||
| 88 | int (*cmp)(); | ||
| 89 | { | ||
| 90 | return cmp(a, b) < 0 ? | ||
| 91 | (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) | ||
| 92 | :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); | ||
| 93 | } | ||
| 94 | |||
| 95 | void | ||
| 96 | qsort(a, n, es, cmp) | ||
| 97 | void *a; | ||
| 98 | size_t n, es; | ||
| 99 | int (*cmp)(); | ||
| 100 | { | ||
| 101 | char *pa, *pb, *pc, *pd, *pl, *pm, *pn; | ||
| 102 | int d, r, swaptype, swap_cnt; | ||
| 103 | |||
| 104 | loop: SWAPINIT(a, es); | ||
| 105 | swap_cnt = 0; | ||
| 106 | if (n < 7) { | ||
| 107 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | ||
| 108 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
| 109 | pl -= es) | ||
| 110 | swap(pl, pl - es); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | pm = a + (n / 2) * es; | ||
| 114 | if (n > 7) { | ||
| 115 | pl = a; | ||
| 116 | pn = a + (n - 1) * es; | ||
| 117 | if (n > 40) { | ||
| 118 | d = (n / 8) * es; | ||
| 119 | pl = med3(pl, pl + d, pl + 2 * d, cmp); | ||
| 120 | pm = med3(pm - d, pm, pm + d, cmp); | ||
| 121 | pn = med3(pn - 2 * d, pn - d, pn, cmp); | ||
| 122 | } | ||
| 123 | pm = med3(pl, pm, pn, cmp); | ||
| 124 | } | ||
| 125 | swap(a, pm); | ||
| 126 | pa = pb = a + es; | ||
| 127 | |||
| 128 | pc = pd = a + (n - 1) * es; | ||
| 129 | for (;;) { | ||
| 130 | while (pb <= pc && (r = cmp(pb, a)) <= 0) { | ||
| 131 | if (r == 0) { | ||
| 132 | swap_cnt = 1; | ||
| 133 | swap(pa, pb); | ||
| 134 | pa += es; | ||
| 135 | } | ||
| 136 | pb += es; | ||
| 137 | } | ||
| 138 | while (pb <= pc && (r = cmp(pc, a)) >= 0) { | ||
| 139 | if (r == 0) { | ||
| 140 | swap_cnt = 1; | ||
| 141 | swap(pc, pd); | ||
| 142 | pd -= es; | ||
| 143 | } | ||
| 144 | pc -= es; | ||
| 145 | } | ||
| 146 | if (pb > pc) | ||
| 147 | break; | ||
| 148 | swap(pb, pc); | ||
| 149 | swap_cnt = 1; | ||
| 150 | pb += es; | ||
| 151 | pc -= es; | ||
| 152 | } | ||
| 153 | if (swap_cnt == 0) { /* Switch to insertion sort */ | ||
| 154 | for (pm = a + es; pm < (char *) a + n * es; pm += es) | ||
| 155 | for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; | ||
| 156 | pl -= es) | ||
| 157 | swap(pl, pl - es); | ||
| 158 | return; | ||
| 159 | } | ||
| 160 | |||
| 161 | pn = a + n * es; | ||
| 162 | r = min(pa - (char *)a, pb - pa); | ||
| 163 | vecswap(a, pb - r, r); | ||
| 164 | r = min(pd - pc, pn - pd - es); | ||
| 165 | vecswap(pb, pn - r, r); | ||
| 166 | if ((r = pb - pa) > es) | ||
| 167 | qsort(a, r / es, es, cmp); | ||
| 168 | if ((r = pd - pc) > es) { | ||
| 169 | /* Iterate rather than recurse to save stack space */ | ||
| 170 | a = pn - r; | ||
| 171 | n = r / es; | ||
| 172 | goto loop; | ||
| 173 | } | ||
| 174 | /* qsort(pn - r, r / es, es, cmp);*/ | ||
| 175 | } | ||
diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3 new file mode 100644 index 0000000000..a2af9f17a4 --- /dev/null +++ b/src/lib/libc/stdlib/radixsort.3 | |||
| @@ -0,0 +1,161 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991, 1993 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. 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: @(#)radixsort.3 8.2 (Berkeley) 1/27/94 | ||
| 33 | .\" $Id: radixsort.3,v 1.1.1.1 1995/10/18 08:42:18 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd January 27, 1994 | ||
| 36 | .Dt RADIXSORT 3 | ||
| 37 | .Os | ||
| 38 | .Sh NAME | ||
| 39 | .Nm radixsort | ||
| 40 | .Nd radix sort | ||
| 41 | .Sh SYNOPSIS | ||
| 42 | .Fd #include <limits.h> | ||
| 43 | .Fd #include <stdlib.h> | ||
| 44 | .Ft int | ||
| 45 | .Fn radixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" | ||
| 46 | .Ft int | ||
| 47 | .Fn sradixsort "u_char **base" "int nmemb" "u_char *table" "u_int endbyte" | ||
| 48 | .Sh DESCRIPTION | ||
| 49 | The | ||
| 50 | .Fn radixsort | ||
| 51 | and | ||
| 52 | .Fn sradixsort | ||
| 53 | functions | ||
| 54 | are implementations of radix sort. | ||
| 55 | .Pp | ||
| 56 | These functions sort an array of pointers to byte strings, the initial | ||
| 57 | member of which is referenced by | ||
| 58 | .Fa base . | ||
| 59 | The byte strings may contain any values; the end of each string | ||
| 60 | is denoted by the user-specified value | ||
| 61 | .Fa endbyte . | ||
| 62 | .Pp | ||
| 63 | Applications may specify a sort order by providing the | ||
| 64 | .Fa table | ||
| 65 | argument. | ||
| 66 | If | ||
| 67 | .Pf non- Dv NULL , | ||
| 68 | .Fa table | ||
| 69 | must reference an array of | ||
| 70 | .Dv UCHAR_MAX | ||
| 71 | + 1 bytes which contains the sort | ||
| 72 | weight of each possible byte value. | ||
| 73 | The end-of-string byte must have a sort weight of 0 or 255 | ||
| 74 | (for sorting in reverse order). | ||
| 75 | More than one byte may have the same sort weight. | ||
| 76 | The | ||
| 77 | .Fa table | ||
| 78 | argument | ||
| 79 | is useful for applications which wish to sort different characters | ||
| 80 | equally, for example, providing a table with the same weights | ||
| 81 | for A-Z as for a-z will result in a case-insensitive sort. | ||
| 82 | If | ||
| 83 | .Fa table | ||
| 84 | is NULL, the contents of the array are sorted in ascending order | ||
| 85 | according to the | ||
| 86 | .Tn ASCII | ||
| 87 | order of the byte strings they reference and | ||
| 88 | .Fa endbyte | ||
| 89 | has a sorting weight of 0. | ||
| 90 | .Pp | ||
| 91 | The | ||
| 92 | .Fn sradixsort | ||
| 93 | function is stable, that is, if two elements compare as equal, their | ||
| 94 | order in the sorted array is unchanged. | ||
| 95 | The | ||
| 96 | .Fn sradixsort | ||
| 97 | function uses additional memory sufficient to hold | ||
| 98 | .Fa nmemb | ||
| 99 | pointers. | ||
| 100 | .Pp | ||
| 101 | The | ||
| 102 | .Fn radixsort | ||
| 103 | function is not stable, but uses no additional memory. | ||
| 104 | .Pp | ||
| 105 | These functions are variants of most-significant-byte radix sorting; in | ||
| 106 | 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. | ||
| 108 | .Sh RETURN VALUES | ||
| 109 | Upon successful completion 0 is returned. | ||
| 110 | Otherwise, \-1 is returned and the global variable | ||
| 111 | .Va errno | ||
| 112 | is set to indicate the error. | ||
| 113 | .Sh ERRORS | ||
| 114 | .Bl -tag -width Er | ||
| 115 | .It Bq Er EINVAL | ||
| 116 | The value of the | ||
| 117 | .Fa endbyte | ||
| 118 | element of | ||
| 119 | .Fa table | ||
| 120 | is not 0 or 255. | ||
| 121 | .El | ||
| 122 | .Pp | ||
| 123 | Additionally, the | ||
| 124 | .Fn sradixsort | ||
| 125 | function | ||
| 126 | may fail and set | ||
| 127 | .Va errno | ||
| 128 | for any of the errors specified for the library routine | ||
| 129 | .Xr malloc 3 . | ||
| 130 | .Sh SEE ALSO | ||
| 131 | .Xr sort 1 , | ||
| 132 | .Xr qsort 3 | ||
| 133 | .Pp | ||
| 134 | .Rs | ||
| 135 | .%A Knuth, D.E. | ||
| 136 | .%D 1968 | ||
| 137 | .%B "The Art of Computer Programming" | ||
| 138 | .%T "Sorting and Searching" | ||
| 139 | .%V Vol. 3 | ||
| 140 | .%P pp. 170-178 | ||
| 141 | .Re | ||
| 142 | .Rs | ||
| 143 | .%A Paige, R. | ||
| 144 | .%D 1987 | ||
| 145 | .%T "Three Partition Refinement Algorithms" | ||
| 146 | .%J "SIAM J. Comput." | ||
| 147 | .%V Vol. 16 | ||
| 148 | .%N No. 6 | ||
| 149 | .Re | ||
| 150 | .Rs | ||
| 151 | .%A McIlroy, P. | ||
| 152 | .%D 1993 | ||
| 153 | .%B "Engineering Radix Sort" | ||
| 154 | .%T "Computing Systems" | ||
| 155 | .%V Vol. 6:1 | ||
| 156 | .%P pp. 5-27 | ||
| 157 | .Re | ||
| 158 | .Sh HISTORY | ||
| 159 | The | ||
| 160 | .Fn radixsort | ||
| 161 | function first appeared in 4.4BSD. | ||
diff --git a/src/lib/libc/stdlib/radixsort.c b/src/lib/libc/stdlib/radixsort.c new file mode 100644 index 0000000000..dd51013c94 --- /dev/null +++ b/src/lib/libc/stdlib/radixsort.c | |||
| @@ -0,0 +1,318 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990, 1993 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Peter McIlroy and by Dan Bernstein at New York University, | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #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 | /* | ||
| 43 | * Radixsort routines. | ||
| 44 | * | ||
| 45 | * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. | ||
| 46 | * Use radixsort(a, n, trace, endchar) for this case. | ||
| 47 | * | ||
| 48 | * For stable sorting (using N extra pointers) use sradixsort(), which calls | ||
| 49 | * r_sort_b(). | ||
| 50 | * | ||
| 51 | * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, | ||
| 52 | * "Engineering Radix Sort". | ||
| 53 | */ | ||
| 54 | |||
| 55 | #include <sys/types.h> | ||
| 56 | #include <stdlib.h> | ||
| 57 | #include <errno.h> | ||
| 58 | |||
| 59 | typedef struct { | ||
| 60 | const u_char **sa; | ||
| 61 | int sn, si; | ||
| 62 | } stack; | ||
| 63 | |||
| 64 | static inline void simplesort | ||
| 65 | __P((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)); | ||
| 67 | static void r_sort_b __P((const u_char **, | ||
| 68 | const u_char **, int, int, const u_char *, u_int)); | ||
| 69 | |||
| 70 | #define THRESHOLD 20 /* Divert to simplesort(). */ | ||
| 71 | #define SIZE 512 /* Default stack size. */ | ||
| 72 | |||
| 73 | #define SETUP { \ | ||
| 74 | if (tab == NULL) { \ | ||
| 75 | tr = tr0; \ | ||
| 76 | for (c = 0; c < endch; c++) \ | ||
| 77 | tr0[c] = c + 1; \ | ||
| 78 | tr0[c] = 0; \ | ||
| 79 | for (c++; c < 256; c++) \ | ||
| 80 | tr0[c] = c; \ | ||
| 81 | endch = 0; \ | ||
| 82 | } else { \ | ||
| 83 | endch = tab[endch]; \ | ||
| 84 | tr = tab; \ | ||
| 85 | if (endch != 0 && endch != 255) { \ | ||
| 86 | errno = EINVAL; \ | ||
| 87 | return (-1); \ | ||
| 88 | } \ | ||
| 89 | } \ | ||
| 90 | } | ||
| 91 | |||
| 92 | int | ||
| 93 | radixsort(a, n, tab, endch) | ||
| 94 | const u_char **a, *tab; | ||
| 95 | int n; | ||
| 96 | u_int endch; | ||
| 97 | { | ||
| 98 | const u_char *tr; | ||
| 99 | int c; | ||
| 100 | u_char tr0[256]; | ||
| 101 | |||
| 102 | SETUP; | ||
| 103 | r_sort_a(a, n, 0, tr, endch); | ||
| 104 | return (0); | ||
| 105 | } | ||
| 106 | |||
| 107 | int | ||
| 108 | sradixsort(a, n, tab, endch) | ||
| 109 | const u_char **a, *tab; | ||
| 110 | int n; | ||
| 111 | u_int endch; | ||
| 112 | { | ||
| 113 | const u_char *tr, **ta; | ||
| 114 | int c; | ||
| 115 | u_char tr0[256]; | ||
| 116 | |||
| 117 | SETUP; | ||
| 118 | if (n < THRESHOLD) | ||
| 119 | simplesort(a, n, 0, tr, endch); | ||
| 120 | else { | ||
| 121 | if ((ta = malloc(n * sizeof(a))) == NULL) | ||
| 122 | return (-1); | ||
| 123 | r_sort_b(a, ta, n, 0, tr, endch); | ||
| 124 | free(ta); | ||
| 125 | } | ||
| 126 | return (0); | ||
| 127 | } | ||
| 128 | |||
| 129 | #define empty(s) (s >= sp) | ||
| 130 | #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si | ||
| 131 | #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i | ||
| 132 | #define swap(a, b, t) t = a, a = b, b = t | ||
| 133 | |||
| 134 | /* Unstable, in-place sort. */ | ||
| 135 | void | ||
| 136 | r_sort_a(a, n, i, tr, endch) | ||
| 137 | const u_char **a; | ||
| 138 | int n, i; | ||
| 139 | const u_char *tr; | ||
| 140 | u_int endch; | ||
| 141 | { | ||
| 142 | static int count[256], nc, bmin; | ||
| 143 | register int c; | ||
| 144 | register const u_char **ak, *r; | ||
| 145 | stack s[SIZE], *sp, *sp0, *sp1, temp; | ||
| 146 | int *cp, bigc; | ||
| 147 | const u_char **an, *t, **aj, **top[256]; | ||
| 148 | |||
| 149 | /* Set up stack. */ | ||
| 150 | sp = s; | ||
| 151 | push(a, n, i); | ||
| 152 | while (!empty(s)) { | ||
| 153 | pop(a, n, i); | ||
| 154 | if (n < THRESHOLD) { | ||
| 155 | simplesort(a, n, i, tr, endch); | ||
| 156 | continue; | ||
| 157 | } | ||
| 158 | an = a + n; | ||
| 159 | |||
| 160 | /* Make character histogram. */ | ||
| 161 | if (nc == 0) { | ||
| 162 | bmin = 255; /* First occupied bin, excluding eos. */ | ||
| 163 | for (ak = a; ak < an;) { | ||
| 164 | c = tr[(*ak++)[i]]; | ||
| 165 | if (++count[c] == 1 && c != endch) { | ||
| 166 | if (c < bmin) | ||
| 167 | bmin = c; | ||
| 168 | nc++; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | if (sp + nc > s + SIZE) { /* Get more stack. */ | ||
| 172 | r_sort_a(a, n, i, tr, endch); | ||
| 173 | continue; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | /* | ||
| 178 | * Set top[]; push incompletely sorted bins onto stack. | ||
| 179 | * top[] = pointers to last out-of-place element in bins. | ||
| 180 | * count[] = counts of elements in bins. | ||
| 181 | * Before permuting: top[c-1] + count[c] = top[c]; | ||
| 182 | * during deal: top[c] counts down to top[c-1]. | ||
| 183 | */ | ||
| 184 | sp0 = sp1 = sp; /* Stack position of biggest bin. */ | ||
| 185 | bigc = 2; /* Size of biggest bin. */ | ||
| 186 | if (endch == 0) /* Special case: set top[eos]. */ | ||
| 187 | top[0] = ak = a + count[0]; | ||
| 188 | else { | ||
| 189 | ak = a; | ||
| 190 | top[255] = an; | ||
| 191 | } | ||
| 192 | for (cp = count + bmin; nc > 0; cp++) { | ||
| 193 | while (*cp == 0) /* Find next non-empty pile. */ | ||
| 194 | cp++; | ||
| 195 | if (*cp > 1) { | ||
| 196 | if (*cp > bigc) { | ||
| 197 | bigc = *cp; | ||
| 198 | sp1 = sp; | ||
| 199 | } | ||
| 200 | push(ak, *cp, i+1); | ||
| 201 | } | ||
| 202 | top[cp-count] = ak += *cp; | ||
| 203 | nc--; | ||
| 204 | } | ||
| 205 | swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ | ||
| 206 | |||
| 207 | /* | ||
| 208 | * Permute misplacements home. Already home: everything | ||
| 209 | * before aj, and in bin[c], items from top[c] on. | ||
| 210 | * Inner loop: | ||
| 211 | * r = next element to put in place; | ||
| 212 | * ak = top[r[i]] = location to put the next element. | ||
| 213 | * aj = bottom of 1st disordered bin. | ||
| 214 | * Outer loop: | ||
| 215 | * Once the 1st disordered bin is done, ie. aj >= ak, | ||
| 216 | * aj<-aj + count[c] connects the bins in a linked list; | ||
| 217 | * reset count[c]. | ||
| 218 | */ | ||
| 219 | for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) | ||
| 220 | for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) | ||
| 221 | swap(*ak, r, t); | ||
| 222 | } | ||
| 223 | } | ||
| 224 | |||
| 225 | /* Stable sort, requiring additional memory. */ | ||
| 226 | void | ||
| 227 | r_sort_b(a, ta, n, i, tr, endch) | ||
| 228 | const u_char **a, **ta; | ||
| 229 | int n, i; | ||
| 230 | const u_char *tr; | ||
| 231 | u_int endch; | ||
| 232 | { | ||
| 233 | static int count[256], nc, bmin; | ||
| 234 | register int c; | ||
| 235 | register const u_char **ak, **ai; | ||
| 236 | stack s[512], *sp, *sp0, *sp1, temp; | ||
| 237 | const u_char **top[256]; | ||
| 238 | int *cp, bigc; | ||
| 239 | |||
| 240 | sp = s; | ||
| 241 | push(a, n, i); | ||
| 242 | while (!empty(s)) { | ||
| 243 | pop(a, n, i); | ||
| 244 | if (n < THRESHOLD) { | ||
| 245 | simplesort(a, n, i, tr, endch); | ||
| 246 | continue; | ||
| 247 | } | ||
| 248 | |||
| 249 | if (nc == 0) { | ||
| 250 | bmin = 255; | ||
| 251 | for (ak = a + n; --ak >= a;) { | ||
| 252 | c = tr[(*ak)[i]]; | ||
| 253 | if (++count[c] == 1 && c != endch) { | ||
| 254 | if (c < bmin) | ||
| 255 | bmin = c; | ||
| 256 | nc++; | ||
| 257 | } | ||
| 258 | } | ||
| 259 | if (sp + nc > s + SIZE) { | ||
| 260 | r_sort_b(a, ta, n, i, tr, endch); | ||
| 261 | continue; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | sp0 = sp1 = sp; | ||
| 266 | bigc = 2; | ||
| 267 | if (endch == 0) { | ||
| 268 | top[0] = ak = a + count[0]; | ||
| 269 | count[0] = 0; | ||
| 270 | } else { | ||
| 271 | ak = a; | ||
| 272 | top[255] = a + n; | ||
| 273 | count[255] = 0; | ||
| 274 | } | ||
| 275 | for (cp = count + bmin; nc > 0; cp++) { | ||
| 276 | while (*cp == 0) | ||
| 277 | cp++; | ||
| 278 | if ((c = *cp) > 1) { | ||
| 279 | if (c > bigc) { | ||
| 280 | bigc = c; | ||
| 281 | sp1 = sp; | ||
| 282 | } | ||
| 283 | push(ak, c, i+1); | ||
| 284 | } | ||
| 285 | top[cp-count] = ak += c; | ||
| 286 | *cp = 0; /* Reset count[]. */ | ||
| 287 | nc--; | ||
| 288 | } | ||
| 289 | swap(*sp0, *sp1, temp); | ||
| 290 | |||
| 291 | for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ | ||
| 292 | *--ak = *--ai; | ||
| 293 | for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ | ||
| 294 | *--top[tr[(*ak)[i]]] = *ak; | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | static inline void | ||
| 299 | simplesort(a, n, b, tr, endch) /* insertion sort */ | ||
| 300 | register const u_char **a; | ||
| 301 | int n, b; | ||
| 302 | register const u_char *tr; | ||
| 303 | u_int endch; | ||
| 304 | { | ||
| 305 | register u_char ch; | ||
| 306 | const u_char **ak, **ai, *s, *t; | ||
| 307 | |||
| 308 | for (ak = a+1; --n >= 1; ak++) | ||
| 309 | for (ai = ak; ai > a; ai--) { | ||
| 310 | for (s = ai[0] + b, t = ai[-1] + b; | ||
| 311 | (ch = tr[*s]) != endch; s++, t++) | ||
| 312 | if (ch != tr[*t]) | ||
| 313 | break; | ||
| 314 | if (ch >= tr[*t]) | ||
| 315 | break; | ||
| 316 | swap(ai[0], ai[-1], s); | ||
| 317 | } | ||
| 318 | } | ||
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3 new file mode 100644 index 0000000000..a0e7740e66 --- /dev/null +++ b/src/lib/libc/stdlib/rand.3 | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)rand.3 6.7 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: rand.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt RAND 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm rand , | ||
| 44 | .Nm srand | ||
| 45 | .Nd bad random number generator | ||
| 46 | .Sh SYNOPSIS | ||
| 47 | .Fd #include <stdlib.h> | ||
| 48 | .Ft void | ||
| 49 | .Fn srand "unsigned seed" | ||
| 50 | .Ft int | ||
| 51 | .Fn rand void | ||
| 52 | .Sh DESCRIPTION | ||
| 53 | .Bf -symbolic | ||
| 54 | These interfaces are obsoleted by random(3). | ||
| 55 | .Ef | ||
| 56 | .Pp | ||
| 57 | The | ||
| 58 | .Fn rand | ||
| 59 | function computes a sequence of pseudo-random integers in the range | ||
| 60 | of 0 to | ||
| 61 | .Dv RAND_MAX | ||
| 62 | (as defined by the header file | ||
| 63 | .Aq Pa stdlib.h ) . | ||
| 64 | .Pp | ||
| 65 | The | ||
| 66 | .Fn srand | ||
| 67 | function sets its argument as the seed for a new sequence of | ||
| 68 | pseudo-random numbers to be returned by | ||
| 69 | .Fn rand . | ||
| 70 | These sequences are repeatable by calling | ||
| 71 | .Fn srand | ||
| 72 | with the same seed value. | ||
| 73 | .Pp | ||
| 74 | If no seed value is provided, the functions are automatically | ||
| 75 | seeded with a value of 1. | ||
| 76 | .Sh SEE ALSO | ||
| 77 | .Xr random 3 | ||
| 78 | .Sh STANDARDS | ||
| 79 | The | ||
| 80 | .Fn rand | ||
| 81 | and | ||
| 82 | .Fn srand | ||
| 83 | functions | ||
| 84 | conform to | ||
| 85 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c new file mode 100644 index 0000000000..361d473448 --- /dev/null +++ b/src/lib/libc/stdlib/rand.c | |||
| @@ -0,0 +1,55 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <stdlib.h> | ||
| 41 | |||
| 42 | static u_long next = 1; | ||
| 43 | |||
| 44 | int | ||
| 45 | rand() | ||
| 46 | { | ||
| 47 | return ((next = next * 1103515245 + 12345) % ((u_int)RAND_MAX + 1)); | ||
| 48 | } | ||
| 49 | |||
| 50 | void | ||
| 51 | srand(seed) | ||
| 52 | u_int seed; | ||
| 53 | { | ||
| 54 | next = seed; | ||
| 55 | } | ||
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 new file mode 100644 index 0000000000..5a772c9a8c --- /dev/null +++ b/src/lib/libc/stdlib/rand48.3 | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | \" Copyright (c) 1993 Martin Birgmeier | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" You may redistribute unmodified or modified versions of this source | ||
| 5 | .\" code provided that the above copyright notice and this and the | ||
| 6 | .\" following conditions are retained. | ||
| 7 | .\" | ||
| 8 | .\" This software is provided ``as is'', and comes with no warranties | ||
| 9 | .\" of any kind. I shall in no event be liable for anything that happens | ||
| 10 | .\" to anyone/anything when using this software. | ||
| 11 | .\" | ||
| 12 | .\" $Id: rand48.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 13 | .\" | ||
| 14 | .Dd October 8, 1993 | ||
| 15 | .Dt RAND48 3 | ||
| 16 | .Os | ||
| 17 | .Sh NAME | ||
| 18 | .Nm drand48 , | ||
| 19 | .Nm erand48 , | ||
| 20 | .Nm lrand48 , | ||
| 21 | .Nm nrand48 , | ||
| 22 | .Nm mrand48 , | ||
| 23 | .Nm jrand48 , | ||
| 24 | .Nm srand48 , | ||
| 25 | .Nm seed48 , | ||
| 26 | .Nm lcong48 | ||
| 27 | .Nd pseudo random number generators and initialization routines | ||
| 28 | .Sh SYNOPSIS | ||
| 29 | .Fd #include <stdlib.h> | ||
| 30 | .Ft double | ||
| 31 | .Fn drand48 void | ||
| 32 | .Ft double | ||
| 33 | .Fn erand48 "unsigned short xseed[3]" | ||
| 34 | .Ft long | ||
| 35 | .Fn lrand48 void | ||
| 36 | .Ft long | ||
| 37 | .Fn nrand48 "unsigned short xseed[3]" | ||
| 38 | .Ft long | ||
| 39 | .Fn mrand48 void | ||
| 40 | .Ft long | ||
| 41 | .Fn jrand48 "unsigned short xseed[3]" | ||
| 42 | .Ft void | ||
| 43 | .Fn srand48 "long seed" | ||
| 44 | .Ft "unsigned short *" | ||
| 45 | .Fn seed48 "unsigned short xseed[3]" | ||
| 46 | .Ft void | ||
| 47 | .Fn lcong48 "unsigned short p[7]" | ||
| 48 | .Sh DESCRIPTION | ||
| 49 | The | ||
| 50 | .Fn rand48 | ||
| 51 | family of functions generates pseudo-random numbers using a linear | ||
| 52 | congruential algorithm working on integers 48 bits in size. The | ||
| 53 | particular formula employed is | ||
| 54 | r(n+1) = (a * r(n) + c) mod m | ||
| 55 | where the default values are | ||
| 56 | for the multiplicand a = 0xfdeece66d = 25214903917 and | ||
| 57 | the addend c = 0xb = 11. The modulus is always fixed at m = 2 ** 48. | ||
| 58 | r(n) is called the seed of the random number generator. | ||
| 59 | .Pp | ||
| 60 | For all the six generator routines described next, the first | ||
| 61 | computational step is to perform a single iteration of the algorithm. | ||
| 62 | .Pp | ||
| 63 | .Fn drand48 | ||
| 64 | and | ||
| 65 | .Fn erand48 | ||
| 66 | return values of type double. The full 48 bits of r(n+1) are | ||
| 67 | 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). | ||
| 69 | .Pp | ||
| 70 | .Fn lrand48 | ||
| 71 | and | ||
| 72 | .Fn nrand48 | ||
| 73 | return values of type long in the range | ||
| 74 | [0, 2**31-1]. The high-order (31) bits of | ||
| 75 | r(n+1) are loaded into the lower bits of the returned value, with | ||
| 76 | the topmost (sign) bit set to zero. | ||
| 77 | .Pp | ||
| 78 | .Fn mrand48 | ||
| 79 | and | ||
| 80 | .Fn jrand48 | ||
| 81 | return values of type long in the range | ||
| 82 | [-2**31, 2**31-1]. The high-order (32) bits of | ||
| 83 | r(n+1) are loaded into the returned value. | ||
| 84 | .Pp | ||
| 85 | .Fn drand48 , | ||
| 86 | .Fn lrand48 , | ||
| 87 | and | ||
| 88 | .Fn mrand48 | ||
| 89 | use an internal buffer to store r(n). For these functions | ||
| 90 | the initial value of r(0) = 0x1234abcd330e = 20017429951246. | ||
| 91 | .Pp | ||
| 92 | On the other hand, | ||
| 93 | .Fn erand48 , | ||
| 94 | .Fn nrand48 , | ||
| 95 | and | ||
| 96 | .Fn jrand48 | ||
| 97 | use a user-supplied buffer to store the seed r(n), | ||
| 98 | which consists of an array of 3 shorts, where the zeroth member | ||
| 99 | holds the least significant bits. | ||
| 100 | .Pp | ||
| 101 | All functions share the same multiplicand and addend. | ||
| 102 | .Pp | ||
| 103 | .Fn srand48 | ||
| 104 | is used to initialize the internal buffer r(n) of | ||
| 105 | .Fn drand48 , | ||
| 106 | .Fn lrand48 , | ||
| 107 | and | ||
| 108 | .Fn mrand48 | ||
| 109 | such that the 32 bits of the seed value are copied into the upper 32 bits | ||
| 110 | of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. | ||
| 111 | Additionally, the constant multiplicand and addend of the algorithm are | ||
| 112 | reset to the default values given above. | ||
| 113 | .Pp | ||
| 114 | .Fn seed48 | ||
| 115 | also initializes the internal buffer r(n) of | ||
| 116 | .Fn drand48 , | ||
| 117 | .Fn lrand48 , | ||
| 118 | and | ||
| 119 | .Fn mrand48 , | ||
| 120 | 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, | ||
| 122 | the constant multiplicand and addend of the algorithm are | ||
| 123 | reset to the default values given above. | ||
| 124 | .Fn seed48 | ||
| 125 | 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 | ||
| 127 | each new call to | ||
| 128 | .Fn seed48 . | ||
| 129 | .Pp | ||
| 130 | Finally, | ||
| 131 | .Fn lcong48 | ||
| 132 | allows full control over the multiplicand and addend used in | ||
| 133 | .Fn drand48 , | ||
| 134 | .Fn erand48 , | ||
| 135 | .Fn lrand48 , | ||
| 136 | .Fn nrand48 , | ||
| 137 | .Fn mrand48 , | ||
| 138 | and | ||
| 139 | .Fn jrand48 , | ||
| 140 | and the seed used in | ||
| 141 | .Fn drand48 , | ||
| 142 | .Fn lrand48 , | ||
| 143 | and | ||
| 144 | .Fn mrand48 . | ||
| 145 | An array of 7 shorts is passed as parameter; the first three shorts are | ||
| 146 | used to initialize the seed; the second three are used to initialize the | ||
| 147 | multiplicand; and the last short is used to initialize the addend. | ||
| 148 | It is thus not possible to use values greater than 0xffff as the addend. | ||
| 149 | .Pp | ||
| 150 | Note that all three methods of seeding the random number generator | ||
| 151 | always also set the multiplicand and addend for any of the six | ||
| 152 | generator calls. | ||
| 153 | .Pp | ||
| 154 | For a more powerful random number generator, see | ||
| 155 | .Xr random 3 | ||
| 156 | .Sh AUTHOR | ||
| 157 | Martin Birgmeier | ||
| 158 | .Sh SEE ALSO | ||
| 159 | .Xr rand 3 , | ||
| 160 | .Xr random 3 . | ||
diff --git a/src/lib/libc/stdlib/rand48.h b/src/lib/libc/stdlib/rand48.h new file mode 100644 index 0000000000..12496d1c8c --- /dev/null +++ b/src/lib/libc/stdlib/rand48.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #ifndef _RAND48_H_ | ||
| 15 | #define _RAND48_H_ | ||
| 16 | |||
| 17 | #include <math.h> | ||
| 18 | #include <stdlib.h> | ||
| 19 | |||
| 20 | void __dorand48 __P((unsigned short[3])); | ||
| 21 | |||
| 22 | #define RAND48_SEED_0 (0x330e) | ||
| 23 | #define RAND48_SEED_1 (0xabcd) | ||
| 24 | #define RAND48_SEED_2 (0x1234) | ||
| 25 | #define RAND48_MULT_0 (0xe66d) | ||
| 26 | #define RAND48_MULT_1 (0xdeec) | ||
| 27 | #define RAND48_MULT_2 (0x0005) | ||
| 28 | #define RAND48_ADD (0x000b) | ||
| 29 | |||
| 30 | #endif /* _RAND48_H_ */ | ||
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3 new file mode 100644 index 0000000000..38c15a9803 --- /dev/null +++ b/src/lib/libc/stdlib/random.3 | |||
| @@ -0,0 +1,167 @@ | |||
| 1 | .\" Copyright (c) 1983, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" Redistribution and use in source and binary forms, with or without | ||
| 5 | .\" modification, are permitted provided that the following conditions | ||
| 6 | .\" are met: | ||
| 7 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 8 | .\" notice, this list of conditions and the following disclaimer. | ||
| 9 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 10 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 11 | .\" documentation and/or other materials provided with the distribution. | ||
| 12 | .\" 3. 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: @(#)random.3 6.5 (Berkeley) 4/19/91 | ||
| 33 | .\" $Id: random.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 34 | .\" | ||
| 35 | .Dd April 19, 1991 | ||
| 36 | .Dt RANDOM 3 | ||
| 37 | .Os BSD 4.2 | ||
| 38 | .Sh NAME | ||
| 39 | .Nm random , | ||
| 40 | .Nm srandom , | ||
| 41 | .Nm initstate , | ||
| 42 | .Nm setstate | ||
| 43 | .Nd better random number generator; routines for changing generators | ||
| 44 | .Sh SYNOPSIS | ||
| 45 | .Fd #include <stdlib.h> | ||
| 46 | .Ft long | ||
| 47 | .Fn random void | ||
| 48 | .Ft void | ||
| 49 | .Fn srandom "unsigned seed" | ||
| 50 | .Ft char * | ||
| 51 | .Fn initstate "unsigned seed" "char *state" "int n" | ||
| 52 | .Ft char * | ||
| 53 | .Fn setstate "char *state" | ||
| 54 | .Sh DESCRIPTION | ||
| 55 | The | ||
| 56 | .Fn random | ||
| 57 | function | ||
| 58 | uses a non-linear additive feedback random number generator employing a | ||
| 59 | default table of size 31 long integers to return successive pseudo-random | ||
| 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 | ||
| 64 | .if t 16\(mu(2\u\s731\s10\d\(mi1). | ||
| 65 | .if n 16*((2**31)\(mi1). | ||
| 66 | .Pp | ||
| 67 | The | ||
| 68 | .Fn random Ns / Fn srandom | ||
| 69 | have (almost) the same calling sequence and initialization properties as | ||
| 70 | .Xr rand 3 Ns / Xr srand 3 . | ||
| 71 | The difference is that | ||
| 72 | .Xr rand | ||
| 73 | 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 | ||
| 75 | .Fn random | ||
| 76 | are usable. For example, | ||
| 77 | .Sq Li random()&01 | ||
| 78 | will produce a random binary | ||
| 79 | value. | ||
| 80 | .Pp | ||
| 81 | Unlike | ||
| 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 , | ||
| 89 | however, | ||
| 90 | .Fn random | ||
| 91 | will by default produce a sequence of numbers that can be duplicated | ||
| 92 | by calling | ||
| 93 | .Fn srandom | ||
| 94 | with | ||
| 95 | .Ql 1 | ||
| 96 | as the seed. | ||
| 97 | .Pp | ||
| 98 | The | ||
| 99 | .Fn initstate | ||
| 100 | 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 | ||
| 102 | .Fn initstate | ||
| 103 | to decide how sophisticated a random number generator it should use \(em the | ||
| 104 | more state, the better the random numbers will be. | ||
| 105 | (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 | ||
| 107 | the nearest known amount. Using less than 8 bytes will cause an error.) | ||
| 108 | The seed for the initialization (which specifies a starting point for | ||
| 109 | the random number sequence, and provides for restarting at the same | ||
| 110 | point) is also an argument. | ||
| 111 | The | ||
| 112 | .Fn initstate | ||
| 113 | function | ||
| 114 | returns a pointer to the previous state information array. | ||
| 115 | .Pp | ||
| 116 | Once a state has been initialized, the | ||
| 117 | .Fn setstate | ||
| 118 | routine provides for rapid switching between states. | ||
| 119 | The | ||
| 120 | .Fn setstate | ||
| 121 | function | ||
| 122 | returns a pointer to the previous state array; its | ||
| 123 | argument state array is used for further random number generation | ||
| 124 | until the next call to | ||
| 125 | .Fn initstate | ||
| 126 | or | ||
| 127 | .Fn setstate . | ||
| 128 | .Pp | ||
| 129 | Once a state array has been initialized, it may be restarted at a | ||
| 130 | different point either by calling | ||
| 131 | .Fn initstate | ||
| 132 | (with the desired seed, the state array, and its size) or by calling | ||
| 133 | both | ||
| 134 | .Fn setstate | ||
| 135 | (with the state array) and | ||
| 136 | .Fn srandom | ||
| 137 | (with the desired seed). | ||
| 138 | The advantage of calling both | ||
| 139 | .Fn setstate | ||
| 140 | and | ||
| 141 | .Fn srandom | ||
| 142 | is that the size of the state array does not have to be remembered after | ||
| 143 | it is initialized. | ||
| 144 | .Pp | ||
| 145 | With 256 bytes of state information, the period of the random number | ||
| 146 | generator is greater than | ||
| 147 | .if t 2\u\s769\s10\d, | ||
| 148 | .if n 2**69 | ||
| 149 | which should be sufficient for most purposes. | ||
| 150 | .Sh AUTHOR | ||
| 151 | Earl T. Cohen | ||
| 152 | .Sh DIAGNOSTICS | ||
| 153 | If | ||
| 154 | .Fn initstate | ||
| 155 | is called with less than 8 bytes of state information, or if | ||
| 156 | .Fn setstate | ||
| 157 | detects that the state information has been garbled, error | ||
| 158 | messages are printed on the standard error output. | ||
| 159 | .Sh SEE ALSO | ||
| 160 | .Xr rand 3 | ||
| 161 | .Sh HISTORY | ||
| 162 | These | ||
| 163 | functions appeared in | ||
| 164 | .Bx 4.2 . | ||
| 165 | .Sh BUGS | ||
| 166 | About 2/3 the speed of | ||
| 167 | .Xr rand 3 . | ||
diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c new file mode 100644 index 0000000000..469b6d976a --- /dev/null +++ b/src/lib/libc/stdlib/random.c | |||
| @@ -0,0 +1,364 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1983 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)random.c 5.9 (Berkeley) 2/23/91";*/ | ||
| 36 | static char *rcsid = "$Id: random.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $"; | ||
| 37 | #endif /* LIBC_SCCS and not lint */ | ||
| 38 | |||
| 39 | #include <stdio.h> | ||
| 40 | #include <stdlib.h> | ||
| 41 | |||
| 42 | /* | ||
| 43 | * random.c: | ||
| 44 | * | ||
| 45 | * An improved random number generation package. In addition to the standard | ||
| 46 | * rand()/srand() like interface, this package also has a special state info | ||
| 47 | * interface. The initstate() routine is called with a seed, an array of | ||
| 48 | * bytes, and a count of how many bytes are being passed in; this array is | ||
| 49 | * then initialized to contain information for random number generation with | ||
| 50 | * that much state information. Good sizes for the amount of state | ||
| 51 | * information are 32, 64, 128, and 256 bytes. The state can be switched by | ||
| 52 | * calling the setstate() routine with the same array as was initiallized | ||
| 53 | * with initstate(). By default, the package runs with 128 bytes of state | ||
| 54 | * information and generates far better random numbers than a linear | ||
| 55 | * congruential generator. If the amount of state information is less than | ||
| 56 | * 32 bytes, a simple linear congruential R.N.G. is used. | ||
| 57 | * | ||
| 58 | * Internally, the state information is treated as an array of longs; the | ||
| 59 | * zeroeth element of the array is the type of R.N.G. being used (small | ||
| 60 | * integer); the remainder of the array is the state information for the | ||
| 61 | * R.N.G. Thus, 32 bytes of state information will give 7 longs worth of | ||
| 62 | * state information, which will allow a degree seven polynomial. (Note: | ||
| 63 | * the zeroeth word of state information also has some other information | ||
| 64 | * stored in it -- see setstate() for details). | ||
| 65 | * | ||
| 66 | * The random number generation technique is a linear feedback shift register | ||
| 67 | * approach, employing trinomials (since there are fewer terms to sum up that | ||
| 68 | * way). In this approach, the least significant bit of all the numbers in | ||
| 69 | * the state table will act as a linear feedback shift register, and will | ||
| 70 | * have period 2^deg - 1 (where deg is the degree of the polynomial being | ||
| 71 | * used, assuming that the polynomial is irreducible and primitive). The | ||
| 72 | * higher order bits will have longer periods, since their values are also | ||
| 73 | * influenced by pseudo-random carries out of the lower bits. The total | ||
| 74 | * period of the generator is approximately deg*(2**deg - 1); thus doubling | ||
| 75 | * the amount of state information has a vast influence on the period of the | ||
| 76 | * generator. Note: the deg*(2**deg - 1) is an approximation only good for | ||
| 77 | * large deg, when the period of the shift register is the dominant factor. | ||
| 78 | * With deg equal to seven, the period is actually much longer than the | ||
| 79 | * 7*(2**7 - 1) predicted by this formula. | ||
| 80 | */ | ||
| 81 | |||
| 82 | /* | ||
| 83 | * For each of the currently supported random number generators, we have a | ||
| 84 | * break value on the amount of state information (you need at least this | ||
| 85 | * many bytes of state info to support this random number generator), a degree | ||
| 86 | * for the polynomial (actually a trinomial) that the R.N.G. is based on, and | ||
| 87 | * the separation between the two lower order coefficients of the trinomial. | ||
| 88 | */ | ||
| 89 | #define TYPE_0 0 /* linear congruential */ | ||
| 90 | #define BREAK_0 8 | ||
| 91 | #define DEG_0 0 | ||
| 92 | #define SEP_0 0 | ||
| 93 | |||
| 94 | #define TYPE_1 1 /* x**7 + x**3 + 1 */ | ||
| 95 | #define BREAK_1 32 | ||
| 96 | #define DEG_1 7 | ||
| 97 | #define SEP_1 3 | ||
| 98 | |||
| 99 | #define TYPE_2 2 /* x**15 + x + 1 */ | ||
| 100 | #define BREAK_2 64 | ||
| 101 | #define DEG_2 15 | ||
| 102 | #define SEP_2 1 | ||
| 103 | |||
| 104 | #define TYPE_3 3 /* x**31 + x**3 + 1 */ | ||
| 105 | #define BREAK_3 128 | ||
| 106 | #define DEG_3 31 | ||
| 107 | #define SEP_3 3 | ||
| 108 | |||
| 109 | #define TYPE_4 4 /* x**63 + x + 1 */ | ||
| 110 | #define BREAK_4 256 | ||
| 111 | #define DEG_4 63 | ||
| 112 | #define SEP_4 1 | ||
| 113 | |||
| 114 | /* | ||
| 115 | * Array versions of the above information to make code run faster -- | ||
| 116 | * relies on fact that TYPE_i == i. | ||
| 117 | */ | ||
| 118 | #define MAX_TYPES 5 /* max number of types above */ | ||
| 119 | |||
| 120 | static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; | ||
| 121 | static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; | ||
| 122 | |||
| 123 | /* | ||
| 124 | * Initially, everything is set up as if from: | ||
| 125 | * | ||
| 126 | * initstate(1, &randtbl, 128); | ||
| 127 | * | ||
| 128 | * Note that this initialization takes advantage of the fact that srandom() | ||
| 129 | * advances the front and rear pointers 10*rand_deg times, and hence the | ||
| 130 | * rear pointer which starts at 0 will also end up at zero; thus the zeroeth | ||
| 131 | * element of the state information, which contains info about the current | ||
| 132 | * position of the rear pointer is just | ||
| 133 | * | ||
| 134 | * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. | ||
| 135 | */ | ||
| 136 | |||
| 137 | static long randtbl[DEG_3 + 1] = { | ||
| 138 | TYPE_3, | ||
| 139 | 0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342, 0xde3b81e0, 0xdf0a6fb5, | ||
| 140 | 0xf103bc02, 0x48f340fb, 0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd, | ||
| 141 | 0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86, 0xda672e2a, 0x1588ca88, | ||
| 142 | 0xe369735d, 0x904f35f7, 0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc, | ||
| 143 | 0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b, 0xf5ad9d0e, 0x8999220b, | ||
| 144 | 0x27fb47b9, | ||
| 145 | }; | ||
| 146 | |||
| 147 | /* | ||
| 148 | * fptr and rptr are two pointers into the state info, a front and a rear | ||
| 149 | * pointer. These two pointers are always rand_sep places aparts, as they | ||
| 150 | * cycle cyclically through the state information. (Yes, this does mean we | ||
| 151 | * could get away with just one pointer, but the code for random() is more | ||
| 152 | * efficient this way). The pointers are left positioned as they would be | ||
| 153 | * from the call | ||
| 154 | * | ||
| 155 | * initstate(1, randtbl, 128); | ||
| 156 | * | ||
| 157 | * (The position of the rear pointer, rptr, is really 0 (as explained above | ||
| 158 | * in the initialization of randtbl) because the state table pointer is set | ||
| 159 | * to point to randtbl[1] (as explained below). | ||
| 160 | */ | ||
| 161 | static long *fptr = &randtbl[SEP_3 + 1]; | ||
| 162 | static long *rptr = &randtbl[1]; | ||
| 163 | |||
| 164 | /* | ||
| 165 | * The following things are the pointer to the state information table, the | ||
| 166 | * type of the current generator, the degree of the current polynomial being | ||
| 167 | * used, and the separation between the two pointers. Note that for efficiency | ||
| 168 | * of random(), we remember the first location of the state information, not | ||
| 169 | * the zeroeth. Hence it is valid to access state[-1], which is used to | ||
| 170 | * store the type of the R.N.G. Also, we remember the last location, since | ||
| 171 | * this is more efficient than indexing every time to find the address of | ||
| 172 | * the last element to see if the front and rear pointers have wrapped. | ||
| 173 | */ | ||
| 174 | static long *state = &randtbl[1]; | ||
| 175 | static int rand_type = TYPE_3; | ||
| 176 | static int rand_deg = DEG_3; | ||
| 177 | static int rand_sep = SEP_3; | ||
| 178 | static long *end_ptr = &randtbl[DEG_3 + 1]; | ||
| 179 | |||
| 180 | /* | ||
| 181 | * srandom: | ||
| 182 | * | ||
| 183 | * Initialize the random number generator based on the given seed. If the | ||
| 184 | * type is the trivial no-state-information type, just remember the seed. | ||
| 185 | * Otherwise, initializes state[] based on the given "seed" via a linear | ||
| 186 | * congruential generator. Then, the pointers are set to known locations | ||
| 187 | * that are exactly rand_sep places apart. Lastly, it cycles the state | ||
| 188 | * information a given number of times to get rid of any initial dependencies | ||
| 189 | * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] | ||
| 190 | * for default usage relies on values produced by this routine. | ||
| 191 | */ | ||
| 192 | void | ||
| 193 | srandom(x) | ||
| 194 | u_int x; | ||
| 195 | { | ||
| 196 | register int i, j; | ||
| 197 | |||
| 198 | if (rand_type == TYPE_0) | ||
| 199 | state[0] = x; | ||
| 200 | else { | ||
| 201 | j = 1; | ||
| 202 | state[0] = x; | ||
| 203 | for (i = 1; i < rand_deg; i++) | ||
| 204 | state[i] = 1103515245 * state[i - 1] + 12345; | ||
| 205 | fptr = &state[rand_sep]; | ||
| 206 | rptr = &state[0]; | ||
| 207 | for (i = 0; i < 10 * rand_deg; i++) | ||
| 208 | (void)random(); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | /* | ||
| 213 | * initstate: | ||
| 214 | * | ||
| 215 | * Initialize the state information in the given array of n bytes for future | ||
| 216 | * random number generation. Based on the number of bytes we are given, and | ||
| 217 | * the break values for the different R.N.G.'s, we choose the best (largest) | ||
| 218 | * one we can and set things up for it. srandom() is then called to | ||
| 219 | * initialize the state information. | ||
| 220 | * | ||
| 221 | * Note that on return from srandom(), we set state[-1] to be the type | ||
| 222 | * multiplexed with the current value of the rear pointer; this is so | ||
| 223 | * successive calls to initstate() won't lose this information and will be | ||
| 224 | * able to restart with setstate(). | ||
| 225 | * | ||
| 226 | * Note: the first thing we do is save the current state, if any, just like | ||
| 227 | * setstate() so that it doesn't matter when initstate is called. | ||
| 228 | * | ||
| 229 | * Returns a pointer to the old state. | ||
| 230 | */ | ||
| 231 | char * | ||
| 232 | initstate(seed, arg_state, 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 | { | ||
| 237 | register char *ostate = (char *)(&state[-1]); | ||
| 238 | |||
| 239 | if (rand_type == TYPE_0) | ||
| 240 | state[-1] = rand_type; | ||
| 241 | else | ||
| 242 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
| 243 | if (n < BREAK_0) { | ||
| 244 | (void)fprintf(stderr, | ||
| 245 | "random: not enough state (%d bytes); ignored.\n", n); | ||
| 246 | return(0); | ||
| 247 | } | ||
| 248 | if (n < BREAK_1) { | ||
| 249 | rand_type = TYPE_0; | ||
| 250 | rand_deg = DEG_0; | ||
| 251 | rand_sep = SEP_0; | ||
| 252 | } else if (n < BREAK_2) { | ||
| 253 | rand_type = TYPE_1; | ||
| 254 | rand_deg = DEG_1; | ||
| 255 | rand_sep = SEP_1; | ||
| 256 | } else if (n < BREAK_3) { | ||
| 257 | rand_type = TYPE_2; | ||
| 258 | rand_deg = DEG_2; | ||
| 259 | rand_sep = SEP_2; | ||
| 260 | } else if (n < BREAK_4) { | ||
| 261 | rand_type = TYPE_3; | ||
| 262 | rand_deg = DEG_3; | ||
| 263 | rand_sep = SEP_3; | ||
| 264 | } else { | ||
| 265 | rand_type = TYPE_4; | ||
| 266 | rand_deg = DEG_4; | ||
| 267 | rand_sep = SEP_4; | ||
| 268 | } | ||
| 269 | state = &(((long *)arg_state)[1]); /* first location */ | ||
| 270 | end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ | ||
| 271 | srandom(seed); | ||
| 272 | if (rand_type == TYPE_0) | ||
| 273 | state[-1] = rand_type; | ||
| 274 | else | ||
| 275 | state[-1] = MAX_TYPES*(rptr - state) + rand_type; | ||
| 276 | return(ostate); | ||
| 277 | } | ||
| 278 | |||
| 279 | /* | ||
| 280 | * setstate: | ||
| 281 | * | ||
| 282 | * Restore the state from the given state array. | ||
| 283 | * | ||
| 284 | * Note: it is important that we also remember the locations of the pointers | ||
| 285 | * in the current state information, and restore the locations of the pointers | ||
| 286 | * from the old state information. This is done by multiplexing the pointer | ||
| 287 | * location into the zeroeth word of the state information. | ||
| 288 | * | ||
| 289 | * Note that due to the order in which things are done, it is OK to call | ||
| 290 | * setstate() with the same state as the current state. | ||
| 291 | * | ||
| 292 | * Returns a pointer to the old state information. | ||
| 293 | */ | ||
| 294 | char * | ||
| 295 | setstate(arg_state) | ||
| 296 | char *arg_state; | ||
| 297 | { | ||
| 298 | register long *new_state = (long *)arg_state; | ||
| 299 | register int type = new_state[0] % MAX_TYPES; | ||
| 300 | register int rear = new_state[0] / MAX_TYPES; | ||
| 301 | char *ostate = (char *)(&state[-1]); | ||
| 302 | |||
| 303 | if (rand_type == TYPE_0) | ||
| 304 | state[-1] = rand_type; | ||
| 305 | else | ||
| 306 | state[-1] = MAX_TYPES * (rptr - state) + rand_type; | ||
| 307 | switch(type) { | ||
| 308 | case TYPE_0: | ||
| 309 | case TYPE_1: | ||
| 310 | case TYPE_2: | ||
| 311 | case TYPE_3: | ||
| 312 | case TYPE_4: | ||
| 313 | rand_type = type; | ||
| 314 | rand_deg = degrees[type]; | ||
| 315 | rand_sep = seps[type]; | ||
| 316 | break; | ||
| 317 | default: | ||
| 318 | (void)fprintf(stderr, | ||
| 319 | "random: state info corrupted; not changed.\n"); | ||
| 320 | } | ||
| 321 | state = &new_state[1]; | ||
| 322 | if (rand_type != TYPE_0) { | ||
| 323 | rptr = &state[rear]; | ||
| 324 | fptr = &state[(rear + rand_sep) % rand_deg]; | ||
| 325 | } | ||
| 326 | end_ptr = &state[rand_deg]; /* set end_ptr too */ | ||
| 327 | return(ostate); | ||
| 328 | } | ||
| 329 | |||
| 330 | /* | ||
| 331 | * random: | ||
| 332 | * | ||
| 333 | * If we are using the trivial TYPE_0 R.N.G., just do the old linear | ||
| 334 | * congruential bit. Otherwise, we do our fancy trinomial stuff, which is | ||
| 335 | * the same in all the other cases due to all the global variables that have | ||
| 336 | * been set up. The basic operation is to add the number at the rear pointer | ||
| 337 | * into the one at the front pointer. Then both pointers are advanced to | ||
| 338 | * the next location cyclically in the table. The value returned is the sum | ||
| 339 | * generated, reduced to 31 bits by throwing away the "least random" low bit. | ||
| 340 | * | ||
| 341 | * Note: the code takes advantage of the fact that both the front and | ||
| 342 | * rear pointers can't wrap on the same call by not testing the rear | ||
| 343 | * pointer if the front one has wrapped. | ||
| 344 | * | ||
| 345 | * Returns a 31-bit random number. | ||
| 346 | */ | ||
| 347 | long | ||
| 348 | random() | ||
| 349 | { | ||
| 350 | long i; | ||
| 351 | |||
| 352 | if (rand_type == TYPE_0) | ||
| 353 | i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; | ||
| 354 | else { | ||
| 355 | *fptr += *rptr; | ||
| 356 | i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ | ||
| 357 | if (++fptr >= end_ptr) { | ||
| 358 | fptr = state; | ||
| 359 | ++rptr; | ||
| 360 | } else if (++rptr >= end_ptr) | ||
| 361 | rptr = state; | ||
| 362 | } | ||
| 363 | return(i); | ||
| 364 | } | ||
diff --git a/src/lib/libc/stdlib/realloc.3 b/src/lib/libc/stdlib/realloc.3 new file mode 100644 index 0000000000..66f09b2081 --- /dev/null +++ b/src/lib/libc/stdlib/realloc.3 | |||
| @@ -0,0 +1,100 @@ | |||
| 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 new file mode 100644 index 0000000000..9d8b1ff2ce --- /dev/null +++ b/src/lib/libc/stdlib/realpath.3 | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | .\" Copyright (c) 1994 | ||
| 2 | .\" The Regents of the University of California. All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" Jan-Simon Pendry. | ||
| 6 | .\" | ||
| 7 | .\" Redistribution and use in source and binary forms, with or without | ||
| 8 | .\" modification, are permitted provided that the following conditions | ||
| 9 | .\" are met: | ||
| 10 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 11 | .\" notice, this list of conditions and the following disclaimer. | ||
| 12 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 14 | .\" documentation and/or other materials provided with the distribution. | ||
| 15 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 16 | .\" must display the following acknowledgement: | ||
| 17 | .\" This product includes software developed by the University of | ||
| 18 | .\" California, Berkeley and its contributors. | ||
| 19 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 20 | .\" may be used to endorse or promote products derived from this software | ||
| 21 | .\" without specific prior written permission. | ||
| 22 | .\" | ||
| 23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 33 | .\" SUCH DAMAGE. | ||
| 34 | .\" | ||
| 35 | .\" from: @(#)realpath.3 8.2 (Berkeley) 2/16/94 | ||
| 36 | .\" $Id: realpath.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 37 | .\" | ||
| 38 | .Dd "February 16, 1994" | ||
| 39 | .Dt REALPATH 3 | ||
| 40 | .Os | ||
| 41 | .Sh NAME | ||
| 42 | .Nm realpath | ||
| 43 | .Nd returns the canonicalized absolute pathname | ||
| 44 | .Sh SYNOPSIS | ||
| 45 | .Fd #include <sys/param.h> | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft "char *" | ||
| 48 | .Fn realpath "const char *pathname" "char resolvedname[MAXPATHLEN]" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn realpath | ||
| 52 | function resolves all symbolic links, extra | ||
| 53 | .Dq / | ||
| 54 | characters and references to | ||
| 55 | .Pa /./ | ||
| 56 | and | ||
| 57 | .Pa /../ | ||
| 58 | in | ||
| 59 | .Fa pathname , | ||
| 60 | and copies the resulting absolute pathname into | ||
| 61 | the memory referenced by | ||
| 62 | .Fa resolvedname . | ||
| 63 | The | ||
| 64 | .Fa resolvedname | ||
| 65 | argument | ||
| 66 | .Em must | ||
| 67 | refer to a buffer capable of storing at least | ||
| 68 | .Dv MAXPATHLEN | ||
| 69 | characters. | ||
| 70 | .Pp | ||
| 71 | The | ||
| 72 | .Fn realpath | ||
| 73 | function will resolve both absolute and relative paths | ||
| 74 | and return the absolute pathname corresponding to | ||
| 75 | .Fa pathname . | ||
| 76 | All but the last component of | ||
| 77 | .Fa pathname | ||
| 78 | must exist when | ||
| 79 | .Fn realpath | ||
| 80 | is called. | ||
| 81 | .Sh "RETURN VALUES" | ||
| 82 | The | ||
| 83 | .Fn realpath | ||
| 84 | function returns | ||
| 85 | .Fa resolved_name | ||
| 86 | on success. | ||
| 87 | If an error occurs, | ||
| 88 | .Fn realpath | ||
| 89 | returns | ||
| 90 | .Dv NULL , | ||
| 91 | and | ||
| 92 | .Fa resolved_name | ||
| 93 | contains the pathname which caused the problem. | ||
| 94 | .Sh ERRORS | ||
| 95 | The function | ||
| 96 | .Fn realpath | ||
| 97 | may fail and set the external variable | ||
| 98 | .Va errno | ||
| 99 | 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 , | ||
| 104 | .Xr open 2 , | ||
| 105 | .Xr readlink 2 | ||
| 106 | and | ||
| 107 | .Xr getcwd 3 . | ||
| 108 | .Sh CAVEATS | ||
| 109 | This implementation of | ||
| 110 | .Fn realpath | ||
| 111 | differs slightly from the Solaris implementation. | ||
| 112 | The | ||
| 113 | .Bx 4.4 | ||
| 114 | version always returns absolute pathnames, | ||
| 115 | whereas the Solaris implementation will, | ||
| 116 | under certain circumstances, return a relative | ||
| 117 | .Fa resolved_path | ||
| 118 | when given a relative | ||
| 119 | .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 new file mode 100644 index 0000000000..e349b7e068 --- /dev/null +++ b/src/lib/libc/stdlib/realpath.c | |||
| @@ -0,0 +1,159 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1994 | ||
| 3 | * The Regents of the University of California. All rights reserved. | ||
| 4 | * | ||
| 5 | * This code is derived from software contributed to Berkeley by | ||
| 6 | * Jan-Simon Pendry. | ||
| 7 | * | ||
| 8 | * Redistribution and use in source and binary forms, with or without | ||
| 9 | * modification, are permitted provided that the following conditions | ||
| 10 | * are met: | ||
| 11 | * 1. Redistributions of source code must retain the above copyright | ||
| 12 | * notice, this list of conditions and the following disclaimer. | ||
| 13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | * notice, this list of conditions and the following disclaimer in the | ||
| 15 | * documentation and/or other materials provided with the distribution. | ||
| 16 | * 3. All advertising materials mentioning features or use of this software | ||
| 17 | * must display the following acknowledgement: | ||
| 18 | * This product includes software developed by the University of | ||
| 19 | * California, Berkeley and its contributors. | ||
| 20 | * 4. Neither the name of the University nor the names of its contributors | ||
| 21 | * may be used to endorse or promote products derived from this software | ||
| 22 | * without specific prior written permission. | ||
| 23 | * | ||
| 24 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | * SUCH DAMAGE. | ||
| 35 | */ | ||
| 36 | |||
| 37 | #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> | ||
| 43 | #include <sys/stat.h> | ||
| 44 | |||
| 45 | #include <errno.h> | ||
| 46 | #include <fcntl.h> | ||
| 47 | #include <stdlib.h> | ||
| 48 | #include <string.h> | ||
| 49 | #include <unistd.h> | ||
| 50 | |||
| 51 | /* | ||
| 52 | * char *realpath(const char *path, char resolved_path[MAXPATHLEN]); | ||
| 53 | * | ||
| 54 | * Find the real name of path, by removing all ".", ".." and symlink | ||
| 55 | * components. Returns (resolved) on success, or (NULL) on failure, | ||
| 56 | * in which case the path which caused trouble is left in (resolved). | ||
| 57 | */ | ||
| 58 | char * | ||
| 59 | realpath(path, resolved) | ||
| 60 | const char *path; | ||
| 61 | char *resolved; | ||
| 62 | { | ||
| 63 | struct stat sb; | ||
| 64 | int fd, n, rootd, serrno; | ||
| 65 | char *p, *q, wbuf[MAXPATHLEN]; | ||
| 66 | |||
| 67 | /* Save the starting point. */ | ||
| 68 | if ((fd = open(".", O_RDONLY)) < 0) { | ||
| 69 | (void)strcpy(resolved, "."); | ||
| 70 | return (NULL); | ||
| 71 | } | ||
| 72 | |||
| 73 | /* | ||
| 74 | * Find the dirname and basename from the path to be resolved. | ||
| 75 | * Change directory to the dirname component. | ||
| 76 | * lstat the basename part. | ||
| 77 | * if it is a symlink, read in the value and loop. | ||
| 78 | * if it is a directory, then change to that directory. | ||
| 79 | * get the current directory name and append the basename. | ||
| 80 | */ | ||
| 81 | (void)strncpy(resolved, path, MAXPATHLEN - 1); | ||
| 82 | resolved[MAXPATHLEN - 1] = '\0'; | ||
| 83 | loop: | ||
| 84 | q = strrchr(resolved, '/'); | ||
| 85 | if (q != NULL) { | ||
| 86 | p = q + 1; | ||
| 87 | if (q == resolved) | ||
| 88 | q = "/"; | ||
| 89 | else { | ||
| 90 | do { | ||
| 91 | --q; | ||
| 92 | } while (q > resolved && *q == '/'); | ||
| 93 | q[1] = '\0'; | ||
| 94 | q = resolved; | ||
| 95 | } | ||
| 96 | if (chdir(q) < 0) | ||
| 97 | goto err1; | ||
| 98 | } else | ||
| 99 | p = resolved; | ||
| 100 | |||
| 101 | /* Deal with the last component. */ | ||
| 102 | if (lstat(p, &sb) == 0) { | ||
| 103 | if (S_ISLNK(sb.st_mode)) { | ||
| 104 | n = readlink(p, resolved, MAXPATHLEN); | ||
| 105 | if (n < 0) | ||
| 106 | goto err1; | ||
| 107 | resolved[n] = '\0'; | ||
| 108 | goto loop; | ||
| 109 | } | ||
| 110 | if (S_ISDIR(sb.st_mode)) { | ||
| 111 | if (chdir(p) < 0) | ||
| 112 | goto err1; | ||
| 113 | p = ""; | ||
| 114 | } | ||
| 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 | |||
| 125 | /* | ||
| 126 | * Join the two strings together, ensuring that the right thing | ||
| 127 | * happens if the last component is empty, or the dirname is root. | ||
| 128 | */ | ||
| 129 | if (resolved[0] == '/' && resolved[1] == '\0') | ||
| 130 | rootd = 1; | ||
| 131 | else | ||
| 132 | rootd = 0; | ||
| 133 | |||
| 134 | if (*wbuf) { | ||
| 135 | if (strlen(resolved) + strlen(wbuf) + rootd + 1 > MAXPATHLEN) { | ||
| 136 | errno = ENAMETOOLONG; | ||
| 137 | goto err1; | ||
| 138 | } | ||
| 139 | if (rootd == 0) | ||
| 140 | (void)strcat(resolved, "/"); | ||
| 141 | (void)strcat(resolved, wbuf); | ||
| 142 | } | ||
| 143 | |||
| 144 | /* Go back to where we came from. */ | ||
| 145 | if (fchdir(fd) < 0) { | ||
| 146 | serrno = errno; | ||
| 147 | goto err2; | ||
| 148 | } | ||
| 149 | |||
| 150 | /* It's okay if the close fails, what's an fd more or less? */ | ||
| 151 | (void)close(fd); | ||
| 152 | return (resolved); | ||
| 153 | |||
| 154 | err1: serrno = errno; | ||
| 155 | (void)fchdir(fd); | ||
| 156 | err2: (void)close(fd); | ||
| 157 | errno = serrno; | ||
| 158 | return (NULL); | ||
| 159 | } | ||
diff --git a/src/lib/libc/stdlib/seed48.c b/src/lib/libc/stdlib/seed48.c new file mode 100644 index 0000000000..e3d31901dd --- /dev/null +++ b/src/lib/libc/stdlib/seed48.c | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | extern unsigned short __rand48_mult[3]; | ||
| 18 | extern unsigned short __rand48_add; | ||
| 19 | |||
| 20 | unsigned short * | ||
| 21 | seed48(unsigned short xseed[3]) | ||
| 22 | { | ||
| 23 | static unsigned short sseed[3]; | ||
| 24 | |||
| 25 | sseed[0] = __rand48_seed[0]; | ||
| 26 | sseed[1] = __rand48_seed[1]; | ||
| 27 | sseed[2] = __rand48_seed[2]; | ||
| 28 | __rand48_seed[0] = xseed[0]; | ||
| 29 | __rand48_seed[1] = xseed[1]; | ||
| 30 | __rand48_seed[2] = xseed[2]; | ||
| 31 | __rand48_mult[0] = RAND48_MULT_0; | ||
| 32 | __rand48_mult[1] = RAND48_MULT_1; | ||
| 33 | __rand48_mult[2] = RAND48_MULT_2; | ||
| 34 | __rand48_add = RAND48_ADD; | ||
| 35 | return sseed; | ||
| 36 | } | ||
diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c new file mode 100644 index 0000000000..a36669888d --- /dev/null +++ b/src/lib/libc/stdlib/setenv.c | |||
| @@ -0,0 +1,120 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1987 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <string.h> | ||
| 41 | |||
| 42 | /* | ||
| 43 | * setenv -- | ||
| 44 | * Set the value of the environmental variable "name" to be | ||
| 45 | * "value". If rewrite is set, replace any current value. | ||
| 46 | */ | ||
| 47 | int | ||
| 48 | setenv(name, value, rewrite) | ||
| 49 | register const char *name; | ||
| 50 | register const char *value; | ||
| 51 | int rewrite; | ||
| 52 | { | ||
| 53 | extern char **environ; | ||
| 54 | static int alloced; /* if allocated space before */ | ||
| 55 | register char *C; | ||
| 56 | int l_value, offset; | ||
| 57 | char *__findenv(); | ||
| 58 | |||
| 59 | if (*value == '=') /* no `=' in value */ | ||
| 60 | ++value; | ||
| 61 | l_value = strlen(value); | ||
| 62 | if ((C = __findenv(name, &offset))) { /* find if already exists */ | ||
| 63 | if (!rewrite) | ||
| 64 | return (0); | ||
| 65 | if (strlen(C) >= l_value) { /* old larger; copy over */ | ||
| 66 | while (*C++ = *value++); | ||
| 67 | return (0); | ||
| 68 | } | ||
| 69 | } else { /* create new slot */ | ||
| 70 | register int cnt; | ||
| 71 | register char **P; | ||
| 72 | |||
| 73 | for (P = environ, cnt = 0; *P; ++P, ++cnt); | ||
| 74 | if (alloced) { /* just increase size */ | ||
| 75 | environ = (char **)realloc((char *)environ, | ||
| 76 | (size_t)(sizeof(char *) * (cnt + 2))); | ||
| 77 | if (!environ) | ||
| 78 | return (-1); | ||
| 79 | } | ||
| 80 | else { /* get new space */ | ||
| 81 | alloced = 1; /* copy old entries into it */ | ||
| 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; | ||
| 91 | } | ||
| 92 | for (C = (char *)name; *C && *C != '='; ++C); /* no `=' in name */ | ||
| 93 | if (!(environ[offset] = /* name + `=' + value */ | ||
| 94 | malloc((size_t)((int)(C - name) + l_value + 2)))) | ||
| 95 | return (-1); | ||
| 96 | for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) | ||
| 97 | ; | ||
| 98 | for (*C++ = '='; *C++ = *value++; ) | ||
| 99 | ; | ||
| 100 | return (0); | ||
| 101 | } | ||
| 102 | |||
| 103 | /* | ||
| 104 | * unsetenv(name) -- | ||
| 105 | * Delete environmental variable "name". | ||
| 106 | */ | ||
| 107 | void | ||
| 108 | unsetenv(name) | ||
| 109 | const char *name; | ||
| 110 | { | ||
| 111 | extern char **environ; | ||
| 112 | register char **P; | ||
| 113 | int offset; | ||
| 114 | char *__findenv(); | ||
| 115 | |||
| 116 | while (__findenv(name, &offset)) /* if set multiple times */ | ||
| 117 | for (P = &environ[offset];; ++P) | ||
| 118 | if (!(*P = *(P + 1))) | ||
| 119 | break; | ||
| 120 | } | ||
diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c new file mode 100644 index 0000000000..daf733f93e --- /dev/null +++ b/src/lib/libc/stdlib/srand48.c | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1993 Martin Birgmeier | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * You may redistribute unmodified or modified versions of this source | ||
| 6 | * code provided that the above copyright notice and this and the | ||
| 7 | * following conditions are retained. | ||
| 8 | * | ||
| 9 | * This software is provided ``as is'', and comes with no warranties | ||
| 10 | * of any kind. I shall in no event be liable for anything that happens | ||
| 11 | * to anyone/anything when using this software. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include "rand48.h" | ||
| 15 | |||
| 16 | extern unsigned short __rand48_seed[3]; | ||
| 17 | extern unsigned short __rand48_mult[3]; | ||
| 18 | extern unsigned short __rand48_add; | ||
| 19 | |||
| 20 | void | ||
| 21 | srand48(long seed) | ||
| 22 | { | ||
| 23 | __rand48_seed[0] = RAND48_SEED_0; | ||
| 24 | __rand48_seed[1] = (unsigned short) seed; | ||
| 25 | __rand48_seed[2] = (unsigned short) (seed >> 16); | ||
| 26 | __rand48_mult[0] = RAND48_MULT_0; | ||
| 27 | __rand48_mult[1] = RAND48_MULT_1; | ||
| 28 | __rand48_mult[2] = RAND48_MULT_2; | ||
| 29 | __rand48_add = RAND48_ADD; | ||
| 30 | } | ||
diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3 new file mode 100644 index 0000000000..0b7f973857 --- /dev/null +++ b/src/lib/libc/stdlib/strtod.3 | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strtod.3 5.3 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: strtod.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt STRTOD 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strtod | ||
| 44 | .Nd convert | ||
| 45 | .Tn ASCII | ||
| 46 | string to double | ||
| 47 | .Sh SYNOPSIS | ||
| 48 | .Fd #include <stdlib.h> | ||
| 49 | .Ft double | ||
| 50 | .Fn strtod "const char *nptr" "char **endptr" | ||
| 51 | .Sh DESCRIPTION | ||
| 52 | The | ||
| 53 | .Fn strtod | ||
| 54 | function converts the initial portion of the string | ||
| 55 | pointed to by | ||
| 56 | .Fa nptr | ||
| 57 | to | ||
| 58 | .Em double | ||
| 59 | representation. | ||
| 60 | .Pp | ||
| 61 | The expected form of the string is an optional plus (``+'') or minus | ||
| 62 | sign (``-'') followed by a sequence of digits optionally containing | ||
| 63 | a decimal-point character, optionally followed by an exponent. | ||
| 64 | An exponent consists of an ``E'' or ``e'', followed by an optional plus | ||
| 65 | or minus sign, followed by a sequence of digits. | ||
| 66 | .Pp | ||
| 67 | Leading white-space characters in the string (as defined by the | ||
| 68 | .Xr isspace 3 | ||
| 69 | function) are skipped. | ||
| 70 | .Sh RETURN VALUES | ||
| 71 | The | ||
| 72 | .Fn strtod | ||
| 73 | function returns the converted value, if any. | ||
| 74 | .Pp | ||
| 75 | If | ||
| 76 | .Fa endptr | ||
| 77 | is not | ||
| 78 | .Dv NULL , | ||
| 79 | a pointer to the character after the last character used | ||
| 80 | in the conversion is stored in the location referenced by | ||
| 81 | .Fa endptr . | ||
| 82 | .Pp | ||
| 83 | If no conversion is performed, zero is returned and the value of | ||
| 84 | .Fa nptr | ||
| 85 | is stored in the location referenced by | ||
| 86 | .Fa endptr . | ||
| 87 | .Pp | ||
| 88 | If the correct value would cause overflow, plus or minus | ||
| 89 | .Dv HUGE_VAL | ||
| 90 | is returned (according to the sign of the value), and | ||
| 91 | .Dv ERANGE | ||
| 92 | is stored in | ||
| 93 | .Va errno . | ||
| 94 | If the correct value would cause underflow, zero is | ||
| 95 | returned and | ||
| 96 | .Dv ERANGE | ||
| 97 | is stored in | ||
| 98 | .Va errno . | ||
| 99 | .Sh ERRORS | ||
| 100 | .Bl -tag -width Er | ||
| 101 | .It Bq Er ERANGE | ||
| 102 | Overflow or underflow occurred. | ||
| 103 | .Sh SEE ALSO | ||
| 104 | .Xr atof 3 , | ||
| 105 | .Xr atoi 3 , | ||
| 106 | .Xr atol 3 , | ||
| 107 | .Xr strtol 3 , | ||
| 108 | .Xr strtoul 3 | ||
| 109 | .Sh STANDARDS | ||
| 110 | The | ||
| 111 | .Fn strtod | ||
| 112 | function | ||
| 113 | conforms to | ||
| 114 | .St -ansiC . | ||
diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c new file mode 100644 index 0000000000..b13fa128f5 --- /dev/null +++ b/src/lib/libc/stdlib/strtod.c | |||
| @@ -0,0 +1,2499 @@ | |||
| 1 | /**************************************************************** | ||
| 2 | * | ||
| 3 | * The author of this software is David M. Gay. | ||
| 4 | * | ||
| 5 | * Copyright (c) 1991 by AT&T. | ||
| 6 | * | ||
| 7 | * Permission to use, copy, modify, and distribute this software for any | ||
| 8 | * purpose without fee is hereby granted, provided that this entire notice | ||
| 9 | * is included in all copies of any software which is or includes a copy | ||
| 10 | * or modification of this software and in all copies of the supporting | ||
| 11 | * documentation for such software. | ||
| 12 | * | ||
| 13 | * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED | ||
| 14 | * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY | ||
| 15 | * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY | ||
| 16 | * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. | ||
| 17 | * | ||
| 18 | ***************************************************************/ | ||
| 19 | |||
| 20 | /* Please send bug reports to | ||
| 21 | David M. Gay | ||
| 22 | AT&T Bell Laboratories, Room 2C-463 | ||
| 23 | 600 Mountain Avenue | ||
| 24 | Murray Hill, NJ 07974-2070 | ||
| 25 | U.S.A. | ||
| 26 | dmg@research.att.com or research!dmg | ||
| 27 | */ | ||
| 28 | |||
| 29 | /* strtod for IEEE-, VAX-, and IBM-arithmetic machines. | ||
| 30 | * | ||
| 31 | * This strtod returns a nearest machine number to the input decimal | ||
| 32 | * string (or sets errno to ERANGE). With IEEE arithmetic, ties are | ||
| 33 | * broken by the IEEE round-even rule. Otherwise ties are broken by | ||
| 34 | * biased rounding (add half and chop). | ||
| 35 | * | ||
| 36 | * Inspired loosely by William D. Clinger's paper "How to Read Floating | ||
| 37 | * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. | ||
| 38 | * | ||
| 39 | * Modifications: | ||
| 40 | * | ||
| 41 | * 1. We only require IEEE, IBM, or VAX double-precision | ||
| 42 | * arithmetic (not IEEE double-extended). | ||
| 43 | * 2. We get by with floating-point arithmetic in a case that | ||
| 44 | * Clinger missed -- when we're computing d * 10^n | ||
| 45 | * for a small integer d and the integer n is not too | ||
| 46 | * much larger than 22 (the maximum integer k for which | ||
| 47 | * we can represent 10^k exactly), we may be able to | ||
| 48 | * compute (d*10^k) * 10^(e-k) with just one roundoff. | ||
| 49 | * 3. Rather than a bit-at-a-time adjustment of the binary | ||
| 50 | * result in the hard case, we use floating-point | ||
| 51 | * arithmetic to determine the adjustment to within | ||
| 52 | * one bit; only in really hard cases do we need to | ||
| 53 | * compute a second residual. | ||
| 54 | * 4. Because of 3., we don't need a large table of powers of 10 | ||
| 55 | * for ten-to-e (just some small tables, e.g. of 10^k | ||
| 56 | * for 0 <= k <= 22). | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least | ||
| 61 | * significant byte has the lowest address. | ||
| 62 | * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most | ||
| 63 | * significant byte has the lowest address. | ||
| 64 | * #define Long int on machines with 32-bit ints and 64-bit longs. | ||
| 65 | * #define Sudden_Underflow for IEEE-format machines without gradual | ||
| 66 | * underflow (i.e., that flush to zero on underflow). | ||
| 67 | * #define IBM for IBM mainframe-style floating-point arithmetic. | ||
| 68 | * #define VAX for VAX-style floating-point arithmetic. | ||
| 69 | * #define Unsigned_Shifts if >> does treats its left operand as unsigned. | ||
| 70 | * #define No_leftright to omit left-right logic in fast floating-point | ||
| 71 | * computation of dtoa. | ||
| 72 | * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3. | ||
| 73 | * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines | ||
| 74 | * that use extended-precision instructions to compute rounded | ||
| 75 | * products and quotients) with IBM. | ||
| 76 | * #define ROUND_BIASED for IEEE-format with biased rounding. | ||
| 77 | * #define Inaccurate_Divide for IEEE-format with correctly rounded | ||
| 78 | * products but inaccurate quotients, e.g., for Intel i860. | ||
| 79 | * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision | ||
| 80 | * integer arithmetic. Whether this speeds things up or slows things | ||
| 81 | * down depends on the machine and the number being converted. | ||
| 82 | * #define KR_headers for old-style C function headers. | ||
| 83 | * #define Bad_float_h if your system lacks a float.h or if it does not | ||
| 84 | * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, | ||
| 85 | * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. | ||
| 86 | * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) | ||
| 87 | * if memory is available and otherwise does something you deem | ||
| 88 | * appropriate. If MALLOC is undefined, malloc will be invoked | ||
| 89 | * directly -- and assumed always to succeed. | ||
| 90 | */ | ||
| 91 | |||
| 92 | #if defined(LIBC_SCCS) && !defined(lint) | ||
| 93 | static char *rcsid = "$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__) || \ | ||
| 97 | defined(__mips__) || defined(__ns32k__) || defined(__alpha__) | ||
| 98 | #include <machine/endian.h> | ||
| 99 | #if BYTE_ORDER == BIG_ENDIAN | ||
| 100 | #define IEEE_BIG_ENDIAN | ||
| 101 | #else | ||
| 102 | #define IEEE_LITTLE_ENDIAN | ||
| 103 | #endif | ||
| 104 | #endif | ||
| 105 | |||
| 106 | #ifdef vax | ||
| 107 | #define VAX | ||
| 108 | #endif | ||
| 109 | |||
| 110 | #define Long int32_t | ||
| 111 | #define ULong u_int32_t | ||
| 112 | |||
| 113 | #ifdef DEBUG | ||
| 114 | #include "stdio.h" | ||
| 115 | #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} | ||
| 116 | #endif | ||
| 117 | |||
| 118 | #ifdef __cplusplus | ||
| 119 | #include "malloc.h" | ||
| 120 | #include "memory.h" | ||
| 121 | #else | ||
| 122 | #ifndef KR_headers | ||
| 123 | #include "stdlib.h" | ||
| 124 | #include "string.h" | ||
| 125 | #include "locale.h" | ||
| 126 | #else | ||
| 127 | #include "malloc.h" | ||
| 128 | #include "memory.h" | ||
| 129 | #endif | ||
| 130 | #endif | ||
| 131 | |||
| 132 | #ifdef MALLOC | ||
| 133 | #ifdef KR_headers | ||
| 134 | extern char *MALLOC(); | ||
| 135 | #else | ||
| 136 | extern void *MALLOC(size_t); | ||
| 137 | #endif | ||
| 138 | #else | ||
| 139 | #define MALLOC malloc | ||
| 140 | #endif | ||
| 141 | |||
| 142 | #include "ctype.h" | ||
| 143 | #include "errno.h" | ||
| 144 | |||
| 145 | #ifdef Bad_float_h | ||
| 146 | #undef __STDC__ | ||
| 147 | #ifdef IEEE_BIG_ENDIAN | ||
| 148 | #define IEEE_ARITHMETIC | ||
| 149 | #endif | ||
| 150 | #ifdef IEEE_LITTLE_ENDIAN | ||
| 151 | #define IEEE_ARITHMETIC | ||
| 152 | #endif | ||
| 153 | |||
| 154 | #ifdef IEEE_ARITHMETIC | ||
| 155 | #define DBL_DIG 15 | ||
| 156 | #define DBL_MAX_10_EXP 308 | ||
| 157 | #define DBL_MAX_EXP 1024 | ||
| 158 | #define FLT_RADIX 2 | ||
| 159 | #define FLT_ROUNDS 1 | ||
| 160 | #define DBL_MAX 1.7976931348623157e+308 | ||
| 161 | #endif | ||
| 162 | |||
| 163 | #ifdef IBM | ||
| 164 | #define DBL_DIG 16 | ||
| 165 | #define DBL_MAX_10_EXP 75 | ||
| 166 | #define DBL_MAX_EXP 63 | ||
| 167 | #define FLT_RADIX 16 | ||
| 168 | #define FLT_ROUNDS 0 | ||
| 169 | #define DBL_MAX 7.2370055773322621e+75 | ||
| 170 | #endif | ||
| 171 | |||
| 172 | #ifdef VAX | ||
| 173 | #define DBL_DIG 16 | ||
| 174 | #define DBL_MAX_10_EXP 38 | ||
| 175 | #define DBL_MAX_EXP 127 | ||
| 176 | #define FLT_RADIX 2 | ||
| 177 | #define FLT_ROUNDS 1 | ||
| 178 | #define DBL_MAX 1.7014118346046923e+38 | ||
| 179 | #endif | ||
| 180 | |||
| 181 | #ifndef LONG_MAX | ||
| 182 | #define LONG_MAX 2147483647 | ||
| 183 | #endif | ||
| 184 | #else | ||
| 185 | #include "float.h" | ||
| 186 | #endif | ||
| 187 | #ifndef __MATH_H__ | ||
| 188 | #include "math.h" | ||
| 189 | #endif | ||
| 190 | |||
| 191 | #ifdef __cplusplus | ||
| 192 | extern "C" { | ||
| 193 | #endif | ||
| 194 | |||
| 195 | #ifndef CONST | ||
| 196 | #ifdef KR_headers | ||
| 197 | #define CONST /* blank */ | ||
| 198 | #else | ||
| 199 | #define CONST const | ||
| 200 | #endif | ||
| 201 | #endif | ||
| 202 | |||
| 203 | #ifdef Unsigned_Shifts | ||
| 204 | #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; | ||
| 205 | #else | ||
| 206 | #define Sign_Extend(a,b) /*no-op*/ | ||
| 207 | #endif | ||
| 208 | |||
| 209 | #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \ | ||
| 210 | defined(IBM) != 1 | ||
| 211 | Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or | ||
| 212 | IBM should be defined. | ||
| 213 | #endif | ||
| 214 | |||
| 215 | #ifdef IEEE_LITTLE_ENDIAN | ||
| 216 | #define word0(x) ((ULong *)&x)[1] | ||
| 217 | #define word1(x) ((ULong *)&x)[0] | ||
| 218 | #else | ||
| 219 | #define word0(x) ((ULong *)&x)[0] | ||
| 220 | #define word1(x) ((ULong *)&x)[1] | ||
| 221 | #endif | ||
| 222 | |||
| 223 | /* The following definition of Storeinc is appropriate for MIPS processors. | ||
| 224 | * An alternative that might be better on some machines is | ||
| 225 | * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) | ||
| 226 | */ | ||
| 227 | #if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) | ||
| 228 | #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ | ||
| 229 | ((unsigned short *)a)[0] = (unsigned short)c, a++) | ||
| 230 | #else | ||
| 231 | #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ | ||
| 232 | ((unsigned short *)a)[1] = (unsigned short)c, a++) | ||
| 233 | #endif | ||
| 234 | |||
| 235 | /* #define P DBL_MANT_DIG */ | ||
| 236 | /* Ten_pmax = floor(P*log(2)/log(5)) */ | ||
| 237 | /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ | ||
| 238 | /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ | ||
| 239 | /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ | ||
| 240 | |||
| 241 | #if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) | ||
| 242 | #define Exp_shift 20 | ||
| 243 | #define Exp_shift1 20 | ||
| 244 | #define Exp_msk1 0x100000 | ||
| 245 | #define Exp_msk11 0x100000 | ||
| 246 | #define Exp_mask 0x7ff00000 | ||
| 247 | #define P 53 | ||
| 248 | #define Bias 1023 | ||
| 249 | #define IEEE_Arith | ||
| 250 | #define Emin (-1022) | ||
| 251 | #define Exp_1 0x3ff00000 | ||
| 252 | #define Exp_11 0x3ff00000 | ||
| 253 | #define Ebits 11 | ||
| 254 | #define Frac_mask 0xfffff | ||
| 255 | #define Frac_mask1 0xfffff | ||
| 256 | #define Ten_pmax 22 | ||
| 257 | #define Bletch 0x10 | ||
| 258 | #define Bndry_mask 0xfffff | ||
| 259 | #define Bndry_mask1 0xfffff | ||
| 260 | #define LSB 1 | ||
| 261 | #define Sign_bit 0x80000000 | ||
| 262 | #define Log2P 1 | ||
| 263 | #define Tiny0 0 | ||
| 264 | #define Tiny1 1 | ||
| 265 | #define Quick_max 14 | ||
| 266 | #define Int_max 14 | ||
| 267 | #define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */ | ||
| 268 | #else | ||
| 269 | #undef Sudden_Underflow | ||
| 270 | #define Sudden_Underflow | ||
| 271 | #ifdef IBM | ||
| 272 | #define Exp_shift 24 | ||
| 273 | #define Exp_shift1 24 | ||
| 274 | #define Exp_msk1 0x1000000 | ||
| 275 | #define Exp_msk11 0x1000000 | ||
| 276 | #define Exp_mask 0x7f000000 | ||
| 277 | #define P 14 | ||
| 278 | #define Bias 65 | ||
| 279 | #define Exp_1 0x41000000 | ||
| 280 | #define Exp_11 0x41000000 | ||
| 281 | #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ | ||
| 282 | #define Frac_mask 0xffffff | ||
| 283 | #define Frac_mask1 0xffffff | ||
| 284 | #define Bletch 4 | ||
| 285 | #define Ten_pmax 22 | ||
| 286 | #define Bndry_mask 0xefffff | ||
| 287 | #define Bndry_mask1 0xffffff | ||
| 288 | #define LSB 1 | ||
| 289 | #define Sign_bit 0x80000000 | ||
| 290 | #define Log2P 4 | ||
| 291 | #define Tiny0 0x100000 | ||
| 292 | #define Tiny1 0 | ||
| 293 | #define Quick_max 14 | ||
| 294 | #define Int_max 15 | ||
| 295 | #else /* VAX */ | ||
| 296 | #define Exp_shift 23 | ||
| 297 | #define Exp_shift1 7 | ||
| 298 | #define Exp_msk1 0x80 | ||
| 299 | #define Exp_msk11 0x800000 | ||
| 300 | #define Exp_mask 0x7f80 | ||
| 301 | #define P 56 | ||
| 302 | #define Bias 129 | ||
| 303 | #define Exp_1 0x40800000 | ||
| 304 | #define Exp_11 0x4080 | ||
| 305 | #define Ebits 8 | ||
| 306 | #define Frac_mask 0x7fffff | ||
| 307 | #define Frac_mask1 0xffff007f | ||
| 308 | #define Ten_pmax 24 | ||
| 309 | #define Bletch 2 | ||
| 310 | #define Bndry_mask 0xffff007f | ||
| 311 | #define Bndry_mask1 0xffff007f | ||
| 312 | #define LSB 0x10000 | ||
| 313 | #define Sign_bit 0x8000 | ||
| 314 | #define Log2P 1 | ||
| 315 | #define Tiny0 0x80 | ||
| 316 | #define Tiny1 0 | ||
| 317 | #define Quick_max 15 | ||
| 318 | #define Int_max 15 | ||
| 319 | #endif | ||
| 320 | #endif | ||
| 321 | |||
| 322 | #ifndef IEEE_Arith | ||
| 323 | #define ROUND_BIASED | ||
| 324 | #endif | ||
| 325 | |||
| 326 | #ifdef RND_PRODQUOT | ||
| 327 | #define rounded_product(a,b) a = rnd_prod(a, b) | ||
| 328 | #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); | ||
| 333 | #endif | ||
| 334 | #else | ||
| 335 | #define rounded_product(a,b) a *= b | ||
| 336 | #define rounded_quotient(a,b) a /= b | ||
| 337 | #endif | ||
| 338 | |||
| 339 | #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) | ||
| 340 | #define Big1 0xffffffff | ||
| 341 | |||
| 342 | #ifndef Just_16 | ||
| 343 | /* When Pack_32 is not defined, we store 16 bits per 32-bit Long. | ||
| 344 | * This makes some inner loops simpler and sometimes saves work | ||
| 345 | * during multiplications, but it often seems to make things slightly | ||
| 346 | * slower. Hence the default is now to store 32 bits per Long. | ||
| 347 | */ | ||
| 348 | #ifndef Pack_32 | ||
| 349 | #define Pack_32 | ||
| 350 | #endif | ||
| 351 | #endif | ||
| 352 | |||
| 353 | #define Kmax 15 | ||
| 354 | |||
| 355 | #ifdef __cplusplus | ||
| 356 | extern "C" double strtod(const char *s00, char **se); | ||
| 357 | extern "C" char *__dtoa(double d, int mode, int ndigits, | ||
| 358 | int *decpt, int *sign, char **rve); | ||
| 359 | #endif | ||
| 360 | |||
| 361 | struct | ||
| 362 | Bigint { | ||
| 363 | struct Bigint *next; | ||
| 364 | int k, maxwds, sign, wds; | ||
| 365 | ULong x[1]; | ||
| 366 | }; | ||
| 367 | |||
| 368 | typedef struct Bigint Bigint; | ||
| 369 | |||
| 370 | static Bigint *freelist[Kmax+1]; | ||
| 371 | |||
| 372 | static Bigint * | ||
| 373 | Balloc | ||
| 374 | #ifdef KR_headers | ||
| 375 | (k) int k; | ||
| 376 | #else | ||
| 377 | (int k) | ||
| 378 | #endif | ||
| 379 | { | ||
| 380 | int x; | ||
| 381 | Bigint *rv; | ||
| 382 | |||
| 383 | if (rv = freelist[k]) { | ||
| 384 | freelist[k] = rv->next; | ||
| 385 | } | ||
| 386 | else { | ||
| 387 | x = 1 << k; | ||
| 388 | rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)); | ||
| 389 | rv->k = k; | ||
| 390 | rv->maxwds = x; | ||
| 391 | } | ||
| 392 | rv->sign = rv->wds = 0; | ||
| 393 | return rv; | ||
| 394 | } | ||
| 395 | |||
| 396 | static void | ||
| 397 | Bfree | ||
| 398 | #ifdef KR_headers | ||
| 399 | (v) Bigint *v; | ||
| 400 | #else | ||
| 401 | (Bigint *v) | ||
| 402 | #endif | ||
| 403 | { | ||
| 404 | if (v) { | ||
| 405 | v->next = freelist[v->k]; | ||
| 406 | freelist[v->k] = v; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | |||
| 410 | #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ | ||
| 411 | y->wds*sizeof(Long) + 2*sizeof(int)) | ||
| 412 | |||
| 413 | static Bigint * | ||
| 414 | multadd | ||
| 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 | { | ||
| 421 | int i, wds; | ||
| 422 | ULong *x, y; | ||
| 423 | #ifdef Pack_32 | ||
| 424 | ULong xi, z; | ||
| 425 | #endif | ||
| 426 | Bigint *b1; | ||
| 427 | |||
| 428 | wds = b->wds; | ||
| 429 | x = b->x; | ||
| 430 | i = 0; | ||
| 431 | do { | ||
| 432 | #ifdef Pack_32 | ||
| 433 | xi = *x; | ||
| 434 | y = (xi & 0xffff) * m + a; | ||
| 435 | z = (xi >> 16) * m + (y >> 16); | ||
| 436 | a = (int)(z >> 16); | ||
| 437 | *x++ = (z << 16) + (y & 0xffff); | ||
| 438 | #else | ||
| 439 | y = *x * m + a; | ||
| 440 | a = (int)(y >> 16); | ||
| 441 | *x++ = y & 0xffff; | ||
| 442 | #endif | ||
| 443 | } | ||
| 444 | while(++i < wds); | ||
| 445 | if (a) { | ||
| 446 | if (wds >= b->maxwds) { | ||
| 447 | b1 = Balloc(b->k+1); | ||
| 448 | Bcopy(b1, b); | ||
| 449 | Bfree(b); | ||
| 450 | b = b1; | ||
| 451 | } | ||
| 452 | b->x[wds++] = a; | ||
| 453 | b->wds = wds; | ||
| 454 | } | ||
| 455 | return b; | ||
| 456 | } | ||
| 457 | |||
| 458 | static Bigint * | ||
| 459 | s2b | ||
| 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 | { | ||
| 466 | Bigint *b; | ||
| 467 | int i, k; | ||
| 468 | Long x, y; | ||
| 469 | |||
| 470 | x = (nd + 8) / 9; | ||
| 471 | for(k = 0, y = 1; x > y; y <<= 1, k++) ; | ||
| 472 | #ifdef Pack_32 | ||
| 473 | b = Balloc(k); | ||
| 474 | b->x[0] = y9; | ||
| 475 | b->wds = 1; | ||
| 476 | #else | ||
| 477 | b = Balloc(k+1); | ||
| 478 | b->x[0] = y9 & 0xffff; | ||
| 479 | b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; | ||
| 480 | #endif | ||
| 481 | |||
| 482 | i = 9; | ||
| 483 | if (9 < nd0) { | ||
| 484 | s += 9; | ||
| 485 | do b = multadd(b, 10, *s++ - '0'); | ||
| 486 | while(++i < nd0); | ||
| 487 | s++; | ||
| 488 | } | ||
| 489 | else | ||
| 490 | s += 10; | ||
| 491 | for(; i < nd; i++) | ||
| 492 | b = multadd(b, 10, *s++ - '0'); | ||
| 493 | return b; | ||
| 494 | } | ||
| 495 | |||
| 496 | static int | ||
| 497 | hi0bits | ||
| 498 | #ifdef KR_headers | ||
| 499 | (x) register ULong x; | ||
| 500 | #else | ||
| 501 | (register ULong x) | ||
| 502 | #endif | ||
| 503 | { | ||
| 504 | register int k = 0; | ||
| 505 | |||
| 506 | if (!(x & 0xffff0000)) { | ||
| 507 | k = 16; | ||
| 508 | x <<= 16; | ||
| 509 | } | ||
| 510 | if (!(x & 0xff000000)) { | ||
| 511 | k += 8; | ||
| 512 | x <<= 8; | ||
| 513 | } | ||
| 514 | if (!(x & 0xf0000000)) { | ||
| 515 | k += 4; | ||
| 516 | x <<= 4; | ||
| 517 | } | ||
| 518 | if (!(x & 0xc0000000)) { | ||
| 519 | k += 2; | ||
| 520 | x <<= 2; | ||
| 521 | } | ||
| 522 | if (!(x & 0x80000000)) { | ||
| 523 | k++; | ||
| 524 | if (!(x & 0x40000000)) | ||
| 525 | return 32; | ||
| 526 | } | ||
| 527 | return k; | ||
| 528 | } | ||
| 529 | |||
| 530 | static int | ||
| 531 | lo0bits | ||
| 532 | #ifdef KR_headers | ||
| 533 | (y) ULong *y; | ||
| 534 | #else | ||
| 535 | (ULong *y) | ||
| 536 | #endif | ||
| 537 | { | ||
| 538 | register int k; | ||
| 539 | register ULong x = *y; | ||
| 540 | |||
| 541 | if (x & 7) { | ||
| 542 | if (x & 1) | ||
| 543 | return 0; | ||
| 544 | if (x & 2) { | ||
| 545 | *y = x >> 1; | ||
| 546 | return 1; | ||
| 547 | } | ||
| 548 | *y = x >> 2; | ||
| 549 | return 2; | ||
| 550 | } | ||
| 551 | k = 0; | ||
| 552 | if (!(x & 0xffff)) { | ||
| 553 | k = 16; | ||
| 554 | x >>= 16; | ||
| 555 | } | ||
| 556 | if (!(x & 0xff)) { | ||
| 557 | k += 8; | ||
| 558 | x >>= 8; | ||
| 559 | } | ||
| 560 | if (!(x & 0xf)) { | ||
| 561 | k += 4; | ||
| 562 | x >>= 4; | ||
| 563 | } | ||
| 564 | if (!(x & 0x3)) { | ||
| 565 | k += 2; | ||
| 566 | x >>= 2; | ||
| 567 | } | ||
| 568 | if (!(x & 1)) { | ||
| 569 | k++; | ||
| 570 | x >>= 1; | ||
| 571 | if (!x & 1) | ||
| 572 | return 32; | ||
| 573 | } | ||
| 574 | *y = x; | ||
| 575 | return k; | ||
| 576 | } | ||
| 577 | |||
| 578 | static Bigint * | ||
| 579 | i2b | ||
| 580 | #ifdef KR_headers | ||
| 581 | (i) int i; | ||
| 582 | #else | ||
| 583 | (int i) | ||
| 584 | #endif | ||
| 585 | { | ||
| 586 | Bigint *b; | ||
| 587 | |||
| 588 | b = Balloc(1); | ||
| 589 | b->x[0] = i; | ||
| 590 | b->wds = 1; | ||
| 591 | return b; | ||
| 592 | } | ||
| 593 | |||
| 594 | static Bigint * | ||
| 595 | mult | ||
| 596 | #ifdef KR_headers | ||
| 597 | (a, b) Bigint *a, *b; | ||
| 598 | #else | ||
| 599 | (Bigint *a, Bigint *b) | ||
| 600 | #endif | ||
| 601 | { | ||
| 602 | Bigint *c; | ||
| 603 | int k, wa, wb, wc; | ||
| 604 | ULong carry, y, z; | ||
| 605 | ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; | ||
| 606 | #ifdef Pack_32 | ||
| 607 | ULong z2; | ||
| 608 | #endif | ||
| 609 | |||
| 610 | if (a->wds < b->wds) { | ||
| 611 | c = a; | ||
| 612 | a = b; | ||
| 613 | b = c; | ||
| 614 | } | ||
| 615 | k = a->k; | ||
| 616 | wa = a->wds; | ||
| 617 | wb = b->wds; | ||
| 618 | wc = wa + wb; | ||
| 619 | if (wc > a->maxwds) | ||
| 620 | k++; | ||
| 621 | c = Balloc(k); | ||
| 622 | for(x = c->x, xa = x + wc; x < xa; x++) | ||
| 623 | *x = 0; | ||
| 624 | xa = a->x; | ||
| 625 | xae = xa + wa; | ||
| 626 | xb = b->x; | ||
| 627 | xbe = xb + wb; | ||
| 628 | xc0 = c->x; | ||
| 629 | #ifdef Pack_32 | ||
| 630 | for(; xb < xbe; xb++, xc0++) { | ||
| 631 | if (y = *xb & 0xffff) { | ||
| 632 | x = xa; | ||
| 633 | xc = xc0; | ||
| 634 | carry = 0; | ||
| 635 | do { | ||
| 636 | z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; | ||
| 637 | carry = z >> 16; | ||
| 638 | z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; | ||
| 639 | carry = z2 >> 16; | ||
| 640 | Storeinc(xc, z2, z); | ||
| 641 | } | ||
| 642 | while(x < xae); | ||
| 643 | *xc = carry; | ||
| 644 | } | ||
| 645 | if (y = *xb >> 16) { | ||
| 646 | x = xa; | ||
| 647 | xc = xc0; | ||
| 648 | carry = 0; | ||
| 649 | z2 = *xc; | ||
| 650 | do { | ||
| 651 | z = (*x & 0xffff) * y + (*xc >> 16) + carry; | ||
| 652 | carry = z >> 16; | ||
| 653 | Storeinc(xc, z, z2); | ||
| 654 | z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; | ||
| 655 | carry = z2 >> 16; | ||
| 656 | } | ||
| 657 | while(x < xae); | ||
| 658 | *xc = z2; | ||
| 659 | } | ||
| 660 | } | ||
| 661 | #else | ||
| 662 | for(; xb < xbe; xc0++) { | ||
| 663 | if (y = *xb++) { | ||
| 664 | x = xa; | ||
| 665 | xc = xc0; | ||
| 666 | carry = 0; | ||
| 667 | do { | ||
| 668 | z = *x++ * y + *xc + carry; | ||
| 669 | carry = z >> 16; | ||
| 670 | *xc++ = z & 0xffff; | ||
| 671 | } | ||
| 672 | while(x < xae); | ||
| 673 | *xc = carry; | ||
| 674 | } | ||
| 675 | } | ||
| 676 | #endif | ||
| 677 | for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; | ||
| 678 | c->wds = wc; | ||
| 679 | return c; | ||
| 680 | } | ||
| 681 | |||
| 682 | static Bigint *p5s; | ||
| 683 | |||
| 684 | static Bigint * | ||
| 685 | pow5mult | ||
| 686 | #ifdef KR_headers | ||
| 687 | (b, k) Bigint *b; int k; | ||
| 688 | #else | ||
| 689 | (Bigint *b, int k) | ||
| 690 | #endif | ||
| 691 | { | ||
| 692 | Bigint *b1, *p5, *p51; | ||
| 693 | int i; | ||
| 694 | static int p05[3] = { 5, 25, 125 }; | ||
| 695 | |||
| 696 | if (i = k & 3) | ||
| 697 | b = multadd(b, p05[i-1], 0); | ||
| 698 | |||
| 699 | if (!(k >>= 2)) | ||
| 700 | return b; | ||
| 701 | if (!(p5 = p5s)) { | ||
| 702 | /* first time */ | ||
| 703 | p5 = p5s = i2b(625); | ||
| 704 | p5->next = 0; | ||
| 705 | } | ||
| 706 | for(;;) { | ||
| 707 | if (k & 1) { | ||
| 708 | b1 = mult(b, p5); | ||
| 709 | Bfree(b); | ||
| 710 | b = b1; | ||
| 711 | } | ||
| 712 | if (!(k >>= 1)) | ||
| 713 | break; | ||
| 714 | if (!(p51 = p5->next)) { | ||
| 715 | p51 = p5->next = mult(p5,p5); | ||
| 716 | p51->next = 0; | ||
| 717 | } | ||
| 718 | p5 = p51; | ||
| 719 | } | ||
| 720 | return b; | ||
| 721 | } | ||
| 722 | |||
| 723 | static Bigint * | ||
| 724 | lshift | ||
| 725 | #ifdef KR_headers | ||
| 726 | (b, k) Bigint *b; int k; | ||
| 727 | #else | ||
| 728 | (Bigint *b, int k) | ||
| 729 | #endif | ||
| 730 | { | ||
| 731 | int i, k1, n, n1; | ||
| 732 | Bigint *b1; | ||
| 733 | ULong *x, *x1, *xe, z; | ||
| 734 | |||
| 735 | #ifdef Pack_32 | ||
| 736 | n = k >> 5; | ||
| 737 | #else | ||
| 738 | n = k >> 4; | ||
| 739 | #endif | ||
| 740 | k1 = b->k; | ||
| 741 | n1 = n + b->wds + 1; | ||
| 742 | for(i = b->maxwds; n1 > i; i <<= 1) | ||
| 743 | k1++; | ||
| 744 | b1 = Balloc(k1); | ||
| 745 | x1 = b1->x; | ||
| 746 | for(i = 0; i < n; i++) | ||
| 747 | *x1++ = 0; | ||
| 748 | x = b->x; | ||
| 749 | xe = x + b->wds; | ||
| 750 | #ifdef Pack_32 | ||
| 751 | if (k &= 0x1f) { | ||
| 752 | k1 = 32 - k; | ||
| 753 | z = 0; | ||
| 754 | do { | ||
| 755 | *x1++ = *x << k | z; | ||
| 756 | z = *x++ >> k1; | ||
| 757 | } | ||
| 758 | while(x < xe); | ||
| 759 | if (*x1 = z) | ||
| 760 | ++n1; | ||
| 761 | } | ||
| 762 | #else | ||
| 763 | if (k &= 0xf) { | ||
| 764 | k1 = 16 - k; | ||
| 765 | z = 0; | ||
| 766 | do { | ||
| 767 | *x1++ = *x << k & 0xffff | z; | ||
| 768 | z = *x++ >> k1; | ||
| 769 | } | ||
| 770 | while(x < xe); | ||
| 771 | if (*x1 = z) | ||
| 772 | ++n1; | ||
| 773 | } | ||
| 774 | #endif | ||
| 775 | else do | ||
| 776 | *x1++ = *x++; | ||
| 777 | while(x < xe); | ||
| 778 | b1->wds = n1 - 1; | ||
| 779 | Bfree(b); | ||
| 780 | return b1; | ||
| 781 | } | ||
| 782 | |||
| 783 | static int | ||
| 784 | cmp | ||
| 785 | #ifdef KR_headers | ||
| 786 | (a, b) Bigint *a, *b; | ||
| 787 | #else | ||
| 788 | (Bigint *a, Bigint *b) | ||
| 789 | #endif | ||
| 790 | { | ||
| 791 | ULong *xa, *xa0, *xb, *xb0; | ||
| 792 | int i, j; | ||
| 793 | |||
| 794 | i = a->wds; | ||
| 795 | j = b->wds; | ||
| 796 | #ifdef DEBUG | ||
| 797 | if (i > 1 && !a->x[i-1]) | ||
| 798 | Bug("cmp called with a->x[a->wds-1] == 0"); | ||
| 799 | if (j > 1 && !b->x[j-1]) | ||
| 800 | Bug("cmp called with b->x[b->wds-1] == 0"); | ||
| 801 | #endif | ||
| 802 | if (i -= j) | ||
| 803 | return i; | ||
| 804 | xa0 = a->x; | ||
| 805 | xa = xa0 + j; | ||
| 806 | xb0 = b->x; | ||
| 807 | xb = xb0 + j; | ||
| 808 | for(;;) { | ||
| 809 | if (*--xa != *--xb) | ||
| 810 | return *xa < *xb ? -1 : 1; | ||
| 811 | if (xa <= xa0) | ||
| 812 | break; | ||
| 813 | } | ||
| 814 | return 0; | ||
| 815 | } | ||
| 816 | |||
| 817 | static Bigint * | ||
| 818 | diff | ||
| 819 | #ifdef KR_headers | ||
| 820 | (a, b) Bigint *a, *b; | ||
| 821 | #else | ||
| 822 | (Bigint *a, Bigint *b) | ||
| 823 | #endif | ||
| 824 | { | ||
| 825 | Bigint *c; | ||
| 826 | int i, wa, wb; | ||
| 827 | Long borrow, y; /* We need signed shifts here. */ | ||
| 828 | ULong *xa, *xae, *xb, *xbe, *xc; | ||
| 829 | #ifdef Pack_32 | ||
| 830 | Long z; | ||
| 831 | #endif | ||
| 832 | |||
| 833 | i = cmp(a,b); | ||
| 834 | if (!i) { | ||
| 835 | c = Balloc(0); | ||
| 836 | c->wds = 1; | ||
| 837 | c->x[0] = 0; | ||
| 838 | return c; | ||
| 839 | } | ||
| 840 | if (i < 0) { | ||
| 841 | c = a; | ||
| 842 | a = b; | ||
| 843 | b = c; | ||
| 844 | i = 1; | ||
| 845 | } | ||
| 846 | else | ||
| 847 | i = 0; | ||
| 848 | c = Balloc(a->k); | ||
| 849 | c->sign = i; | ||
| 850 | wa = a->wds; | ||
| 851 | xa = a->x; | ||
| 852 | xae = xa + wa; | ||
| 853 | wb = b->wds; | ||
| 854 | xb = b->x; | ||
| 855 | xbe = xb + wb; | ||
| 856 | xc = c->x; | ||
| 857 | borrow = 0; | ||
| 858 | #ifdef Pack_32 | ||
| 859 | do { | ||
| 860 | y = (*xa & 0xffff) - (*xb & 0xffff) + borrow; | ||
| 861 | borrow = y >> 16; | ||
| 862 | Sign_Extend(borrow, y); | ||
| 863 | z = (*xa++ >> 16) - (*xb++ >> 16) + borrow; | ||
| 864 | borrow = z >> 16; | ||
| 865 | Sign_Extend(borrow, z); | ||
| 866 | Storeinc(xc, z, y); | ||
| 867 | } | ||
| 868 | while(xb < xbe); | ||
| 869 | while(xa < xae) { | ||
| 870 | y = (*xa & 0xffff) + borrow; | ||
| 871 | borrow = y >> 16; | ||
| 872 | Sign_Extend(borrow, y); | ||
| 873 | z = (*xa++ >> 16) + borrow; | ||
| 874 | borrow = z >> 16; | ||
| 875 | Sign_Extend(borrow, z); | ||
| 876 | Storeinc(xc, z, y); | ||
| 877 | } | ||
| 878 | #else | ||
| 879 | do { | ||
| 880 | y = *xa++ - *xb++ + borrow; | ||
| 881 | borrow = y >> 16; | ||
| 882 | Sign_Extend(borrow, y); | ||
| 883 | *xc++ = y & 0xffff; | ||
| 884 | } | ||
| 885 | while(xb < xbe); | ||
| 886 | while(xa < xae) { | ||
| 887 | y = *xa++ + borrow; | ||
| 888 | borrow = y >> 16; | ||
| 889 | Sign_Extend(borrow, y); | ||
| 890 | *xc++ = y & 0xffff; | ||
| 891 | } | ||
| 892 | #endif | ||
| 893 | while(!*--xc) | ||
| 894 | wa--; | ||
| 895 | c->wds = wa; | ||
| 896 | return c; | ||
| 897 | } | ||
| 898 | |||
| 899 | static double | ||
| 900 | ulp | ||
| 901 | #ifdef KR_headers | ||
| 902 | (x) double x; | ||
| 903 | #else | ||
| 904 | (double x) | ||
| 905 | #endif | ||
| 906 | { | ||
| 907 | register Long L; | ||
| 908 | double a; | ||
| 909 | |||
| 910 | L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; | ||
| 911 | #ifndef Sudden_Underflow | ||
| 912 | if (L > 0) { | ||
| 913 | #endif | ||
| 914 | #ifdef IBM | ||
| 915 | L |= Exp_msk1 >> 4; | ||
| 916 | #endif | ||
| 917 | word0(a) = L; | ||
| 918 | word1(a) = 0; | ||
| 919 | #ifndef Sudden_Underflow | ||
| 920 | } | ||
| 921 | else { | ||
| 922 | L = -L >> Exp_shift; | ||
| 923 | if (L < Exp_shift) { | ||
| 924 | word0(a) = 0x80000 >> L; | ||
| 925 | word1(a) = 0; | ||
| 926 | } | ||
| 927 | else { | ||
| 928 | word0(a) = 0; | ||
| 929 | L -= Exp_shift; | ||
| 930 | word1(a) = L >= 31 ? 1 : 1 << 31 - L; | ||
| 931 | } | ||
| 932 | } | ||
| 933 | #endif | ||
| 934 | return a; | ||
| 935 | } | ||
| 936 | |||
| 937 | static double | ||
| 938 | b2d | ||
| 939 | #ifdef KR_headers | ||
| 940 | (a, e) Bigint *a; int *e; | ||
| 941 | #else | ||
| 942 | (Bigint *a, int *e) | ||
| 943 | #endif | ||
| 944 | { | ||
| 945 | ULong *xa, *xa0, w, y, z; | ||
| 946 | int k; | ||
| 947 | double d; | ||
| 948 | #ifdef VAX | ||
| 949 | ULong d0, d1; | ||
| 950 | #else | ||
| 951 | #define d0 word0(d) | ||
| 952 | #define d1 word1(d) | ||
| 953 | #endif | ||
| 954 | |||
| 955 | xa0 = a->x; | ||
| 956 | xa = xa0 + a->wds; | ||
| 957 | y = *--xa; | ||
| 958 | #ifdef DEBUG | ||
| 959 | if (!y) Bug("zero y in b2d"); | ||
| 960 | #endif | ||
| 961 | k = hi0bits(y); | ||
| 962 | *e = 32 - k; | ||
| 963 | #ifdef Pack_32 | ||
| 964 | if (k < Ebits) { | ||
| 965 | d0 = Exp_1 | y >> Ebits - k; | ||
| 966 | w = xa > xa0 ? *--xa : 0; | ||
| 967 | d1 = y << (32-Ebits) + k | w >> Ebits - k; | ||
| 968 | goto ret_d; | ||
| 969 | } | ||
| 970 | z = xa > xa0 ? *--xa : 0; | ||
| 971 | if (k -= Ebits) { | ||
| 972 | d0 = Exp_1 | y << k | z >> 32 - k; | ||
| 973 | y = xa > xa0 ? *--xa : 0; | ||
| 974 | d1 = z << k | y >> 32 - k; | ||
| 975 | } | ||
| 976 | else { | ||
| 977 | d0 = Exp_1 | y; | ||
| 978 | d1 = z; | ||
| 979 | } | ||
| 980 | #else | ||
| 981 | if (k < Ebits + 16) { | ||
| 982 | z = xa > xa0 ? *--xa : 0; | ||
| 983 | d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; | ||
| 984 | w = xa > xa0 ? *--xa : 0; | ||
| 985 | y = xa > xa0 ? *--xa : 0; | ||
| 986 | d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; | ||
| 987 | goto ret_d; | ||
| 988 | } | ||
| 989 | z = xa > xa0 ? *--xa : 0; | ||
| 990 | w = xa > xa0 ? *--xa : 0; | ||
| 991 | k -= Ebits + 16; | ||
| 992 | d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; | ||
| 993 | y = xa > xa0 ? *--xa : 0; | ||
| 994 | d1 = w << k + 16 | y << k; | ||
| 995 | #endif | ||
| 996 | ret_d: | ||
| 997 | #ifdef VAX | ||
| 998 | word0(d) = d0 >> 16 | d0 << 16; | ||
| 999 | word1(d) = d1 >> 16 | d1 << 16; | ||
| 1000 | #else | ||
| 1001 | #undef d0 | ||
| 1002 | #undef d1 | ||
| 1003 | #endif | ||
| 1004 | return d; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | static Bigint * | ||
| 1008 | d2b | ||
| 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 | { | ||
| 1015 | Bigint *b; | ||
| 1016 | int de, i, k; | ||
| 1017 | ULong *x, y, z; | ||
| 1018 | #ifdef VAX | ||
| 1019 | ULong d0, d1; | ||
| 1020 | d0 = word0(d) >> 16 | word0(d) << 16; | ||
| 1021 | d1 = word1(d) >> 16 | word1(d) << 16; | ||
| 1022 | #else | ||
| 1023 | #define d0 word0(d) | ||
| 1024 | #define d1 word1(d) | ||
| 1025 | #endif | ||
| 1026 | |||
| 1027 | #ifdef Pack_32 | ||
| 1028 | b = Balloc(1); | ||
| 1029 | #else | ||
| 1030 | b = Balloc(2); | ||
| 1031 | #endif | ||
| 1032 | x = b->x; | ||
| 1033 | |||
| 1034 | z = d0 & Frac_mask; | ||
| 1035 | d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ | ||
| 1036 | #ifdef Sudden_Underflow | ||
| 1037 | de = (int)(d0 >> Exp_shift); | ||
| 1038 | #ifndef IBM | ||
| 1039 | z |= Exp_msk11; | ||
| 1040 | #endif | ||
| 1041 | #else | ||
| 1042 | if (de = (int)(d0 >> Exp_shift)) | ||
| 1043 | z |= Exp_msk1; | ||
| 1044 | #endif | ||
| 1045 | #ifdef Pack_32 | ||
| 1046 | if (y = d1) { | ||
| 1047 | if (k = lo0bits(&y)) { | ||
| 1048 | x[0] = y | z << 32 - k; | ||
| 1049 | z >>= k; | ||
| 1050 | } | ||
| 1051 | else | ||
| 1052 | x[0] = y; | ||
| 1053 | i = b->wds = (x[1] = z) ? 2 : 1; | ||
| 1054 | } | ||
| 1055 | else { | ||
| 1056 | #ifdef DEBUG | ||
| 1057 | if (!z) | ||
| 1058 | Bug("Zero passed to d2b"); | ||
| 1059 | #endif | ||
| 1060 | k = lo0bits(&z); | ||
| 1061 | x[0] = z; | ||
| 1062 | i = b->wds = 1; | ||
| 1063 | k += 32; | ||
| 1064 | } | ||
| 1065 | #else | ||
| 1066 | if (y = d1) { | ||
| 1067 | if (k = lo0bits(&y)) | ||
| 1068 | if (k >= 16) { | ||
| 1069 | x[0] = y | z << 32 - k & 0xffff; | ||
| 1070 | x[1] = z >> k - 16 & 0xffff; | ||
| 1071 | x[2] = z >> k; | ||
| 1072 | i = 2; | ||
| 1073 | } | ||
| 1074 | else { | ||
| 1075 | x[0] = y & 0xffff; | ||
| 1076 | x[1] = y >> 16 | z << 16 - k & 0xffff; | ||
| 1077 | x[2] = z >> k & 0xffff; | ||
| 1078 | x[3] = z >> k+16; | ||
| 1079 | i = 3; | ||
| 1080 | } | ||
| 1081 | else { | ||
| 1082 | x[0] = y & 0xffff; | ||
| 1083 | x[1] = y >> 16; | ||
| 1084 | x[2] = z & 0xffff; | ||
| 1085 | x[3] = z >> 16; | ||
| 1086 | i = 3; | ||
| 1087 | } | ||
| 1088 | } | ||
| 1089 | else { | ||
| 1090 | #ifdef DEBUG | ||
| 1091 | if (!z) | ||
| 1092 | Bug("Zero passed to d2b"); | ||
| 1093 | #endif | ||
| 1094 | k = lo0bits(&z); | ||
| 1095 | if (k >= 16) { | ||
| 1096 | x[0] = z; | ||
| 1097 | i = 0; | ||
| 1098 | } | ||
| 1099 | else { | ||
| 1100 | x[0] = z & 0xffff; | ||
| 1101 | x[1] = z >> 16; | ||
| 1102 | i = 1; | ||
| 1103 | } | ||
| 1104 | k += 32; | ||
| 1105 | } | ||
| 1106 | while(!x[i]) | ||
| 1107 | --i; | ||
| 1108 | b->wds = i + 1; | ||
| 1109 | #endif | ||
| 1110 | #ifndef Sudden_Underflow | ||
| 1111 | if (de) { | ||
| 1112 | #endif | ||
| 1113 | #ifdef IBM | ||
| 1114 | *e = (de - Bias - (P-1) << 2) + k; | ||
| 1115 | *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); | ||
| 1116 | #else | ||
| 1117 | *e = de - Bias - (P-1) + k; | ||
| 1118 | *bits = P - k; | ||
| 1119 | #endif | ||
| 1120 | #ifndef Sudden_Underflow | ||
| 1121 | } | ||
| 1122 | else { | ||
| 1123 | *e = de - Bias - (P-1) + 1 + k; | ||
| 1124 | #ifdef Pack_32 | ||
| 1125 | *bits = 32*i - hi0bits(x[i-1]); | ||
| 1126 | #else | ||
| 1127 | *bits = (i+2)*16 - hi0bits(x[i]); | ||
| 1128 | #endif | ||
| 1129 | } | ||
| 1130 | #endif | ||
| 1131 | return b; | ||
| 1132 | } | ||
| 1133 | #undef d0 | ||
| 1134 | #undef d1 | ||
| 1135 | |||
| 1136 | static double | ||
| 1137 | ratio | ||
| 1138 | #ifdef KR_headers | ||
| 1139 | (a, b) Bigint *a, *b; | ||
| 1140 | #else | ||
| 1141 | (Bigint *a, Bigint *b) | ||
| 1142 | #endif | ||
| 1143 | { | ||
| 1144 | double da, db; | ||
| 1145 | int k, ka, kb; | ||
| 1146 | |||
| 1147 | da = b2d(a, &ka); | ||
| 1148 | db = b2d(b, &kb); | ||
| 1149 | #ifdef Pack_32 | ||
| 1150 | k = ka - kb + 32*(a->wds - b->wds); | ||
| 1151 | #else | ||
| 1152 | k = ka - kb + 16*(a->wds - b->wds); | ||
| 1153 | #endif | ||
| 1154 | #ifdef IBM | ||
| 1155 | if (k > 0) { | ||
| 1156 | word0(da) += (k >> 2)*Exp_msk1; | ||
| 1157 | if (k &= 3) | ||
| 1158 | da *= 1 << k; | ||
| 1159 | } | ||
| 1160 | else { | ||
| 1161 | k = -k; | ||
| 1162 | word0(db) += (k >> 2)*Exp_msk1; | ||
| 1163 | if (k &= 3) | ||
| 1164 | db *= 1 << k; | ||
| 1165 | } | ||
| 1166 | #else | ||
| 1167 | if (k > 0) | ||
| 1168 | word0(da) += k*Exp_msk1; | ||
| 1169 | else { | ||
| 1170 | k = -k; | ||
| 1171 | word0(db) += k*Exp_msk1; | ||
| 1172 | } | ||
| 1173 | #endif | ||
| 1174 | return da / db; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | static CONST double | ||
| 1178 | tens[] = { | ||
| 1179 | 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, | ||
| 1180 | 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, | ||
| 1181 | 1e20, 1e21, 1e22 | ||
| 1182 | #ifdef VAX | ||
| 1183 | , 1e23, 1e24 | ||
| 1184 | #endif | ||
| 1185 | }; | ||
| 1186 | |||
| 1187 | #ifdef IEEE_Arith | ||
| 1188 | static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; | ||
| 1189 | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 }; | ||
| 1190 | #define n_bigtens 5 | ||
| 1191 | #else | ||
| 1192 | #ifdef IBM | ||
| 1193 | static CONST double bigtens[] = { 1e16, 1e32, 1e64 }; | ||
| 1194 | static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; | ||
| 1195 | #define n_bigtens 3 | ||
| 1196 | #else | ||
| 1197 | static CONST double bigtens[] = { 1e16, 1e32 }; | ||
| 1198 | static CONST double tinytens[] = { 1e-16, 1e-32 }; | ||
| 1199 | #define n_bigtens 2 | ||
| 1200 | #endif | ||
| 1201 | #endif | ||
| 1202 | |||
| 1203 | double | ||
| 1204 | strtod | ||
| 1205 | #ifdef KR_headers | ||
| 1206 | (s00, se) CONST char *s00; char **se; | ||
| 1207 | #else | ||
| 1208 | (CONST char *s00, char **se) | ||
| 1209 | #endif | ||
| 1210 | { | ||
| 1211 | int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, | ||
| 1212 | e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; | ||
| 1213 | CONST char *s, *s0, *s1; | ||
| 1214 | double aadj, aadj1, adj, rv, rv0; | ||
| 1215 | Long L; | ||
| 1216 | ULong y, z; | ||
| 1217 | Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; | ||
| 1218 | |||
| 1219 | #ifndef KR_headers | ||
| 1220 | CONST char decimal_point = localeconv()->decimal_point[0]; | ||
| 1221 | #else | ||
| 1222 | CONST char decimal_point = '.'; | ||
| 1223 | #endif | ||
| 1224 | |||
| 1225 | sign = nz0 = nz = 0; | ||
| 1226 | rv = 0.; | ||
| 1227 | |||
| 1228 | |||
| 1229 | for(s = s00; isspace(*s); s++) | ||
| 1230 | ; | ||
| 1231 | |||
| 1232 | if (*s == '-') { | ||
| 1233 | sign = 1; | ||
| 1234 | s++; | ||
| 1235 | } else if (*s == '+') { | ||
| 1236 | s++; | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | if (*s == '\0') { | ||
| 1240 | s = s00; | ||
| 1241 | goto ret; | ||
| 1242 | } | ||
| 1243 | |||
| 1244 | if (*s == '0') { | ||
| 1245 | nz0 = 1; | ||
| 1246 | while(*++s == '0') ; | ||
| 1247 | if (!*s) | ||
| 1248 | goto ret; | ||
| 1249 | } | ||
| 1250 | s0 = s; | ||
| 1251 | y = z = 0; | ||
| 1252 | for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) | ||
| 1253 | if (nd < 9) | ||
| 1254 | y = 10*y + c - '0'; | ||
| 1255 | else if (nd < 16) | ||
| 1256 | z = 10*z + c - '0'; | ||
| 1257 | nd0 = nd; | ||
| 1258 | if (c == decimal_point) { | ||
| 1259 | c = *++s; | ||
| 1260 | if (!nd) { | ||
| 1261 | for(; c == '0'; c = *++s) | ||
| 1262 | nz++; | ||
| 1263 | if (c > '0' && c <= '9') { | ||
| 1264 | s0 = s; | ||
| 1265 | nf += nz; | ||
| 1266 | nz = 0; | ||
| 1267 | goto have_dig; | ||
| 1268 | } | ||
| 1269 | goto dig_done; | ||
| 1270 | } | ||
| 1271 | for(; c >= '0' && c <= '9'; c = *++s) { | ||
| 1272 | have_dig: | ||
| 1273 | nz++; | ||
| 1274 | if (c -= '0') { | ||
| 1275 | nf += nz; | ||
| 1276 | for(i = 1; i < nz; i++) | ||
| 1277 | if (nd++ < 9) | ||
| 1278 | y *= 10; | ||
| 1279 | else if (nd <= DBL_DIG + 1) | ||
| 1280 | z *= 10; | ||
| 1281 | if (nd++ < 9) | ||
| 1282 | y = 10*y + c; | ||
| 1283 | else if (nd <= DBL_DIG + 1) | ||
| 1284 | z = 10*z + c; | ||
| 1285 | nz = 0; | ||
| 1286 | } | ||
| 1287 | } | ||
| 1288 | } | ||
| 1289 | dig_done: | ||
| 1290 | e = 0; | ||
| 1291 | if (c == 'e' || c == 'E') { | ||
| 1292 | if (!nd && !nz && !nz0) { | ||
| 1293 | s = s00; | ||
| 1294 | goto ret; | ||
| 1295 | } | ||
| 1296 | s00 = s; | ||
| 1297 | esign = 0; | ||
| 1298 | switch(c = *++s) { | ||
| 1299 | case '-': | ||
| 1300 | esign = 1; | ||
| 1301 | case '+': | ||
| 1302 | c = *++s; | ||
| 1303 | } | ||
| 1304 | if (c >= '0' && c <= '9') { | ||
| 1305 | while(c == '0') | ||
| 1306 | c = *++s; | ||
| 1307 | if (c > '0' && c <= '9') { | ||
| 1308 | L = c - '0'; | ||
| 1309 | s1 = s; | ||
| 1310 | while((c = *++s) >= '0' && c <= '9') | ||
| 1311 | L = 10*L + c - '0'; | ||
| 1312 | if (s - s1 > 8 || L > 19999) | ||
| 1313 | /* Avoid confusion from exponents | ||
| 1314 | * so large that e might overflow. | ||
| 1315 | */ | ||
| 1316 | e = 19999; /* safe for 16 bit ints */ | ||
| 1317 | else | ||
| 1318 | e = (int)L; | ||
| 1319 | if (esign) | ||
| 1320 | e = -e; | ||
| 1321 | } | ||
| 1322 | else | ||
| 1323 | e = 0; | ||
| 1324 | } | ||
| 1325 | else | ||
| 1326 | s = s00; | ||
| 1327 | } | ||
| 1328 | if (!nd) { | ||
| 1329 | if (!nz && !nz0) | ||
| 1330 | s = s00; | ||
| 1331 | goto ret; | ||
| 1332 | } | ||
| 1333 | e1 = e -= nf; | ||
| 1334 | |||
| 1335 | /* Now we have nd0 digits, starting at s0, followed by a | ||
| 1336 | * decimal point, followed by nd-nd0 digits. The number we're | ||
| 1337 | * after is the integer represented by those digits times | ||
| 1338 | * 10**e */ | ||
| 1339 | |||
| 1340 | if (!nd0) | ||
| 1341 | nd0 = nd; | ||
| 1342 | k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; | ||
| 1343 | rv = y; | ||
| 1344 | if (k > 9) | ||
| 1345 | rv = tens[k - 9] * rv + z; | ||
| 1346 | bd0 = 0; | ||
| 1347 | if (nd <= DBL_DIG | ||
| 1348 | #ifndef RND_PRODQUOT | ||
| 1349 | && FLT_ROUNDS == 1 | ||
| 1350 | #endif | ||
| 1351 | ) { | ||
| 1352 | if (!e) | ||
| 1353 | goto ret; | ||
| 1354 | if (e > 0) { | ||
| 1355 | if (e <= Ten_pmax) { | ||
| 1356 | #ifdef VAX | ||
| 1357 | goto vax_ovfl_check; | ||
| 1358 | #else | ||
| 1359 | /* rv = */ rounded_product(rv, tens[e]); | ||
| 1360 | goto ret; | ||
| 1361 | #endif | ||
| 1362 | } | ||
| 1363 | i = DBL_DIG - nd; | ||
| 1364 | if (e <= Ten_pmax + i) { | ||
| 1365 | /* A fancier test would sometimes let us do | ||
| 1366 | * this for larger i values. | ||
| 1367 | */ | ||
| 1368 | e -= i; | ||
| 1369 | rv *= tens[i]; | ||
| 1370 | #ifdef VAX | ||
| 1371 | /* VAX exponent range is so narrow we must | ||
| 1372 | * worry about overflow here... | ||
| 1373 | */ | ||
| 1374 | vax_ovfl_check: | ||
| 1375 | word0(rv) -= P*Exp_msk1; | ||
| 1376 | /* rv = */ rounded_product(rv, tens[e]); | ||
| 1377 | if ((word0(rv) & Exp_mask) | ||
| 1378 | > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) | ||
| 1379 | goto ovfl; | ||
| 1380 | word0(rv) += P*Exp_msk1; | ||
| 1381 | #else | ||
| 1382 | /* rv = */ rounded_product(rv, tens[e]); | ||
| 1383 | #endif | ||
| 1384 | goto ret; | ||
| 1385 | } | ||
| 1386 | } | ||
| 1387 | #ifndef Inaccurate_Divide | ||
| 1388 | else if (e >= -Ten_pmax) { | ||
| 1389 | /* rv = */ rounded_quotient(rv, tens[-e]); | ||
| 1390 | goto ret; | ||
| 1391 | } | ||
| 1392 | #endif | ||
| 1393 | } | ||
| 1394 | e1 += nd - k; | ||
| 1395 | |||
| 1396 | /* Get starting approximation = rv * 10**e1 */ | ||
| 1397 | |||
| 1398 | if (e1 > 0) { | ||
| 1399 | if (i = e1 & 15) | ||
| 1400 | rv *= tens[i]; | ||
| 1401 | if (e1 &= ~15) { | ||
| 1402 | if (e1 > DBL_MAX_10_EXP) { | ||
| 1403 | ovfl: | ||
| 1404 | errno = ERANGE; | ||
| 1405 | #ifdef __STDC__ | ||
| 1406 | rv = HUGE_VAL; | ||
| 1407 | #else | ||
| 1408 | /* Can't trust HUGE_VAL */ | ||
| 1409 | #ifdef IEEE_Arith | ||
| 1410 | word0(rv) = Exp_mask; | ||
| 1411 | word1(rv) = 0; | ||
| 1412 | #else | ||
| 1413 | word0(rv) = Big0; | ||
| 1414 | word1(rv) = Big1; | ||
| 1415 | #endif | ||
| 1416 | #endif | ||
| 1417 | if (bd0) | ||
| 1418 | goto retfree; | ||
| 1419 | goto ret; | ||
| 1420 | } | ||
| 1421 | if (e1 >>= 4) { | ||
| 1422 | for(j = 0; e1 > 1; j++, e1 >>= 1) | ||
| 1423 | if (e1 & 1) | ||
| 1424 | rv *= bigtens[j]; | ||
| 1425 | /* The last multiplication could overflow. */ | ||
| 1426 | word0(rv) -= P*Exp_msk1; | ||
| 1427 | rv *= bigtens[j]; | ||
| 1428 | if ((z = word0(rv) & Exp_mask) | ||
| 1429 | > Exp_msk1*(DBL_MAX_EXP+Bias-P)) | ||
| 1430 | goto ovfl; | ||
| 1431 | if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { | ||
| 1432 | /* set to largest number */ | ||
| 1433 | /* (Can't trust DBL_MAX) */ | ||
| 1434 | word0(rv) = Big0; | ||
| 1435 | word1(rv) = Big1; | ||
| 1436 | } | ||
| 1437 | else | ||
| 1438 | word0(rv) += P*Exp_msk1; | ||
| 1439 | } | ||
| 1440 | |||
| 1441 | } | ||
| 1442 | } | ||
| 1443 | else if (e1 < 0) { | ||
| 1444 | e1 = -e1; | ||
| 1445 | if (i = e1 & 15) | ||
| 1446 | rv /= tens[i]; | ||
| 1447 | if (e1 &= ~15) { | ||
| 1448 | e1 >>= 4; | ||
| 1449 | if (e1 >= 1 << n_bigtens) | ||
| 1450 | goto undfl; | ||
| 1451 | for(j = 0; e1 > 1; j++, e1 >>= 1) | ||
| 1452 | if (e1 & 1) | ||
| 1453 | rv *= tinytens[j]; | ||
| 1454 | /* The last multiplication could underflow. */ | ||
| 1455 | rv0 = rv; | ||
| 1456 | rv *= tinytens[j]; | ||
| 1457 | if (!rv) { | ||
| 1458 | rv = 2.*rv0; | ||
| 1459 | rv *= tinytens[j]; | ||
| 1460 | if (!rv) { | ||
| 1461 | undfl: | ||
| 1462 | rv = 0.; | ||
| 1463 | errno = ERANGE; | ||
| 1464 | if (bd0) | ||
| 1465 | goto retfree; | ||
| 1466 | goto ret; | ||
| 1467 | } | ||
| 1468 | word0(rv) = Tiny0; | ||
| 1469 | word1(rv) = Tiny1; | ||
| 1470 | /* The refinement below will clean | ||
| 1471 | * this approximation up. | ||
| 1472 | */ | ||
| 1473 | } | ||
| 1474 | } | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | /* Now the hard part -- adjusting rv to the correct value.*/ | ||
| 1478 | |||
| 1479 | /* Put digits into bd: true value = bd * 10^e */ | ||
| 1480 | |||
| 1481 | bd0 = s2b(s0, nd0, nd, y); | ||
| 1482 | |||
| 1483 | for(;;) { | ||
| 1484 | bd = Balloc(bd0->k); | ||
| 1485 | Bcopy(bd, bd0); | ||
| 1486 | bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */ | ||
| 1487 | bs = i2b(1); | ||
| 1488 | |||
| 1489 | if (e >= 0) { | ||
| 1490 | bb2 = bb5 = 0; | ||
| 1491 | bd2 = bd5 = e; | ||
| 1492 | } | ||
| 1493 | else { | ||
| 1494 | bb2 = bb5 = -e; | ||
| 1495 | bd2 = bd5 = 0; | ||
| 1496 | } | ||
| 1497 | if (bbe >= 0) | ||
| 1498 | bb2 += bbe; | ||
| 1499 | else | ||
| 1500 | bd2 -= bbe; | ||
| 1501 | bs2 = bb2; | ||
| 1502 | #ifdef Sudden_Underflow | ||
| 1503 | #ifdef IBM | ||
| 1504 | j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); | ||
| 1505 | #else | ||
| 1506 | j = P + 1 - bbbits; | ||
| 1507 | #endif | ||
| 1508 | #else | ||
| 1509 | i = bbe + bbbits - 1; /* logb(rv) */ | ||
| 1510 | if (i < Emin) /* denormal */ | ||
| 1511 | j = bbe + (P-Emin); | ||
| 1512 | else | ||
| 1513 | j = P + 1 - bbbits; | ||
| 1514 | #endif | ||
| 1515 | bb2 += j; | ||
| 1516 | bd2 += j; | ||
| 1517 | i = bb2 < bd2 ? bb2 : bd2; | ||
| 1518 | if (i > bs2) | ||
| 1519 | i = bs2; | ||
| 1520 | if (i > 0) { | ||
| 1521 | bb2 -= i; | ||
| 1522 | bd2 -= i; | ||
| 1523 | bs2 -= i; | ||
| 1524 | } | ||
| 1525 | if (bb5 > 0) { | ||
| 1526 | bs = pow5mult(bs, bb5); | ||
| 1527 | bb1 = mult(bs, bb); | ||
| 1528 | Bfree(bb); | ||
| 1529 | bb = bb1; | ||
| 1530 | } | ||
| 1531 | if (bb2 > 0) | ||
| 1532 | bb = lshift(bb, bb2); | ||
| 1533 | if (bd5 > 0) | ||
| 1534 | bd = pow5mult(bd, bd5); | ||
| 1535 | if (bd2 > 0) | ||
| 1536 | bd = lshift(bd, bd2); | ||
| 1537 | if (bs2 > 0) | ||
| 1538 | bs = lshift(bs, bs2); | ||
| 1539 | delta = diff(bb, bd); | ||
| 1540 | dsign = delta->sign; | ||
| 1541 | delta->sign = 0; | ||
| 1542 | i = cmp(delta, bs); | ||
| 1543 | if (i < 0) { | ||
| 1544 | /* Error is less than half an ulp -- check for | ||
| 1545 | * special case of mantissa a power of two. | ||
| 1546 | */ | ||
| 1547 | if (dsign || word1(rv) || word0(rv) & Bndry_mask) | ||
| 1548 | break; | ||
| 1549 | delta = lshift(delta,Log2P); | ||
| 1550 | if (cmp(delta, bs) > 0) | ||
| 1551 | goto drop_down; | ||
| 1552 | break; | ||
| 1553 | } | ||
| 1554 | if (i == 0) { | ||
| 1555 | /* exactly half-way between */ | ||
| 1556 | if (dsign) { | ||
| 1557 | if ((word0(rv) & Bndry_mask1) == Bndry_mask1 | ||
| 1558 | && word1(rv) == 0xffffffff) { | ||
| 1559 | /*boundary case -- increment exponent*/ | ||
| 1560 | word0(rv) = (word0(rv) & Exp_mask) | ||
| 1561 | + Exp_msk1 | ||
| 1562 | #ifdef IBM | ||
| 1563 | | Exp_msk1 >> 4 | ||
| 1564 | #endif | ||
| 1565 | ; | ||
| 1566 | word1(rv) = 0; | ||
| 1567 | break; | ||
| 1568 | } | ||
| 1569 | } | ||
| 1570 | else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { | ||
| 1571 | drop_down: | ||
| 1572 | /* boundary case -- decrement exponent */ | ||
| 1573 | #ifdef Sudden_Underflow | ||
| 1574 | L = word0(rv) & Exp_mask; | ||
| 1575 | #ifdef IBM | ||
| 1576 | if (L < Exp_msk1) | ||
| 1577 | #else | ||
| 1578 | if (L <= Exp_msk1) | ||
| 1579 | #endif | ||
| 1580 | goto undfl; | ||
| 1581 | L -= Exp_msk1; | ||
| 1582 | #else | ||
| 1583 | L = (word0(rv) & Exp_mask) - Exp_msk1; | ||
| 1584 | #endif | ||
| 1585 | word0(rv) = L | Bndry_mask1; | ||
| 1586 | word1(rv) = 0xffffffff; | ||
| 1587 | #ifdef IBM | ||
| 1588 | goto cont; | ||
| 1589 | #else | ||
| 1590 | break; | ||
| 1591 | #endif | ||
| 1592 | } | ||
| 1593 | #ifndef ROUND_BIASED | ||
| 1594 | if (!(word1(rv) & LSB)) | ||
| 1595 | break; | ||
| 1596 | #endif | ||
| 1597 | if (dsign) | ||
| 1598 | rv += ulp(rv); | ||
| 1599 | #ifndef ROUND_BIASED | ||
| 1600 | else { | ||
| 1601 | rv -= ulp(rv); | ||
| 1602 | #ifndef Sudden_Underflow | ||
| 1603 | if (!rv) | ||
| 1604 | goto undfl; | ||
| 1605 | #endif | ||
| 1606 | } | ||
| 1607 | #endif | ||
| 1608 | break; | ||
| 1609 | } | ||
| 1610 | if ((aadj = ratio(delta, bs)) <= 2.) { | ||
| 1611 | if (dsign) | ||
| 1612 | aadj = aadj1 = 1.; | ||
| 1613 | else if (word1(rv) || word0(rv) & Bndry_mask) { | ||
| 1614 | #ifndef Sudden_Underflow | ||
| 1615 | if (word1(rv) == Tiny1 && !word0(rv)) | ||
| 1616 | goto undfl; | ||
| 1617 | #endif | ||
| 1618 | aadj = 1.; | ||
| 1619 | aadj1 = -1.; | ||
| 1620 | } | ||
| 1621 | else { | ||
| 1622 | /* special case -- power of FLT_RADIX to be */ | ||
| 1623 | /* rounded down... */ | ||
| 1624 | |||
| 1625 | if (aadj < 2./FLT_RADIX) | ||
| 1626 | aadj = 1./FLT_RADIX; | ||
| 1627 | else | ||
| 1628 | aadj *= 0.5; | ||
| 1629 | aadj1 = -aadj; | ||
| 1630 | } | ||
| 1631 | } | ||
| 1632 | else { | ||
| 1633 | aadj *= 0.5; | ||
| 1634 | aadj1 = dsign ? aadj : -aadj; | ||
| 1635 | #ifdef Check_FLT_ROUNDS | ||
| 1636 | switch(FLT_ROUNDS) { | ||
| 1637 | case 2: /* towards +infinity */ | ||
| 1638 | aadj1 -= 0.5; | ||
| 1639 | break; | ||
| 1640 | case 0: /* towards 0 */ | ||
| 1641 | case 3: /* towards -infinity */ | ||
| 1642 | aadj1 += 0.5; | ||
| 1643 | } | ||
| 1644 | #else | ||
| 1645 | if (FLT_ROUNDS == 0) | ||
| 1646 | aadj1 += 0.5; | ||
| 1647 | #endif | ||
| 1648 | } | ||
| 1649 | y = word0(rv) & Exp_mask; | ||
| 1650 | |||
| 1651 | /* Check for overflow */ | ||
| 1652 | |||
| 1653 | if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { | ||
| 1654 | rv0 = rv; | ||
| 1655 | word0(rv) -= P*Exp_msk1; | ||
| 1656 | adj = aadj1 * ulp(rv); | ||
| 1657 | rv += adj; | ||
| 1658 | if ((word0(rv) & Exp_mask) >= | ||
| 1659 | Exp_msk1*(DBL_MAX_EXP+Bias-P)) { | ||
| 1660 | if (word0(rv0) == Big0 && word1(rv0) == Big1) | ||
| 1661 | goto ovfl; | ||
| 1662 | word0(rv) = Big0; | ||
| 1663 | word1(rv) = Big1; | ||
| 1664 | goto cont; | ||
| 1665 | } | ||
| 1666 | else | ||
| 1667 | word0(rv) += P*Exp_msk1; | ||
| 1668 | } | ||
| 1669 | else { | ||
| 1670 | #ifdef Sudden_Underflow | ||
| 1671 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { | ||
| 1672 | rv0 = rv; | ||
| 1673 | word0(rv) += P*Exp_msk1; | ||
| 1674 | adj = aadj1 * ulp(rv); | ||
| 1675 | rv += adj; | ||
| 1676 | #ifdef IBM | ||
| 1677 | if ((word0(rv) & Exp_mask) < P*Exp_msk1) | ||
| 1678 | #else | ||
| 1679 | if ((word0(rv) & Exp_mask) <= P*Exp_msk1) | ||
| 1680 | #endif | ||
| 1681 | { | ||
| 1682 | if (word0(rv0) == Tiny0 | ||
| 1683 | && word1(rv0) == Tiny1) | ||
| 1684 | goto undfl; | ||
| 1685 | word0(rv) = Tiny0; | ||
| 1686 | word1(rv) = Tiny1; | ||
| 1687 | goto cont; | ||
| 1688 | } | ||
| 1689 | else | ||
| 1690 | word0(rv) -= P*Exp_msk1; | ||
| 1691 | } | ||
| 1692 | else { | ||
| 1693 | adj = aadj1 * ulp(rv); | ||
| 1694 | rv += adj; | ||
| 1695 | } | ||
| 1696 | #else | ||
| 1697 | /* Compute adj so that the IEEE rounding rules will | ||
| 1698 | * correctly round rv + adj in some half-way cases. | ||
| 1699 | * If rv * ulp(rv) is denormalized (i.e., | ||
| 1700 | * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid | ||
| 1701 | * trouble from bits lost to denormalization; | ||
| 1702 | * example: 1.2e-307 . | ||
| 1703 | */ | ||
| 1704 | if (y <= (P-1)*Exp_msk1 && aadj >= 1.) { | ||
| 1705 | aadj1 = (double)(int)(aadj + 0.5); | ||
| 1706 | if (!dsign) | ||
| 1707 | aadj1 = -aadj1; | ||
| 1708 | } | ||
| 1709 | adj = aadj1 * ulp(rv); | ||
| 1710 | rv += adj; | ||
| 1711 | #endif | ||
| 1712 | } | ||
| 1713 | z = word0(rv) & Exp_mask; | ||
| 1714 | if (y == z) { | ||
| 1715 | /* Can we stop now? */ | ||
| 1716 | L = aadj; | ||
| 1717 | aadj -= L; | ||
| 1718 | /* The tolerances below are conservative. */ | ||
| 1719 | if (dsign || word1(rv) || word0(rv) & Bndry_mask) { | ||
| 1720 | if (aadj < .4999999 || aadj > .5000001) | ||
| 1721 | break; | ||
| 1722 | } | ||
| 1723 | else if (aadj < .4999999/FLT_RADIX) | ||
| 1724 | break; | ||
| 1725 | } | ||
| 1726 | cont: | ||
| 1727 | Bfree(bb); | ||
| 1728 | Bfree(bd); | ||
| 1729 | Bfree(bs); | ||
| 1730 | Bfree(delta); | ||
| 1731 | } | ||
| 1732 | retfree: | ||
| 1733 | Bfree(bb); | ||
| 1734 | Bfree(bd); | ||
| 1735 | Bfree(bs); | ||
| 1736 | Bfree(bd0); | ||
| 1737 | Bfree(delta); | ||
| 1738 | ret: | ||
| 1739 | if (se) | ||
| 1740 | *se = (char *)s; | ||
| 1741 | return sign ? -rv : rv; | ||
| 1742 | } | ||
| 1743 | |||
| 1744 | static int | ||
| 1745 | quorem | ||
| 1746 | #ifdef KR_headers | ||
| 1747 | (b, S) Bigint *b, *S; | ||
| 1748 | #else | ||
| 1749 | (Bigint *b, Bigint *S) | ||
| 1750 | #endif | ||
| 1751 | { | ||
| 1752 | int n; | ||
| 1753 | Long borrow, y; | ||
| 1754 | ULong carry, q, ys; | ||
| 1755 | ULong *bx, *bxe, *sx, *sxe; | ||
| 1756 | #ifdef Pack_32 | ||
| 1757 | Long z; | ||
| 1758 | ULong si, zs; | ||
| 1759 | #endif | ||
| 1760 | |||
| 1761 | n = S->wds; | ||
| 1762 | #ifdef DEBUG | ||
| 1763 | /*debug*/ if (b->wds > n) | ||
| 1764 | /*debug*/ Bug("oversize b in quorem"); | ||
| 1765 | #endif | ||
| 1766 | if (b->wds < n) | ||
| 1767 | return 0; | ||
| 1768 | sx = S->x; | ||
| 1769 | sxe = sx + --n; | ||
| 1770 | bx = b->x; | ||
| 1771 | bxe = bx + n; | ||
| 1772 | q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ | ||
| 1773 | #ifdef DEBUG | ||
| 1774 | /*debug*/ if (q > 9) | ||
| 1775 | /*debug*/ Bug("oversized quotient in quorem"); | ||
| 1776 | #endif | ||
| 1777 | if (q) { | ||
| 1778 | borrow = 0; | ||
| 1779 | carry = 0; | ||
| 1780 | do { | ||
| 1781 | #ifdef Pack_32 | ||
| 1782 | si = *sx++; | ||
| 1783 | ys = (si & 0xffff) * q + carry; | ||
| 1784 | zs = (si >> 16) * q + (ys >> 16); | ||
| 1785 | carry = zs >> 16; | ||
| 1786 | y = (*bx & 0xffff) - (ys & 0xffff) + borrow; | ||
| 1787 | borrow = y >> 16; | ||
| 1788 | Sign_Extend(borrow, y); | ||
| 1789 | z = (*bx >> 16) - (zs & 0xffff) + borrow; | ||
| 1790 | borrow = z >> 16; | ||
| 1791 | Sign_Extend(borrow, z); | ||
| 1792 | Storeinc(bx, z, y); | ||
| 1793 | #else | ||
| 1794 | ys = *sx++ * q + carry; | ||
| 1795 | carry = ys >> 16; | ||
| 1796 | y = *bx - (ys & 0xffff) + borrow; | ||
| 1797 | borrow = y >> 16; | ||
| 1798 | Sign_Extend(borrow, y); | ||
| 1799 | *bx++ = y & 0xffff; | ||
| 1800 | #endif | ||
| 1801 | } | ||
| 1802 | while(sx <= sxe); | ||
| 1803 | if (!*bxe) { | ||
| 1804 | bx = b->x; | ||
| 1805 | while(--bxe > bx && !*bxe) | ||
| 1806 | --n; | ||
| 1807 | b->wds = n; | ||
| 1808 | } | ||
| 1809 | } | ||
| 1810 | if (cmp(b, S) >= 0) { | ||
| 1811 | q++; | ||
| 1812 | borrow = 0; | ||
| 1813 | carry = 0; | ||
| 1814 | bx = b->x; | ||
| 1815 | sx = S->x; | ||
| 1816 | do { | ||
| 1817 | #ifdef Pack_32 | ||
| 1818 | si = *sx++; | ||
| 1819 | ys = (si & 0xffff) + carry; | ||
| 1820 | zs = (si >> 16) + (ys >> 16); | ||
| 1821 | carry = zs >> 16; | ||
| 1822 | y = (*bx & 0xffff) - (ys & 0xffff) + borrow; | ||
| 1823 | borrow = y >> 16; | ||
| 1824 | Sign_Extend(borrow, y); | ||
| 1825 | z = (*bx >> 16) - (zs & 0xffff) + borrow; | ||
| 1826 | borrow = z >> 16; | ||
| 1827 | Sign_Extend(borrow, z); | ||
| 1828 | Storeinc(bx, z, y); | ||
| 1829 | #else | ||
| 1830 | ys = *sx++ + carry; | ||
| 1831 | carry = ys >> 16; | ||
| 1832 | y = *bx - (ys & 0xffff) + borrow; | ||
| 1833 | borrow = y >> 16; | ||
| 1834 | Sign_Extend(borrow, y); | ||
| 1835 | *bx++ = y & 0xffff; | ||
| 1836 | #endif | ||
| 1837 | } | ||
| 1838 | while(sx <= sxe); | ||
| 1839 | bx = b->x; | ||
| 1840 | bxe = bx + n; | ||
| 1841 | if (!*bxe) { | ||
| 1842 | while(--bxe > bx && !*bxe) | ||
| 1843 | --n; | ||
| 1844 | b->wds = n; | ||
| 1845 | } | ||
| 1846 | } | ||
| 1847 | return q; | ||
| 1848 | } | ||
| 1849 | |||
| 1850 | /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. | ||
| 1851 | * | ||
| 1852 | * Inspired by "How to Print Floating-Point Numbers Accurately" by | ||
| 1853 | * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101]. | ||
| 1854 | * | ||
| 1855 | * Modifications: | ||
| 1856 | * 1. Rather than iterating, we use a simple numeric overestimate | ||
| 1857 | * to determine k = floor(log10(d)). We scale relevant | ||
| 1858 | * quantities using O(log2(k)) rather than O(k) multiplications. | ||
| 1859 | * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't | ||
| 1860 | * try to generate digits strictly left to right. Instead, we | ||
| 1861 | * compute with fewer bits and propagate the carry if necessary | ||
| 1862 | * when rounding the final digit up. This is often faster. | ||
| 1863 | * 3. Under the assumption that input will be rounded nearest, | ||
| 1864 | * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. | ||
| 1865 | * That is, we allow equality in stopping tests when the | ||
| 1866 | * round-nearest rule will give the same floating-point value | ||
| 1867 | * as would satisfaction of the stopping test with strict | ||
| 1868 | * inequality. | ||
| 1869 | * 4. We remove common factors of powers of 2 from relevant | ||
| 1870 | * quantities. | ||
| 1871 | * 5. When converting floating-point integers less than 1e16, | ||
| 1872 | * we use floating-point arithmetic rather than resorting | ||
| 1873 | * to multiple-precision integers. | ||
| 1874 | * 6. When asked to produce fewer than 15 digits, we first try | ||
| 1875 | * to get by with floating-point arithmetic; we resort to | ||
| 1876 | * multiple-precision integer arithmetic only if we cannot | ||
| 1877 | * guarantee that the floating-point calculation has given | ||
| 1878 | * the correctly rounded result. For k requested digits and | ||
| 1879 | * "uniformly" distributed input, the probability is | ||
| 1880 | * something like 10^(k-15) that we must resort to the Long | ||
| 1881 | * calculation. | ||
| 1882 | */ | ||
| 1883 | |||
| 1884 | char * | ||
| 1885 | __dtoa | ||
| 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 | { | ||
| 1893 | /* Arguments ndigits, decpt, sign are similar to those | ||
| 1894 | of ecvt and fcvt; trailing zeros are suppressed from | ||
| 1895 | the returned string. If not null, *rve is set to point | ||
| 1896 | to the end of the return value. If d is +-Infinity or NaN, | ||
| 1897 | then *decpt is set to 9999. | ||
| 1898 | |||
| 1899 | mode: | ||
| 1900 | 0 ==> shortest string that yields d when read in | ||
| 1901 | and rounded to nearest. | ||
| 1902 | 1 ==> like 0, but with Steele & White stopping rule; | ||
| 1903 | e.g. with IEEE P754 arithmetic , mode 0 gives | ||
| 1904 | 1e23 whereas mode 1 gives 9.999999999999999e22. | ||
| 1905 | 2 ==> max(1,ndigits) significant digits. This gives a | ||
| 1906 | return value similar to that of ecvt, except | ||
| 1907 | that trailing zeros are suppressed. | ||
| 1908 | 3 ==> through ndigits past the decimal point. This | ||
| 1909 | gives a return value similar to that from fcvt, | ||
| 1910 | except that trailing zeros are suppressed, and | ||
| 1911 | ndigits can be negative. | ||
| 1912 | 4-9 should give the same return values as 2-3, i.e., | ||
| 1913 | 4 <= mode <= 9 ==> same return as mode | ||
| 1914 | 2 + (mode & 1). These modes are mainly for | ||
| 1915 | debugging; often they run slower but sometimes | ||
| 1916 | faster than modes 2-3. | ||
| 1917 | 4,5,8,9 ==> left-to-right digit generation. | ||
| 1918 | 6-9 ==> don't try fast floating-point estimate | ||
| 1919 | (if applicable). | ||
| 1920 | |||
| 1921 | Values of mode other than 0-9 are treated as mode 0. | ||
| 1922 | |||
| 1923 | Sufficient space is allocated to the return value | ||
| 1924 | to hold the suppressed trailing zeros. | ||
| 1925 | */ | ||
| 1926 | |||
| 1927 | int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, | ||
| 1928 | j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, | ||
| 1929 | spec_case, try_quick; | ||
| 1930 | Long L; | ||
| 1931 | #ifndef Sudden_Underflow | ||
| 1932 | int denorm; | ||
| 1933 | ULong x; | ||
| 1934 | #endif | ||
| 1935 | Bigint *b, *b1, *delta, *mlo, *mhi, *S; | ||
| 1936 | double d2, ds, eps; | ||
| 1937 | char *s, *s0; | ||
| 1938 | static Bigint *result; | ||
| 1939 | static int result_k; | ||
| 1940 | |||
| 1941 | if (result) { | ||
| 1942 | result->k = result_k; | ||
| 1943 | result->maxwds = 1 << result_k; | ||
| 1944 | Bfree(result); | ||
| 1945 | result = 0; | ||
| 1946 | } | ||
| 1947 | |||
| 1948 | if (word0(d) & Sign_bit) { | ||
| 1949 | /* set sign for everything, including 0's and NaNs */ | ||
| 1950 | *sign = 1; | ||
| 1951 | word0(d) &= ~Sign_bit; /* clear sign bit */ | ||
| 1952 | } | ||
| 1953 | else | ||
| 1954 | *sign = 0; | ||
| 1955 | |||
| 1956 | #if defined(IEEE_Arith) + defined(VAX) | ||
| 1957 | #ifdef IEEE_Arith | ||
| 1958 | if ((word0(d) & Exp_mask) == Exp_mask) | ||
| 1959 | #else | ||
| 1960 | if (word0(d) == 0x8000) | ||
| 1961 | #endif | ||
| 1962 | { | ||
| 1963 | /* Infinity or NaN */ | ||
| 1964 | *decpt = 9999; | ||
| 1965 | s = | ||
| 1966 | #ifdef IEEE_Arith | ||
| 1967 | !word1(d) && !(word0(d) & 0xfffff) ? "Infinity" : | ||
| 1968 | #endif | ||
| 1969 | "NaN"; | ||
| 1970 | if (rve) | ||
| 1971 | *rve = | ||
| 1972 | #ifdef IEEE_Arith | ||
| 1973 | s[3] ? s + 8 : | ||
| 1974 | #endif | ||
| 1975 | s + 3; | ||
| 1976 | return s; | ||
| 1977 | } | ||
| 1978 | #endif | ||
| 1979 | #ifdef IBM | ||
| 1980 | d += 0; /* normalize */ | ||
| 1981 | #endif | ||
| 1982 | if (!d) { | ||
| 1983 | *decpt = 1; | ||
| 1984 | s = "0"; | ||
| 1985 | if (rve) | ||
| 1986 | *rve = s + 1; | ||
| 1987 | return s; | ||
| 1988 | } | ||
| 1989 | |||
| 1990 | b = d2b(d, &be, &bbits); | ||
| 1991 | #ifdef Sudden_Underflow | ||
| 1992 | i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); | ||
| 1993 | #else | ||
| 1994 | if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { | ||
| 1995 | #endif | ||
| 1996 | d2 = d; | ||
| 1997 | word0(d2) &= Frac_mask1; | ||
| 1998 | word0(d2) |= Exp_11; | ||
| 1999 | #ifdef IBM | ||
| 2000 | if (j = 11 - hi0bits(word0(d2) & Frac_mask)) | ||
| 2001 | d2 /= 1 << j; | ||
| 2002 | #endif | ||
| 2003 | |||
| 2004 | /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 | ||
| 2005 | * log10(x) = log(x) / log(10) | ||
| 2006 | * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) | ||
| 2007 | * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) | ||
| 2008 | * | ||
| 2009 | * This suggests computing an approximation k to log10(d) by | ||
| 2010 | * | ||
| 2011 | * k = (i - Bias)*0.301029995663981 | ||
| 2012 | * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); | ||
| 2013 | * | ||
| 2014 | * We want k to be too large rather than too small. | ||
| 2015 | * The error in the first-order Taylor series approximation | ||
| 2016 | * is in our favor, so we just round up the constant enough | ||
| 2017 | * to compensate for any error in the multiplication of | ||
| 2018 | * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, | ||
| 2019 | * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, | ||
| 2020 | * adding 1e-13 to the constant term more than suffices. | ||
| 2021 | * Hence we adjust the constant term to 0.1760912590558. | ||
| 2022 | * (We could get a more accurate k by invoking log10, | ||
| 2023 | * but this is probably not worthwhile.) | ||
| 2024 | */ | ||
| 2025 | |||
| 2026 | i -= Bias; | ||
| 2027 | #ifdef IBM | ||
| 2028 | i <<= 2; | ||
| 2029 | i += j; | ||
| 2030 | #endif | ||
| 2031 | #ifndef Sudden_Underflow | ||
| 2032 | denorm = 0; | ||
| 2033 | } | ||
| 2034 | else { | ||
| 2035 | /* d is denormalized */ | ||
| 2036 | |||
| 2037 | i = bbits + be + (Bias + (P-1) - 1); | ||
| 2038 | x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 | ||
| 2039 | : word1(d) << 32 - i; | ||
| 2040 | d2 = x; | ||
| 2041 | word0(d2) -= 31*Exp_msk1; /* adjust exponent */ | ||
| 2042 | i -= (Bias + (P-1) - 1) + 1; | ||
| 2043 | denorm = 1; | ||
| 2044 | } | ||
| 2045 | #endif | ||
| 2046 | ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981; | ||
| 2047 | k = (int)ds; | ||
| 2048 | if (ds < 0. && ds != k) | ||
| 2049 | k--; /* want k = floor(ds) */ | ||
| 2050 | k_check = 1; | ||
| 2051 | if (k >= 0 && k <= Ten_pmax) { | ||
| 2052 | if (d < tens[k]) | ||
| 2053 | k--; | ||
| 2054 | k_check = 0; | ||
| 2055 | } | ||
| 2056 | j = bbits - i - 1; | ||
| 2057 | if (j >= 0) { | ||
| 2058 | b2 = 0; | ||
| 2059 | s2 = j; | ||
| 2060 | } | ||
| 2061 | else { | ||
| 2062 | b2 = -j; | ||
| 2063 | s2 = 0; | ||
| 2064 | } | ||
| 2065 | if (k >= 0) { | ||
| 2066 | b5 = 0; | ||
| 2067 | s5 = k; | ||
| 2068 | s2 += k; | ||
| 2069 | } | ||
| 2070 | else { | ||
| 2071 | b2 -= k; | ||
| 2072 | b5 = -k; | ||
| 2073 | s5 = 0; | ||
| 2074 | } | ||
| 2075 | if (mode < 0 || mode > 9) | ||
| 2076 | mode = 0; | ||
| 2077 | try_quick = 1; | ||
| 2078 | if (mode > 5) { | ||
| 2079 | mode -= 4; | ||
| 2080 | try_quick = 0; | ||
| 2081 | } | ||
| 2082 | leftright = 1; | ||
| 2083 | switch(mode) { | ||
| 2084 | case 0: | ||
| 2085 | case 1: | ||
| 2086 | ilim = ilim1 = -1; | ||
| 2087 | i = 18; | ||
| 2088 | ndigits = 0; | ||
| 2089 | break; | ||
| 2090 | case 2: | ||
| 2091 | leftright = 0; | ||
| 2092 | /* no break */ | ||
| 2093 | case 4: | ||
| 2094 | if (ndigits <= 0) | ||
| 2095 | ndigits = 1; | ||
| 2096 | ilim = ilim1 = i = ndigits; | ||
| 2097 | break; | ||
| 2098 | case 3: | ||
| 2099 | leftright = 0; | ||
| 2100 | /* no break */ | ||
| 2101 | case 5: | ||
| 2102 | i = ndigits + k + 1; | ||
| 2103 | ilim = i; | ||
| 2104 | ilim1 = i - 1; | ||
| 2105 | if (i <= 0) | ||
| 2106 | i = 1; | ||
| 2107 | } | ||
| 2108 | j = sizeof(ULong); | ||
| 2109 | for(result_k = 0; sizeof(Bigint) - sizeof(ULong) + j <= i; | ||
| 2110 | j <<= 1) result_k++; | ||
| 2111 | result = Balloc(result_k); | ||
| 2112 | s = s0 = (char *)result; | ||
| 2113 | |||
| 2114 | if (ilim >= 0 && ilim <= Quick_max && try_quick) { | ||
| 2115 | |||
| 2116 | /* Try to get by with floating-point arithmetic. */ | ||
| 2117 | |||
| 2118 | i = 0; | ||
| 2119 | d2 = d; | ||
| 2120 | k0 = k; | ||
| 2121 | ilim0 = ilim; | ||
| 2122 | ieps = 2; /* conservative */ | ||
| 2123 | if (k > 0) { | ||
| 2124 | ds = tens[k&0xf]; | ||
| 2125 | j = k >> 4; | ||
| 2126 | if (j & Bletch) { | ||
| 2127 | /* prevent overflows */ | ||
| 2128 | j &= Bletch - 1; | ||
| 2129 | d /= bigtens[n_bigtens-1]; | ||
| 2130 | ieps++; | ||
| 2131 | } | ||
| 2132 | for(; j; j >>= 1, i++) | ||
| 2133 | if (j & 1) { | ||
| 2134 | ieps++; | ||
| 2135 | ds *= bigtens[i]; | ||
| 2136 | } | ||
| 2137 | d /= ds; | ||
| 2138 | } | ||
| 2139 | else if (j1 = -k) { | ||
| 2140 | d *= tens[j1 & 0xf]; | ||
| 2141 | for(j = j1 >> 4; j; j >>= 1, i++) | ||
| 2142 | if (j & 1) { | ||
| 2143 | ieps++; | ||
| 2144 | d *= bigtens[i]; | ||
| 2145 | } | ||
| 2146 | } | ||
| 2147 | if (k_check && d < 1. && ilim > 0) { | ||
| 2148 | if (ilim1 <= 0) | ||
| 2149 | goto fast_failed; | ||
| 2150 | ilim = ilim1; | ||
| 2151 | k--; | ||
| 2152 | d *= 10.; | ||
| 2153 | ieps++; | ||
| 2154 | } | ||
| 2155 | eps = ieps*d + 7.; | ||
| 2156 | word0(eps) -= (P-1)*Exp_msk1; | ||
| 2157 | if (ilim == 0) { | ||
| 2158 | S = mhi = 0; | ||
| 2159 | d -= 5.; | ||
| 2160 | if (d > eps) | ||
| 2161 | goto one_digit; | ||
| 2162 | if (d < -eps) | ||
| 2163 | goto no_digits; | ||
| 2164 | goto fast_failed; | ||
| 2165 | } | ||
| 2166 | #ifndef No_leftright | ||
| 2167 | if (leftright) { | ||
| 2168 | /* Use Steele & White method of only | ||
| 2169 | * generating digits needed. | ||
| 2170 | */ | ||
| 2171 | eps = 0.5/tens[ilim-1] - eps; | ||
| 2172 | for(i = 0;;) { | ||
| 2173 | L = d; | ||
| 2174 | d -= L; | ||
| 2175 | *s++ = '0' + (int)L; | ||
| 2176 | if (d < eps) | ||
| 2177 | goto ret1; | ||
| 2178 | if (1. - d < eps) | ||
| 2179 | goto bump_up; | ||
| 2180 | if (++i >= ilim) | ||
| 2181 | break; | ||
| 2182 | eps *= 10.; | ||
| 2183 | d *= 10.; | ||
| 2184 | } | ||
| 2185 | } | ||
| 2186 | else { | ||
| 2187 | #endif | ||
| 2188 | /* Generate ilim digits, then fix them up. */ | ||
| 2189 | eps *= tens[ilim-1]; | ||
| 2190 | for(i = 1;; i++, d *= 10.) { | ||
| 2191 | L = d; | ||
| 2192 | d -= L; | ||
| 2193 | *s++ = '0' + (int)L; | ||
| 2194 | if (i == ilim) { | ||
| 2195 | if (d > 0.5 + eps) | ||
| 2196 | goto bump_up; | ||
| 2197 | else if (d < 0.5 - eps) { | ||
| 2198 | while(*--s == '0'); | ||
| 2199 | s++; | ||
| 2200 | goto ret1; | ||
| 2201 | } | ||
| 2202 | break; | ||
| 2203 | } | ||
| 2204 | } | ||
| 2205 | #ifndef No_leftright | ||
| 2206 | } | ||
| 2207 | #endif | ||
| 2208 | fast_failed: | ||
| 2209 | s = s0; | ||
| 2210 | d = d2; | ||
| 2211 | k = k0; | ||
| 2212 | ilim = ilim0; | ||
| 2213 | } | ||
| 2214 | |||
| 2215 | /* Do we have a "small" integer? */ | ||
| 2216 | |||
| 2217 | if (be >= 0 && k <= Int_max) { | ||
| 2218 | /* Yes. */ | ||
| 2219 | ds = tens[k]; | ||
| 2220 | if (ndigits < 0 && ilim <= 0) { | ||
| 2221 | S = mhi = 0; | ||
| 2222 | if (ilim < 0 || d <= 5*ds) | ||
| 2223 | goto no_digits; | ||
| 2224 | goto one_digit; | ||
| 2225 | } | ||
| 2226 | for(i = 1;; i++) { | ||
| 2227 | L = d / ds; | ||
| 2228 | d -= L*ds; | ||
| 2229 | #ifdef Check_FLT_ROUNDS | ||
| 2230 | /* If FLT_ROUNDS == 2, L will usually be high by 1 */ | ||
| 2231 | if (d < 0) { | ||
| 2232 | L--; | ||
| 2233 | d += ds; | ||
| 2234 | } | ||
| 2235 | #endif | ||
| 2236 | *s++ = '0' + (int)L; | ||
| 2237 | if (i == ilim) { | ||
| 2238 | d += d; | ||
| 2239 | if (d > ds || d == ds && L & 1) { | ||
| 2240 | bump_up: | ||
| 2241 | while(*--s == '9') | ||
| 2242 | if (s == s0) { | ||
| 2243 | k++; | ||
| 2244 | *s = '0'; | ||
| 2245 | break; | ||
| 2246 | } | ||
| 2247 | ++*s++; | ||
| 2248 | } | ||
| 2249 | break; | ||
| 2250 | } | ||
| 2251 | if (!(d *= 10.)) | ||
| 2252 | break; | ||
| 2253 | } | ||
| 2254 | goto ret1; | ||
| 2255 | } | ||
| 2256 | |||
| 2257 | m2 = b2; | ||
| 2258 | m5 = b5; | ||
| 2259 | mhi = mlo = 0; | ||
| 2260 | if (leftright) { | ||
| 2261 | if (mode < 2) { | ||
| 2262 | i = | ||
| 2263 | #ifndef Sudden_Underflow | ||
| 2264 | denorm ? be + (Bias + (P-1) - 1 + 1) : | ||
| 2265 | #endif | ||
| 2266 | #ifdef IBM | ||
| 2267 | 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); | ||
| 2268 | #else | ||
| 2269 | 1 + P - bbits; | ||
| 2270 | #endif | ||
| 2271 | } | ||
| 2272 | else { | ||
| 2273 | j = ilim - 1; | ||
| 2274 | if (m5 >= j) | ||
| 2275 | m5 -= j; | ||
| 2276 | else { | ||
| 2277 | s5 += j -= m5; | ||
| 2278 | b5 += j; | ||
| 2279 | m5 = 0; | ||
| 2280 | } | ||
| 2281 | if ((i = ilim) < 0) { | ||
| 2282 | m2 -= i; | ||
| 2283 | i = 0; | ||
| 2284 | } | ||
| 2285 | } | ||
| 2286 | b2 += i; | ||
| 2287 | s2 += i; | ||
| 2288 | mhi = i2b(1); | ||
| 2289 | } | ||
| 2290 | if (m2 > 0 && s2 > 0) { | ||
| 2291 | i = m2 < s2 ? m2 : s2; | ||
| 2292 | b2 -= i; | ||
| 2293 | m2 -= i; | ||
| 2294 | s2 -= i; | ||
| 2295 | } | ||
| 2296 | if (b5 > 0) { | ||
| 2297 | if (leftright) { | ||
| 2298 | if (m5 > 0) { | ||
| 2299 | mhi = pow5mult(mhi, m5); | ||
| 2300 | b1 = mult(mhi, b); | ||
| 2301 | Bfree(b); | ||
| 2302 | b = b1; | ||
| 2303 | } | ||
| 2304 | if (j = b5 - m5) | ||
| 2305 | b = pow5mult(b, j); | ||
| 2306 | } | ||
| 2307 | else | ||
| 2308 | b = pow5mult(b, b5); | ||
| 2309 | } | ||
| 2310 | S = i2b(1); | ||
| 2311 | if (s5 > 0) | ||
| 2312 | S = pow5mult(S, s5); | ||
| 2313 | |||
| 2314 | /* Check for special case that d is a normalized power of 2. */ | ||
| 2315 | |||
| 2316 | if (mode < 2) { | ||
| 2317 | if (!word1(d) && !(word0(d) & Bndry_mask) | ||
| 2318 | #ifndef Sudden_Underflow | ||
| 2319 | && word0(d) & Exp_mask | ||
| 2320 | #endif | ||
| 2321 | ) { | ||
| 2322 | /* The special case */ | ||
| 2323 | b2 += Log2P; | ||
| 2324 | s2 += Log2P; | ||
| 2325 | spec_case = 1; | ||
| 2326 | } | ||
| 2327 | else | ||
| 2328 | spec_case = 0; | ||
| 2329 | } | ||
| 2330 | |||
| 2331 | /* Arrange for convenient computation of quotients: | ||
| 2332 | * shift left if necessary so divisor has 4 leading 0 bits. | ||
| 2333 | * | ||
| 2334 | * Perhaps we should just compute leading 28 bits of S once | ||
| 2335 | * and for all and pass them and a shift to quorem, so it | ||
| 2336 | * can do shifts and ors to compute the numerator for q. | ||
| 2337 | */ | ||
| 2338 | #ifdef Pack_32 | ||
| 2339 | if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) | ||
| 2340 | i = 32 - i; | ||
| 2341 | #else | ||
| 2342 | if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) | ||
| 2343 | i = 16 - i; | ||
| 2344 | #endif | ||
| 2345 | if (i > 4) { | ||
| 2346 | i -= 4; | ||
| 2347 | b2 += i; | ||
| 2348 | m2 += i; | ||
| 2349 | s2 += i; | ||
| 2350 | } | ||
| 2351 | else if (i < 4) { | ||
| 2352 | i += 28; | ||
| 2353 | b2 += i; | ||
| 2354 | m2 += i; | ||
| 2355 | s2 += i; | ||
| 2356 | } | ||
| 2357 | if (b2 > 0) | ||
| 2358 | b = lshift(b, b2); | ||
| 2359 | if (s2 > 0) | ||
| 2360 | S = lshift(S, s2); | ||
| 2361 | if (k_check) { | ||
| 2362 | if (cmp(b,S) < 0) { | ||
| 2363 | k--; | ||
| 2364 | b = multadd(b, 10, 0); /* we botched the k estimate */ | ||
| 2365 | if (leftright) | ||
| 2366 | mhi = multadd(mhi, 10, 0); | ||
| 2367 | ilim = ilim1; | ||
| 2368 | } | ||
| 2369 | } | ||
| 2370 | if (ilim <= 0 && mode > 2) { | ||
| 2371 | if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { | ||
| 2372 | /* no digits, fcvt style */ | ||
| 2373 | no_digits: | ||
| 2374 | k = -1 - ndigits; | ||
| 2375 | goto ret; | ||
| 2376 | } | ||
| 2377 | one_digit: | ||
| 2378 | *s++ = '1'; | ||
| 2379 | k++; | ||
| 2380 | goto ret; | ||
| 2381 | } | ||
| 2382 | if (leftright) { | ||
| 2383 | if (m2 > 0) | ||
| 2384 | mhi = lshift(mhi, m2); | ||
| 2385 | |||
| 2386 | /* Compute mlo -- check for special case | ||
| 2387 | * that d is a normalized power of 2. | ||
| 2388 | */ | ||
| 2389 | |||
| 2390 | mlo = mhi; | ||
| 2391 | if (spec_case) { | ||
| 2392 | mhi = Balloc(mhi->k); | ||
| 2393 | Bcopy(mhi, mlo); | ||
| 2394 | mhi = lshift(mhi, Log2P); | ||
| 2395 | } | ||
| 2396 | |||
| 2397 | for(i = 1;;i++) { | ||
| 2398 | dig = quorem(b,S) + '0'; | ||
| 2399 | /* Do we yet have the shortest decimal string | ||
| 2400 | * that will round to d? | ||
| 2401 | */ | ||
| 2402 | j = cmp(b, mlo); | ||
| 2403 | delta = diff(S, mhi); | ||
| 2404 | j1 = delta->sign ? 1 : cmp(b, delta); | ||
| 2405 | Bfree(delta); | ||
| 2406 | #ifndef ROUND_BIASED | ||
| 2407 | if (j1 == 0 && !mode && !(word1(d) & 1)) { | ||
| 2408 | if (dig == '9') | ||
| 2409 | goto round_9_up; | ||
| 2410 | if (j > 0) | ||
| 2411 | dig++; | ||
| 2412 | *s++ = dig; | ||
| 2413 | goto ret; | ||
| 2414 | } | ||
| 2415 | #endif | ||
| 2416 | if (j < 0 || j == 0 && !mode | ||
| 2417 | #ifndef ROUND_BIASED | ||
| 2418 | && !(word1(d) & 1) | ||
| 2419 | #endif | ||
| 2420 | ) { | ||
| 2421 | if (j1 > 0) { | ||
| 2422 | b = lshift(b, 1); | ||
| 2423 | j1 = cmp(b, S); | ||
| 2424 | if ((j1 > 0 || j1 == 0 && dig & 1) | ||
| 2425 | && dig++ == '9') | ||
| 2426 | goto round_9_up; | ||
| 2427 | } | ||
| 2428 | *s++ = dig; | ||
| 2429 | goto ret; | ||
| 2430 | } | ||
| 2431 | if (j1 > 0) { | ||
| 2432 | if (dig == '9') { /* possible if i == 1 */ | ||
| 2433 | round_9_up: | ||
| 2434 | *s++ = '9'; | ||
| 2435 | goto roundoff; | ||
| 2436 | } | ||
| 2437 | *s++ = dig + 1; | ||
| 2438 | goto ret; | ||
| 2439 | } | ||
| 2440 | *s++ = dig; | ||
| 2441 | if (i == ilim) | ||
| 2442 | break; | ||
| 2443 | b = multadd(b, 10, 0); | ||
| 2444 | if (mlo == mhi) | ||
| 2445 | mlo = mhi = multadd(mhi, 10, 0); | ||
| 2446 | else { | ||
| 2447 | mlo = multadd(mlo, 10, 0); | ||
| 2448 | mhi = multadd(mhi, 10, 0); | ||
| 2449 | } | ||
| 2450 | } | ||
| 2451 | } | ||
| 2452 | else | ||
| 2453 | for(i = 1;; i++) { | ||
| 2454 | *s++ = dig = quorem(b,S) + '0'; | ||
| 2455 | if (i >= ilim) | ||
| 2456 | break; | ||
| 2457 | b = multadd(b, 10, 0); | ||
| 2458 | } | ||
| 2459 | |||
| 2460 | /* Round off last digit */ | ||
| 2461 | |||
| 2462 | b = lshift(b, 1); | ||
| 2463 | j = cmp(b, S); | ||
| 2464 | if (j > 0 || j == 0 && dig & 1) { | ||
| 2465 | roundoff: | ||
| 2466 | while(*--s == '9') | ||
| 2467 | if (s == s0) { | ||
| 2468 | k++; | ||
| 2469 | *s++ = '1'; | ||
| 2470 | goto ret; | ||
| 2471 | } | ||
| 2472 | ++*s++; | ||
| 2473 | } | ||
| 2474 | else { | ||
| 2475 | while(*--s == '0'); | ||
| 2476 | s++; | ||
| 2477 | } | ||
| 2478 | ret: | ||
| 2479 | Bfree(S); | ||
| 2480 | if (mhi) { | ||
| 2481 | if (mlo && mlo != mhi) | ||
| 2482 | Bfree(mlo); | ||
| 2483 | Bfree(mhi); | ||
| 2484 | } | ||
| 2485 | ret1: | ||
| 2486 | Bfree(b); | ||
| 2487 | if (s == s0) { /* don't return empty string */ | ||
| 2488 | *s++ = '0'; | ||
| 2489 | k = 0; | ||
| 2490 | } | ||
| 2491 | *s = 0; | ||
| 2492 | *decpt = k + 1; | ||
| 2493 | if (rve) | ||
| 2494 | *rve = s; | ||
| 2495 | return s0; | ||
| 2496 | } | ||
| 2497 | #ifdef __cplusplus | ||
| 2498 | } | ||
| 2499 | #endif | ||
diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 new file mode 100644 index 0000000000..808ba90165 --- /dev/null +++ b/src/lib/libc/stdlib/strtol.3 | |||
| @@ -0,0 +1,169 @@ | |||
| 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. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strtol.3 5.4 (Berkeley) 6/25/92 | ||
| 37 | .\" $Id: strtol.3,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 25, 1992 | ||
| 40 | .Dt STRTOL 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strtol, strtoq | ||
| 44 | .Nd convert string value to a long or quad_t integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Fd #include <limits.h> | ||
| 48 | .Ft long | ||
| 49 | .Fn strtol "char *nptr" "char **endptr" "int base" | ||
| 50 | |||
| 51 | .Fd #include <sys/types.h> | ||
| 52 | .Fd #include <stdlib.h> | ||
| 53 | .Fd #include <limits.h> | ||
| 54 | .Ft quad_t | ||
| 55 | .Fn strtoq "char *nptr" "char **endptr" "int base" | ||
| 56 | .Sh DESCRIPTION | ||
| 57 | The | ||
| 58 | .Fn strtol | ||
| 59 | function | ||
| 60 | converts the string in | ||
| 61 | .Fa nptr | ||
| 62 | to a | ||
| 63 | .Em long | ||
| 64 | value. | ||
| 65 | The | ||
| 66 | .Fn strtoq | ||
| 67 | function | ||
| 68 | converts the string in | ||
| 69 | .Fa nptr | ||
| 70 | to a | ||
| 71 | .Em quad_t | ||
| 72 | value. | ||
| 73 | The conversion is done according to the given | ||
| 74 | .Fa base , | ||
| 75 | which must be between 2 and 36 inclusive, | ||
| 76 | or be the special value 0. | ||
| 77 | .Pp | ||
| 78 | The string may begin with an arbitrary amount of white space | ||
| 79 | (as determined by | ||
| 80 | .Xr isspace 3 ) | ||
| 81 | followed by a single optional | ||
| 82 | .Ql + | ||
| 83 | or | ||
| 84 | .Ql - | ||
| 85 | sign. | ||
| 86 | If | ||
| 87 | .Fa base | ||
| 88 | is zero or 16, | ||
| 89 | the string may then include a | ||
| 90 | .Ql 0x | ||
| 91 | prefix, | ||
| 92 | and the number will be read in base 16; otherwise, a zero | ||
| 93 | .Fa base | ||
| 94 | is taken as 10 (decimal) unless the next character is | ||
| 95 | .Ql 0 , | ||
| 96 | in which case it is taken as 8 (octal). | ||
| 97 | .Pp | ||
| 98 | The remainder of the string is converted to a | ||
| 99 | .Em long | ||
| 100 | value in the obvious manner, | ||
| 101 | stopping at the first character which is not a valid digit | ||
| 102 | in the given base. | ||
| 103 | (In bases above 10, the letter | ||
| 104 | .Ql A | ||
| 105 | in either upper or lower case | ||
| 106 | represents 10, | ||
| 107 | .Ql B | ||
| 108 | represents 11, and so forth, with | ||
| 109 | .Ql Z | ||
| 110 | representing 35.) | ||
| 111 | .Pp | ||
| 112 | If | ||
| 113 | .Fa endptr | ||
| 114 | is non nil, | ||
| 115 | .Fn strtol | ||
| 116 | stores the address of the first invalid character in | ||
| 117 | .Fa *endptr . | ||
| 118 | If there were no digits at all, however, | ||
| 119 | .Fn strtol | ||
| 120 | stores the original value of | ||
| 121 | .Fa nptr | ||
| 122 | in | ||
| 123 | .Fa *endptr . | ||
| 124 | (Thus, if | ||
| 125 | .Fa *nptr | ||
| 126 | is not | ||
| 127 | .Ql \e0 | ||
| 128 | but | ||
| 129 | .Fa **endptr | ||
| 130 | is | ||
| 131 | .Ql \e0 | ||
| 132 | on return, the entire string was valid.) | ||
| 133 | .Sh RETURN VALUES | ||
| 134 | The | ||
| 135 | .Fn strtol | ||
| 136 | function | ||
| 137 | returns the result of the conversion, | ||
| 138 | unless the value would underflow or overflow. | ||
| 139 | If an underflow occurs, | ||
| 140 | .Fn strtol | ||
| 141 | returns | ||
| 142 | .Dv LONG_MIN . | ||
| 143 | If an overflow occurs, | ||
| 144 | .Fn strtol | ||
| 145 | returns | ||
| 146 | .Dv LONG_MAX . | ||
| 147 | In both cases, | ||
| 148 | .Va errno | ||
| 149 | is set to | ||
| 150 | .Er ERANGE . | ||
| 151 | .Sh ERRORS | ||
| 152 | .Bl -tag -width Er | ||
| 153 | .It Bq Er ERANGE | ||
| 154 | The given string was out of range; the value converted has been clamped. | ||
| 155 | .El | ||
| 156 | .Sh SEE ALSO | ||
| 157 | .Xr atof 3 , | ||
| 158 | .Xr atoi 3 , | ||
| 159 | .Xr atol 3 , | ||
| 160 | .Xr strtod 3 , | ||
| 161 | .Xr strtoul 3 | ||
| 162 | .Sh STANDARDS | ||
| 163 | The | ||
| 164 | .Fn strtol | ||
| 165 | function | ||
| 166 | conforms to | ||
| 167 | .St -ansiC . | ||
| 168 | .Sh BUGS | ||
| 169 | Ignores the current locale. | ||
diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c new file mode 100644 index 0000000000..6f374abd5f --- /dev/null +++ b/src/lib/libc/stdlib/strtol.c | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1990 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | |||
| 44 | |||
| 45 | /* | ||
| 46 | * Convert a string to a long integer. | ||
| 47 | * | ||
| 48 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 49 | * alphabets and digits are each contiguous. | ||
| 50 | */ | ||
| 51 | long | ||
| 52 | strtol(nptr, endptr, base) | ||
| 53 | const char *nptr; | ||
| 54 | char **endptr; | ||
| 55 | register int base; | ||
| 56 | { | ||
| 57 | register const char *s = nptr; | ||
| 58 | register unsigned long acc; | ||
| 59 | register int c; | ||
| 60 | register unsigned long cutoff; | ||
| 61 | register int neg = 0, any, cutlim; | ||
| 62 | |||
| 63 | /* | ||
| 64 | * Skip white space and pick up leading +/- sign if any. | ||
| 65 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
| 66 | * assume decimal; if base is already 16, allow 0x. | ||
| 67 | */ | ||
| 68 | do { | ||
| 69 | c = *s++; | ||
| 70 | } while (isspace(c)); | ||
| 71 | if (c == '-') { | ||
| 72 | neg = 1; | ||
| 73 | c = *s++; | ||
| 74 | } else if (c == '+') | ||
| 75 | c = *s++; | ||
| 76 | if ((base == 0 || base == 16) && | ||
| 77 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 78 | c = s[1]; | ||
| 79 | s += 2; | ||
| 80 | base = 16; | ||
| 81 | } | ||
| 82 | if (base == 0) | ||
| 83 | base = c == '0' ? 8 : 10; | ||
| 84 | |||
| 85 | /* | ||
| 86 | * Compute the cutoff value between legal numbers and illegal | ||
| 87 | * numbers. That is the largest legal value, divided by the | ||
| 88 | * base. An input number that is greater than this value, if | ||
| 89 | * followed by a legal input character, is too big. One that | ||
| 90 | * is equal to this value may be valid or not; the limit | ||
| 91 | * between valid and invalid numbers is then based on the last | ||
| 92 | * digit. For instance, if the range for longs is | ||
| 93 | * [-2147483648..2147483647] and the input base is 10, | ||
| 94 | * cutoff will be set to 214748364 and cutlim to either | ||
| 95 | * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated | ||
| 96 | * a value > 214748364, or equal but the next digit is > 7 (or 8), | ||
| 97 | * the number is too big, and we will return a range error. | ||
| 98 | * | ||
| 99 | * Set any if any `digits' consumed; make it negative to indicate | ||
| 100 | * overflow. | ||
| 101 | */ | ||
| 102 | cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX; | ||
| 103 | cutlim = cutoff % (unsigned long)base; | ||
| 104 | cutoff /= (unsigned long)base; | ||
| 105 | for (acc = 0, any = 0;; c = *s++) { | ||
| 106 | if (isdigit(c)) | ||
| 107 | c -= '0'; | ||
| 108 | else if (isalpha(c)) | ||
| 109 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 110 | else | ||
| 111 | break; | ||
| 112 | if (c >= base) | ||
| 113 | break; | ||
| 114 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 115 | any = -1; | ||
| 116 | else { | ||
| 117 | any = 1; | ||
| 118 | acc *= base; | ||
| 119 | acc += c; | ||
| 120 | } | ||
| 121 | } | ||
| 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) | ||
| 128 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 129 | return (acc); | ||
| 130 | } | ||
diff --git a/src/lib/libc/stdlib/strtoq.c b/src/lib/libc/stdlib/strtoq.c new file mode 100644 index 0000000000..fc559e9d7f --- /dev/null +++ b/src/lib/libc/stdlib/strtoq.c | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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[] = "@(#)strtoq.c 5.1 (Berkeley) 6/26/92"; | ||
| 36 | #endif /* LIBC_SCCS and not lint */ | ||
| 37 | |||
| 38 | #include <sys/types.h> | ||
| 39 | |||
| 40 | #include <limits.h> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <ctype.h> | ||
| 43 | #include <stdlib.h> | ||
| 44 | |||
| 45 | /* | ||
| 46 | * Convert a string to a quad integer. | ||
| 47 | * | ||
| 48 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 49 | * alphabets and digits are each contiguous. | ||
| 50 | */ | ||
| 51 | quad_t | ||
| 52 | strtoq(nptr, endptr, base) | ||
| 53 | const char *nptr; | ||
| 54 | char **endptr; | ||
| 55 | register int base; | ||
| 56 | { | ||
| 57 | register const char *s; | ||
| 58 | register u_quad_t acc; | ||
| 59 | register int c; | ||
| 60 | register u_quad_t qbase, cutoff; | ||
| 61 | register int neg, any, cutlim; | ||
| 62 | |||
| 63 | /* | ||
| 64 | * Skip white space and pick up leading +/- sign if any. | ||
| 65 | * If base is 0, allow 0x for hex and 0 for octal, else | ||
| 66 | * assume decimal; if base is already 16, allow 0x. | ||
| 67 | */ | ||
| 68 | s = nptr; | ||
| 69 | do { | ||
| 70 | c = *s++; | ||
| 71 | } while (isspace(c)); | ||
| 72 | if (c == '-') { | ||
| 73 | neg = 1; | ||
| 74 | c = *s++; | ||
| 75 | } else { | ||
| 76 | neg = 0; | ||
| 77 | if (c == '+') | ||
| 78 | c = *s++; | ||
| 79 | } | ||
| 80 | if ((base == 0 || base == 16) && | ||
| 81 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 82 | c = s[1]; | ||
| 83 | s += 2; | ||
| 84 | base = 16; | ||
| 85 | } | ||
| 86 | if (base == 0) | ||
| 87 | base = c == '0' ? 8 : 10; | ||
| 88 | |||
| 89 | /* | ||
| 90 | * Compute the cutoff value between legal numbers and illegal | ||
| 91 | * numbers. That is the largest legal value, divided by the | ||
| 92 | * base. An input number that is greater than this value, if | ||
| 93 | * followed by a legal input character, is too big. One that | ||
| 94 | * is equal to this value may be valid or not; the limit | ||
| 95 | * between valid and invalid numbers is then based on the last | ||
| 96 | * digit. For instance, if the range for quads is | ||
| 97 | * [-9223372036854775808..9223372036854775807] and the input base | ||
| 98 | * 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 | ||
| 100 | * accumulated a value > 922337203685477580, or equal but the | ||
| 101 | * next digit is > 7 (or 8), the number is too big, and we will | ||
| 102 | * return a range error. | ||
| 103 | * | ||
| 104 | * Set any if any `digits' consumed; make it negative to indicate | ||
| 105 | * overflow. | ||
| 106 | */ | ||
| 107 | qbase = (unsigned)base; | ||
| 108 | cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX; | ||
| 109 | cutlim = cutoff % qbase; | ||
| 110 | cutoff /= qbase; | ||
| 111 | for (acc = 0, any = 0;; c = *s++) { | ||
| 112 | if (isdigit(c)) | ||
| 113 | c -= '0'; | ||
| 114 | else if (isalpha(c)) | ||
| 115 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 116 | else | ||
| 117 | break; | ||
| 118 | if (c >= base) | ||
| 119 | break; | ||
| 120 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 121 | any = -1; | ||
| 122 | else { | ||
| 123 | any = 1; | ||
| 124 | acc *= qbase; | ||
| 125 | acc += c; | ||
| 126 | } | ||
| 127 | } | ||
| 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) | ||
| 134 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 135 | return (acc); | ||
| 136 | } | ||
diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 new file mode 100644 index 0000000000..db551b0141 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.3 | |||
| @@ -0,0 +1,164 @@ | |||
| 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. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)strtoul.3 5.4 (Berkeley) 6/25/92 | ||
| 37 | .\" $Id: strtoul.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 25, 1992 | ||
| 40 | .Dt STRTOUL 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm strtoul, strtouq | ||
| 44 | .Nd convert a string to an unsigned long or uquad_t integer | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Fd #include <limits.h> | ||
| 48 | .Ft unsigned long | ||
| 49 | .Fn strtoul "const char *nptr" "char **endptr" "int base" | ||
| 50 | |||
| 51 | .Fd #include <sys/types.h> | ||
| 52 | .Fd #include <stdlib.h> | ||
| 53 | .Fd #include <limits.h> | ||
| 54 | .Ft u_quad_t | ||
| 55 | .Fn strtouq "const char *nptr" "char **endptr" "int base" | ||
| 56 | .Sh DESCRIPTION | ||
| 57 | The | ||
| 58 | .Fn strtoul | ||
| 59 | function | ||
| 60 | converts the string in | ||
| 61 | .Fa nptr | ||
| 62 | to an | ||
| 63 | .Em unsigned long | ||
| 64 | value. | ||
| 65 | The | ||
| 66 | .Fn strtouq | ||
| 67 | function | ||
| 68 | converts the string in | ||
| 69 | .Fa nptr | ||
| 70 | to a | ||
| 71 | .Em u_quad_t | ||
| 72 | value. | ||
| 73 | The conversion is done according to the given | ||
| 74 | .Fa base , | ||
| 75 | which must be between 2 and 36 inclusive, | ||
| 76 | or be the special value 0. | ||
| 77 | .Pp | ||
| 78 | The string may begin with an arbitrary amount of white space | ||
| 79 | (as determined by | ||
| 80 | .Xr isspace 3 ) | ||
| 81 | followed by a single optional | ||
| 82 | .Ql + | ||
| 83 | or | ||
| 84 | .Ql - | ||
| 85 | sign. | ||
| 86 | If | ||
| 87 | .Fa base | ||
| 88 | is zero or 16, | ||
| 89 | the string may then include a | ||
| 90 | .Ql 0x | ||
| 91 | prefix, | ||
| 92 | and the number will be read in base 16; otherwise, a zero | ||
| 93 | .Fa base | ||
| 94 | is taken as 10 (decimal) unless the next character is | ||
| 95 | .Ql 0 , | ||
| 96 | in which case it is taken as 8 (octal). | ||
| 97 | .Pp | ||
| 98 | The remainder of the string is converted to an | ||
| 99 | .Em unsigned long | ||
| 100 | value in the obvious manner, | ||
| 101 | stopping at the end of the string | ||
| 102 | or at the first character that does not produce a valid digit | ||
| 103 | in the given base. | ||
| 104 | (In bases above 10, the letter | ||
| 105 | .Ql A | ||
| 106 | in either upper or lower case | ||
| 107 | represents 10, | ||
| 108 | .Ql B | ||
| 109 | represents 11, and so forth, with | ||
| 110 | .Ql Z | ||
| 111 | representing 35.) | ||
| 112 | .Pp | ||
| 113 | If | ||
| 114 | .Fa endptr | ||
| 115 | is non nil, | ||
| 116 | .Fn strtoul | ||
| 117 | stores the address of the first invalid character in | ||
| 118 | .Fa *endptr . | ||
| 119 | If there were no digits at all, however, | ||
| 120 | .Fn strtoul | ||
| 121 | stores the original value of | ||
| 122 | .Fa nptr | ||
| 123 | in | ||
| 124 | .Fa *endptr . | ||
| 125 | (Thus, if | ||
| 126 | .Fa *nptr | ||
| 127 | is not | ||
| 128 | .Ql \e0 | ||
| 129 | but | ||
| 130 | .Fa **endptr | ||
| 131 | is | ||
| 132 | .Ql \e0 | ||
| 133 | on return, the entire string was valid.) | ||
| 134 | .Sh RETURN VALUES | ||
| 135 | The | ||
| 136 | .Fn strtoul | ||
| 137 | function | ||
| 138 | returns either the result of the conversion | ||
| 139 | or, if there was a leading minus sign, | ||
| 140 | the negation of the result of the conversion, | ||
| 141 | unless the original (non-negated) value would overflow; | ||
| 142 | in the latter case, | ||
| 143 | .Fn strtoul | ||
| 144 | returns | ||
| 145 | .Dv ULONG_MAX | ||
| 146 | and sets the global variable | ||
| 147 | .Va errno | ||
| 148 | to | ||
| 149 | .Er ERANGE . | ||
| 150 | .Sh ERRORS | ||
| 151 | .Bl -tag -width Er | ||
| 152 | .It Bq Er ERANGE | ||
| 153 | The given string was out of range; the value converted has been clamped. | ||
| 154 | .El | ||
| 155 | .Sh SEE ALSO | ||
| 156 | .Xr strtol 3 | ||
| 157 | .Sh STANDARDS | ||
| 158 | The | ||
| 159 | .Fn strtoul | ||
| 160 | function | ||
| 161 | conforms to | ||
| 162 | .St -ansiC . | ||
| 163 | .Sh BUGS | ||
| 164 | Ignores the current locale. | ||
diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c new file mode 100644 index 0000000000..00f7210fa1 --- /dev/null +++ b/src/lib/libc/stdlib/strtoul.c | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1990 Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | |||
| 44 | /* | ||
| 45 | * Convert a string to an unsigned long integer. | ||
| 46 | * | ||
| 47 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 48 | * alphabets and digits are each contiguous. | ||
| 49 | */ | ||
| 50 | unsigned long | ||
| 51 | strtoul(nptr, endptr, base) | ||
| 52 | const char *nptr; | ||
| 53 | char **endptr; | ||
| 54 | register int base; | ||
| 55 | { | ||
| 56 | register const char *s = nptr; | ||
| 57 | register unsigned long acc; | ||
| 58 | register int c; | ||
| 59 | register unsigned long cutoff; | ||
| 60 | register int neg = 0, any, cutlim; | ||
| 61 | |||
| 62 | /* | ||
| 63 | * See strtol for comments as to the logic used. | ||
| 64 | */ | ||
| 65 | do { | ||
| 66 | c = *s++; | ||
| 67 | } while (isspace(c)); | ||
| 68 | if (c == '-') { | ||
| 69 | neg = 1; | ||
| 70 | c = *s++; | ||
| 71 | } else if (c == '+') | ||
| 72 | c = *s++; | ||
| 73 | if ((base == 0 || base == 16) && | ||
| 74 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 75 | c = s[1]; | ||
| 76 | s += 2; | ||
| 77 | base = 16; | ||
| 78 | } | ||
| 79 | if (base == 0) | ||
| 80 | base = c == '0' ? 8 : 10; | ||
| 81 | cutoff = (unsigned long)ULONG_MAX / (unsigned long)base; | ||
| 82 | cutlim = (unsigned long)ULONG_MAX % (unsigned long)base; | ||
| 83 | for (acc = 0, any = 0;; c = *s++) { | ||
| 84 | if (isdigit(c)) | ||
| 85 | c -= '0'; | ||
| 86 | else if (isalpha(c)) | ||
| 87 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 88 | else | ||
| 89 | break; | ||
| 90 | if (c >= base) | ||
| 91 | break; | ||
| 92 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 93 | any = -1; | ||
| 94 | else { | ||
| 95 | any = 1; | ||
| 96 | acc *= base; | ||
| 97 | acc += c; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | if (any < 0) { | ||
| 101 | acc = ULONG_MAX; | ||
| 102 | errno = ERANGE; | ||
| 103 | } else if (neg) | ||
| 104 | acc = -acc; | ||
| 105 | if (endptr != 0) | ||
| 106 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 107 | return (acc); | ||
| 108 | } | ||
diff --git a/src/lib/libc/stdlib/strtouq.c b/src/lib/libc/stdlib/strtouq.c new file mode 100644 index 0000000000..cc647d8d28 --- /dev/null +++ b/src/lib/libc/stdlib/strtouq.c | |||
| @@ -0,0 +1,114 @@ | |||
| 1 | /*- | ||
| 2 | * Copyright (c) 1992 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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[] = "@(#)strtouq.c 5.1 (Berkeley) 6/26/92"; | ||
| 36 | #endif /* LIBC_SCCS and not lint */ | ||
| 37 | |||
| 38 | #include <sys/types.h> | ||
| 39 | |||
| 40 | #include <limits.h> | ||
| 41 | #include <errno.h> | ||
| 42 | #include <ctype.h> | ||
| 43 | #include <stdlib.h> | ||
| 44 | |||
| 45 | /* | ||
| 46 | * Convert a string to an unsigned quad integer. | ||
| 47 | * | ||
| 48 | * Ignores `locale' stuff. Assumes that the upper and lower case | ||
| 49 | * alphabets and digits are each contiguous. | ||
| 50 | */ | ||
| 51 | u_quad_t | ||
| 52 | strtouq(nptr, endptr, base) | ||
| 53 | const char *nptr; | ||
| 54 | char **endptr; | ||
| 55 | register int base; | ||
| 56 | { | ||
| 57 | register const char *s = nptr; | ||
| 58 | register u_quad_t acc; | ||
| 59 | register int c; | ||
| 60 | register u_quad_t qbase, cutoff; | ||
| 61 | register int neg, any, cutlim; | ||
| 62 | |||
| 63 | /* | ||
| 64 | * See strtoq for comments as to the logic used. | ||
| 65 | */ | ||
| 66 | s = nptr; | ||
| 67 | do { | ||
| 68 | c = *s++; | ||
| 69 | } while (isspace(c)); | ||
| 70 | if (c == '-') { | ||
| 71 | neg = 1; | ||
| 72 | c = *s++; | ||
| 73 | } else { | ||
| 74 | neg = 0; | ||
| 75 | if (c == '+') | ||
| 76 | c = *s++; | ||
| 77 | } | ||
| 78 | if ((base == 0 || base == 16) && | ||
| 79 | c == '0' && (*s == 'x' || *s == 'X')) { | ||
| 80 | c = s[1]; | ||
| 81 | s += 2; | ||
| 82 | base = 16; | ||
| 83 | } | ||
| 84 | if (base == 0) | ||
| 85 | base = c == '0' ? 8 : 10; | ||
| 86 | qbase = (unsigned)base; | ||
| 87 | cutoff = (u_quad_t)UQUAD_MAX / qbase; | ||
| 88 | cutlim = (u_quad_t)UQUAD_MAX % qbase; | ||
| 89 | for (acc = 0, any = 0;; c = *s++) { | ||
| 90 | if (isdigit(c)) | ||
| 91 | c -= '0'; | ||
| 92 | else if (isalpha(c)) | ||
| 93 | c -= isupper(c) ? 'A' - 10 : 'a' - 10; | ||
| 94 | else | ||
| 95 | break; | ||
| 96 | if (c >= base) | ||
| 97 | break; | ||
| 98 | if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim) | ||
| 99 | any = -1; | ||
| 100 | else { | ||
| 101 | any = 1; | ||
| 102 | acc *= qbase; | ||
| 103 | acc += c; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | if (any < 0) { | ||
| 107 | acc = UQUAD_MAX; | ||
| 108 | errno = ERANGE; | ||
| 109 | } else if (neg) | ||
| 110 | acc = -acc; | ||
| 111 | if (endptr != 0) | ||
| 112 | *endptr = (char *) (any ? s - 1 : nptr); | ||
| 113 | return (acc); | ||
| 114 | } | ||
diff --git a/src/lib/libc/stdlib/system.3 b/src/lib/libc/stdlib/system.3 new file mode 100644 index 0000000000..520f51db0a --- /dev/null +++ b/src/lib/libc/stdlib/system.3 | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | .\" Copyright (c) 1990, 1991 The Regents of the University of California. | ||
| 2 | .\" All rights reserved. | ||
| 3 | .\" | ||
| 4 | .\" This code is derived from software contributed to Berkeley by | ||
| 5 | .\" the American National Standards Committee X3, on Information | ||
| 6 | .\" Processing Systems. | ||
| 7 | .\" | ||
| 8 | .\" Redistribution and use in source and binary forms, with or without | ||
| 9 | .\" modification, are permitted provided that the following conditions | ||
| 10 | .\" are met: | ||
| 11 | .\" 1. Redistributions of source code must retain the above copyright | ||
| 12 | .\" notice, this list of conditions and the following disclaimer. | ||
| 13 | .\" 2. Redistributions in binary form must reproduce the above copyright | ||
| 14 | .\" notice, this list of conditions and the following disclaimer in the | ||
| 15 | .\" documentation and/or other materials provided with the distribution. | ||
| 16 | .\" 3. All advertising materials mentioning features or use of this software | ||
| 17 | .\" must display the following acknowledgement: | ||
| 18 | .\" This product includes software developed by the University of | ||
| 19 | .\" California, Berkeley and its contributors. | ||
| 20 | .\" 4. Neither the name of the University nor the names of its contributors | ||
| 21 | .\" may be used to endorse or promote products derived from this software | ||
| 22 | .\" without specific prior written permission. | ||
| 23 | .\" | ||
| 24 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
| 25 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 26 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 27 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| 28 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 29 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 30 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 31 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 32 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 33 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 34 | .\" SUCH DAMAGE. | ||
| 35 | .\" | ||
| 36 | .\" from: @(#)system.3 6.5 (Berkeley) 6/29/91 | ||
| 37 | .\" $Id: system.3,v 1.1.1.1 1995/10/18 08:42:20 deraadt Exp $ | ||
| 38 | .\" | ||
| 39 | .Dd June 29, 1991 | ||
| 40 | .Dt SYSTEM 3 | ||
| 41 | .Os | ||
| 42 | .Sh NAME | ||
| 43 | .Nm system | ||
| 44 | .Nd pass a command to the shell | ||
| 45 | .Sh SYNOPSIS | ||
| 46 | .Fd #include <stdlib.h> | ||
| 47 | .Ft int | ||
| 48 | .Fn system "const char *string" | ||
| 49 | .Sh DESCRIPTION | ||
| 50 | The | ||
| 51 | .Fn system | ||
| 52 | function | ||
| 53 | hands the argument | ||
| 54 | .Fa string | ||
| 55 | to the command interpreter | ||
| 56 | .Xr sh 1 . | ||
| 57 | The calling process waits for the shell to finish executing the command, | ||
| 58 | ignoring | ||
| 59 | .Dv SIGINT | ||
| 60 | and | ||
| 61 | .Dv SIGQUIT , | ||
| 62 | and blocking | ||
| 63 | .Dv SIGCHLD . | ||
| 64 | .Pp | ||
| 65 | If | ||
| 66 | .Fa string | ||
| 67 | is a | ||
| 68 | .Dv NULL | ||
| 69 | pointer, | ||
| 70 | .Fn system | ||
| 71 | will return non-zero. | ||
| 72 | Otherwise, | ||
| 73 | .Fn system | ||
| 74 | returns the termination status of the shell in the format specified by | ||
| 75 | .Xr waitpid 3 . | ||
| 76 | .Sh RETURN VALUES | ||
| 77 | If a child process cannot be created, or the termination status of | ||
| 78 | the shell cannot be obtained, | ||
| 79 | .Fn system | ||
| 80 | returns -1 and sets | ||
| 81 | .Va errno | ||
| 82 | to indicate the error. | ||
| 83 | If execution of the shell fails, | ||
| 84 | .Fn system | ||
| 85 | returns the termination status for a program that terminates with a call of | ||
| 86 | .Fn exit 127 . | ||
| 87 | .Sh SEE ALSO | ||
| 88 | .Xr sh 1 , | ||
| 89 | .Xr execve 2 , | ||
| 90 | .Xr popen 3 , | ||
| 91 | .Xr waitpid 3 , | ||
| 92 | .Sh STANDARDS | ||
| 93 | The | ||
| 94 | .Fn system | ||
| 95 | function | ||
| 96 | conforms to | ||
| 97 | .St -ansiC | ||
| 98 | and | ||
| 99 | .St -1003.2-92 . | ||
diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c new file mode 100644 index 0000000000..c2f39325f6 --- /dev/null +++ b/src/lib/libc/stdlib/system.c | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 1988 The Regents of the University of California. | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * 1. Redistributions of source code must retain the above copyright | ||
| 9 | * notice, this list of conditions and the following disclaimer. | ||
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 11 | * notice, this list of conditions and the following disclaimer in the | ||
| 12 | * documentation and/or other materials provided with the distribution. | ||
| 13 | * 3. 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: @(#)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> | ||
| 40 | #include <sys/wait.h> | ||
| 41 | #include <signal.h> | ||
| 42 | #include <stdlib.h> | ||
| 43 | #include <unistd.h> | ||
| 44 | #include <paths.h> | ||
| 45 | |||
| 46 | extern char **environ; | ||
| 47 | |||
| 48 | int | ||
| 49 | system(command) | ||
| 50 | const char *command; | ||
| 51 | { | ||
| 52 | pid_t pid; | ||
| 53 | sig_t intsave, quitsave; | ||
| 54 | int omask; | ||
| 55 | int pstat; | ||
| 56 | char *argp[] = {"sh", "-c", (char *) command, NULL}; | ||
| 57 | |||
| 58 | if (!command) /* just checking... */ | ||
| 59 | return(1); | ||
| 60 | |||
| 61 | omask = sigblock(sigmask(SIGCHLD)); | ||
| 62 | switch(pid = vfork()) { | ||
| 63 | case -1: /* error */ | ||
| 64 | (void)sigsetmask(omask); | ||
| 65 | return(-1); | ||
| 66 | case 0: /* child */ | ||
| 67 | (void)sigsetmask(omask); | ||
| 68 | execve(_PATH_BSHELL, argp, environ); | ||
| 69 | _exit(127); | ||
| 70 | } | ||
| 71 | |||
| 72 | intsave = signal(SIGINT, SIG_IGN); | ||
| 73 | quitsave = signal(SIGQUIT, SIG_IGN); | ||
| 74 | pid = waitpid(pid, (int *)&pstat, 0); | ||
| 75 | (void)sigsetmask(omask); | ||
| 76 | (void)signal(SIGINT, intsave); | ||
| 77 | (void)signal(SIGQUIT, quitsave); | ||
| 78 | return(pid == -1 ? -1 : pstat); | ||
| 79 | } | ||
