From daac2f633f49a45baa8412c5e4e71e594236e7b8 Mon Sep 17 00:00:00 2001 From: cvs2svn Date: Mon, 28 Jul 2008 23:44:12 +0000 Subject: This commit was manufactured by cvs2git to create tag 'pre_openssl_0_9_8h'. --- src/lib/libc/stdlib/Makefile.inc | 73 -- src/lib/libc/stdlib/_Exit.c | 22 - src/lib/libc/stdlib/_rand48.c | 47 - src/lib/libc/stdlib/a64l.3 | 133 -- src/lib/libc/stdlib/a64l.c | 42 - src/lib/libc/stdlib/abort.3 | 63 - src/lib/libc/stdlib/abort.c | 78 -- src/lib/libc/stdlib/abs.3 | 66 - src/lib/libc/stdlib/abs.c | 37 - src/lib/libc/stdlib/alloca.3 | 80 -- src/lib/libc/stdlib/atexit.3 | 77 -- src/lib/libc/stdlib/atexit.c | 209 ---- src/lib/libc/stdlib/atexit.h | 51 - src/lib/libc/stdlib/atof.3 | 69 -- src/lib/libc/stdlib/atof.c | 37 - src/lib/libc/stdlib/atoi.3 | 86 -- src/lib/libc/stdlib/atoi.c | 37 - src/lib/libc/stdlib/atol.3 | 70 -- src/lib/libc/stdlib/atol.c | 37 - src/lib/libc/stdlib/atoll.3 | 70 -- src/lib/libc/stdlib/atoll.c | 38 - src/lib/libc/stdlib/bsearch.3 | 84 -- src/lib/libc/stdlib/bsearch.c | 67 - src/lib/libc/stdlib/calloc.c | 50 - src/lib/libc/stdlib/cfree.c | 40 - src/lib/libc/stdlib/div.3 | 65 - src/lib/libc/stdlib/div.c | 71 -- src/lib/libc/stdlib/drand48.c | 23 - src/lib/libc/stdlib/ecvt.3 | 165 --- src/lib/libc/stdlib/ecvt.c | 105 -- src/lib/libc/stdlib/erand48.c | 24 - src/lib/libc/stdlib/exit.3 | 114 -- src/lib/libc/stdlib/exit.c | 59 - src/lib/libc/stdlib/gcvt.c | 111 -- src/lib/libc/stdlib/getenv.3 | 148 --- src/lib/libc/stdlib/getenv.c | 80 -- src/lib/libc/stdlib/getopt.3 | 371 ------ src/lib/libc/stdlib/getopt_long.3 | 464 ------- src/lib/libc/stdlib/getopt_long.c | 517 -------- src/lib/libc/stdlib/getsubopt.3 | 145 --- src/lib/libc/stdlib/getsubopt.c | 92 -- src/lib/libc/stdlib/hcreate.3 | 184 --- src/lib/libc/stdlib/hcreate.c | 191 --- src/lib/libc/stdlib/heapsort.c | 173 --- src/lib/libc/stdlib/imaxabs.3 | 66 - src/lib/libc/stdlib/imaxabs.c | 38 - src/lib/libc/stdlib/imaxdiv.3 | 67 - src/lib/libc/stdlib/imaxdiv.c | 50 - src/lib/libc/stdlib/insque.3 | 106 -- src/lib/libc/stdlib/insque.c | 48 - src/lib/libc/stdlib/jrand48.c | 22 - src/lib/libc/stdlib/l64a.c | 42 - src/lib/libc/stdlib/labs.3 | 69 -- src/lib/libc/stdlib/labs.c | 37 - src/lib/libc/stdlib/lcong48.c | 31 - src/lib/libc/stdlib/ldiv.3 | 67 - src/lib/libc/stdlib/ldiv.c | 50 - src/lib/libc/stdlib/llabs.c | 38 - src/lib/libc/stdlib/lldiv.3 | 67 - src/lib/libc/stdlib/lldiv.c | 50 - src/lib/libc/stdlib/lrand48.c | 24 - src/lib/libc/stdlib/lsearch.3 | 108 -- src/lib/libc/stdlib/lsearch.c | 88 -- src/lib/libc/stdlib/malloc.3 | 456 ------- src/lib/libc/stdlib/malloc.c | 1325 -------------------- src/lib/libc/stdlib/merge.c | 333 ----- src/lib/libc/stdlib/mrand48.c | 24 - src/lib/libc/stdlib/nrand48.c | 22 - src/lib/libc/stdlib/putenv.c | 50 - src/lib/libc/stdlib/qabs.3 | 57 - src/lib/libc/stdlib/qabs.c | 37 - src/lib/libc/stdlib/qdiv.3 | 62 - src/lib/libc/stdlib/qdiv.c | 50 - src/lib/libc/stdlib/qsort.3 | 233 ---- src/lib/libc/stdlib/qsort.c | 161 --- src/lib/libc/stdlib/radixsort.3 | 155 --- src/lib/libc/stdlib/radixsort.c | 294 ----- src/lib/libc/stdlib/rand.3 | 98 -- src/lib/libc/stdlib/rand.c | 52 - src/lib/libc/stdlib/rand48.3 | 165 --- src/lib/libc/stdlib/rand48.h | 32 - src/lib/libc/stdlib/random.3 | 185 --- src/lib/libc/stdlib/random.c | 395 ------ src/lib/libc/stdlib/realpath.3 | 117 -- src/lib/libc/stdlib/realpath.c | 190 --- src/lib/libc/stdlib/remque.c | 44 - src/lib/libc/stdlib/seed48.c | 37 - src/lib/libc/stdlib/setenv.c | 103 -- src/lib/libc/stdlib/srand48.c | 31 - src/lib/libc/stdlib/strtod.3 | 129 -- src/lib/libc/stdlib/strtod.c | 2459 ------------------------------------- src/lib/libc/stdlib/strtof.c | 39 - src/lib/libc/stdlib/strtoimax.c | 140 --- src/lib/libc/stdlib/strtol.3 | 264 ---- src/lib/libc/stdlib/strtol.c | 140 --- src/lib/libc/stdlib/strtoll.c | 153 --- src/lib/libc/stdlib/strtonum.3 | 152 --- src/lib/libc/stdlib/strtonum.c | 65 - src/lib/libc/stdlib/strtoul.3 | 248 ---- src/lib/libc/stdlib/strtoul.c | 102 -- src/lib/libc/stdlib/strtoull.c | 115 -- src/lib/libc/stdlib/strtoumax.c | 102 -- src/lib/libc/stdlib/system.3 | 104 -- src/lib/libc/stdlib/system.c | 74 -- src/lib/libc/stdlib/tfind.c | 41 - src/lib/libc/stdlib/tsearch.3 | 127 -- src/lib/libc/stdlib/tsearch.c | 121 -- 107 files changed, 15116 deletions(-) delete mode 100644 src/lib/libc/stdlib/Makefile.inc delete mode 100644 src/lib/libc/stdlib/_Exit.c delete mode 100644 src/lib/libc/stdlib/_rand48.c delete mode 100644 src/lib/libc/stdlib/a64l.3 delete mode 100644 src/lib/libc/stdlib/a64l.c delete mode 100644 src/lib/libc/stdlib/abort.3 delete mode 100644 src/lib/libc/stdlib/abort.c delete mode 100644 src/lib/libc/stdlib/abs.3 delete mode 100644 src/lib/libc/stdlib/abs.c delete mode 100644 src/lib/libc/stdlib/alloca.3 delete mode 100644 src/lib/libc/stdlib/atexit.3 delete mode 100644 src/lib/libc/stdlib/atexit.c delete mode 100644 src/lib/libc/stdlib/atexit.h delete mode 100644 src/lib/libc/stdlib/atof.3 delete mode 100644 src/lib/libc/stdlib/atof.c delete mode 100644 src/lib/libc/stdlib/atoi.3 delete mode 100644 src/lib/libc/stdlib/atoi.c delete mode 100644 src/lib/libc/stdlib/atol.3 delete mode 100644 src/lib/libc/stdlib/atol.c delete mode 100644 src/lib/libc/stdlib/atoll.3 delete mode 100644 src/lib/libc/stdlib/atoll.c delete mode 100644 src/lib/libc/stdlib/bsearch.3 delete mode 100644 src/lib/libc/stdlib/bsearch.c delete mode 100644 src/lib/libc/stdlib/calloc.c delete mode 100644 src/lib/libc/stdlib/cfree.c delete mode 100644 src/lib/libc/stdlib/div.3 delete mode 100644 src/lib/libc/stdlib/div.c delete mode 100644 src/lib/libc/stdlib/drand48.c delete mode 100644 src/lib/libc/stdlib/ecvt.3 delete mode 100644 src/lib/libc/stdlib/ecvt.c delete mode 100644 src/lib/libc/stdlib/erand48.c delete mode 100644 src/lib/libc/stdlib/exit.3 delete mode 100644 src/lib/libc/stdlib/exit.c delete mode 100644 src/lib/libc/stdlib/gcvt.c delete mode 100644 src/lib/libc/stdlib/getenv.3 delete mode 100644 src/lib/libc/stdlib/getenv.c delete mode 100644 src/lib/libc/stdlib/getopt.3 delete mode 100644 src/lib/libc/stdlib/getopt_long.3 delete mode 100644 src/lib/libc/stdlib/getopt_long.c delete mode 100644 src/lib/libc/stdlib/getsubopt.3 delete mode 100644 src/lib/libc/stdlib/getsubopt.c delete mode 100644 src/lib/libc/stdlib/hcreate.3 delete mode 100644 src/lib/libc/stdlib/hcreate.c delete mode 100644 src/lib/libc/stdlib/heapsort.c delete mode 100644 src/lib/libc/stdlib/imaxabs.3 delete mode 100644 src/lib/libc/stdlib/imaxabs.c delete mode 100644 src/lib/libc/stdlib/imaxdiv.3 delete mode 100644 src/lib/libc/stdlib/imaxdiv.c delete mode 100644 src/lib/libc/stdlib/insque.3 delete mode 100644 src/lib/libc/stdlib/insque.c delete mode 100644 src/lib/libc/stdlib/jrand48.c delete mode 100644 src/lib/libc/stdlib/l64a.c delete mode 100644 src/lib/libc/stdlib/labs.3 delete mode 100644 src/lib/libc/stdlib/labs.c delete mode 100644 src/lib/libc/stdlib/lcong48.c delete mode 100644 src/lib/libc/stdlib/ldiv.3 delete mode 100644 src/lib/libc/stdlib/ldiv.c delete mode 100644 src/lib/libc/stdlib/llabs.c delete mode 100644 src/lib/libc/stdlib/lldiv.3 delete mode 100644 src/lib/libc/stdlib/lldiv.c delete mode 100644 src/lib/libc/stdlib/lrand48.c delete mode 100644 src/lib/libc/stdlib/lsearch.3 delete mode 100644 src/lib/libc/stdlib/lsearch.c delete mode 100644 src/lib/libc/stdlib/malloc.3 delete mode 100644 src/lib/libc/stdlib/malloc.c delete mode 100644 src/lib/libc/stdlib/merge.c delete mode 100644 src/lib/libc/stdlib/mrand48.c delete mode 100644 src/lib/libc/stdlib/nrand48.c delete mode 100644 src/lib/libc/stdlib/putenv.c delete mode 100644 src/lib/libc/stdlib/qabs.3 delete mode 100644 src/lib/libc/stdlib/qabs.c delete mode 100644 src/lib/libc/stdlib/qdiv.3 delete mode 100644 src/lib/libc/stdlib/qdiv.c delete mode 100644 src/lib/libc/stdlib/qsort.3 delete mode 100644 src/lib/libc/stdlib/qsort.c delete mode 100644 src/lib/libc/stdlib/radixsort.3 delete mode 100644 src/lib/libc/stdlib/radixsort.c delete mode 100644 src/lib/libc/stdlib/rand.3 delete mode 100644 src/lib/libc/stdlib/rand.c delete mode 100644 src/lib/libc/stdlib/rand48.3 delete mode 100644 src/lib/libc/stdlib/rand48.h delete mode 100644 src/lib/libc/stdlib/random.3 delete mode 100644 src/lib/libc/stdlib/random.c delete mode 100644 src/lib/libc/stdlib/realpath.3 delete mode 100644 src/lib/libc/stdlib/realpath.c delete mode 100644 src/lib/libc/stdlib/remque.c delete mode 100644 src/lib/libc/stdlib/seed48.c delete mode 100644 src/lib/libc/stdlib/setenv.c delete mode 100644 src/lib/libc/stdlib/srand48.c delete mode 100644 src/lib/libc/stdlib/strtod.3 delete mode 100644 src/lib/libc/stdlib/strtod.c delete mode 100644 src/lib/libc/stdlib/strtof.c delete mode 100644 src/lib/libc/stdlib/strtoimax.c delete mode 100644 src/lib/libc/stdlib/strtol.3 delete mode 100644 src/lib/libc/stdlib/strtol.c delete mode 100644 src/lib/libc/stdlib/strtoll.c delete mode 100644 src/lib/libc/stdlib/strtonum.3 delete mode 100644 src/lib/libc/stdlib/strtonum.c delete mode 100644 src/lib/libc/stdlib/strtoul.3 delete mode 100644 src/lib/libc/stdlib/strtoul.c delete mode 100644 src/lib/libc/stdlib/strtoull.c delete mode 100644 src/lib/libc/stdlib/strtoumax.c delete mode 100644 src/lib/libc/stdlib/system.3 delete mode 100644 src/lib/libc/stdlib/system.c delete mode 100644 src/lib/libc/stdlib/tfind.c delete mode 100644 src/lib/libc/stdlib/tsearch.3 delete mode 100644 src/lib/libc/stdlib/tsearch.c (limited to 'src/lib/libc/stdlib') diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc deleted file mode 100644 index f1708a1111..0000000000 --- a/src/lib/libc/stdlib/Makefile.inc +++ /dev/null @@ -1,73 +0,0 @@ -# $OpenBSD: Makefile.inc,v 1.38 2008/06/13 21:04:24 landry Exp $ - -# stdlib sources -.PATH: ${LIBCSRCDIR}/arch/${MACHINE_ARCH}/stdlib ${LIBCSRCDIR}/stdlib - -SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ - calloc.c cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ - getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ - lldiv.c lsearch.c malloc.c merge.c putenv.c qsort.c radixsort.c rand.c \ - random.c realpath.c setenv.c strtoimax.c strtod.c strtof.c strtol.c \ - strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \ - tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ - lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c - -.if (${MACHINE_ARCH} == "m68k") -SRCS+= abs.S div.c labs.c ldiv.c -LSRCS+= abs.c -.elif (${MACHINE_ARCH} == "i386") -SRCS+= abs.S div.S labs.S ldiv.S -LSRCS+= abs.c div.c labs.c ldiv.c -.elif (${MACHINE_ARCH} == "ns32k") -SRCS+= abs.S div.c labs.c ldiv.c -LSRCS+= abs.c -.elif (${MACHINE_ARCH} == "tahoe") -SRCS+= abs.S div.c labs.c ldiv.c -LSRCS+= abs.c -.elif (${MACHINE_ARCH} == "vax") -SRCS+= abs.c div.c labs.c ldiv.c -.elif (${MACHINE_ARCH} == "alpha") -# XXX should be .S's -SRCS+= abs.c div.c labs.c ldiv.c -.else -SRCS+= abs.c div.c labs.c ldiv.c -.endif - -.if (${MACHINE_ARCH} == "vax") || (${MACHINE_ARCH} == "m68k") -SRCS+= insque.S remque.S -.else -SRCS+= insque.c remque.c -.endif - -MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ - bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ - getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \ - lldiv.3 lsearch.3 malloc.3 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 \ - rand.3 random.3 realpath.3 strtod.3 strtonum.3 strtol.3 strtoul.3 \ - system.3 tsearch.3 - -MLINKS+=exit.3 _Exit.3 -MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 -MLINKS+=getenv.3 setenv.3 getenv.3 unsetenv.3 getenv.3 putenv.3 -MLINKS+=getopt_long.3 getopt_long_only.3 -MLINKS+=hcreate.3 hdestroy.3 hcreate.3 hsearch.3 -MLINKS+=insque.3 remque.3 -MLINKS+=labs.3 llabs.3 -MLINKS+=lsearch.3 lfind.3 -MLINKS+=malloc.3 free.3 malloc.3 realloc.3 malloc.3 calloc.3 -MLINKS+=malloc.3 cfree.3 malloc.3 malloc.conf.5 -MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 -MLINKS+=radixsort.3 sradixsort.3 -MLINKS+=rand.3 srand.3 rand.3 rand_r.3 -MLINKS+=random.3 initstate.3 random.3 setstate.3 -MLINKS+=random.3 srandom.3 random.3 srandomdev.3 -MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 -MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 -MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 -MLINKS+=strtod.3 strtof.3 -MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3 -MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3 -MLINKS+=tsearch.3 tfind.3 -MLINKS+=tsearch.3 tdelete.3 -MLINKS+=tsearch.3 twalk.3 -MLINKS+=a64l.3 l64a.3 diff --git a/src/lib/libc/stdlib/_Exit.c b/src/lib/libc/stdlib/_Exit.c deleted file mode 100644 index 5a2291a931..0000000000 --- a/src/lib/libc/stdlib/_Exit.c +++ /dev/null @@ -1,22 +0,0 @@ -/* $OpenBSD: _Exit.c,v 1.2 2005/08/08 08:05:36 espie Exp $ */ - -/* - * Placed in the public domain by Todd C. Miller on January 21, 2004. - */ - -#include -#include - -/* - * _Exit() is the ISO/ANSI C99 equivalent of the POSIX _exit() function. - * No atexit() handlers are called and no signal handlers are run. - * Whether or not stdio buffers are flushed or temporary files are removed - * is implementation-dependent. As such it is safest to *not* flush - * stdio buffers or remove temporary files. This is also consistent - * with most other implementations. - */ -void -_Exit(int status) -{ - _exit(status); -} diff --git a/src/lib/libc/stdlib/_rand48.c b/src/lib/libc/stdlib/_rand48.c deleted file mode 100644 index 7c950f7cee..0000000000 --- a/src/lib/libc/stdlib/_rand48.c +++ /dev/null @@ -1,47 +0,0 @@ -/* $OpenBSD: _rand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -unsigned short __rand48_seed[3] = { - RAND48_SEED_0, - RAND48_SEED_1, - RAND48_SEED_2 -}; -unsigned short __rand48_mult[3] = { - RAND48_MULT_0, - RAND48_MULT_1, - RAND48_MULT_2 -}; -unsigned short __rand48_add = RAND48_ADD; - -void -__dorand48(unsigned short xseed[3]) -{ - unsigned long accu; - unsigned short temp[2]; - - accu = (unsigned long) __rand48_mult[0] * (unsigned long) xseed[0] + - (unsigned long) __rand48_add; - temp[0] = (unsigned short) accu; /* lower 16 bits */ - accu >>= sizeof(unsigned short) * 8; - accu += (unsigned long) __rand48_mult[0] * (unsigned long) xseed[1] + - (unsigned long) __rand48_mult[1] * (unsigned long) xseed[0]; - temp[1] = (unsigned short) accu; /* middle 16 bits */ - accu >>= sizeof(unsigned short) * 8; - accu += __rand48_mult[0] * xseed[2] + __rand48_mult[1] * xseed[1] + __rand48_mult[2] * xseed[0]; - xseed[0] = temp[0]; - xseed[1] = temp[1]; - xseed[2] = (unsigned short) accu; -} diff --git a/src/lib/libc/stdlib/a64l.3 b/src/lib/libc/stdlib/a64l.3 deleted file mode 100644 index 6eec5ce3b3..0000000000 --- a/src/lib/libc/stdlib/a64l.3 +++ /dev/null @@ -1,133 +0,0 @@ -.\" $OpenBSD: a64l.3,v 1.10 2007/05/31 19:19:31 jmc Exp $ -.\" -.\" Copyright (c) 1997 Todd C. Miller -.\" -.\" Permission to use, copy, modify, and distribute this software for any -.\" purpose with or without fee is hereby granted, provided that the above -.\" copyright notice and this permission notice appear in all copies. -.\" -.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt A64L 3 -.Os -.Sh NAME -.Nm a64l , -.Nm l64a -.Nd convert between 32-bit integer and radix-64 ASCII string -.Sh SYNOPSIS -.Fd #include -.Ft long -.Fn a64l "const char *s" -.Ft char * -.Fn l64a "long l" -.Sh DESCRIPTION -The -.Fn a64l -and -.Fn l64a -functions are used to maintain numbers stored in radix-64 -.Tn ASCII -characters. -This is a notation by which 32-bit integers -can be represented by up to six characters; each character represents a -.Dq digit -in a radix-64 notation. -.Pp -The characters used to represent digits are -.Ql \&. -for 0, -.Ql / -for 1, -.Ql 0 -through -.Ql 9 -for 2-11, -.Ql A -through -.Ql Z -for 12-37, and -.Ql a -through -.Ql z -for 38-63. -.Pp -The -.Fn a64l -function takes a pointer to a NUL-terminated radix-64 representation -and returns a corresponding 32-bit value. -If the string pointed to by -.Fa s -contains more than six characters, -.Fn a64l -will use the first six. -.Fn a64l -scans the character string from left to right, decoding -each character as a 6-bit radix-64 number. -If a long integer is -larger than 32 bits, the return value will be sign-extended. -.Pp -.Fn l64a -takes a long integer argument -.Fa l -and returns a pointer to the corresponding radix-64 representation. -.Sh RETURN VALUES -On success, -.Fn a64l -returns a 32-bit representation of -.Fa s . -If -.Fa s -is a null pointer or if it contains digits other than those described above, -.Fn a64l -returns \-1 and sets the global variable -.Va errno -to -.Er EINVAL . -.Pp -On success, -.Fn l64a -returns a pointer to a string containing the radix-64 representation of -.Fa l . -If -.Fa l -is 0, -.Fn l64a -returns a pointer to the empty string. -If -.Fa l -is negative, -.Fn l64a -returns a null pointer and sets the global variable -.Va errno -to -.Er EINVAL . -.Sh WARNINGS -The value returned by -.Fn l64a -is a pointer into a static buffer, the contents of which -will be overwritten by subsequent calls. -.Pp -The value returned by -.Fn a64l -may be incorrect if the value is too large; for that reason, only strings -that resulted from a call to -.Fn l64a -should be used to call -.Fn a64l . -.Pp -If a long integer is larger than 32 bits, only the low-order -32 bits are used. -.Sh STANDARDS -The -.Fn a64l -and -.Fn l64a -functions conform to -.St -xpg4.2 . diff --git a/src/lib/libc/stdlib/a64l.c b/src/lib/libc/stdlib/a64l.c deleted file mode 100644 index 5312929c6f..0000000000 --- a/src/lib/libc/stdlib/a64l.c +++ /dev/null @@ -1,42 +0,0 @@ -/* $OpenBSD: a64l.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/* - * Written by J.T. Conklin . - * Public domain. - */ - -#include -#include - -long -a64l(const char *s) -{ - long value, digit, shift; - int i; - - if (s == NULL) { - errno = EINVAL; - return(-1L); - } - - value = 0; - shift = 0; - for (i = 0; *s && i < 6; i++, s++) { - if (*s >= '.' && *s <= '/') - digit = *s - '.'; - else if (*s >= '0' && *s <= '9') - digit = *s - '0' + 2; - else if (*s >= 'A' && *s <= 'Z') - digit = *s - 'A' + 12; - else if (*s >= 'a' && *s <= 'z') - digit = *s - 'a' + 38; - else { - errno = EINVAL; - return(-1L); - } - - value |= digit << shift; - shift += 6; - } - - return(value); -} diff --git a/src/lib/libc/stdlib/abort.3 b/src/lib/libc/stdlib/abort.3 deleted file mode 100644 index 4ec85882b2..0000000000 --- a/src/lib/libc/stdlib/abort.3 +++ /dev/null @@ -1,63 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: abort.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ABORT 3 -.Os -.Sh NAME -.Nm abort -.Nd cause abnormal program termination -.Sh SYNOPSIS -.Fd #include -.Ft void -.Fn abort void -.Sh DESCRIPTION -The -.Fn abort -function causes abnormal program termination to occur, unless the signal -.Dv SIGABRT -is being caught and the signal handler does not return. -.Pp -Any open streams are flushed and closed. -.Sh RETURN VALUES -The -.Fn abort -function never returns. -.Sh SEE ALSO -.Xr sigaction 2 , -.Xr exit 3 -.Sh STANDARDS -The -.Fn abort -function conforms to -.St -p1003.1-90 . diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c deleted file mode 100644 index 244e3b28aa..0000000000 --- a/src/lib/libc/stdlib/abort.c +++ /dev/null @@ -1,78 +0,0 @@ -/* $OpenBSD: abort.c,v 1.15 2007/09/03 14:40:16 millert Exp $ */ -/* - * Copyright (c) 1985 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include "thread_private.h" -#include "atexit.h" - -void -abort(void) -{ - struct atexit *p = __atexit; - static int cleanup_called = 0; - sigset_t mask; - - - sigfillset(&mask); - /* - * don't block SIGABRT to give any handler a chance; we ignore - * any errors -- X311J doesn't allow abort to return anyway. - */ - sigdelset(&mask, SIGABRT); - (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); - - /* - * POSIX requires we flush stdio buffers on abort - */ - if (cleanup_called == 0) { - /* the cleanup routine lives in fns[0] on the last page */ - while (p != NULL && p->next != NULL) - p = p->next; - /* the check for fn_dso == NULL is mostly paranoia */ - if (p != NULL && p->fns[0].fn_dso == NULL && - p->fns[0].fn_ptr.std_func != NULL) { - cleanup_called = 1; - (*p->fns[0].fn_ptr.std_func)(); - } - } - - (void)kill(getpid(), SIGABRT); - - /* - * if SIGABRT ignored, or caught and the handler returns, do - * it again, only harder. - */ - (void)signal(SIGABRT, SIG_DFL); - (void)_thread_sys_sigprocmask(SIG_SETMASK, &mask, (sigset_t *)NULL); - (void)kill(getpid(), SIGABRT); - _exit(1); -} diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3 deleted file mode 100644 index c4135f9c84..0000000000 --- a/src/lib/libc/stdlib/abs.3 +++ /dev/null @@ -1,66 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: abs.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ABS 3 -.Os -.Sh NAME -.Nm abs -.Nd integer absolute value function -.Sh SYNOPSIS -.Fd #include -.Ft int -.Fn abs "int j" -.Sh DESCRIPTION -The -.Fn abs -function computes the absolute value of the integer -.Fa j . -.Sh RETURN VALUES -The -.Fn abs -function returns the absolute value. -.Sh SEE ALSO -.Xr cabs 3 , -.Xr floor 3 , -.Xr hypot 3 , -.Xr imaxabs 3 , -.Xr labs 3 , -.Xr math 3 -.Sh STANDARDS -The -.Fn abs -function conforms to -.St -ansiC . -.Sh BUGS -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 deleted file mode 100644 index 5d2fbae69f..0000000000 --- a/src/lib/libc/stdlib/abs.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $OpenBSD: abs.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -int -abs(int j) -{ - return(j < 0 ? -j : j); -} diff --git a/src/lib/libc/stdlib/alloca.3 b/src/lib/libc/stdlib/alloca.3 deleted file mode 100644 index d7fddb04c2..0000000000 --- a/src/lib/libc/stdlib/alloca.3 +++ /dev/null @@ -1,80 +0,0 @@ -.\" Copyright (c) 1980, 1991 Regents of the University of California. -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: alloca.3,v 1.11 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ALLOCA 3 -.Os -.Sh NAME -.Nm alloca -.Nd memory allocator -.Sh SYNOPSIS -.Fd #include -.Ft void * -.Fn alloca "size_t size" -.Sh DESCRIPTION -The -.Fn alloca -function allocates -.Fa size -bytes of space in the stack frame of the caller. -This temporary space is automatically freed on return. -.Sh RETURN VALUES -The -.Fn alloca -function returns a pointer to the beginning of the allocated space. -.Sh SEE ALSO -.Xr pagesize 1 , -.Xr brk 2 , -.Xr calloc 3 , -.Xr malloc 3 , -.Xr realloc 3 -.Sh BUGS -The -.Fn alloca -function is machine dependent; its use is discouraged. -.\" .Sh HISTORY -.\" The -.\" .Fn alloca -.\" function appeared in -.\" .Bx ?? . -.\" The function appeared in 32v, pwb and pwb.2 and in 3bsd 4bsd -.\" The first man page (or link to a man page that I can find at the -.\" moment is 4.3... -.Pp -The -.Fn alloca -function is slightly unsafe because it cannot ensure that the pointer -returned points to a valid and usable block of memory. -The allocation made may exceed the bounds of the stack, or even go -further into other objects in memory, and -.Fn alloca -cannot determine such an error. -Avoid -.Fn alloca -with large unbounded allocations. diff --git a/src/lib/libc/stdlib/atexit.3 b/src/lib/libc/stdlib/atexit.3 deleted file mode 100644 index 0615feabb1..0000000000 --- a/src/lib/libc/stdlib/atexit.3 +++ /dev/null @@ -1,77 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: atexit.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ATEXIT 3 -.Os -.Sh NAME -.Nm atexit -.Nd register a function to be called on exit -.Sh SYNOPSIS -.Fd #include -.Ft int -.Fn atexit "void (*function)(void)" -.Sh DESCRIPTION -The -.Fn atexit -function registers the given -.Fa function -to be called at program exit, whether via -.Xr exit 3 -or via return from the program's -.Fn main . -Functions so registered are called in reverse order; -no arguments are passed. -At least 32 functions can always be registered, -and more are allowed as long as sufficient memory can be allocated. -.Pp -.Fn atexit -is very difficult to use correctly without creating -.Xr exit 3 Ns -time -races. -Unless absolutely necessary, please avoid using it. -.Sh RETURN VALUES -.Rv -std atexit -.Sh ERRORS -.Bl -tag -width Er -.It Bq Er ENOMEM -No memory was available to add the function to the list. -The existing list of functions is unmodified. -.El -.Sh SEE ALSO -.Xr exit 3 -.Sh STANDARDS -The -.Fn atexit -function conforms to -.St -ansiC . diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c deleted file mode 100644 index 4d28fb5ef6..0000000000 --- a/src/lib/libc/stdlib/atexit.c +++ /dev/null @@ -1,209 +0,0 @@ -/* $OpenBSD: atexit.c,v 1.14 2007/09/05 20:47:47 chl Exp $ */ -/* - * Copyright (c) 2002 Daniel Hartmeier - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - -#include -#include -#include -#include -#include -#include "atexit.h" -#include "thread_private.h" - -int __atexit_invalid = 1; -struct atexit *__atexit; - -/* - * Function pointers are stored in a linked list of pages. The list - * is initially empty, and pages are allocated on demand. The first - * function pointer in the first allocated page (the last one in - * the linked list) is reserved for the cleanup function. - * - * Outside the following functions, all pages are mprotect()'ed - * to prevent unintentional/malicious corruption. - */ - -/* - * Register a function to be performed at exit or when a shared object - * with the given dso handle is unloaded dynamically. Also used as - * the backend for atexit(). For more info on this API, see: - * - * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor - */ -int -__cxa_atexit(void (*func)(void *), void *arg, void *dso) -{ - struct atexit *p = __atexit; - struct atexit_fn *fnp; - int pgsize = getpagesize(); - int ret = -1; - - if (pgsize < sizeof(*p)) - return (-1); - _ATEXIT_LOCK(); - p = __atexit; - if (p != NULL) { - if (p->ind + 1 >= p->max) - p = NULL; - else if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) - goto unlock; - } - if (p == NULL) { - p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); - if (p == MAP_FAILED) - goto unlock; - if (__atexit == NULL) { - memset(&p->fns[0], 0, sizeof(p->fns[0])); - p->ind = 1; - } else - p->ind = 0; - p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / - sizeof(p->fns[0]); - p->next = __atexit; - __atexit = p; - if (__atexit_invalid) - __atexit_invalid = 0; - } - fnp = &p->fns[p->ind++]; - fnp->fn_ptr.cxa_func = func; - fnp->fn_arg = arg; - fnp->fn_dso = dso; - if (mprotect(p, pgsize, PROT_READ)) - goto unlock; - ret = 0; -unlock: - _ATEXIT_UNLOCK(); - return (ret); -} - -/* - * Register a function to be performed at exit. - */ -int -atexit(void (*func)(void)) -{ - return (__cxa_atexit((void (*)(void *))func, NULL, NULL)); -} - -/* - * Call all handlers registered with __cxa_atexit() for the shared - * object owning 'dso'. - * Note: if 'dso' is NULL, then all remaining handlers are called. - */ -void -__cxa_finalize(void *dso) -{ - struct atexit *p, *q; - struct atexit_fn fn; - int n, pgsize = getpagesize(); - static int call_depth; - - if (__atexit_invalid) - return; - - call_depth++; - - for (p = __atexit; p != NULL; p = p->next) { - for (n = p->ind; --n >= 0;) { - if (p->fns[n].fn_ptr.cxa_func == NULL) - continue; /* already called */ - if (dso != NULL && dso != p->fns[n].fn_dso) - continue; /* wrong DSO */ - - /* - * Mark handler as having been already called to avoid - * dupes and loops, then call the appropriate function. - */ - fn = p->fns[n]; - if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) { - p->fns[n].fn_ptr.cxa_func = NULL; - mprotect(p, pgsize, PROT_READ); - } - if (dso != NULL) - (*fn.fn_ptr.cxa_func)(fn.fn_arg); - else - (*fn.fn_ptr.std_func)(); - } - } - - /* - * If called via exit(), unmap the pages since we have now run - * all the handlers. We defer this until calldepth == 0 so that - * we don't unmap things prematurely if called recursively. - */ - if (dso == NULL && --call_depth == 0) { - for (p = __atexit; p != NULL; ) { - q = p; - p = p->next; - munmap(q, pgsize); - } - __atexit = NULL; - } -} - -/* - * Register the cleanup function - */ -void -__atexit_register_cleanup(void (*func)(void)) -{ - struct atexit *p; - int pgsize = getpagesize(); - - if (pgsize < sizeof(*p)) - return; - _ATEXIT_LOCK(); - p = __atexit; - while (p != NULL && p->next != NULL) - p = p->next; - if (p == NULL) { - p = mmap(NULL, pgsize, PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); - if (p == MAP_FAILED) - goto unlock; - p->ind = 1; - p->max = (pgsize - ((char *)&p->fns[0] - (char *)p)) / - sizeof(p->fns[0]); - p->next = NULL; - __atexit = p; - if (__atexit_invalid) - __atexit_invalid = 0; - } else { - if (mprotect(p, pgsize, PROT_READ | PROT_WRITE)) - goto unlock; - } - p->fns[0].fn_ptr.std_func = func; - p->fns[0].fn_arg = NULL; - p->fns[0].fn_dso = NULL; - mprotect(p, pgsize, PROT_READ); -unlock: - _ATEXIT_UNLOCK(); -} diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h deleted file mode 100644 index 1b23565dd0..0000000000 --- a/src/lib/libc/stdlib/atexit.h +++ /dev/null @@ -1,51 +0,0 @@ -/* $OpenBSD: atexit.h,v 1.7 2007/09/03 14:40:16 millert Exp $ */ - -/* - * Copyright (c) 2002 Daniel Hartmeier - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - */ - -struct atexit { - struct atexit *next; /* next in list */ - int ind; /* next index in this table */ - int max; /* max entries >= ATEXIT_SIZE */ - struct atexit_fn { - union { - void (*std_func)(void); - void (*cxa_func)(void *); - } fn_ptr; - void *fn_arg; /* argument for CXA callback */ - void *fn_dso; /* shared module handle */ - } fns[1]; /* the table itself */ -}; - -extern int __atexit_invalid; -extern struct atexit *__atexit; /* points to head of LIFO stack */ - -int __cxa_atexit(void (*)(void *), void *, void *); -void __cxa_finalize(void *); diff --git a/src/lib/libc/stdlib/atof.3 b/src/lib/libc/stdlib/atof.3 deleted file mode 100644 index 2538d0c879..0000000000 --- a/src/lib/libc/stdlib/atof.3 +++ /dev/null @@ -1,69 +0,0 @@ -.\" Copyright (c) 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: atof.3,v 1.5 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ATOF 3 -.Os -.Sh NAME -.Nm atof -.Nd convert -.Tn ASCII -string to double -.Sh SYNOPSIS -.Fd #include -.Ft double -.Fn atof "const char *nptr" -.Sh DESCRIPTION -The -.Fn atof -function converts the initial portion of the string pointed to by -.Fa nptr -to -.Li double -representation. -.Pp -It is equivalent to: -.Bd -literal -offset indent -strtod(nptr, (char **)NULL); -.Ed -.Sh SEE ALSO -.Xr atoi 3 , -.Xr atol 3 , -.Xr strtod 3 , -.Xr strtol 3 , -.Xr strtoul 3 -.Sh STANDARDS -The -.Fn atof -function conforms to -.St -ansiC . diff --git a/src/lib/libc/stdlib/atof.c b/src/lib/libc/stdlib/atof.c deleted file mode 100644 index d14b58b070..0000000000 --- a/src/lib/libc/stdlib/atof.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $OpenBSD: atof.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1988 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -double -atof(const char *ascii) -{ - return(strtod(ascii, (char **)NULL)); -} diff --git a/src/lib/libc/stdlib/atoi.3 b/src/lib/libc/stdlib/atoi.3 deleted file mode 100644 index 0c6b62b25f..0000000000 --- a/src/lib/libc/stdlib/atoi.3 +++ /dev/null @@ -1,86 +0,0 @@ -.\" Copyright (c) 1990, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: atoi.3,v 1.9 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ATOI 3 -.Os -.Sh NAME -.Nm atoi -.Nd convert -.Tn ASCII -string to integer -.Sh SYNOPSIS -.Fd #include -.Ft int -.Fn atoi "const char *nptr" -.Sh DESCRIPTION -The -.Fn atoi -function converts the initial portion of the string pointed to by -.Fa nptr -to -.Li integer -representation. -.Pp -It is equivalent to: -.Bd -literal -offset indent -(int)strtol(nptr, (char **)NULL, 10); -.Ed -.Sh SEE ALSO -.Xr atof 3 , -.Xr atol 3 , -.Xr strtod 3 , -.Xr strtol 3 , -.Xr strtonum 3 , -.Xr strtoul 3 -.Sh STANDARDS -The -.Fn atoi -function conforms to -.St -ansiC . -.Sh CAVEATS -.Nm -does no overflow checking, handles unsigned numbers poorly, -and handles strings containing trailing extra characters -(like -.Dq "123abc" ) -poorly. -Careful use of -.Xr strtol 3 -and -.Xr strtoul 3 -can alleviate these problems, -but -.Xr strtonum 3 -can be used to convert numbers from strings much more safely -and easily. diff --git a/src/lib/libc/stdlib/atoi.c b/src/lib/libc/stdlib/atoi.c deleted file mode 100644 index b0842678e2..0000000000 --- a/src/lib/libc/stdlib/atoi.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $OpenBSD: atoi.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1988 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -int -atoi(const char *str) -{ - return((int)strtol(str, (char **)NULL, 10)); -} diff --git a/src/lib/libc/stdlib/atol.3 b/src/lib/libc/stdlib/atol.3 deleted file mode 100644 index 3c5551182d..0000000000 --- a/src/lib/libc/stdlib/atol.3 +++ /dev/null @@ -1,70 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: atol.3,v 1.7 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ATOL 3 -.Os -.Sh NAME -.Nm atol -.Nd convert -.Tn ASCII -string to long integer -.Sh SYNOPSIS -.Fd #include -.Ft long -.Fn atol "const char *nptr" -.Sh DESCRIPTION -The -.Fn atol -function converts the initial portion of the string pointed to by -.Fa nptr -to -.Li long integer -representation. -.Pp -It is equivalent to: -.Bd -literal -offset indent -strtol(nptr, (char **)NULL, 10); -.Ed -.Sh SEE ALSO -.Xr atof 3 , -.Xr atoi 3 , -.Xr atoll 3 , -.Xr strtod 3 , -.Xr strtol 3 , -.Xr strtoul 3 -.Sh STANDARDS -The -.Fn atol -function conforms to -.St -ansiC-99 . diff --git a/src/lib/libc/stdlib/atol.c b/src/lib/libc/stdlib/atol.c deleted file mode 100644 index 1970804401..0000000000 --- a/src/lib/libc/stdlib/atol.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $OpenBSD: atol.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1988 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -long -atol(const char *str) -{ - return(strtol(str, (char **)NULL, 10)); -} diff --git a/src/lib/libc/stdlib/atoll.3 b/src/lib/libc/stdlib/atoll.3 deleted file mode 100644 index 608a61f4ce..0000000000 --- a/src/lib/libc/stdlib/atoll.3 +++ /dev/null @@ -1,70 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: atoll.3,v 1.5 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ATOLL 3 -.Os -.Sh NAME -.Nm atoll -.Nd convert -.Tn ASCII -string to long long integer -.Sh SYNOPSIS -.Fd #include -.Ft long long -.Fn atoll "const char *nptr" -.Sh DESCRIPTION -The -.Fn atoll -function converts the initial portion of the string pointed to by -.Fa nptr -to -.Li long long integer -representation. -.Pp -It is equivalent to: -.Bd -literal -offset indent -strtoll(nptr, (char **)NULL, 10); -.Ed -.Sh SEE ALSO -.Xr atof 3 , -.Xr atoi 3 , -.Xr atol 3 , -.Xr strtod 3 , -.Xr strtol 3 , -.Xr strtoul 3 -.Sh STANDARDS -The -.Fn atoll -function conforms to -.St -ansiC-99 . diff --git a/src/lib/libc/stdlib/atoll.c b/src/lib/libc/stdlib/atoll.c deleted file mode 100644 index a65e682cfb..0000000000 --- a/src/lib/libc/stdlib/atoll.c +++ /dev/null @@ -1,38 +0,0 @@ -/* $OpenBSD: atoll.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1988 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -long long -atoll(str) - const char *str; -{ - return(strtoll(str, (char **)NULL, 10)); -} diff --git a/src/lib/libc/stdlib/bsearch.3 b/src/lib/libc/stdlib/bsearch.3 deleted file mode 100644 index 4877a1023e..0000000000 --- a/src/lib/libc/stdlib/bsearch.3 +++ /dev/null @@ -1,84 +0,0 @@ -.\" Copyright (c) 1990, 1991, 1993, 1994 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: bsearch.3,v 1.7 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt BSEARCH 3 -.Os -.Sh NAME -.Nm bsearch -.Nd binary search of a sorted table -.Sh SYNOPSIS -.Fd #include -.Ft void * -.Fn bsearch "const void *key" "const void *base" "size_t nmemb" "size_t size" "int (*compar) (const void *, const void *)" -.Sh DESCRIPTION -The -.Fn bsearch -function searches an array of -.Fa nmemb -objects, the initial member of which is -pointed to by -.Fa base , -for a member that matches the object pointed to by -.Fa key . -The size of each member of the array is specified by -.Fa size . -.Pp -The contents of the array should be in ascending sorted order according -to the comparison function referenced by -.Fa compar . -The -.Fa compar -routine is expected to have two arguments which point to the -.Fa key -object and to an array member, in that order, and should return an integer -less than, equal to, or greater than zero if the -.Fa key -object is found, respectively, to be less than, to match, or be -greater than the array member. -.Sh RETURN VALUES -The -.Fn bsearch -function returns a pointer to a matching member of the array, or a null -pointer if no match is found. -If two members compare as equal, which member is matched is unspecified. -.Sh SEE ALSO -.Xr db 3 , -.Xr lsearch 3 , -.Xr qsort 3 , -.Xr tsearch 3 -.Sh STANDARDS -The -.Fn bsearch -function conforms to -.St -ansiC . diff --git a/src/lib/libc/stdlib/bsearch.c b/src/lib/libc/stdlib/bsearch.c deleted file mode 100644 index 8193d27c60..0000000000 --- a/src/lib/libc/stdlib/bsearch.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -/* - * Perform a binary search. - * - * The code below is a bit sneaky. After a comparison fails, we - * divide the work in half by moving either left or right. If lim - * is odd, moving left simply involves halving lim: e.g., when lim - * is 5 we look at item 2, so we change lim to 2 so that we will - * look at items 0 & 1. If lim is even, the same applies. If lim - * is odd, moving right again involes halving lim, this time moving - * the base up one item past p: e.g., when lim is 5 we change base - * to item 3 and make lim 2 so that we will look at items 3 and 4. - * If lim is even, however, we have to shrink it by one before - * halving: e.g., when lim is 4, we still looked at item 2, so we - * have to make lim 3, then halve, obtaining 1, so that we will only - * look at item 3. - */ -void * -bsearch(const void *key, const void *base0, size_t nmemb, size_t size, - int (*compar)(const void *, const void *)) -{ - const char *base = base0; - int lim, cmp; - const void *p; - - for (lim = nmemb; lim != 0; lim >>= 1) { - p = base + (lim >> 1) * size; - cmp = (*compar)(key, p); - if (cmp == 0) - return ((void *)p); - if (cmp > 0) { /* key > p: move right */ - base = (char *)p + size; - lim--; - } /* else move left */ - } - return (NULL); -} diff --git a/src/lib/libc/stdlib/calloc.c b/src/lib/libc/stdlib/calloc.c deleted file mode 100644 index 43a0d1632c..0000000000 --- a/src/lib/libc/stdlib/calloc.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $OpenBSD: calloc.c,v 1.11 2006/04/02 18:22:14 otto Exp $ */ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include - -void * -calloc(size_t num, size_t size) -{ - void *p; - - if (num && SIZE_MAX / num < size) { - errno = ENOMEM; - return NULL; - } - size *= num; - p = malloc(size); - if (p) - memset(p, 0, size); - return(p); -} diff --git a/src/lib/libc/stdlib/cfree.c b/src/lib/libc/stdlib/cfree.c deleted file mode 100644 index 373c7ff75d..0000000000 --- a/src/lib/libc/stdlib/cfree.c +++ /dev/null @@ -1,40 +0,0 @@ -/* $OpenBSD: cfree.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ - -/* - * Copyright (c) 1996 SigmaSoft, Th. Lockert - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL - * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include - -#ifdef __indr_reference -__indr_reference(free, cfree); -#else - -void -cfree(void *p) -{ - free(p); -} -#endif diff --git a/src/lib/libc/stdlib/div.3 b/src/lib/libc/stdlib/div.3 deleted file mode 100644 index 4a48ab1ea4..0000000000 --- a/src/lib/libc/stdlib/div.3 +++ /dev/null @@ -1,65 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek. -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: div.3,v 1.9 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt DIV 3 -.Os -.Sh NAME -.Nm div -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.Fd #include -.Ft div_t -.Fn div "int num" "int denom" -.Sh DESCRIPTION -The -.Fn div -function computes the value -.Fa num Ns / Ns Fa denom -and returns the quotient and remainder in a structure named -.Fa div_t -that contains two -.Li int -members named -.Fa quot -and -.Fa rem . -.Sh SEE ALSO -.Xr imaxdiv 3 , -.Xr ldiv 3 , -.Xr lldiv 3 , -.Xr math 3 , -.Xr qdiv 3 -.Sh STANDARDS -The -.Fn div -function conforms to -.St -ansiC . diff --git a/src/lib/libc/stdlib/div.c b/src/lib/libc/stdlib/div.c deleted file mode 100644 index f7ac2db4b0..0000000000 --- a/src/lib/libc/stdlib/div.c +++ /dev/null @@ -1,71 +0,0 @@ -/* $OpenBSD: div.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include /* div_t */ - -div_t -div(int num, int denom) -{ - div_t r; - - r.quot = num / denom; - r.rem = num % denom; - /* - * The ANSI standard says that |r.quot| <= |n/d|, where - * n/d is to be computed in infinite precision. In other - * words, we should always truncate the quotient towards - * 0, never -infinity. - * - * Machine division and remainer may work either way when - * one or both of n or d is negative. If only one is - * negative and r.quot has been truncated towards -inf, - * r.rem will have the same sign as denom and the opposite - * sign of num; if both are negative and r.quot has been - * truncated towards -inf, r.rem will be positive (will - * have the opposite sign of num). These are considered - * `wrong'. - * - * If both are num and denom are positive, r will always - * be positive. - * - * This all boils down to: - * if num >= 0, but r.rem < 0, we got the wrong answer. - * In that case, to get the right answer, add 1 to r.quot and - * subtract denom from r.rem. - */ - if (num >= 0 && r.rem < 0) { - r.quot++; - r.rem -= denom; - } - return (r); -} diff --git a/src/lib/libc/stdlib/drand48.c b/src/lib/libc/stdlib/drand48.c deleted file mode 100644 index b6c046c831..0000000000 --- a/src/lib/libc/stdlib/drand48.c +++ /dev/null @@ -1,23 +0,0 @@ -/* $OpenBSD: drand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -extern unsigned short __rand48_seed[3]; - -double -drand48(void) -{ - return erand48(__rand48_seed); -} diff --git a/src/lib/libc/stdlib/ecvt.3 b/src/lib/libc/stdlib/ecvt.3 deleted file mode 100644 index 0717a2c02a..0000000000 --- a/src/lib/libc/stdlib/ecvt.3 +++ /dev/null @@ -1,165 +0,0 @@ -.\" $OpenBSD: ecvt.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ -.\" -.\" Copyright (c) 2002 Todd C. Miller -.\" -.\" Permission to use, copy, modify, and distribute this software for any -.\" purpose with or without fee is hereby granted, provided that the above -.\" copyright notice and this permission notice appear in all copies. -.\" -.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -.\" -.\" Sponsored in part by the Defense Advanced Research Projects -.\" Agency (DARPA) and Air Force Research Laboratory, Air Force -.\" Materiel Command, USAF, under agreement number F39502-99-1-0512. -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt ECVT 3 -.Os -.Sh NAME -.Nm ecvt , -.Nm fcvt , -.Nm gcvt -.Nd convert double to -.Tn ASCII -string -.Sh SYNOPSIS -.Fd #include -.Ft char * -.Fn ecvt "double value" "int ndigit" "int *decpt" "int *sign" -.Ft char * -.Fn fcvt "double value" "int ndigit" "int *decpt" "int *sign" -.Ft char * -.Fn gcvt "double value" "int ndigit" "char *buf" -.Sh DESCRIPTION -.Bf -symbolic -These functions are provided for compatibility with legacy code. -New code should use the -.Xr snprintf 3 -function for improved safety and portability. -.Ef -.Pp -The -.Fn ecvt , -.Fn fcvt -and -.Fn gcvt -functions convert the double precision floating-point number -.Fa value -to a NUL-terminated -.Tn ASCII -string. -.Pp -The -.Fn ecvt -function converts -.Fa value -to a NUL-terminated string of exactly -.Fa ndigit -digits and returns a pointer to that string. -The result is padded with zeroes from left to right as needed. -There are no leading zeroes unless -.Fa value -itself is 0. -The least significant digit is rounded in an implementation-dependent manner. -The position of the decimal point relative to the beginning of the string -is stored in -.Fa decpt . -A negative value indicates that the decimal point is located -to the left of the returned digits (this occurs when there is no -whole number component to -.Fa value ) . -If -.Fa value -is zero, it is unspecified whether the integer pointed to by -.Fa decpt -will be 0 or 1. -The decimal point itself is not included in the returned string. -If the sign of the result is negative, the integer pointed to by -.Fa sign -is non-zero; otherwise, it is 0. -.Pp -If the converted value is out of range or is not representable, -the contents of the returned string are unspecified. -.Pp -The -.Fn fcvt -function is identical to -.Fn ecvt -with the exception that -.Fa ndigit -specifies the number of digits after the decimal point (zero-padded as -needed). -.Pp -The -.Fn gcvt -function converts -.Fa value -to a NUL-terminated string similar to the %g -.Xr printf 3 -format specifier and stores the result in -.Fa buf . -It produces -.Fa ndigit -significant digits similar to the %f -.Xr printf 3 -format specifier where possible. -If -.Fa ndigit -does allow sufficient precision, the result is stored in -exponential notation similar to the %e -.Xr printf 3 -format specifier. -If -.Fa value -is less than zero, -.Fa buf -will be prefixed with a minus sign. -A decimal point is included in the returned string if -.Fa value -is not a whole number. -Unlike the -.Fn ecvt -and -.Fn fcvt -functions, -.Fa buf -is not zero-padded. -.Sh RETURN VALUES -The -.Fn ecvt , -.Fn fcvt -and -.Fn gcvt -functions return a NUL-terminated string representation of -.Fa value . -.Sh WARNINGS -The -.Fn ecvt -and -.Fn fcvt -functions return a pointer to internal storage space that will be -overwritten by subsequent calls to either function. -.Pp -The maximum possible precision of the return value is limited by the -precision of a double and may not be the same on all architectures. -.Pp -The -.Xr snprintf 3 -function is preferred over these functions for new code. -.Sh SEE ALSO -.Xr printf 3 , -.Xr strtod 3 -.Sh STANDARDS -The -.Fn ecvt , -.Fn fcvt -and -.Fn gcvt -functions conform to -.St -p1003.1-2001 . diff --git a/src/lib/libc/stdlib/ecvt.c b/src/lib/libc/stdlib/ecvt.c deleted file mode 100644 index 719370a8f3..0000000000 --- a/src/lib/libc/stdlib/ecvt.c +++ /dev/null @@ -1,105 +0,0 @@ -/* $OpenBSD: ecvt.c,v 1.6 2006/10/29 18:45:56 deraadt Exp $ */ - -/* - * Copyright (c) 2002, 2006 Todd C. Miller - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Sponsored in part by the Defense Advanced Research Projects - * Agency (DARPA) and Air Force Research Laboratory, Air Force - * Materiel Command, USAF, under agreement number F39502-99-1-0512. - */ - -#include -#include -#include - -extern char *__dtoa(double, int, int, int *, int *, char **); -extern void __freedtoa(char *); -static char *__cvt(double, int, int *, int *, int, int); - -static char * -__cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad) -{ - static char *s; - char *p, *rve, c; - size_t siz; - - if (ndigit == 0) { - *sign = value < 0.0; - *decpt = 0; - return (""); - } - - if (s) { - free(s); - s = NULL; - } - - if (ndigit < 0) - siz = -ndigit + 1; - else - siz = ndigit + 1; - - - /* __dtoa() doesn't allocate space for 0 so we do it by hand */ - if (value == 0.0) { - *decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */ - *sign = 0; - if ((rve = s = (char *)malloc(siz)) == NULL) - return(NULL); - *rve++ = '0'; - *rve = '\0'; - } else { - p = __dtoa(value, fmode + 2, ndigit, decpt, sign, &rve); - if (*decpt == 9999) { - /* Infinity or Nan, convert to inf or nan like printf */ - *decpt = 0; - c = *p; - __freedtoa(p); - return(c == 'I' ? "inf" : "nan"); - } - /* Make a local copy and adjust rve to be in terms of s */ - if (pad && fmode) - siz += *decpt; - if ((s = (char *)malloc(siz)) == NULL) { - __freedtoa(p); - return(NULL); - } - (void) strlcpy(s, p, siz); - rve = s + (rve - p); - __freedtoa(p); - } - - /* Add trailing zeros */ - if (pad) { - siz -= rve - s; - while (--siz) - *rve++ = '0'; - *rve = '\0'; - } - - return(s); -} - -char * -ecvt(double value, int ndigit, int *decpt, int *sign) -{ - return(__cvt(value, ndigit, decpt, sign, 0, 1)); -} - -char * -fcvt(double value, int ndigit, int *decpt, int *sign) -{ - return(__cvt(value, ndigit, decpt, sign, 1, 1)); -} diff --git a/src/lib/libc/stdlib/erand48.c b/src/lib/libc/stdlib/erand48.c deleted file mode 100644 index 2ffeaa6e71..0000000000 --- a/src/lib/libc/stdlib/erand48.c +++ /dev/null @@ -1,24 +0,0 @@ -/* $OpenBSD: erand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -double -erand48(unsigned short xseed[3]) -{ - __dorand48(xseed); - return ldexp((double) xseed[0], -48) + - ldexp((double) xseed[1], -32) + - ldexp((double) xseed[2], -16); -} diff --git a/src/lib/libc/stdlib/exit.3 b/src/lib/libc/stdlib/exit.3 deleted file mode 100644 index 45eae80a08..0000000000 --- a/src/lib/libc/stdlib/exit.3 +++ /dev/null @@ -1,114 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: exit.3,v 1.11 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt EXIT 3 -.Os -.Sh NAME -.Nm exit, _Exit -.Nd perform normal program termination -.Sh SYNOPSIS -.Fd #include -.Ft void -.Fn exit "int status" -.Ft void -.Fn _Exit "int status" -.Sh DESCRIPTION -The -.Fn exit -and -.Fn _Exit -functions terminate a process. -.Pp -Before termination, -.Fn exit -performs the following operations in the order listed: -.Bl -enum -offset indent -.It -Call the functions registered with the -.Xr atexit 3 -function, in the reverse order of their registration. -.It -Flush all open output streams. -.It -Close all open streams. -.It -Unlink all files created with the -.Xr tmpfile 3 -function. -.El -.Pp -The -.Fn _Exit -function terminates without calling the functions registered with the -.Xr atexit 3 -function. -The -.Ox -implementation of -.Fn _Exit -does not flush open output streams or unlink files created with the -.Xr tmpfile 3 -function. -However, this behavior is implementation-specific. -.Pp -Lastly, -.Fn exit -and -.Fn _Exit -call -.Xr _exit 2 . -Note that typically -.Xr _exit 2 -only passes the lower 8 bits of -.Fa status -on to the parent, thus negative values have less meaning. -.Sh RETURN VALUES -The -.Fn exit -and -.Fn _Exit -functions never return. -.Sh SEE ALSO -.Xr _exit 2 , -.Xr atexit 3 , -.Xr intro 3 , -.Xr sysexits 3 , -.Xr tmpfile 3 -.Sh STANDARDS -The -.Fn exit -and -.Fn _Exit -functions conform to -.St -ansiC-99 . diff --git a/src/lib/libc/stdlib/exit.c b/src/lib/libc/stdlib/exit.c deleted file mode 100644 index 83fe3d2de5..0000000000 --- a/src/lib/libc/stdlib/exit.c +++ /dev/null @@ -1,59 +0,0 @@ -/* $OpenBSD: exit.c,v 1.12 2007/09/03 14:40:16 millert Exp $ */ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include "atexit.h" -#include "thread_private.h" - -/* - * This variable is zero until a process has created a thread. - * It is used to avoid calling locking functions in libc when they - * are not required. By default, libc is intended to be(come) - * thread-safe, but without a (significant) penalty to non-threaded - * processes. - */ -int __isthreaded = 0; - -/* - * Exit, flushing stdio buffers if necessary. - */ -void -exit(int status) -{ - /* - * Call functions registered by atexit() or _cxa_atexit() - * (including the stdio cleanup routine) and then _exit(). - */ - __cxa_finalize(NULL); - _exit(status); -} diff --git a/src/lib/libc/stdlib/gcvt.c b/src/lib/libc/stdlib/gcvt.c deleted file mode 100644 index c24157e465..0000000000 --- a/src/lib/libc/stdlib/gcvt.c +++ /dev/null @@ -1,111 +0,0 @@ -/* $OpenBSD: gcvt.c,v 1.10 2006/10/29 18:45:56 deraadt Exp $ */ - -/* - * Copyright (c) 2002, 2003, 2006 Todd C. Miller - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Sponsored in part by the Defense Advanced Research Projects - * Agency (DARPA) and Air Force Research Laboratory, Air Force - * Materiel Command, USAF, under agreement number F39502-99-1-0512. - */ - -#include -#include -#include -#include - -extern char *__dtoa(double, int, int, int *, int *, char **); -extern void __freedtoa(char *); - -char * -gcvt(double value, int ndigit, char *buf) -{ - char *digits, *dst, *src; - int i, decpt, sign; - struct lconv *lconv; - - lconv = localeconv(); - if (ndigit == 0) { - buf[0] = '\0'; - return (buf); - } - - digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL); - if (decpt == 9999) { - /* - * Infinity or NaN, convert to inf or nan with sign. - * We assume the buffer is at least ndigit long. - */ - snprintf(buf, ndigit + 1, "%s%s", sign ? "-" : "", - *digits == 'I' ? "inf" : "nan"); - __freedtoa(digits); - return (buf); - } - - dst = buf; - if (sign) - *dst++ = '-'; - - if (decpt < 0 || decpt > ndigit) { - /* exponential format (e.g. 1.2345e+13) */ - if (--decpt < 0) { - sign = 1; - decpt = -decpt; - } else - sign = 0; - src = digits; - *dst++ = *src++; - *dst++ = *lconv->decimal_point; - while (*src != '\0') - *dst++ = *src++; - *dst++ = 'e'; - if (sign) - *dst++ = '-'; - else - *dst++ = '+'; - if (decpt < 10) { - *dst++ = '0'; - *dst++ = '0' + decpt; - *dst = '\0'; - } else { - /* XXX - optimize */ - for (sign = decpt, i = 0; (sign /= 10) != 0; i++) - continue; - dst[i + 1] = '\0'; - while (decpt != 0) { - dst[i--] = '0' + decpt % 10; - decpt /= 10; - } - } - } else { - /* standard format */ - for (i = 0, src = digits; i < decpt; i++) { - if (*src != '\0') - *dst++ = *src++; - else - *dst++ = '0'; - } - if (*src != '\0') { - if (src == digits) - *dst++ = '0'; /* zero before decimal point */ - *dst++ = *lconv->decimal_point; - for (i = decpt; digits[i] != '\0'; i++) { - *dst++ = digits[i]; - } - } - *dst = '\0'; - } - __freedtoa(digits); - return (buf); -} diff --git a/src/lib/libc/stdlib/getenv.3 b/src/lib/libc/stdlib/getenv.3 deleted file mode 100644 index 1d8546997f..0000000000 --- a/src/lib/libc/stdlib/getenv.3 +++ /dev/null @@ -1,148 +0,0 @@ -.\" Copyright (c) 1988, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: getenv.3,v 1.12 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt GETENV 3 -.Os -.Sh NAME -.Nm getenv , -.Nm putenv , -.Nm setenv , -.Nm unsetenv -.Nd environment variable functions -.Sh SYNOPSIS -.Fd #include -.Ft char * -.Fn getenv "const char *name" -.Ft int -.Fn setenv "const char *name" "const char *value" "int overwrite" -.Ft int -.Fn putenv "const char *string" -.Ft void -.Fn unsetenv "const char *name" -.Sh DESCRIPTION -These functions set, unset, and fetch environment variables from the host -.Em environment list . -For compatibility with differing environment conventions, the given arguments -.Fa name -and -.Fa value -may be appended and prepended, respectively, with an equal sign -.Dq Li \&= . -.Pp -The -.Fn getenv -function obtains the current value of the environment variable -.Fa name . -If the variable -.Fa name -is not in the current environment, a null pointer is returned. -.Pp -The -.Fn setenv -function inserts or resets the environment variable -.Fa name -in the current environment list. -If the variable -.Fa name -does not exist in the list, it is inserted with the given -.Fa value . -If the variable does exist, the argument -.Fa overwrite -is tested; if -.Fa overwrite -is zero, the variable is not reset, otherwise it is reset to the given -.Fa value . -.Pp -The -.Fn putenv -function takes an argument of the form -.Ar name Ns = Ns Ar value -and is equivalent to: -.Bd -literal -offset indent -setenv(name, value, 1); -.Ed -.Pp -The -.Fn unsetenv -function deletes all instances of the variable name pointed to by -.Fa name -from the list. -.Sh RETURN VALUES -The functions -.Fn setenv -and -.Fn putenv -return zero if successful; otherwise the global variable -.Va errno -is set to indicate the error and \-1 is returned. -.Pp -If -.Fn getenv -is successful, the string returned should be considered read-only. -.Sh ERRORS -.Bl -tag -width Er -.It Bq Er ENOMEM -The function -.Fn setenv -or -.Fn putenv -failed because they were unable to allocate memory for the environment. -.El -.Sh SEE ALSO -.Xr csh 1 , -.Xr sh 1 , -.Xr execve 2 , -.Xr environ 7 -.Sh STANDARDS -The -.Fn getenv -function conforms to -.St -ansiC . -.Sh HISTORY -The function -.Fn getenv -appeared in -.At v7 -and -.Bx 3 . -The functions -.Fn setenv -and -.Fn unsetenv -appeared in -.Bx 4.3 Tahoe . -The -.Fn putenv -function appeared in -.Bx 4.3 Reno . diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c deleted file mode 100644 index 72367b34e2..0000000000 --- a/src/lib/libc/stdlib/getenv.c +++ /dev/null @@ -1,80 +0,0 @@ -/* $OpenBSD: getenv.c,v 1.8 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1987, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -char *__findenv(const char *name, int *offset); - -/* - * __findenv -- - * Returns pointer to value associated with name, if any, else NULL. - * Sets offset to be the offset of the name/value combination in the - * environmental array, for use by setenv(3) and unsetenv(3). - * Explicitly removes '=' in argument name. - * - * This routine *should* be a static; don't use it. - */ -char * -__findenv(const char *name, int *offset) -{ - extern char **environ; - int len, i; - const char *np; - char **p, *cp; - - if (name == NULL || environ == NULL) - return (NULL); - for (np = name; *np && *np != '='; ++np) - ; - len = np - name; - for (p = environ; (cp = *p) != NULL; ++p) { - for (np = name, i = len; i && *cp; i--) - if (*cp++ != *np++) - break; - if (i == 0 && *cp++ == '=') { - *offset = p - environ; - return (cp); - } - } - return (NULL); -} - -/* - * getenv -- - * Returns ptr to value associated with name, if any, else NULL. - */ -char * -getenv(const char *name) -{ - int offset; - - return (__findenv(name, &offset)); -} diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 deleted file mode 100644 index 06167cb7d6..0000000000 --- a/src/lib/libc/stdlib/getopt.3 +++ /dev/null @@ -1,371 +0,0 @@ -.\" Copyright (c) 1988, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: getopt.3,v 1.40 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt GETOPT 3 -.Os -.Sh NAME -.Nm getopt -.Nd get option character from command line argument list -.Sh SYNOPSIS -.Fd #include -.Vt extern char *optarg; -.Vt extern int opterr; -.Vt extern int optind; -.Vt extern int optopt; -.Vt extern int optreset; -.Ft int -.Fn getopt "int argc" "char * const *argv" "const char *optstring" -.Sh DESCRIPTION -The -.Fn getopt -function incrementally parses a command line argument list -.Fa argv -and returns the next -.Em known -option character. -An option character is -.Em known -if it has been specified in the string of accepted option characters, -.Fa optstring . -.Pp -The option string -.Fa optstring -may contain the following elements: individual characters, -characters followed by a colon, and characters followed by two colons. -A character followed by a single colon indicates that an argument -is to follow the option on the command line. -Two colons indicates that the argument is optional \- this is an -extension not covered by POSIX. -For example, an option string -.Qq x -recognizes an option -.Fl x , -and an option string -.Qq Li x: -recognizes an option and argument -.Fl x Ar argument . -It does not matter to -.Fn getopt -if a following argument has leading whitespace. -.Pp -On return from -.Fn getopt , -.Va optarg -points to an option argument, if it is anticipated, -and the variable -.Va optind -contains the index to the next -.Fa argv -argument for a subsequent call -to -.Fn getopt . -.Pp -The variables -.Va opterr -and -.Va optind -are both initialized to 1. -The -.Va optind -variable may be set to another value larger than 0 before a set of calls to -.Fn getopt -in order to skip over more or less -.Fa argv -entries. -An -.Va optind -value of 0 is reserved for compatibility with GNU -.Fn getopt . -.Pp -In order to use -.Fn getopt -to evaluate multiple sets of arguments, or to evaluate a single set of -arguments multiple times, -the variable -.Va optreset -must be set to 1 before the second and each additional set of calls to -.Fn getopt , -and the variable -.Va optind -must be reinitialized. -.Pp -The -.Fn getopt -function returns \-1 when the argument list is exhausted. -The interpretation of options in the argument list may be cancelled -by the option -.Ql -- -(double dash) which causes -.Fn getopt -to signal the end of argument processing and return \-1. -When all options have been processed (i.e., up to the first non-option -argument), -.Fn getopt -returns \-1. -.Sh RETURN VALUES -The -.Fn getopt -function returns the next known option character in -.Fa optstring . -If -.Fn getopt -encounters a character not found in -.Fa optstring -or if it detects a missing option argument, -it returns -.Sq \&? -(question mark). -If -.Fa optstring -has a leading -.Sq \&: -then a missing option argument causes -.Sq \&: -to be returned instead of -.Sq \&? . -In either case, the variable -.Va optopt -is set to the character that caused the error. -The -.Fn getopt -function returns \-1 when the argument list is exhausted. -.Sh ENVIRONMENT -.Bl -tag -width POSIXLY_CORRECTXX -.It Ev POSIXLY_CORRECT -If set, a leading -.Sq - -in -.Ar optstring -is ignored. -.El -.Sh EXAMPLES -The following code accepts the options -.Fl b -and -.Fl f Ar argument -and adjusts -.Va argc -and -.Va argv -after option argument processing has completed. -.Bd -literal -offset indent -int bflag, ch, fd; - -bflag = 0; -while ((ch = getopt(argc, argv, "bf:")) != -1) { - switch (ch) { - case 'b': - bflag = 1; - break; - case 'f': - if ((fd = open(optarg, O_RDONLY, 0)) == -1) - err(1, "%s", optarg); - break; - default: - usage(); - /* NOTREACHED */ - } -} -argc -= optind; -argv += optind; -.Ed -.Sh DIAGNOSTICS -If the -.Fn getopt -function encounters a character not found in the string -.Fa optstring -or detects -a missing option argument, it writes an error message to -.Em stderr -and returns -.Ql \&? . -Setting -.Va opterr -to a zero will disable these error messages. -If -.Fa optstring -has a leading -.Ql \&: -then a missing option argument causes a -.Ql \&: -to be returned in addition to suppressing any error messages. -.Pp -Option arguments are allowed to begin with -.Ql - ; -this is reasonable but reduces the amount of error checking possible. -.Sh SEE ALSO -.Xr getopt 1 , -.Xr getopt_long 3 , -.Xr getsubopt 3 -.Sh STANDARDS -The -.Fn getopt -function implements a superset of the functionality specified by -.St -p1003.1 . -.Pp -The following extensions are supported: -.Bl -tag -width "xxx" -.It Li o -The -.Va optreset -variable was added to make it possible to call the -.Fn getopt -function multiple times. -.It Li o -If the -.Va optind -variable is set to 0, -.Fn getopt -will behave as if the -.Va optreset -variable has been set. -This is for compatibility with -.Tn GNU -.Fn getopt . -New code should use -.Va optreset -instead. -.It Li o -If the first character of -.Fa optstring -is a plus sign -.Pq Ql + , -it will be ignored. -This is for compatibility with -.Tn GNU -.Fn getopt . -.It Li o -If the first character of -.Fa optstring -is a dash -.Pq Ql - , -non-options will be returned as arguments to the option character -.Ql \e1 . -This is for compatibility with -.Tn GNU -.Fn getopt . -.It Li o -A single dash -.Pq Ql - -may be specified as a character in -.Fa optstring , -however it should -.Em never -have an argument associated with it. -This allows -.Fn getopt -to be used with programs that expect -.Ql - -as an option flag. -This practice is wrong, and should not be used in any current development. -It is provided for backward compatibility -.Em only . -Care should be taken not to use -.Ql - -as the first character in -.Fa optstring -to avoid a semantic conflict with -.Tn GNU -.Fn getopt -semantics (see above). -By default, a single dash causes -.Fn getopt -to return \-1. -.El -.Pp -Historic -.Bx -versions of -.Fn getopt -set -.Fa optopt -to the last option character processed. -However, this conflicts with -.St -p1003.1 -which stipulates that -.Fa optopt -be set to the last character that caused an error. -.Sh HISTORY -The -.Fn getopt -function appeared in -.Bx 4.3 . -.Sh BUGS -The -.Fn getopt -function was once specified to return -.Dv EOF -instead of \-1. -This was changed by -.St -p1003.2-92 -to decouple -.Fn getopt -from -.Aq Pa stdio.h . -.Pp -It is possible to handle digits as option letters. -This allows -.Fn getopt -to be used with programs that expect a number -.Pq Dq Li \-3 -as an option. -This practice is wrong, and should not be used in any current development. -It is provided for backward compatibility -.Em only . -The following code fragment works in most cases and can handle mixed -number and letter arguments. -.Bd -literal -offset indent -int aflag = 0, bflag = 0, ch, lastch = '\e0'; -int length = -1, newarg = 1, prevoptind = 1; - -while ((ch = getopt(argc, argv, "0123456789ab")) != -1) { - switch (ch) { - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - if (newarg || !isdigit(lastch)) - length = 0; - else if (length > INT_MAX / 10) - usage(); - length = (length * 10) + (ch - '0'); - break; - case 'a': - aflag = 1; - break; - case 'b': - bflag = 1; - break; - default: - usage(); - } - lastch = ch; - newarg = optind != prevoptind; - prevoptind = optind; -} -.Ed diff --git a/src/lib/libc/stdlib/getopt_long.3 b/src/lib/libc/stdlib/getopt_long.3 deleted file mode 100644 index 2bab963734..0000000000 --- a/src/lib/libc/stdlib/getopt_long.3 +++ /dev/null @@ -1,464 +0,0 @@ -.\" $OpenBSD: getopt_long.3,v 1.15 2007/07/03 12:06:07 jmc Exp $ -.\" $NetBSD: getopt_long.3,v 1.11 2002/10/02 10:54:19 wiz Exp $ -.\" -.\" Copyright (c) 1988, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)getopt.3 8.5 (Berkeley) 4/27/95 -.\" -.Dd $Mdocdate: July 3 2007 $ -.Dt GETOPT_LONG 3 -.Os -.Sh NAME -.Nm getopt_long , -.Nm getopt_long_only -.Nd get long options from command line argument list -.Sh SYNOPSIS -.Fd #include -.Vt extern char *optarg; -.Vt extern int optind; -.Vt extern int optopt; -.Vt extern int opterr; -.Vt extern int optreset; -.Ft int -.Fn getopt_long "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex" -.Ft int -.Fn getopt_long_only "int argc" "char * const *argv" "const char *optstring" "const struct option *longopts" "int *longindex" -.Sh DESCRIPTION -The -.Fn getopt_long -function is similar to -.Xr getopt 3 -but it accepts options in two forms: words and characters. -The -.Fn getopt_long -function provides a superset of the functionality of -.Xr getopt 3 . -.Fn getopt_long -can be used in two ways. -In the first way, every long option understood by the program has a -corresponding short option, and the option structure is only used to -translate from long options to short options. -When used in this fashion, -.Fn getopt_long -behaves identically to -.Xr getopt 3 . -This is a good way to add long option processing to an existing program -with the minimum of rewriting. -.Pp -In the second mechanism, a long option sets a flag in the -.Fa option -structure passed, or will store a pointer to the command line argument -in the -.Fa option -structure passed to it for options that take arguments. -Additionally, the long option's argument may be specified as a single -argument with an equal sign, e.g. -.Bd -literal -offset indent -$ myprogram --myoption=somevalue -.Ed -.Pp -When a long option is processed, the call to -.Fn getopt_long -will return 0. -For this reason, long option processing without -shortcuts is not backwards compatible with -.Xr getopt 3 . -.Pp -It is possible to combine these methods, providing for long options -processing with short option equivalents for some options. -Less frequently used options would be processed as long options only. -.Pp -Abbreviated long option names are accepted when -.Fn getopt_long -processes long options if the abbreviation is unique. -An exact match is always preferred for a defined long option. -.Pp -The -.Fn getopt_long -call requires a structure to be initialized describing the long -options. -The structure is: -.Bd -literal -offset indent -struct option { - char *name; - int has_arg; - int *flag; - int val; -}; -.Ed -.Pp -The -.Fa name -field should contain the option name without the leading double dash. -.Pp -The -.Fa has_arg -field should be one of: -.Pp -.Bl -tag -width "optional_argument" -compact -offset indent -.It Dv no_argument -no argument to the option is expected. -.It Dv required_argument -an argument to the option is required. -.It Dv optional_argument -an argument to the option may be presented. -.El -.Pp -If -.Fa flag -is not -.Dv NULL , -then the integer pointed to by it will be set to the value in the -.Fa val -field. -If the -.Fa flag -field is -.Dv NULL , -then the -.Fa val -field will be returned. -Setting -.Fa flag -to -.Dv NULL -and setting -.Fa val -to the corresponding short option will make this function act just -like -.Xr getopt 3 . -.Pp -If the -.Fa longindex -field is not -.Dv NULL , -then the integer pointed to by it will be set to the index of the long -option relative to -.Fa longopts . -.Pp -The last element of the -.Fa longopts -array has to be filled with zeroes. -.Pp -The -.Fn getopt_long_only -function behaves identically to -.Fn getopt_long -with the exception that long options may start with -.Sq - -in addition to -.Sq -- . -If an option starting with -.Sq - -does not match a long option but does match a single-character option, -the single-character option is returned. -.Sh RETURN VALUES -If the -.Fa flag -field in -.Li struct option -is -.Dv NULL , -.Fn getopt_long -and -.Fn getopt_long_only -return the value specified in the -.Fa val -field, which is usually just the corresponding short option. -If -.Fa flag -is not -.Dv NULL , -these functions return 0 and store -.Fa val -in the location pointed to by -.Fa flag . -These functions return -.Sq \: -if there was a missing option argument, -.Sq \&? -if the user specified an unknown or ambiguous option, and -\-1 when the argument list has been exhausted. -.Sh EXAMPLES -.Bd -literal -int bflag, ch, fd; -int daggerset; - -/* options descriptor */ -static struct option longopts[] = { - { "buffy", no_argument, NULL, 'b' }, - { "fluoride", required_argument, NULL, 'f' }, - { "daggerset", no_argument, &daggerset, 1 }, - { NULL, 0, NULL, 0 } -}; - -bflag = 0; -while ((ch = getopt_long(argc, argv, "bf:", longopts, NULL)) != -1) - switch (ch) { - case 'b': - bflag = 1; - break; - case 'f': - if ((fd = open(optarg, O_RDONLY, 0)) == -1) - err(1, "unable to open %s", optarg); - break; - case 0: - if (daggerset) - fprintf(stderr, "Buffy will use her dagger to " - "apply fluoride to dracula's teeth\en"); - break; - default: - usage(); - /* NOTREACHED */ - } -argc -= optind; -argv += optind; -.Ed -.Sh IMPLEMENTATION DIFFERENCES -This section describes differences to the GNU implementation -found in glibc-2.1.3: -.Bl -bullet -.It -handling of -.Ql - -as the first character of the option string in the presence of the -environment variable -.Ev POSIXLY_CORRECT : -.Bl -tag -width "OpenBSD" -.It GNU -ignores -.Ev POSIXLY_CORRECT -and returns non-options as arguments to option -.Ql \e1 . -.It OpenBSD -honors -.Ev POSIXLY_CORRECT -and stops at the first non-option. -.El -.It -handling of -.Ql - -within the option string (not the first character): -.Bl -tag -width "OpenBSD" -.It GNU -treats a -.Ql - -on the command line as a non-argument. -.It OpenBSD -a -.Ql - -within the option string matches a -.Ql - -(single dash) on the command line. -This functionality is provided for backward compatibility with -programs, such as -.Xr su 1 , -that use -.Ql - -as an option flag. -This practice is wrong, and should not be used in any current development. -.El -.It -handling of -.Ql :: -in the option string in the presence of -.Ev POSIXLY_CORRECT : -.Bl -tag -width "OpenBSD" -.It Both -GNU and -.Ox -ignore -.Ev POSIXLY_CORRECT -here and take -.Ql :: -to mean the preceding option takes an optional argument. -.El -.It -return value in case of missing argument if first character -(after -.Ql + -or -.Ql - ) -in the option string is not -.Ql \&: : -.Bl -tag -width "OpenBSD" -.It GNU -returns -.Ql \&? -.It OpenBSD -returns -.Ql \&: -(since -.Ox Ns 's -.Xr getopt 3 -does). -.El -.It -handling of -.Ql --a -in -.Xr getopt 3 : -.Bl -tag -width "OpenBSD" -.It GNU -parses this as option -.Ql - , -option -.Ql a . -.It OpenBSD -parses this as -.Ql -- , -and returns \-1 (ignoring the -.Ql a ) -(because the original -.Fn getopt -did.) -.El -.It -setting of -.Va optopt -for long options with -.Va flag -.No non- Ns Dv NULL : -.Bl -tag -width "OpenBSD" -.It GNU -sets -.Va optopt -to -.Va val . -.It OpenBSD -sets -.Va optopt -to 0 (since -.Va val -would never be returned). -.El -.It -handling of -.Ql -W -with -.Ql W; -in the option string in -.Xr getopt 3 -(not -.Fn getopt_long ) : -.Bl -tag -width "OpenBSD" -.It GNU -causes a segmentation fault. -.It OpenBSD -no special handling is done; -.Ql W; -is interpreted as two separate options, neither of which take an argument. -.El -.It -setting of -.Va optarg -for long options without an argument that are invoked via -.Ql -W -(with -.Ql W; -in the option string): -.Bl -tag -width "OpenBSD" -.It GNU -sets -.Va optarg -to the option name (the argument of -.Ql -W ) . -.It OpenBSD -sets -.Va optarg -to -.Dv NULL -(the argument of the long option). -.El -.It -handling of -.Ql -W -with an argument that is not (a prefix to) a known long option -(with -.Ql W; -in the option string): -.Bl -tag -width "OpenBSD" -.It GNU -returns -.Ql -W -with -.Va optarg -set to the unknown option. -.It OpenBSD -treats this as an error (unknown option) and returns -.Ql \&? -with -.Va optopt -set to 0 and -.Va optarg -set to -.Dv NULL -(as GNU's man page documents). -.El -.It -The error messages are different. -.It -.Ox -does not permute the argument vector at the same points in -the calling sequence as GNU does. -The aspects normally used by the caller -(ordering after \-1 is returned, value of -.Va optind -relative to current positions) are the same, though. -(We do fewer variable swaps.) -.El -.Sh ENVIRONMENT -.Bl -tag -width Ev -.It Ev POSIXLY_CORRECT -If set, option processing stops when the first non-option is found and -a leading -.Sq - -or -.Sq + -in the -.Ar optstring -is ignored. -.El -.Sh SEE ALSO -.Xr getopt 3 -.Sh HISTORY -The -.Fn getopt_long -and -.Fn getopt_long_only -functions first appeared in GNU libiberty. -This implementation first appeared in -.Ox 3.3 . -.Sh BUGS -The -.Ar argv -argument is not really -.Dv const -as its elements may be permuted (unless -.Ev POSIXLY_CORRECT -is set). diff --git a/src/lib/libc/stdlib/getopt_long.c b/src/lib/libc/stdlib/getopt_long.c deleted file mode 100644 index 73d97c0ed3..0000000000 --- a/src/lib/libc/stdlib/getopt_long.c +++ /dev/null @@ -1,517 +0,0 @@ -/* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */ -/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ - -/* - * Copyright (c) 2002 Todd C. Miller - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Sponsored in part by the Defense Advanced Research Projects - * Agency (DARPA) and Air Force Research Laboratory, Air Force - * Materiel Command, USAF, under agreement number F39502-99-1-0512. - */ -/*- - * Copyright (c) 2000 The NetBSD Foundation, Inc. - * All rights reserved. - * - * This code is derived from software contributed to The NetBSD Foundation - * by Dieter Baron and Thomas Klausner. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS - * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include - -#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ - -#ifdef REPLACE_GETOPT -int opterr = 1; /* if error message should be printed */ -int optind = 1; /* index into parent argv vector */ -int optopt = '?'; /* character checked for validity */ -int optreset; /* reset getopt */ -char *optarg; /* argument associated with option */ -#endif - -#define PRINT_ERROR ((opterr) && (*options != ':')) - -#define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */ -#define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */ -#define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ - -/* return values */ -#define BADCH (int)'?' -#define BADARG ((*options == ':') ? (int)':' : (int)'?') -#define INORDER (int)1 - -#define EMSG "" - -static int getopt_internal(int, char * const *, const char *, - const struct option *, int *, int); -static int parse_long_options(char * const *, const char *, - const struct option *, int *, int); -static int gcd(int, int); -static void permute_args(int, int, int, char * const *); - -static char *place = EMSG; /* option letter processing */ - -/* XXX: set optreset to 1 rather than these two */ -static int nonopt_start = -1; /* first non option argument (for permute) */ -static int nonopt_end = -1; /* first option after non options (for permute) */ - -/* Error messages */ -static const char recargchar[] = "option requires an argument -- %c"; -static const char recargstring[] = "option requires an argument -- %s"; -static const char ambig[] = "ambiguous option -- %.*s"; -static const char noarg[] = "option doesn't take an argument -- %.*s"; -static const char illoptchar[] = "unknown option -- %c"; -static const char illoptstring[] = "unknown option -- %s"; - -/* - * Compute the greatest common divisor of a and b. - */ -static int -gcd(int a, int b) -{ - int c; - - c = a % b; - while (c != 0) { - a = b; - b = c; - c = a % b; - } - - return (b); -} - -/* - * Exchange the block from nonopt_start to nonopt_end with the block - * from nonopt_end to opt_end (keeping the same order of arguments - * in each block). - */ -static void -permute_args(int panonopt_start, int panonopt_end, int opt_end, - char * const *nargv) -{ - int cstart, cyclelen, i, j, ncycle, nnonopts, nopts, pos; - char *swap; - - /* - * compute lengths of blocks and number and size of cycles - */ - nnonopts = panonopt_end - panonopt_start; - nopts = opt_end - panonopt_end; - ncycle = gcd(nnonopts, nopts); - cyclelen = (opt_end - panonopt_start) / ncycle; - - for (i = 0; i < ncycle; i++) { - cstart = panonopt_end+i; - pos = cstart; - for (j = 0; j < cyclelen; j++) { - if (pos >= panonopt_end) - pos -= nnonopts; - else - pos += nopts; - swap = nargv[pos]; - /* LINTED const cast */ - ((char **) nargv)[pos] = nargv[cstart]; - /* LINTED const cast */ - ((char **)nargv)[cstart] = swap; - } - } -} - -/* - * parse_long_options -- - * Parse long options in argc/argv argument vector. - * Returns -1 if short_too is set and the option does not match long_options. - */ -static int -parse_long_options(char * const *nargv, const char *options, - const struct option *long_options, int *idx, int short_too) -{ - char *current_argv, *has_equal; - size_t current_argv_len; - int i, match; - - current_argv = place; - match = -1; - - optind++; - - if ((has_equal = strchr(current_argv, '=')) != NULL) { - /* argument found (--option=arg) */ - current_argv_len = has_equal - current_argv; - has_equal++; - } else - current_argv_len = strlen(current_argv); - - for (i = 0; long_options[i].name; i++) { - /* find matching long option */ - if (strncmp(current_argv, long_options[i].name, - current_argv_len)) - continue; - - if (strlen(long_options[i].name) == current_argv_len) { - /* exact match */ - match = i; - break; - } - /* - * If this is a known short option, don't allow - * a partial match of a single character. - */ - if (short_too && current_argv_len == 1) - continue; - - if (match == -1) /* partial match */ - match = i; - else { - /* ambiguous abbreviation */ - if (PRINT_ERROR) - warnx(ambig, (int)current_argv_len, - current_argv); - optopt = 0; - return (BADCH); - } - } - if (match != -1) { /* option found */ - if (long_options[match].has_arg == no_argument - && has_equal) { - if (PRINT_ERROR) - warnx(noarg, (int)current_argv_len, - current_argv); - /* - * XXX: GNU sets optopt to val regardless of flag - */ - if (long_options[match].flag == NULL) - optopt = long_options[match].val; - else - optopt = 0; - return (BADARG); - } - if (long_options[match].has_arg == required_argument || - long_options[match].has_arg == optional_argument) { - if (has_equal) - optarg = has_equal; - else if (long_options[match].has_arg == - required_argument) { - /* - * optional argument doesn't use next nargv - */ - optarg = nargv[optind++]; - } - } - if ((long_options[match].has_arg == required_argument) - && (optarg == NULL)) { - /* - * Missing argument; leading ':' indicates no error - * should be generated. - */ - if (PRINT_ERROR) - warnx(recargstring, - current_argv); - /* - * XXX: GNU sets optopt to val regardless of flag - */ - if (long_options[match].flag == NULL) - optopt = long_options[match].val; - else - optopt = 0; - --optind; - return (BADARG); - } - } else { /* unknown option */ - if (short_too) { - --optind; - return (-1); - } - if (PRINT_ERROR) - warnx(illoptstring, current_argv); - optopt = 0; - return (BADCH); - } - if (idx) - *idx = match; - if (long_options[match].flag) { - *long_options[match].flag = long_options[match].val; - return (0); - } else - return (long_options[match].val); -} - -/* - * getopt_internal -- - * Parse argc/argv argument vector. Called by user level routines. - */ -static int -getopt_internal(int nargc, char * const *nargv, const char *options, - const struct option *long_options, int *idx, int flags) -{ - char *oli; /* option letter list index */ - int optchar, short_too; - static int posixly_correct = -1; - - if (options == NULL) - return (-1); - - /* - * Disable GNU extensions if POSIXLY_CORRECT is set or options - * string begins with a '+'. - */ - if (posixly_correct == -1) - posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); - if (posixly_correct || *options == '+') - flags &= ~FLAG_PERMUTE; - else if (*options == '-') - flags |= FLAG_ALLARGS; - if (*options == '+' || *options == '-') - options++; - - /* - * XXX Some GNU programs (like cvs) set optind to 0 instead of - * XXX using optreset. Work around this braindamage. - */ - if (optind == 0) - optind = optreset = 1; - - optarg = NULL; - if (optreset) - nonopt_start = nonopt_end = -1; -start: - if (optreset || !*place) { /* update scanning pointer */ - optreset = 0; - if (optind >= nargc) { /* end of argument vector */ - place = EMSG; - if (nonopt_end != -1) { - /* do permutation, if we have to */ - permute_args(nonopt_start, nonopt_end, - optind, nargv); - optind -= nonopt_end - nonopt_start; - } - else if (nonopt_start != -1) { - /* - * If we skipped non-options, set optind - * to the first of them. - */ - optind = nonopt_start; - } - nonopt_start = nonopt_end = -1; - return (-1); - } - if (*(place = nargv[optind]) != '-' || - (place[1] == '\0' && strchr(options, '-') == NULL)) { - place = EMSG; /* found non-option */ - if (flags & FLAG_ALLARGS) { - /* - * GNU extension: - * return non-option as argument to option 1 - */ - optarg = nargv[optind++]; - return (INORDER); - } - if (!(flags & FLAG_PERMUTE)) { - /* - * If no permutation wanted, stop parsing - * at first non-option. - */ - return (-1); - } - /* do permutation */ - if (nonopt_start == -1) - nonopt_start = optind; - else if (nonopt_end != -1) { - permute_args(nonopt_start, nonopt_end, - optind, nargv); - nonopt_start = optind - - (nonopt_end - nonopt_start); - nonopt_end = -1; - } - optind++; - /* process next argument */ - goto start; - } - if (nonopt_start != -1 && nonopt_end == -1) - nonopt_end = optind; - - /* - * If we have "-" do nothing, if "--" we are done. - */ - if (place[1] != '\0' && *++place == '-' && place[1] == '\0') { - optind++; - place = EMSG; - /* - * We found an option (--), so if we skipped - * non-options, we have to permute. - */ - if (nonopt_end != -1) { - permute_args(nonopt_start, nonopt_end, - optind, nargv); - optind -= nonopt_end - nonopt_start; - } - nonopt_start = nonopt_end = -1; - return (-1); - } - } - - /* - * Check long options if: - * 1) we were passed some - * 2) the arg is not just "-" - * 3) either the arg starts with -- we are getopt_long_only() - */ - if (long_options != NULL && place != nargv[optind] && - (*place == '-' || (flags & FLAG_LONGONLY))) { - short_too = 0; - if (*place == '-') - place++; /* --foo long option */ - else if (*place != ':' && strchr(options, *place) != NULL) - short_too = 1; /* could be short option too */ - - optchar = parse_long_options(nargv, options, long_options, - idx, short_too); - if (optchar != -1) { - place = EMSG; - return (optchar); - } - } - - if ((optchar = (int)*place++) == (int)':' || - (optchar == (int)'-' && *place != '\0') || - (oli = strchr(options, optchar)) == NULL) { - /* - * If the user specified "-" and '-' isn't listed in - * options, return -1 (non-option) as per POSIX. - * Otherwise, it is an unknown option character (or ':'). - */ - if (optchar == (int)'-' && *place == '\0') - return (-1); - if (!*place) - ++optind; - if (PRINT_ERROR) - warnx(illoptchar, optchar); - optopt = optchar; - return (BADCH); - } - if (long_options != NULL && optchar == 'W' && oli[1] == ';') { - /* -W long-option */ - if (*place) /* no space */ - /* NOTHING */; - else if (++optind >= nargc) { /* no arg */ - place = EMSG; - if (PRINT_ERROR) - warnx(recargchar, optchar); - optopt = optchar; - return (BADARG); - } else /* white space */ - place = nargv[optind]; - optchar = parse_long_options(nargv, options, long_options, - idx, 0); - place = EMSG; - return (optchar); - } - if (*++oli != ':') { /* doesn't take argument */ - if (!*place) - ++optind; - } else { /* takes (optional) argument */ - optarg = NULL; - if (*place) /* no white space */ - optarg = place; - else if (oli[1] != ':') { /* arg not optional */ - if (++optind >= nargc) { /* no arg */ - place = EMSG; - if (PRINT_ERROR) - warnx(recargchar, optchar); - optopt = optchar; - return (BADARG); - } else - optarg = nargv[optind]; - } - place = EMSG; - ++optind; - } - /* dump back option letter */ - return (optchar); -} - -#ifdef REPLACE_GETOPT -/* - * getopt -- - * Parse argc/argv argument vector. - * - * [eventually this will replace the BSD getopt] - */ -int -getopt(int nargc, char * const *nargv, const char *options) -{ - - /* - * We don't pass FLAG_PERMUTE to getopt_internal() since - * the BSD getopt(3) (unlike GNU) has never done this. - * - * Furthermore, since many privileged programs call getopt() - * before dropping privileges it makes sense to keep things - * as simple (and bug-free) as possible. - */ - return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); -} -#endif /* REPLACE_GETOPT */ - -/* - * getopt_long -- - * Parse argc/argv argument vector. - */ -int -getopt_long(int nargc, char * const *nargv, const char *options, - const struct option *long_options, int *idx) -{ - - return (getopt_internal(nargc, nargv, options, long_options, idx, - FLAG_PERMUTE)); -} - -/* - * getopt_long_only -- - * Parse argc/argv argument vector. - */ -int -getopt_long_only(int nargc, char * const *nargv, const char *options, - const struct option *long_options, int *idx) -{ - - return (getopt_internal(nargc, nargv, options, long_options, idx, - FLAG_PERMUTE|FLAG_LONGONLY)); -} diff --git a/src/lib/libc/stdlib/getsubopt.3 b/src/lib/libc/stdlib/getsubopt.3 deleted file mode 100644 index 9f35df703f..0000000000 --- a/src/lib/libc/stdlib/getsubopt.3 +++ /dev/null @@ -1,145 +0,0 @@ -.\" $OpenBSD: getsubopt.3,v 1.12 2007/05/31 19:19:31 jmc Exp $ -.\" -.\" Copyright (c) 1990, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)getsubopt.3 8.1 (Berkeley) 6/9/93 -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt GETSUBOPT 3 -.Os -.Sh NAME -.Nm getsubopt -.Nd get sub options from an argument -.Sh SYNOPSIS -.Fd #include -.Vt extern char *suboptarg; -.Ft int -.Fn getsubopt "char **optionp" "char * const *tokens" "char **valuep" -.Sh DESCRIPTION -The -.Fn getsubopt -function parses a string containing tokens delimited by one or more -tab, space, or comma -.Pq Ql \&, -characters. -It is intended for use in parsing groups of option arguments provided -as part of a utility command line. -.Pp -The argument -.Fa optionp -is a pointer to a pointer to the string. -The argument -.Fa tokens -is a pointer to a null-terminated array of pointers to strings. -.Pp -The -.Fn getsubopt -function returns the zero-based offset of the pointer in the -.Fa tokens -array referencing a string which matches the first token -in the string, or \-1 if the string contains no tokens or -.Fa tokens -does not contain a matching string. -.Pp -If the token is of the form -.Ar name Ns = Ns Ar value , -the location referenced by -.Fa valuep -will be set to point to the start of the -.Dq value -portion of the token. -.Pp -On return from -.Fn getsubopt , -.Fa optionp -will be set to point to the start of the next token in the string, -or the NUL at the end of the string if no more tokens are present. -The external variable -.Fa suboptarg -will be set to point to the start of the current token, or -.Dv NULL -if no tokens were present. -The argument -.Fa valuep -will be set to point to the value portion of the token, or -.Dv NULL -if no value portion was present. -.Sh EXAMPLES -.Bd -literal -char *tokens[] = { - #define ONE 0 - "one", - #define TWO 1 - "two", - NULL -}; - -\&... - -extern char *optarg, *suboptarg; -char *options, *value; - -while ((ch = getopt(argc, argv, "ab:")) != -1) { - switch (ch) { - case 'a': - /* process ``a'' option */ - break; - case 'b': - options = optarg; - while (*options) { - switch (getsubopt(&options, tokens, &value)) { - case ONE: - /* process ``one'' sub option */ - break; - case TWO: - /* process ``two'' sub option */ - if (!value) - error("no value for two"); - i = atoi(value); - break; - case -1: - if (suboptarg) - error("illegal sub option %s", - suboptarg); - else - error("missing sub option"); - break; - } - } - break; - } -} -.Ed -.Sh SEE ALSO -.Xr getopt 3 , -.Xr strsep 3 -.Sh HISTORY -The -.Fn getsubopt -function first appeared in -.Bx 4.4 . diff --git a/src/lib/libc/stdlib/getsubopt.c b/src/lib/libc/stdlib/getsubopt.c deleted file mode 100644 index 735c85ba8a..0000000000 --- a/src/lib/libc/stdlib/getsubopt.c +++ /dev/null @@ -1,92 +0,0 @@ -/* $OpenBSD: getsubopt.c,v 1.4 2005/08/08 08:05:36 espie Exp $ */ - -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include - -/* - * The SVID interface to getsubopt provides no way of figuring out which - * part of the suboptions list wasn't matched. This makes error messages - * tricky... The extern variable suboptarg is a pointer to the token - * which didn't match. - */ -char *suboptarg; - -int -getsubopt(char **optionp, char * const *tokens, char **valuep) -{ - int cnt; - char *p; - - suboptarg = *valuep = NULL; - - if (!optionp || !*optionp) - return(-1); - - /* skip leading white-space, commas */ - for (p = *optionp; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); - - if (!*p) { - *optionp = p; - return(-1); - } - - /* save the start of the token, and skip the rest of the token. */ - for (suboptarg = p; - *++p && *p != ',' && *p != '=' && *p != ' ' && *p != '\t';); - - if (*p) { - /* - * If there's an equals sign, set the value pointer, and - * skip over the value part of the token. Terminate the - * token. - */ - if (*p == '=') { - *p = '\0'; - for (*valuep = ++p; - *p && *p != ',' && *p != ' ' && *p != '\t'; ++p); - if (*p) - *p++ = '\0'; - } else - *p++ = '\0'; - /* Skip any whitespace or commas after this token. */ - for (; *p && (*p == ',' || *p == ' ' || *p == '\t'); ++p); - } - - /* set optionp for next round. */ - *optionp = p; - - for (cnt = 0; *tokens; ++tokens, ++cnt) - if (!strcmp(suboptarg, *tokens)) - return(cnt); - return(-1); -} diff --git a/src/lib/libc/stdlib/hcreate.3 b/src/lib/libc/stdlib/hcreate.3 deleted file mode 100644 index 4ff17a087d..0000000000 --- a/src/lib/libc/stdlib/hcreate.3 +++ /dev/null @@ -1,184 +0,0 @@ -.\" $OpenBSD: hcreate.3,v 1.5 2008/06/26 05:42:05 ray Exp $ -.\" $NetBSD: hcreate.3,v 1.6 2003/04/16 13:34:46 wiz Exp $ -.\" -.\" Copyright (c) 1999 The NetBSD Foundation, Inc. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to The NetBSD Foundation -.\" by Klaus Klein. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS -.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED -.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS -.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE -.\" POSSIBILITY OF SUCH DAMAGE. -.\" -.Dd $Mdocdate: June 26 2008 $ -.Dt HCREATE 3 -.Os -.Sh NAME -.Nm hcreate , -.Nm hdestroy , -.Nm hsearch -.Nd manage hash search table -.Sh SYNOPSIS -.In search.h -.Ft int -.Fn hcreate "size_t nel" -.Ft void -.Fn hdestroy "void" -.Ft ENTRY * -.Fn hsearch "ENTRY item" "ACTION action" -.Sh DESCRIPTION -The -.Fn hcreate , -.Fn hdestroy , -and -.Fn hsearch -functions manage hash search tables. -.Pp -The -.Fn hcreate -function allocates and initializes the table. -The -.Fa nel -argument specifies an estimate of the maximum number of entries to be held -by the table. -Unless further memory allocation fails, supplying an insufficient -.Fa nel -value will not result in functional harm, although a performance degradation -may occur. -Initialization using the -.Fn hcreate -function is mandatory prior to any access operations using -.Fn hsearch . -.Pp -The -.Fn hdestroy -function destroys a table previously created using -.Fn hcreate . -After a call to -.Fn hdestroy , -the data can no longer be accessed. -.Pp -The -.Fn hsearch -function is used to search to the hash table. -It returns a pointer into the -hash table indicating the address of an item. -The -.Fa item -argument is of type -.Dv ENTRY , -a structural type which contains the following members: -.Pp -.Bl -tag -compact -offset indent -width voidX*dataXX -.It Fa char *key -comparison key. -.It Fa void *data -pointer to data associated with -.Fa key . -.El -.Pp -The key comparison function used by -.Fn hsearch -is -.Xr strcmp 3 . -.Pp -The -.Fa action -argument is of type -.Dv ACTION , -an enumeration type which defines the following values: -.Bl -tag -offset indent -width ENTERXX -.It Dv ENTER -Insert -.Fa item -into the hash table. -If an existing item with the same key is found, it is not replaced. -Note that the -.Fa key -and -.Fa data -elements of -.Fa item -are used directly by the new table entry. -The storage for the -key must not be modified during the lifetime of the hash table. -.It Dv FIND -Search the hash table without inserting -.Fa item . -.El -.Sh RETURN VALUES -If successful, the -.Fn hcreate -function returns a non-zero value. -Otherwise, a value of 0 is returned and -.Va errno -is set to indicate the error. -.Pp -The -.Fn hdestroy -functions -returns no value. -.Pp -If successful, the -.Fn hsearch -function returns a pointer to a hash table entry matching -the provided key. -If the action is -.Dv FIND -and the item was not found, or if the action is -.Dv ENTER -and the insertion failed, -.Dv NULL -is returned and -.Va errno -is set to indicate the error. -If the action is -.Dv ENTER -and an entry already existed in the table matching the given -key, the existing entry is returned and is not replaced. -.Sh ERRORS -The -.Fn hcreate -and -.Fn hsearch -functions will fail if: -.Bl -tag -width Er -.It Bq Er ENOMEM -Insufficient memory is available. -.El -.Sh SEE ALSO -.Xr bsearch 3 , -.Xr lsearch 3 , -.Xr malloc 3 , -.Xr strcmp 3 -.Sh STANDARDS -These functions conform to -.St -p1003.1-2004 . -.Sh HISTORY -The -.Fn hcreate , -.Fn hdestroy -and -.Fn hsearch -functions first appeared in -.At V . -.Sh BUGS -The interface permits the use of only one hash table at a time. diff --git a/src/lib/libc/stdlib/hcreate.c b/src/lib/libc/stdlib/hcreate.c deleted file mode 100644 index 094f32c173..0000000000 --- a/src/lib/libc/stdlib/hcreate.c +++ /dev/null @@ -1,191 +0,0 @@ -/* $OpenBSD: hcreate.c,v 1.4 2007/09/02 15:19:17 deraadt Exp $ */ -/* $NetBSD: hcreate.c,v 1.5 2004/04/23 02:48:12 simonb Exp $ */ - -/* - * Copyright (c) 2001 Christopher G. Demetriou - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed for the - * NetBSD Project. See http://www.NetBSD.org/ for - * information about NetBSD. - * 4. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * <> - */ - -/* - * hcreate() / hsearch() / hdestroy() - * - * SysV/XPG4 hash table functions. - * - * Implementation done based on NetBSD manual page and Solaris manual page, - * plus my own personal experience about how they're supposed to work. - * - * I tried to look at Knuth (as cited by the Solaris manual page), but - * nobody had a copy in the office, so... - */ - -#include "namespace.h" -#include -#include -#include -#include -#include -#include -#include - -#ifndef _DIAGASSERT -#define _DIAGASSERT(x) -#endif - -/* - * DO NOT MAKE THIS STRUCTURE LARGER THAN 32 BYTES (4 ptrs on 64-bit - * ptr machine) without adjusting MAX_BUCKETS_LG2 below. - */ -struct internal_entry { - SLIST_ENTRY(internal_entry) link; - ENTRY ent; -}; -SLIST_HEAD(internal_head, internal_entry); - -#define MIN_BUCKETS_LG2 4 -#define MIN_BUCKETS (1 << MIN_BUCKETS_LG2) - -/* - * max * sizeof internal_entry must fit into size_t. - * assumes internal_entry is <= 32 (2^5) bytes. - */ -#define MAX_BUCKETS_LG2 (sizeof (size_t) * 8 - 1 - 5) -#define MAX_BUCKETS ((size_t)1 << MAX_BUCKETS_LG2) - -/* Default hash function, from db/hash/hash_func.c */ -extern u_int32_t (*__default_hash)(const void *, size_t); - -static struct internal_head *htable; -static size_t htablesize; - -int -hcreate(size_t nel) -{ - size_t idx; - unsigned int p2; - - /* Make sure this isn't called when a table already exists. */ - _DIAGASSERT(htable == NULL); - if (htable != NULL) { - errno = EINVAL; - return 0; - } - - /* If nel is too small, make it min sized. */ - if (nel < MIN_BUCKETS) - nel = MIN_BUCKETS; - - /* If it's too large, cap it. */ - if (nel > MAX_BUCKETS) - nel = MAX_BUCKETS; - - /* If it's is not a power of two in size, round up. */ - if ((nel & (nel - 1)) != 0) { - for (p2 = 0; nel != 0; p2++) - nel >>= 1; - _DIAGASSERT(p2 <= MAX_BUCKETS_LG2); - nel = 1 << p2; - } - - /* Allocate the table. */ - htablesize = nel; - htable = calloc(htablesize, sizeof htable[0]); - if (htable == NULL) { - errno = ENOMEM; - return 0; - } - - /* Initialize it. */ - for (idx = 0; idx < htablesize; idx++) - SLIST_INIT(&htable[idx]); - - return 1; -} - -void -hdestroy(void) -{ - struct internal_entry *ie; - size_t idx; - - _DIAGASSERT(htable != NULL); - if (htable == NULL) - return; - - for (idx = 0; idx < htablesize; idx++) { - while (!SLIST_EMPTY(&htable[idx])) { - ie = SLIST_FIRST(&htable[idx]); - SLIST_REMOVE_HEAD(&htable[idx], link); - free(ie->ent.key); - free(ie); - } - } - free(htable); - htable = NULL; -} - -ENTRY * -hsearch(ENTRY item, ACTION action) -{ - struct internal_head *head; - struct internal_entry *ie; - uint32_t hashval; - size_t len; - - _DIAGASSERT(htable != NULL); - _DIAGASSERT(item.key != NULL); - _DIAGASSERT(action == ENTER || action == FIND); - - len = strlen(item.key); - hashval = (*__default_hash)(item.key, len); - - head = &htable[hashval & (htablesize - 1)]; - ie = SLIST_FIRST(head); - while (ie != NULL) { - if (strcmp(ie->ent.key, item.key) == 0) - break; - ie = SLIST_NEXT(ie, link); - } - - if (ie != NULL) - return &ie->ent; - else if (action == FIND) - return NULL; - - ie = malloc(sizeof *ie); - if (ie == NULL) - return NULL; - ie->ent.key = item.key; - ie->ent.data = item.data; - - SLIST_INSERT_HEAD(head, ie, link); - return &ie->ent; -} diff --git a/src/lib/libc/stdlib/heapsort.c b/src/lib/libc/stdlib/heapsort.c deleted file mode 100644 index e6fb61ba2f..0000000000 --- a/src/lib/libc/stdlib/heapsort.c +++ /dev/null @@ -1,173 +0,0 @@ -/*- - * Copyright (c) 1991, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Ronnie Kon at Mindcraft Inc., Kevin Lew and Elmer Yglesias. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include - -/* - * Swap two areas of size number of bytes. Although qsort(3) permits random - * blocks of memory to be sorted, sorting pointers is almost certainly the - * common case (and, were it not, could easily be made so). Regardless, it - * isn't worth optimizing; the SWAP's get sped up by the cache, and pointer - * arithmetic gets lost in the time required for comparison function calls. - */ -#define SWAP(a, b, count, size, tmp) { \ - count = size; \ - do { \ - tmp = *a; \ - *a++ = *b; \ - *b++ = tmp; \ - } while (--count); \ -} - -/* Copy one block of size size to another. */ -#define COPY(a, b, count, size, tmp1, tmp2) { \ - count = size; \ - tmp1 = a; \ - tmp2 = b; \ - do { \ - *tmp1++ = *tmp2++; \ - } while (--count); \ -} - -/* - * Build the list into a heap, where a heap is defined such that for - * the records K1 ... KN, Kj/2 >= Kj for 1 <= j/2 <= j <= N. - * - * There are two cases. If j == nmemb, select largest of Ki and Kj. If - * j < nmemb, select largest of Ki, Kj and Kj+1. - */ -#define CREATE(initval, nmemb, par_i, child_i, par, child, size, count, tmp) { \ - for (par_i = initval; (child_i = par_i * 2) <= nmemb; \ - par_i = child_i) { \ - child = base + child_i * size; \ - if (child_i < nmemb && compar(child, child + size) < 0) { \ - child += size; \ - ++child_i; \ - } \ - par = base + par_i * size; \ - if (compar(child, par) <= 0) \ - break; \ - SWAP(par, child, count, size, tmp); \ - } \ -} - -/* - * Select the top of the heap and 'heapify'. Since by far the most expensive - * action is the call to the compar function, a considerable optimization - * in the average case can be achieved due to the fact that k, the displaced - * element, is usually quite small, so it would be preferable to first - * heapify, always maintaining the invariant that the larger child is copied - * over its parent's record. - * - * Then, starting from the *bottom* of the heap, finding k's correct place, - * again maintaining the invariant. As a result of the invariant no element - * is 'lost' when k is assigned its correct place in the heap. - * - * The time savings from this optimization are on the order of 15-20% for the - * average case. See Knuth, Vol. 3, page 158, problem 18. - * - * XXX Don't break the #define SELECT line, below. Reiser cpp gets upset. - */ -#define SELECT(par_i, child_i, nmemb, par, child, size, k, count, tmp1, tmp2) { \ - for (par_i = 1; (child_i = par_i * 2) <= nmemb; par_i = child_i) { \ - child = base + child_i * size; \ - if (child_i < nmemb && compar(child, child + size) < 0) { \ - child += size; \ - ++child_i; \ - } \ - par = base + par_i * size; \ - COPY(par, child, count, size, tmp1, tmp2); \ - } \ - for (;;) { \ - child_i = par_i; \ - par_i = child_i / 2; \ - child = base + child_i * size; \ - par = base + par_i * size; \ - if (child_i == 1 || compar(k, par) < 0) { \ - COPY(child, k, count, size, tmp1, tmp2); \ - break; \ - } \ - COPY(child, par, count, size, tmp1, tmp2); \ - } \ -} - -/* - * Heapsort -- Knuth, Vol. 3, page 145. Runs in O (N lg N), both average - * and worst. While heapsort is faster than the worst case of quicksort, - * the BSD quicksort does median selection so that the chance of finding - * a data set that will trigger the worst case is nonexistent. Heapsort's - * only advantage over quicksort is that it requires little additional memory. - */ -int -heapsort(void *vbase, size_t nmemb, size_t size, - int (*compar)(const void *, const void *)) -{ - int cnt, i, j, l; - char tmp, *tmp1, *tmp2; - char *base, *k, *p, *t; - - if (nmemb <= 1) - return (0); - - if (!size) { - errno = EINVAL; - return (-1); - } - - if ((k = malloc(size)) == NULL) - return (-1); - - /* - * Items are numbered from 1 to nmemb, so offset from size bytes - * below the starting address. - */ - base = (char *)vbase - size; - - for (l = nmemb / 2 + 1; --l;) - CREATE(l, nmemb, i, j, t, p, size, cnt, tmp); - - /* - * For each element of the heap, save the largest element into its - * final slot, save the displaced element (k), then recreate the - * heap. - */ - while (nmemb > 1) { - COPY(k, base + nmemb * size, cnt, size, tmp1, tmp2); - COPY(base + nmemb * size, base + size, cnt, size, tmp1, tmp2); - --nmemb; - SELECT(i, j, nmemb, t, p, size, k, cnt, tmp1, tmp2); - } - free(k); - return (0); -} diff --git a/src/lib/libc/stdlib/imaxabs.3 b/src/lib/libc/stdlib/imaxabs.3 deleted file mode 100644 index 10933e0a66..0000000000 --- a/src/lib/libc/stdlib/imaxabs.3 +++ /dev/null @@ -1,66 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: imaxabs.3,v 1.3 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt IMAXABS 3 -.Os -.Sh NAME -.Nm imaxabs -.Nd integer absolute value function -.Sh SYNOPSIS -.Fd #include -.Ft intmax_t -.Fn imaxabs "intmax_t j" -.Sh DESCRIPTION -The -.Fn imaxabs -function computes the absolute value of the intmax_t variable -.Fa j . -.Sh RETURN VALUES -The -.Fn imaxabs -function returns the absolute value. -.Sh SEE ALSO -.Xr abs 3 , -.Xr cabs 3 , -.Xr floor 3 , -.Xr hypot 3 , -.Xr labs 3 , -.Xr math 3 -.Sh STANDARDS -The -.Fn abs -function conforms to -.St -ansiC-99 . -.Sh BUGS -The absolute value of the most negative integer remains negative. diff --git a/src/lib/libc/stdlib/imaxabs.c b/src/lib/libc/stdlib/imaxabs.c deleted file mode 100644 index b7e910eefd..0000000000 --- a/src/lib/libc/stdlib/imaxabs.c +++ /dev/null @@ -1,38 +0,0 @@ -/* $OpenBSD: imaxabs.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ - -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -intmax_t -imaxabs(intmax_t j) -{ - return (j < 0 ? -j : j); -} diff --git a/src/lib/libc/stdlib/imaxdiv.3 b/src/lib/libc/stdlib/imaxdiv.3 deleted file mode 100644 index c4dee627aa..0000000000 --- a/src/lib/libc/stdlib/imaxdiv.3 +++ /dev/null @@ -1,67 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: imaxdiv.3,v 1.3 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt IMAXDIV 3 -.Os -.Sh NAME -.Nm imaxdiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.Fd #include -.Ft imaxdiv_t -.Fn imaxdiv "intmax_t num" "intmax_t denom" -.Sh DESCRIPTION -The -.Fn imaxdiv -function computes the value -.Fa num Ns / Ns Fa denom -and returns the quotient and remainder in a structure named -.Li imaxdiv_t -that contains two -.Li intmax_t -members named -.Fa quot -and -.Fa rem . -.Sh SEE ALSO -.Xr div 3 , -.Xr ldiv 3 , -.Xr lldiv 3 , -.Xr math 3 , -.Xr qdiv 3 -.Sh STANDARDS -The -.Fn imaxdiv -function conforms to -.St -ansiC-99 . diff --git a/src/lib/libc/stdlib/imaxdiv.c b/src/lib/libc/stdlib/imaxdiv.c deleted file mode 100644 index 0515a94b96..0000000000 --- a/src/lib/libc/stdlib/imaxdiv.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $OpenBSD: imaxdiv.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ -/* - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include /* imaxdiv_t */ - -imaxdiv_t -imaxdiv(intmax_t num, intmax_t denom) -{ - imaxdiv_t r; - - /* see div.c for comments */ - - r.quot = num / denom; - r.rem = num % denom; - if (num >= 0 && r.rem < 0) { - r.quot++; - r.rem -= denom; - } - return (r); -} diff --git a/src/lib/libc/stdlib/insque.3 b/src/lib/libc/stdlib/insque.3 deleted file mode 100644 index dcb2b0d158..0000000000 --- a/src/lib/libc/stdlib/insque.3 +++ /dev/null @@ -1,106 +0,0 @@ -.\" $OpenBSD: insque.3,v 1.7 2007/05/31 19:19:31 jmc Exp $ -.\" Copyright (c) 1993 John Brezak -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. The name of the author may be used to endorse or promote products -.\" derived from this software without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt INSQUE 3 -.Os -.Sh NAME -.Nm insque , -.Nm remque -.Nd insert/remove element from a queue -.Sh SYNOPSIS -.Fd #include -.Ft void -.Fn insque "void *elem" "void *pred" -.Ft void -.Fn remque "void *elem" -.Sh DESCRIPTION -.Bf -symbolic -These interfaces have been superceded by the -.Xr queue 3 -macros and are provided for compatibility with legacy code. -.Ef -.Pp -.Fn insque -and -.Fn remque -manipulate queues built from doubly linked lists. -The queue can be either circular or linear. -Each element in the queue must be of the following form: -.Bd -literal -offset indent -struct qelem { - struct qelem *q_forw; - struct qelem *q_back; - char q_data[]; -}; -.Ed -.Pp -The first two elements in the struct must be pointers of the -same type that point to the previous and next elements in -the queue respectively. -Any subsequent data in the struct is application-dependent. -.Pp -The -.Fn insque -function inserts -.Fa elem -into a queue immediately after -.Fa pred . -.Pp -The -.Fn remque -function removes -.Fa elem -from the queue. -.Pp -These functions are not atomic unless that machine architecture allows it. -.Sh SEE ALSO -.Xr queue 3 -.Sh STANDARDS -The -.Fn insque -and -.Fn remque -functions conform to the -.St -p1003.1-2001 -and -.St -xpg4.3 -specifications. -.Sh HISTORY -The -.Fn insque -and -.Fn remque -functions are derived from the -.Li insque -and -.Li remque -instructions on the -.Tn VAX . -They first appeared in -.Bx 4.2 . diff --git a/src/lib/libc/stdlib/insque.c b/src/lib/libc/stdlib/insque.c deleted file mode 100644 index 8724efec74..0000000000 --- a/src/lib/libc/stdlib/insque.c +++ /dev/null @@ -1,48 +0,0 @@ -/* $OpenBSD: insque.c,v 1.2 2005/08/08 08:05:36 espie Exp $ */ - -/* - * Copyright (c) 1993 John Brezak - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include - -struct qelem { - struct qelem *q_forw; - struct qelem *q_back; -}; - -void -insque(void *entry, void *pred) -{ - struct qelem *e = (struct qelem *) entry; - struct qelem *p = (struct qelem *) pred; - - e->q_forw = p->q_forw; - e->q_back = p; - p->q_forw->q_back = e; - p->q_forw = e; -} diff --git a/src/lib/libc/stdlib/jrand48.c b/src/lib/libc/stdlib/jrand48.c deleted file mode 100644 index cb8c592750..0000000000 --- a/src/lib/libc/stdlib/jrand48.c +++ /dev/null @@ -1,22 +0,0 @@ -/* $OpenBSD: jrand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -long -jrand48(unsigned short xseed[3]) -{ - __dorand48(xseed); - return ((long) xseed[2] << 16) + (long) xseed[1]; -} diff --git a/src/lib/libc/stdlib/l64a.c b/src/lib/libc/stdlib/l64a.c deleted file mode 100644 index 4f33df37b2..0000000000 --- a/src/lib/libc/stdlib/l64a.c +++ /dev/null @@ -1,42 +0,0 @@ -/* $OpenBSD: l64a.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/* - * Written by J.T. Conklin . - * Public domain. - */ - -#include -#include - -char * -l64a(long value) -{ - static char buf[8]; - char *s = buf; - int digit; - int i; - - if (value < 0) { - errno = EINVAL; - return(NULL); - } - - for (i = 0; value != 0 && i < 6; i++) { - digit = value & 0x3f; - - if (digit < 2) - *s = digit + '.'; - else if (digit < 12) - *s = digit + '0' - 2; - else if (digit < 38) - *s = digit + 'A' - 12; - else - *s = digit + 'a' - 38; - - value >>= 6; - s++; - } - - *s = '\0'; - - return(buf); -} diff --git a/src/lib/libc/stdlib/labs.3 b/src/lib/libc/stdlib/labs.3 deleted file mode 100644 index 21c134fe5d..0000000000 --- a/src/lib/libc/stdlib/labs.3 +++ /dev/null @@ -1,69 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: labs.3,v 1.9 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt LABS 3 -.Os -.Sh NAME -.Nm labs, llabs -.Nd return the absolute value of a long integer -.Sh SYNOPSIS -.Fd #include -.Ft long -.Fn labs "long i" -.Ft long long -.Fn llabs "long long j" -.Sh DESCRIPTION -The -.Fn labs -function returns the absolute value of the long integer -.Fa i . -The -.Fn llabs -function returns the absolute value of the long long integer -.Fa j . -.Sh SEE ALSO -.Xr abs 3 , -.Xr cabs 3 , -.Xr floor 3 , -.Xr imaxabs 3 , -.Xr math 3 -.Sh STANDARDS -The -.Fn labs -and -.Fn llabs -functions conform to -.St -ansiC-99 . -.Sh BUGS -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 deleted file mode 100644 index ca60b9aba2..0000000000 --- a/src/lib/libc/stdlib/labs.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $OpenBSD: labs.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -long -labs(long j) -{ - return(j < 0 ? -j : j); -} diff --git a/src/lib/libc/stdlib/lcong48.c b/src/lib/libc/stdlib/lcong48.c deleted file mode 100644 index 2cf5c271ba..0000000000 --- a/src/lib/libc/stdlib/lcong48.c +++ /dev/null @@ -1,31 +0,0 @@ -/* $OpenBSD: lcong48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -extern unsigned short __rand48_seed[3]; -extern unsigned short __rand48_mult[3]; -extern unsigned short __rand48_add; - -void -lcong48(unsigned short p[7]) -{ - __rand48_seed[0] = p[0]; - __rand48_seed[1] = p[1]; - __rand48_seed[2] = p[2]; - __rand48_mult[0] = p[3]; - __rand48_mult[1] = p[4]; - __rand48_mult[2] = p[5]; - __rand48_add = p[6]; -} diff --git a/src/lib/libc/stdlib/ldiv.3 b/src/lib/libc/stdlib/ldiv.3 deleted file mode 100644 index 9627776899..0000000000 --- a/src/lib/libc/stdlib/ldiv.3 +++ /dev/null @@ -1,67 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: ldiv.3,v 1.9 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt LDIV 3 -.Os -.Sh NAME -.Nm ldiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.Fd #include -.Ft ldiv_t -.Fn ldiv "long num" "long denom" -.Sh DESCRIPTION -The -.Fn ldiv -function computes the value -.Fa num Ns / Ns Fa denom -and returns the quotient and remainder in a structure named -.Li ldiv_t -that contains two -.Li long integer -members named -.Fa quot -and -.Fa rem . -.Sh SEE ALSO -.Xr div 3 , -.Xr imaxdiv 3 , -.Xr lldiv 3 , -.Xr math 3 , -.Xr qdiv 3 -.Sh STANDARDS -The -.Fn ldiv -function conforms to -.St -ansiC . diff --git a/src/lib/libc/stdlib/ldiv.c b/src/lib/libc/stdlib/ldiv.c deleted file mode 100644 index 775065f525..0000000000 --- a/src/lib/libc/stdlib/ldiv.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $OpenBSD: ldiv.c,v 1.5 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include /* ldiv_t */ - -ldiv_t -ldiv(long num, long denom) -{ - ldiv_t r; - - /* see div.c for comments */ - - r.quot = num / denom; - r.rem = num % denom; - if (num >= 0 && r.rem < 0) { - r.quot++; - r.rem -= denom; - } - return (r); -} diff --git a/src/lib/libc/stdlib/llabs.c b/src/lib/libc/stdlib/llabs.c deleted file mode 100644 index fc2cd8261c..0000000000 --- a/src/lib/libc/stdlib/llabs.c +++ /dev/null @@ -1,38 +0,0 @@ -/* $OpenBSD: llabs.c,v 1.3 2007/01/08 19:39:25 deraadt Exp $ */ - -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -long long -llabs(long long j) -{ - return (j < 0 ? -j : j); -} diff --git a/src/lib/libc/stdlib/lldiv.3 b/src/lib/libc/stdlib/lldiv.3 deleted file mode 100644 index a5be188ba2..0000000000 --- a/src/lib/libc/stdlib/lldiv.3 +++ /dev/null @@ -1,67 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: lldiv.3,v 1.2 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt LLDIV 3 -.Os -.Sh NAME -.Nm lldiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.Fd #include -.Ft lldiv_t -.Fn lldiv "long long num" "long long denom" -.Sh DESCRIPTION -The -.Fn lldiv -function computes the value -.Fa num Ns / Ns Fa denom -and returns the quotient and remainder in a structure named -.Li lldiv_t -that contains two -.Li long long integer -members named -.Fa quot -and -.Fa rem . -.Sh SEE ALSO -.Xr div 3 , -.Xr imaxdiv 3 , -.Xr ldiv 3 , -.Xr math 3 , -.Xr qdiv 3 -.Sh STANDARDS -The -.Fn lldiv -function conforms to -.St -ansiC-99 . diff --git a/src/lib/libc/stdlib/lldiv.c b/src/lib/libc/stdlib/lldiv.c deleted file mode 100644 index e8d6c978b3..0000000000 --- a/src/lib/libc/stdlib/lldiv.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $OpenBSD: lldiv.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ -/* - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include /* lldiv_t */ - -lldiv_t -lldiv(long long num, long long denom) -{ - lldiv_t r; - - /* see div.c for comments */ - - r.quot = num / denom; - r.rem = num % denom; - if (num >= 0 && r.rem < 0) { - r.quot++; - r.rem -= denom; - } - return (r); -} diff --git a/src/lib/libc/stdlib/lrand48.c b/src/lib/libc/stdlib/lrand48.c deleted file mode 100644 index 21beb858ca..0000000000 --- a/src/lib/libc/stdlib/lrand48.c +++ /dev/null @@ -1,24 +0,0 @@ -/* $OpenBSD: lrand48.c,v 1.3 2005/08/08 08:05:36 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -extern unsigned short __rand48_seed[3]; - -long -lrand48(void) -{ - __dorand48(__rand48_seed); - return ((long) __rand48_seed[2] << 15) + ((long) __rand48_seed[1] >> 1); -} diff --git a/src/lib/libc/stdlib/lsearch.3 b/src/lib/libc/stdlib/lsearch.3 deleted file mode 100644 index e13ef4ec74..0000000000 --- a/src/lib/libc/stdlib/lsearch.3 +++ /dev/null @@ -1,108 +0,0 @@ -.\" $OpenBSD: lsearch.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ -.\" -.\" Copyright (c) 1989, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" @(#)lsearch.3 8.1 (Berkeley) 6/4/93 -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt LSEARCH 3 -.Os -.Sh NAME -.Nm lsearch , -.Nm lfind -.Nd linear searching routines -.Sh SYNOPSIS -.Fd #include -.Ft char * -.Fn lsearch "const void *key" "const void *base" "size_t *nelp" \ - "size_t width" "int (*compar)(void *, void *)" -.Ft char * -.Fn lfind "const void *key" "const void *base" "size_t *nelp" \ - "size_t width" "int (*compar)(void *, void *)" -.Sh DESCRIPTION -The functions -.Fn lsearch -and -.Fn lfind -provide basic linear searching functionality. -.Pp -.Fa base -is the pointer to the beginning of an array. -The argument -.Fa nelp -is the current number of elements in the array, where each element -is -.Fa width -bytes long. -The -.Fa compar -function -is a comparison routine which is used to compare two elements. -It takes two arguments which point to the -.Fa key -object and to an array member, in that order, and must return an integer -less than, equivalent to, or greater than zero if the -.Fa key -object is considered, respectively, to be less than, equal to, or greater -than the array member. -.Pp -The -.Fn lsearch -and -.Fn lfind -functions -return a pointer into the array referenced by -.Fa base -where -.Fa key -is located. -If -.Fa key -does not exist, -.Fn lfind -will return a null pointer and -.Fn lsearch -will add it to the array. -When an element is added to the array by -.Fn lsearch , -the location referenced by the argument -.Fa nelp -is incremented by one. -.Sh SEE ALSO -.Xr bsearch 3 , -.Xr db 3 -.Sh STANDARDS -The -.Fn lsearch -and -.Fn lfind -functions conform to the -.St -p1003.1-2001 -and -.St -xpg4.3 -specifications. diff --git a/src/lib/libc/stdlib/lsearch.c b/src/lib/libc/stdlib/lsearch.c deleted file mode 100644 index 0815430199..0000000000 --- a/src/lib/libc/stdlib/lsearch.c +++ /dev/null @@ -1,88 +0,0 @@ -/* $OpenBSD: lsearch.c,v 1.3 2004/10/01 04:08:45 jsg Exp $ */ - -/* - * Copyright (c) 1989, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Roger L. Snyder. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)lsearch.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ - -#include -#include -#include - -typedef int (*cmp_fn_t)(const void *, const void *); -static void *linear_base(const void *, const void *, size_t *, size_t, - cmp_fn_t, int); - -void * -lsearch(const void *key, const void *base, size_t *nelp, size_t width, - cmp_fn_t compar) -{ - - return(linear_base(key, base, nelp, width, compar, 1)); -} - -void * -lfind(const void *key, const void *base, size_t *nelp, size_t width, - cmp_fn_t compar) -{ - return(linear_base(key, base, nelp, width, compar, 0)); -} - -static void * -linear_base(const void *key, const void *base, size_t *nelp, size_t width, - cmp_fn_t compar, int add_flag) -{ - const char *element, *end; - - end = (const char *)base + *nelp * width; - for (element = base; element < end; element += width) - if (!compar(key, element)) /* key found */ - return((void *)element); - - if (!add_flag) /* key not found */ - return(NULL); - - /* - * The UNIX System User's Manual, 1986 edition claims that - * a NULL pointer is returned by lsearch with errno set - * appropriately, if there is not enough room in the table - * to add a new item. This can't be done as none of these - * routines have any method of determining the size of the - * table. This comment isn't in the 1986-87 System V - * manual. - */ - ++*nelp; - memcpy((void *)end, key, width); - return((void *)end); -} diff --git a/src/lib/libc/stdlib/malloc.3 b/src/lib/libc/stdlib/malloc.3 deleted file mode 100644 index 8eeea6e58c..0000000000 --- a/src/lib/libc/stdlib/malloc.3 +++ /dev/null @@ -1,456 +0,0 @@ -.\" -.\" Copyright (c) 1980, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: malloc.3,v 1.53 2008/07/28 19:53:03 otto Exp $ -.\" -.Dd $Mdocdate: July 28 2008 $ -.Dt MALLOC 3 -.Os -.Sh NAME -.Nm malloc , -.Nm calloc , -.Nm realloc , -.Nm free , -.Nm cfree -.Nd memory allocation and deallocation -.Sh SYNOPSIS -.Fd #include -.Ft void * -.Fn malloc "size_t size" -.Ft void * -.Fn calloc "size_t nmemb" "size_t size" -.Ft void * -.Fn realloc "void *ptr" "size_t size" -.Ft void -.Fn free "void *ptr" -.Ft void -.Fn cfree "void *ptr" -.Ft char * -.Va malloc_options ; -.Sh DESCRIPTION -The -.Fn malloc -function allocates uninitialized space for an object whose -size is specified by -.Fa size . -The -.Fn malloc -function maintains multiple lists of free blocks according to size, allocating -space from the appropriate list. -.Pp -The allocated space is -suitably aligned (after possible pointer -coercion) for storage of any type of object. -If the space is of -.Em pagesize -or larger, the memory returned will be page-aligned. -.Pp -Allocation of a zero size object returns a pointer to a zero size object. -This zero size object is access protected, so any access to it will -generate an exception (SIGSEGV). -Many zero-sized objects can be placed consecutively in shared -protected pages. -The minimum size of the protection on each object is suitably aligned and -sized as previously stated, but the protection may extend further depending -on where in a protected zone the object lands. -.Pp -When using -.Fn malloc -be careful to avoid the following idiom: -.Bd -literal -offset indent -if ((p = malloc(num * size)) == NULL) - err(1, "malloc"); -.Ed -.Pp -The multiplication may lead to an integer overflow. -To avoid this, -.Fn calloc -is recommended. -.Pp -If -.Fn malloc -must be used, be sure to test for overflow: -.Bd -literal -offset indent -if (size && num > SIZE_MAX / size) { - errno = ENOMEM; - err(1, "overflow"); -} -.Ed -.Pp -The -.Fn calloc -function allocates space for an array of -.Fa nmemb -objects, each of whose size is -.Fa size . -The space is initialized to zero. -The use of -.Fn calloc -is strongly encouraged when allocating multiple sized objects -in order to avoid possible integer overflows. -.Pp -The -.Fn free -function causes the space pointed to by -.Fa ptr -to be either placed on a list of free pages to make it available for future -allocation or, if required, to be returned to the kernel using -.Xr munmap 2 . -If -.Fa ptr -is a null pointer, no action occurs. -.Pp -A -.Fn cfree -function is also provided for compatibility with old systems and other -.Nm malloc -libraries; it is simply an alias for -.Fn free . -.Pp -The -.Fn realloc -function changes the size of the object pointed to by -.Fa ptr -to -.Fa size -bytes and returns a pointer to the (possibly moved) object. -The contents of the object are unchanged up to the lesser -of the new and old sizes. -If the new size is larger, the value of the newly allocated portion -of the object is indeterminate and uninitialized. -If -.Fa ptr -is a null pointer, the -.Fn realloc -function behaves like the -.Fn malloc -function for the specified size. -If the space cannot be allocated, the object -pointed to by -.Fa ptr -is unchanged. -If -.Fa size -is zero and -.Fa ptr -is not a null pointer, the object it points to is freed and a new zero size -object is returned. -.Pp -When using -.Fn realloc -be careful to avoid the following idiom: -.Bd -literal -offset indent -size += 50; -if ((p = realloc(p, size)) == NULL) - return (NULL); -.Ed -.Pp -Do not adjust the variable describing how much memory has been allocated -until the allocation has been successful. -This can cause aberrant program behavior if the incorrect size value is used. -In most cases, the above sample will also result in a leak of memory. -As stated earlier, a return value of -.Dv NULL -indicates that the old object still remains allocated. -Better code looks like this: -.Bd -literal -offset indent -newsize = size + 50; -if ((newp = realloc(p, newsize)) == NULL) { - free(p); - p = NULL; - size = 0; - return (NULL); -} -p = newp; -size = newsize; -.Ed -.Pp -As with -.Fn malloc -it is important to ensure the new size value will not overflow; -i.e. avoid allocations like the following: -.Bd -literal -offset indent -if ((newp = realloc(p, num * size)) == NULL) { - ... -.Ed -.Pp -Malloc will first look for a symbolic link called -.Pa /etc/malloc.conf -and next check the environment for a variable called -.Ev MALLOC_OPTIONS -and finally for the global variable -.Va malloc_options -and scan them for flags in that order. -Flags are single letters, uppercase means on, lowercase means off. -.Bl -tag -width indent -.It Cm A -.Dq Abort . -.Fn malloc -will coredump the process, rather than tolerate failure. -This is a very handy debugging aid, since the core file will represent the -time of failure, rather than when the null pointer was accessed. -.It Cm D -.Dq Dump . -.Fn malloc -will dump statistics in a file called -.Pa malloc.out -at exit. -This option requires the library to have been compiled with -DMALLOC_STATS in -order to have any effect. -.It Cm F -.Dq Freeguard . -Enable use after free protection. -Unused pages on the freelist are read and write protected to -cause a segmentation fault upon access. -.It Cm G -.Dq Guard . -Enable guard pages and chunk randomization. -Each page size or larger allocation is followed by a guard page that will -cause a segmentation fault upon any access. -Smaller than page size chunks are returned in a random order. -.It Cm H -.Dq Hint . -Pass a hint to the kernel about pages we don't use. -If the machine is paging a lot this may help a bit. -.It Cm J -.Dq Junk . -Fill some junk into the area allocated. -Currently junk is bytes of 0xd0 when allocating; this is pronounced -.Dq Duh . -\&:-) -Freed chunks are filled with 0xdf. -.It Cm N -Do not output warning messages when encountering possible corruption -or bad pointers. -.It Cm P -.Dq Move allocations within a page. -Allocations larger than half a page but smaller that a page -are aligned to the end of a page to catch buffer overruns in more -cases. -.It Cm R -.Dq realloc . -Always reallocate when -.Fn realloc -is called, even if the initial allocation was big enough. -This can substantially aid in compacting memory. -.\".Pp -.\".It Cm U -.\".Dq utrace . -.\"Generate entries for -.\".Xr ktrace 1 -.\"for all operations. -.\"Consult the source for this one. -.It Cm X -.Dq xmalloc . -Rather than return failure, -.Xr abort 3 -the program with a diagnostic message on stderr. -It is the intention that this option be set at compile time by -including in the source: -.Bd -literal -offset indent -extern char *malloc_options; -malloc_options = "X"; -.Ed -.Pp -Note that this will cause code that is supposed to handle -out-of-memory conditions gracefully to abort instead. -.It Cm Z -.Dq Zero . -Fill some junk into the area allocated (see -.Cm J ) , -except for the exact length the user asked for, which is zeroed. -.It Cm < -.Dq Half the cache size . -Decrease the size of the free page cache by a factor of two. -.It Cm > -.Dq Double the cache size . -Increase the size of the free page cache by a factor of two. -.El -.Pp -So to set a systemwide reduction of cache size and coredumps on problems: -.Li ln -s 'A<' /etc/malloc.conf -.Pp -The -.Cm J -and -.Cm Z -flags are mostly for testing and debugging. -If a program changes behavior if either of these options are used, -it is buggy. -.Pp -The default number of free pages cached is 64. -.Sh RETURN VALUES -The -.Fn malloc -and -.Fn calloc -functions return a pointer to the allocated space if successful; otherwise, -a null pointer is returned and -.Va errno -is set to -.Er ENOMEM . -.Pp -The -.Fn free -and -.Fn cfree -functions return no value. -.Pp -The -.Fn realloc -function returns a pointer to the (possibly moved) allocated space -if successful; otherwise, a null pointer is returned and -.Va errno -is set to -.Er ENOMEM . -.Sh ENVIRONMENT -.Bl -tag -width Ev -.It Ev MALLOC_OPTIONS -See above. -.El -.Sh FILES -.Bl -tag -width "/etc/malloc.conf" -.It Pa /etc/malloc.conf -symbolic link to filename containing option flags -.El -.Sh DIAGNOSTICS -If -.Fn malloc , -.Fn calloc , -.Fn realloc , -or -.Fn free -detect an error or warning condition, -a message will be printed to file descriptor -2 (not using stdio). -Errors will always result in the process being -.Xr abort 3 'ed. -If the -.Cm A -option has been specified, warnings will also -.Xr abort 3 -the process. -.Pp -Here is a brief description of the error messages and what they mean: -.Bl -tag -width Ds -.It Dq out of memory -If the -.Cm A -option is specified it is an error for -.Fn malloc , -.Fn calloc , -or -.Fn realloc -to return -.Dv NULL . -.It Dq malloc init mmap failed -This is a rather weird condition that is most likely to indicate a -seriously overloaded system or a ulimit restriction. -.It any other error -.Fn malloc -detected an internal error; -consult sources and/or wizards. -.El -.Pp -Here is a brief description of the warning messages and what they mean: -.Bl -tag -width Ds -.It Dq bogus pointer (double free?) -An attempt to -.Fn free -or -.Fn realloc -an unallocated pointer was made. -.It Dq chunk is already free -There was an attempt to free a chunk that had already been freed. -.It Dq modified (chunk-) pointer -The pointer passed to -.Fn free -or -.Fn realloc -has been modified. -.It Dq recursive call -An attempt was made to call recursively into these functions, i.e., from a -signal handler. -This behavior is not supported. -In particular, signal handlers should -.Em not -use any of the -.Fn malloc -functions nor utilize any other functions which may call -.Fn malloc -(e.g., -.Xr stdio 3 -routines). -.It Dq unknown char in MALLOC_OPTIONS -We found something we didn't understand. -.It Dq malloc cache overflow/underflow -The internal malloc page cache has been corrupted. -.It Dq malloc free slot lost -The internal malloc page cache has been corrupted. -.It Dq guard size -An inconsistent guard size was detected. -.El -.Sh SEE ALSO -.Xr brk 2 , -.Xr mmap 2 , -.Xr munmap 2 , -.Xr alloca 3 , -.Xr getpagesize 3 -.Sh STANDARDS -The -.Fn malloc -function conforms to -.St -ansiC . -.Sh HISTORY -The present implementation of -.Fn malloc -started out as a filesystem on a drum -attached to a 20-bit binary challenged computer built with discrete germanium -transistors, and it has since graduated to handle primary storage rather than -secondary. -.Pp -The main difference from other -.Fn malloc -implementations are believed to be that -the free pages are not accessed until allocated. -Most -.Fn malloc -implementations will store a data structure containing a, -possibly double-, linked list in the free chunks of memory, used to tie -all the free memory together. -That is a quite suboptimal thing to do. -Every time the free-list is traversed, all the otherwise unused, and very -likely paged out, pages get faulted into primary memory, just to see what -lies after them in the list. -.Pp -On systems which are paging, this can increase the page-faults -of a process by a factor of five. diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c deleted file mode 100644 index ae8cd8157b..0000000000 --- a/src/lib/libc/stdlib/malloc.c +++ /dev/null @@ -1,1325 +0,0 @@ -/* $OpenBSD: malloc.c,v 1.92 2008/07/28 04:56:38 otto Exp $ */ -/* - * Copyright (c) 2008 Otto Moerbeek - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Parts of this code, mainly the sub page sized chunk management code is - * derived from the malloc implementation with the following license: - */ -/* - * ---------------------------------------------------------------------------- - * "THE BEER-WARE LICENSE" (Revision 42): - * wrote this file. As long as you retain this notice you - * can do whatever you want with this stuff. If we meet some day, and you think - * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp - * ---------------------------------------------------------------------------- - */ - -/* #define MALLOC_STATS */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef MALLOC_STATS -#include -#endif - -#include "thread_private.h" - -#define MALLOC_MINSHIFT 4 -#define MALLOC_MAXSHIFT 16 - -#if defined(__sparc__) && !defined(__sparcv9__) -#define MALLOC_PAGESHIFT (13U) -#else -#define MALLOC_PAGESHIFT (PGSHIFT) -#endif - -#define MALLOC_PAGESIZE (1UL << MALLOC_PAGESHIFT) -#define MALLOC_MINSIZE (1UL << MALLOC_MINSHIFT) -#define MALLOC_PAGEMASK (MALLOC_PAGESIZE - 1) -#define MASK_POINTER(p) ((void *)(((uintptr_t)(p)) & ~MALLOC_PAGEMASK)) - -#define MALLOC_MAXCHUNK (1 << (MALLOC_PAGESHIFT-1)) -#define MALLOC_MAXCACHE 256 -#define MALLOC_DELAYED_CHUNKS 16 /* should be power of 2 */ - -#define PAGEROUND(x) (((x) + (MALLOC_PAGEMASK)) & ~MALLOC_PAGEMASK) - -/* - * What to use for Junk. This is the byte value we use to fill with - * when the 'J' option is enabled. Use SOME_JUNK right after alloc, - * and SOME_FREEJUNK right before free. - */ -#define SOME_JUNK 0xd0 /* as in "Duh" :-) */ -#define SOME_FREEJUNK 0xdf - -#define MMAP(sz) mmap(NULL, (size_t)(sz), PROT_READ | PROT_WRITE, \ - MAP_ANON | MAP_PRIVATE, -1, (off_t) 0) - -struct region_info { - void *p; /* page; low bits used to mark chunks */ - uintptr_t size; /* size for pages, or chunk_info pointer */ -}; - -struct dir_info { - u_int32_t canary1; - struct region_info *r; /* region slots */ - size_t regions_total; /* number of region slots */ - size_t regions_bits; /* log2 of total */ - size_t regions_free; /* number of free slots */ - /* list of free chunk info structs */ - struct chunk_info *chunk_info_list; - /* lists of chunks with free slots */ - struct chunk_info *chunk_dir[MALLOC_MAXSHIFT]; - size_t free_regions_size; /* free pages cached */ - /* free pages cache */ - struct region_info free_regions[MALLOC_MAXCACHE]; - /* delayed free chunk slots */ - void *delayed_chunks[MALLOC_DELAYED_CHUNKS]; -#ifdef MALLOC_STATS - size_t inserts; - size_t insert_collisions; - size_t finds; - size_t find_collisions; - size_t deletes; - size_t delete_moves; -#define STATS_INC(x) ((x)++) -#define STATS_ZERO(x) ((x) = 0) -#else -#define STATS_INC(x) /* nothing */ -#define STATS_ZERO(x) /* nothing */ -#endif /* MALLOC_STATS */ - u_int32_t canary2; -}; - - -/* - * This structure describes a page worth of chunks. - * - * How many bits per u_long in the bitmap - */ -#define MALLOC_BITS (NBBY * sizeof(u_long)) -struct chunk_info { - struct chunk_info *next; /* next on the free list */ - void *page; /* pointer to the page */ - u_int32_t canary; - u_short size; /* size of this page's chunks */ - u_short shift; /* how far to shift for this size */ - u_short free; /* how many free chunks */ - u_short total; /* how many chunk */ - /* which chunks are free */ - u_long bits[(MALLOC_PAGESIZE / MALLOC_MINSIZE) / MALLOC_BITS]; -}; - -static struct dir_info g_pool; -static char *malloc_func; /* current function */ -char *malloc_options; /* compile-time options */ - -static int malloc_abort = 1; /* abort() on error */ -static int malloc_active; /* status of malloc */ -static int malloc_freeprot; /* mprotect free pages PROT_NONE? */ -static int malloc_hint; /* call madvice on free pages? */ -static int malloc_junk; /* junk fill? */ -static int malloc_move; /* move allocations to end of page? */ -static int malloc_realloc; /* always realloc? */ -static int malloc_silent; /* avoid outputting warnings? */ -static int malloc_xmalloc; /* xmalloc behaviour? */ -static int malloc_zero; /* zero fill? */ -static size_t malloc_guard; /* use guard pages after allocations? */ - -static u_int malloc_cache = 64; /* free pages we cache */ -static size_t malloc_guarded; /* bytes used for guards */ -static size_t malloc_used; /* bytes allocated */ - -#ifdef MALLOC_STATS -static int malloc_stats; /* dump statistics at end */ -#endif - -static size_t rbytesused; /* random bytes used */ -static u_char rbytes[4096]; /* random bytes */ -static u_char getrbyte(void); - -extern char *__progname; - -/* low bits of r->p determine size: 0 means >= page size and p->size holding - * real size, otherwise r->size is a shift count, or 1 for malloc(0) - */ -#define REALSIZE(sz, r) \ - (sz) = (uintptr_t)(r)->p & MALLOC_PAGEMASK, \ - (sz) = ((sz) == 0 ? (r)->size : ((sz) == 1 ? 0 : (1 << ((sz)-1)))) - -static inline size_t -hash(void *p) -{ - size_t sum; - union { - uintptr_t p; - unsigned short a[sizeof(void *) / sizeof(short)]; - } u; - u.p = (uintptr_t)p >> MALLOC_PAGESHIFT; - sum = u.a[0]; - sum = (sum << 7) - sum + u.a[1]; -#ifdef __LP64__ - sum = (sum << 7) - sum + u.a[2]; - sum = (sum << 7) - sum + u.a[3]; -#endif - return sum; -} - -#ifdef MALLOC_STATS -static void -dump_chunk(int fd, struct chunk_info *p, int fromfreelist) -{ - char buf[64]; - - while (p) { - snprintf(buf, sizeof(buf), "chunk %d %d/%d %p\n", p->size, - p->free, p->total, p->page); - write(fd, buf, strlen(buf)); - if (!fromfreelist) - break; - p = p->next; - if (p != NULL) { - snprintf(buf, sizeof(buf), " "); - write(fd, buf, strlen(buf)); - } - } -} - -static void -dump_free_chunk_info(int fd, struct dir_info *d) -{ - char buf[64]; - int i; - - snprintf(buf, sizeof(buf), "Free chunk structs:\n"); - write(fd, buf, strlen(buf)); - for (i = 0; i < MALLOC_MAXSHIFT; i++) { - struct chunk_info *p = d->chunk_dir[i]; - if (p != NULL) { - snprintf(buf, sizeof(buf), "%2d) ", i); - write(fd, buf, strlen(buf)); - dump_chunk(fd, p, 1); - } - } - -} - -static void -dump_free_page_info(int fd, struct dir_info *d) -{ - char buf[64]; - int i; - - snprintf(buf, sizeof(buf), "Free pages cached: %zu\n", - d->free_regions_size); - write(fd, buf, strlen(buf)); - for (i = 0; i < malloc_cache; i++) { - if (d->free_regions[i].p != NULL) { - snprintf(buf, sizeof(buf), "%2d) ", i); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "free at %p: %zu\n", - d->free_regions[i].p, d->free_regions[i].size); - write(fd, buf, strlen(buf)); - } - } -} - -static void -malloc_dump1(int fd, struct dir_info *d) -{ - char buf[64]; - size_t i, realsize; - - snprintf(buf, sizeof(buf), "Malloc dir of %s at %p\n", __progname, d); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Regions slots %zu\n", d->regions_total); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Finds %zu/%zu %f\n", d->finds, - d->find_collisions, - 1.0 + (double)d->find_collisions / d->finds); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Inserts %zu/%zu %f\n", d->inserts, - d->insert_collisions, - 1.0 + (double)d->insert_collisions / d->inserts); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Deletes %zu/%zu\n", d->deletes, - d->delete_moves); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Regions slots free %zu\n", d->regions_free); - write(fd, buf, strlen(buf)); - for (i = 0; i < d->regions_total; i++) { - if (d->r[i].p != NULL) { - size_t h = hash(d->r[i].p) & - (d->regions_total - 1); - snprintf(buf, sizeof(buf), "%4zx) #%zx %zd ", - i, h, h - i); - write(fd, buf, strlen(buf)); - REALSIZE(realsize, &d->r[i]); - if (realsize > MALLOC_MAXCHUNK) { - snprintf(buf, sizeof(buf), - "%p: %zu\n", d->r[i].p, realsize); - write(fd, buf, strlen(buf)); - } else - dump_chunk(fd, - (struct chunk_info *)d->r[i].size, 0); - } - } - dump_free_chunk_info(fd, d); - dump_free_page_info(fd, d); - snprintf(buf, sizeof(buf), "In use %zu\n", malloc_used); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Guarded %zu\n", malloc_guarded); - write(fd, buf, strlen(buf)); -} - - -void -malloc_dump(int fd) -{ - malloc_dump1(fd, &g_pool); -} - -static void -malloc_exit(void) -{ - char *q = "malloc() warning: Couldn't dump stats\n"; - int save_errno = errno, fd; - - fd = open("malloc.out", O_RDWR|O_APPEND); - if (fd != -1) { - malloc_dump(fd); - close(fd); - } else - write(STDERR_FILENO, q, strlen(q)); - errno = save_errno; -} -#endif /* MALLOC_STATS */ - - - -static void -wrterror(char *p) -{ - char *q = " error: "; - struct iovec iov[5]; - - iov[0].iov_base = __progname; - iov[0].iov_len = strlen(__progname); - iov[1].iov_base = malloc_func; - iov[1].iov_len = strlen(malloc_func); - iov[2].iov_base = q; - iov[2].iov_len = strlen(q); - iov[3].iov_base = p; - iov[3].iov_len = strlen(p); - iov[4].iov_base = "\n"; - iov[4].iov_len = 1; - writev(STDERR_FILENO, iov, 5); - -#ifdef MALLOC_STATS - if (malloc_stats) - malloc_dump(STDERR_FILENO); -#endif /* MALLOC_STATS */ - //malloc_active--; - if (malloc_abort) - abort(); -} - -static void -wrtwarning(char *p) -{ - char *q = " warning: "; - struct iovec iov[5]; - - if (malloc_abort) - wrterror(p); - else if (malloc_silent) - return; - - iov[0].iov_base = __progname; - iov[0].iov_len = strlen(__progname); - iov[1].iov_base = malloc_func; - iov[1].iov_len = strlen(malloc_func); - iov[2].iov_base = q; - iov[2].iov_len = strlen(q); - iov[3].iov_base = p; - iov[3].iov_len = strlen(p); - iov[4].iov_base = "\n"; - iov[4].iov_len = 1; - - writev(STDERR_FILENO, iov, 5); -} - -/* - * Cache maintenance. We keep at most malloc_cache pages cached. - * If the cache is becoming full, unmap pages in the cache for real, - * and then add the region to the cache - * Opposed to the regular region data structure, the sizes in the - * cache are in MALLOC_PAGESIZE units. - */ -static void -unmap(struct dir_info *d, void *p, size_t sz) -{ - size_t psz = PAGEROUND(sz) >> MALLOC_PAGESHIFT; - size_t rsz, tounmap; - struct region_info *r; - u_int i, offset; - - if (psz > malloc_cache) { - if (munmap(p, sz)) - wrterror("unmap"); - malloc_used -= sz; - return; - } - tounmap = 0; - rsz = malloc_cache - d->free_regions_size; - if (psz > rsz) - tounmap = psz - rsz; - d->free_regions_size -= tounmap; - offset = getrbyte(); - for (i = 0; tounmap > 0 && i < malloc_cache; i++) { - r = &d->free_regions[(i + offset) & (malloc_cache - 1)]; - if (r->p != NULL) { - if (r->size <= tounmap) { - rsz = r->size << MALLOC_PAGESHIFT; - if (munmap(r->p, rsz)) - wrterror("munmap"); - tounmap -= r->size; - r->p = NULL; - r->size = 0; - malloc_used -= rsz; - } else { - rsz = tounmap << MALLOC_PAGESHIFT; - if (munmap((char *)r->p + ((r->size - tounmap) - << MALLOC_PAGESHIFT), rsz)) - wrterror("munmap"); - r->size -= tounmap ; - tounmap = 0; - malloc_used -= rsz; - } - } - } - if (tounmap > 0) - wrtwarning("malloc cache underflow"); - for (i = 0; i < malloc_cache; i++) { - r = &d->free_regions[i]; - if (r->p == NULL) { - if (malloc_hint) - madvise(p, sz, MADV_FREE); - if (malloc_freeprot) - mprotect(p, sz, PROT_NONE); - r->p = p; - r->size = psz; - d->free_regions_size += psz; - break; - } - } - if (i == malloc_cache) - wrtwarning("malloc free slot lost"); - if (d->free_regions_size > malloc_cache) - wrtwarning("malloc cache overflow"); -} - -static void * -map(struct dir_info *d, size_t sz) -{ - size_t psz = PAGEROUND(sz) >> MALLOC_PAGESHIFT; - struct region_info *r, *big = NULL; - u_int i, offset; - void *p; - - if (psz > d->free_regions_size) { - p = MMAP(sz); - if (p != MAP_FAILED) - malloc_used += sz; - return p; - } - offset = getrbyte(); - for (i = 0; i < malloc_cache; i++) { - r = &d->free_regions[(i + offset) & (malloc_cache - 1)]; - if (r->p != NULL) { - if (r->size == psz) { - p = r->p; - if (malloc_freeprot) - mprotect(p, sz, PROT_READ | PROT_WRITE); - if (malloc_hint) - madvise(p, sz, MADV_NORMAL); - r->p = NULL; - r->size = 0; - d->free_regions_size -= psz; - return p; - } else if (r->size > psz) - big = r; - } - } - if (big != NULL) { - r = big; - p = (char *)r->p + ((r->size - psz) << MALLOC_PAGESHIFT); - if (malloc_freeprot) - mprotect(p, sz, PROT_READ | PROT_WRITE); - if (malloc_hint) - madvise(p, sz, MADV_NORMAL); - r->size -= psz; - d->free_regions_size -= psz; - return p; - } - p = MMAP(sz); - if (p != MAP_FAILED) - malloc_used += sz; - if (d->free_regions_size > malloc_cache) - wrtwarning("malloc cache"); - return p; -} - -static void -rbytes_init(void) -{ - arc4random_buf(rbytes, sizeof(rbytes)); - rbytesused = 0; -} - -static u_char -getrbyte(void) -{ - if (rbytesused >= sizeof(rbytes)) - rbytes_init(); - return rbytes[rbytesused++]; -} - -/* - * Initialize a dir_info, which should have been cleared by caller - */ -static int -omalloc_init(struct dir_info *d) -{ - char *p, b[64]; - int i, j, save_errno = errno; - size_t regioninfo_size; - - rbytes_init(); - - for (i = 0; i < 3; i++) { - switch (i) { - case 0: - j = readlink("/etc/malloc.conf", b, sizeof b - 1); - if (j <= 0) - continue; - b[j] = '\0'; - p = b; - break; - case 1: - if (issetugid() == 0) - p = getenv("MALLOC_OPTIONS"); - else - continue; - break; - case 2: - p = malloc_options; - break; - default: - p = NULL; - } - - for (; p != NULL && *p != '\0'; p++) { - switch (*p) { - case '>': - malloc_cache <<= 1; - if (malloc_cache > MALLOC_MAXCACHE) - malloc_cache = MALLOC_MAXCACHE; - break; - case '<': - malloc_cache >>= 1; - break; - case 'a': - malloc_abort = 0; - break; - case 'A': - malloc_abort = 1; - break; -#ifdef MALLOC_STATS - case 'd': - malloc_stats = 0; - break; - case 'D': - malloc_stats = 1; - break; -#endif /* MALLOC_STATS */ - case 'f': - malloc_freeprot = 0; - break; - case 'F': - malloc_freeprot = 1; - break; - case 'g': - malloc_guard = 0; - break; - case 'G': - malloc_guard = MALLOC_PAGESIZE; - break; - case 'h': - malloc_hint = 0; - break; - case 'H': - malloc_hint = 1; - break; - case 'j': - malloc_junk = 0; - break; - case 'J': - malloc_junk = 1; - break; - case 'n': - malloc_silent = 0; - break; - case 'N': - malloc_silent = 1; - break; - case 'p': - malloc_move = 0; - break; - case 'P': - malloc_move = 1; - break; - case 'r': - malloc_realloc = 0; - break; - case 'R': - malloc_realloc = 1; - break; - case 'x': - malloc_xmalloc = 0; - break; - case 'X': - malloc_xmalloc = 1; - break; - case 'z': - malloc_zero = 0; - break; - case 'Z': - malloc_zero = 1; - break; - default: - j = malloc_abort; - malloc_abort = 0; - wrtwarning("unknown char in MALLOC_OPTIONS"); - malloc_abort = j; - break; - } - } - } - - /* - * We want junk in the entire allocation, and zero only in the part - * the user asked for. - */ - if (malloc_zero) - malloc_junk = 1; - -#ifdef MALLOC_STATS - if (malloc_stats && (atexit(malloc_exit) == -1)) - wrtwarning("atexit(2) failed." - " Will not be able to dump malloc stats on exit"); -#endif /* MALLOC_STATS */ - - errno = save_errno; - - d->regions_bits = 9; - d->regions_free = d->regions_total = 1 << d->regions_bits; - regioninfo_size = d->regions_total * sizeof(struct region_info); - d->r = MMAP(regioninfo_size); - if (d->r == MAP_FAILED) { - wrterror("malloc init mmap failed"); - d->regions_total = 0; - return 1; - } - malloc_used += regioninfo_size; - memset(d->r, 0, regioninfo_size); - d->canary1 = arc4random(); - d->canary2 = ~d->canary1; - return 0; -} - -static int -omalloc_grow(struct dir_info *d) -{ - size_t newbits; - size_t newtotal; - size_t newsize; - size_t mask; - size_t i; - struct region_info *p; - - if (d->regions_total > SIZE_MAX / sizeof(struct region_info) / 2 ) - return 1; - - newbits = d->regions_bits + 1; - newtotal = d->regions_total * 2; - newsize = newtotal * sizeof(struct region_info); - mask = newtotal - 1; - - p = MMAP(newsize); - if (p == MAP_FAILED) - return 1; - - malloc_used += newsize; - memset(p, 0, newsize); - STATS_ZERO(d->inserts); - STATS_ZERO(d->insert_collisions); - for (i = 0; i < d->regions_total; i++) { - void *q = d->r[i].p; - if (q != NULL) { - size_t index = hash(q) & mask; - STATS_INC(d->inserts); - while (p[index].p != NULL) { - index = (index - 1) & mask; - STATS_INC(d->insert_collisions); - } - p[index] = d->r[i]; - } - } - /* avoid pages containing meta info to end up in cache */ - if (munmap(d->r, d->regions_total * sizeof(struct region_info))) - wrterror("omalloc_grow munmap"); - else - malloc_used -= d->regions_total * sizeof(struct region_info); - d->regions_free = d->regions_free + d->regions_total; - d->regions_total = newtotal; - d->regions_bits = newbits; - d->r = p; - return 0; -} - -static struct chunk_info * -alloc_chunk_info(struct dir_info *d) -{ - struct chunk_info *p; - int i; - - if (d->chunk_info_list == NULL) { - p = MMAP(MALLOC_PAGESIZE); - if (p == MAP_FAILED) - return NULL; - malloc_used += MALLOC_PAGESIZE; - for (i = 0; i < MALLOC_PAGESIZE / sizeof(*p); i++) { - p[i].next = d->chunk_info_list; - d->chunk_info_list = &p[i]; - } - } - p = d->chunk_info_list; - d->chunk_info_list = p->next; - memset(p, 0, sizeof *p); - p->canary = d->canary1; - return p; -} - - -static void -put_chunk_info(struct dir_info *d, struct chunk_info *p) -{ - p->next = d->chunk_info_list; - d->chunk_info_list = p; -} - -static int -insert(struct dir_info *d, void *p, size_t sz) -{ - size_t index; - size_t mask; - void *q; - - if (d->regions_free * 4 < d->regions_total) { - if (omalloc_grow(d)) - return 1; - } - mask = d->regions_total - 1; - index = hash(p) & mask; - q = d->r[index].p; - STATS_INC(d->inserts); - while (q != NULL) { - index = (index - 1) & mask; - q = d->r[index].p; - STATS_INC(d->insert_collisions); - } - d->r[index].p = p; - d->r[index].size = sz; - d->regions_free--; - return 0; -} - -static struct region_info * -find(struct dir_info *d, void *p) -{ - size_t index; - size_t mask = d->regions_total - 1; - void *q, *r; - - if (d->canary1 != ~d->canary2) - wrterror("internal struct corrupt"); - p = MASK_POINTER(p); - index = hash(p) & mask; - r = d->r[index].p; - q = MASK_POINTER(r); - STATS_INC(d->finds); - while (q != p && r != NULL) { - index = (index - 1) & mask; - r = d->r[index].p; - q = MASK_POINTER(r); - STATS_INC(d->find_collisions); - } - return q == p ? &d->r[index] : NULL; -} - -static void -delete(struct dir_info *d, struct region_info *ri) -{ - /* algorithm R, Knuth Vol III section 6.4 */ - size_t mask = d->regions_total - 1; - size_t i, j, r; - - if (d->regions_total & (d->regions_total - 1)) - wrterror("regions_total not 2^x"); - d->regions_free++; - STATS_INC(g_pool.deletes); - - i = ri - d->r; - for (;;) { - d->r[i].p = NULL; - d->r[i].size = 0; - j = i; - for (;;) { - i = (i - 1) & mask; - if (d->r[i].p == NULL) - return; - r = hash(d->r[i].p) & mask; - if ((i <= r && r < j) || (r < j && j < i) || - (j < i && i <= r)) - continue; - d->r[j] = d->r[i]; - STATS_INC(g_pool.delete_moves); - break; - } - - } -} - -/* - * Allocate a page of chunks - */ -static struct chunk_info * -omalloc_make_chunks(struct dir_info *d, int bits) -{ - struct chunk_info *bp; - void *pp; - long i, k; - - /* Allocate a new bucket */ - pp = map(d, MALLOC_PAGESIZE); - if (pp == MAP_FAILED) - return NULL; - - bp = alloc_chunk_info(d); - if (bp == NULL) { - unmap(d, pp, MALLOC_PAGESIZE); - return NULL; - } - - /* memory protect the page allocated in the malloc(0) case */ - if (bits == 0) { - bp->size = 0; - bp->shift = 1; - i = MALLOC_MINSIZE - 1; - while (i >>= 1) - bp->shift++; - bp->total = bp->free = MALLOC_PAGESIZE >> bp->shift; - bp->page = pp; - - k = mprotect(pp, MALLOC_PAGESIZE, PROT_NONE); - if (k < 0) { - unmap(d, pp, MALLOC_PAGESIZE); - put_chunk_info(d, bp); - return NULL; - } - } else { - bp->size = (1UL << bits); - bp->shift = bits; - bp->total = bp->free = MALLOC_PAGESIZE >> bits; - bp->page = pp; - } - - /* set all valid bits in the bitmap */ - k = bp->total; - i = 0; - - /* Do a bunch at a time */ - for (; (k - i) >= MALLOC_BITS; i += MALLOC_BITS) - bp->bits[i / MALLOC_BITS] = ~0UL; - - for (; i < k; i++) - bp->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); - - bp->next = d->chunk_dir[bits]; - d->chunk_dir[bits] = bp; - - bits++; - if ((uintptr_t)pp & bits) - wrterror("pp & bits"); - - insert(d, (void *)((uintptr_t)pp | bits), (uintptr_t)bp); - return bp; -} - - -/* - * Allocate a chunk - */ -static void * -malloc_bytes(struct dir_info *d, size_t size) -{ - int i, j; - size_t k; - u_long u, *lp; - struct chunk_info *bp; - - /* Don't bother with anything less than this */ - /* unless we have a malloc(0) requests */ - if (size != 0 && size < MALLOC_MINSIZE) - size = MALLOC_MINSIZE; - - /* Find the right bucket */ - if (size == 0) - j = 0; - else { - j = MALLOC_MINSHIFT; - i = (size - 1) >> (MALLOC_MINSHIFT - 1); - while (i >>= 1) - j++; - } - - /* If it's empty, make a page more of that size chunks */ - bp = d->chunk_dir[j]; - if (bp == NULL && (bp = omalloc_make_chunks(d, j)) == NULL) - return NULL; - - if (bp->canary != d->canary1) - wrterror("chunk info corrupted"); - /* Find first word of bitmap which isn't empty */ - for (lp = bp->bits; !*lp; lp++) - /* EMPTY */; - - /* Find that bit, and tweak it */ - u = 1; - k = 0; - while (!(*lp & u)) { - u += u; - k++; - } - - /* advance a random # of positions */ - i = (getrbyte() & (MALLOC_DELAYED_CHUNKS - 1)) % bp->free; - while (i > 0) { - u += u; - k++; - if (k >= MALLOC_BITS) { - lp++; - u = 1; - k = 0; - } - if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) { - wrterror("chunk overflow"); - errno = EFAULT; - return (NULL); - } - if (*lp & u) - i--; - } - - *lp ^= u; - - /* If there are no more free, remove from free-list */ - if (!--bp->free) { - d->chunk_dir[j] = bp->next; - bp->next = NULL; - } - /* Adjust to the real offset of that chunk */ - k += (lp - bp->bits) * MALLOC_BITS; - k <<= bp->shift; - - if (malloc_junk && bp->size > 0) - memset((char *)bp->page + k, SOME_JUNK, bp->size); - return ((char *)bp->page + k); -} - - -/* - * Free a chunk, and possibly the page it's on, if the page becomes empty. - */ -static void -free_bytes(struct dir_info *d, struct region_info *r, void *ptr) -{ - struct chunk_info *info, **mp; - long i; - - info = (struct chunk_info *)r->size; - if (info->canary != d->canary1) - wrterror("chunk info corrupted"); - - /* Find the chunk number on the page */ - i = ((uintptr_t)ptr & MALLOC_PAGEMASK) >> info->shift; - - if ((uintptr_t)ptr & ((1UL << (info->shift)) - 1)) { - wrtwarning("modified (chunk-) pointer"); - return; - } - if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) { - wrtwarning("chunk is already free"); - return; - } - - info->bits[i / MALLOC_BITS] |= 1UL << (i % MALLOC_BITS); - info->free++; - - if (info->size != 0) - mp = d->chunk_dir + info->shift; - else - mp = d->chunk_dir; - - if (info->free == 1) { - /* Page became non-full */ - - /* Insert in address order */ - while (*mp != NULL && (*mp)->next != NULL && - (*mp)->next->page < info->page) - mp = &(*mp)->next; - info->next = *mp; - *mp = info; - return; - } - if (info->free != info->total) - return; - - /* Find & remove this page in the queue */ - while (*mp != info) { - mp = &((*mp)->next); - if (!*mp) { - wrterror("not on queue"); - errno = EFAULT; - return; - } - } - *mp = info->next; - - if (info->size == 0 && !malloc_freeprot) - mprotect(info->page, MALLOC_PAGESIZE, PROT_READ | PROT_WRITE); - unmap(d, info->page, MALLOC_PAGESIZE); - - delete(d, r); - put_chunk_info(d, info); -} - - - -static void * -omalloc(size_t sz, int zero_fill) -{ - void *p; - size_t psz; - - if (sz > MALLOC_MAXCHUNK) { - if (sz >= SIZE_MAX - malloc_guard - MALLOC_PAGESIZE) { - errno = ENOMEM; - return NULL; - } - sz += malloc_guard; - psz = PAGEROUND(sz); - p = map(&g_pool, psz); - if (p == MAP_FAILED) { - errno = ENOMEM; - return NULL; - } - if (insert(&g_pool, p, sz)) { - unmap(&g_pool, p, sz); - errno = ENOMEM; - return NULL; - } - if (malloc_guard) { - if (mprotect((char *)p + psz - malloc_guard, - malloc_guard, PROT_NONE)) - wrterror("mprotect"); - malloc_guarded += malloc_guard; - } - if (malloc_junk) - memset(p, SOME_JUNK, psz - malloc_guard); - - /* shift towards the end */ - if (malloc_move && - sz - malloc_guard < MALLOC_PAGESIZE - MALLOC_MINSIZE) - p = ((char *)p) + ((MALLOC_PAGESIZE - MALLOC_MINSIZE - - (sz - malloc_guard)) & ~(MALLOC_MINSIZE-1)); - if (zero_fill) - memset(p, 0, sz - malloc_guard); - } else { - /* takes care of SOME_JUNK */ - p = malloc_bytes(&g_pool, sz); - if (zero_fill && p != NULL && sz > 0) - memset(p, 0, sz); - } - - return p; -} - -/* - * Common function for handling recursion. Only - * print the error message once, to avoid making the problem - * potentially worse. - */ -static void -malloc_recurse(void) -{ - static int noprint; - - if (noprint == 0) { - noprint = 1; - wrtwarning("recursive call"); - } - malloc_active--; - _MALLOC_UNLOCK(); - errno = EDEADLK; -} - -void * -malloc(size_t size) -{ - void *r; - - _MALLOC_LOCK(); - malloc_func = " in malloc():"; - if (!g_pool.regions_total) { - if (omalloc_init(&g_pool)) { - _MALLOC_UNLOCK(); - if (malloc_xmalloc) - wrterror("out of memory"); - errno = ENOMEM; - return NULL; - } - } - if (malloc_active++) { - malloc_recurse(); - return NULL; - } - r = omalloc(size, malloc_zero); - malloc_active--; - _MALLOC_UNLOCK(); - if (r == NULL && malloc_xmalloc) { - wrterror("out of memory"); - errno = ENOMEM; - } - return r; -} - -static void -ofree(void *p) -{ - struct region_info *r; - size_t sz; - - r = find(&g_pool, p); - if (r == NULL) { - wrtwarning("bogus pointer (double free?)"); - return; - } - REALSIZE(sz, r); - if (sz > MALLOC_MAXCHUNK) { - if (sz - malloc_guard >= MALLOC_PAGESIZE - MALLOC_MINSIZE) { - if (r->p != p) - wrtwarning("bogus pointer"); - } else { -#if notyetbecause_of_realloc - /* shifted towards the end */ - if (p != ((char *)r->p) + ((MALLOC_PAGESIZE - - MALLOC_MINSIZE - sz - malloc_guard) & - ~(MALLOC_MINSIZE-1))) { - } -#endif - p = r->p; - } - if (malloc_guard) { - if (sz < malloc_guard) - wrtwarning("guard size"); - if (!malloc_freeprot) { - if (mprotect((char *)p + PAGEROUND(sz) - - malloc_guard, malloc_guard, - PROT_READ | PROT_WRITE)) - wrterror("mprotect"); - } - malloc_guarded -= malloc_guard; - } - if (malloc_junk) - memset(p, SOME_FREEJUNK, PAGEROUND(sz) - malloc_guard); - unmap(&g_pool, p, sz); - delete(&g_pool, r); - } else { - void *tmp; - int i; - - if (malloc_junk && sz > 0) - memset(p, SOME_FREEJUNK, sz); - i = getrbyte() & (MALLOC_DELAYED_CHUNKS - 1); - tmp = p; - p = g_pool.delayed_chunks[i]; - g_pool.delayed_chunks[i] = tmp; - if (p != NULL) { - r = find(&g_pool, p); - if (r == NULL) { - wrtwarning("bogus pointer (double free?)"); - return; - } - free_bytes(&g_pool, r, p); - } - } -} - -void -free(void *ptr) -{ - /* This is legal. */ - if (ptr == NULL) - return; - - _MALLOC_LOCK(); - malloc_func = " in free():"; - if (malloc_active++) { - malloc_recurse(); - return; - } - ofree(ptr); - malloc_active--; - _MALLOC_UNLOCK(); -} - - -static void * -orealloc(void *p, size_t newsz) -{ - struct region_info *r; - size_t oldsz, goldsz, gnewsz; - void *q; - - if (p == NULL) - return omalloc(newsz, 0); - - r = find(&g_pool, p); - if (r == NULL) { - wrtwarning("bogus pointer (double free?)"); - return NULL; - } - if (newsz >= SIZE_MAX - malloc_guard - MALLOC_PAGESIZE) { - errno = ENOMEM; - return NULL; - } - - REALSIZE(oldsz, r); - goldsz = oldsz; - if (oldsz > MALLOC_MAXCHUNK) { - if (oldsz < malloc_guard) - wrtwarning("guard size"); - oldsz -= malloc_guard; - } - - gnewsz = newsz; - if (gnewsz > MALLOC_MAXCHUNK) - gnewsz += malloc_guard; - - if (newsz > MALLOC_MAXCHUNK && oldsz > MALLOC_MAXCHUNK && p == r->p && - !malloc_realloc) { - size_t roldsz = PAGEROUND(goldsz); - size_t rnewsz = PAGEROUND(gnewsz); - - if (rnewsz < roldsz) { - if (malloc_guard) { - if (mprotect((char *)p + roldsz - malloc_guard, - malloc_guard, PROT_READ | PROT_WRITE)) - wrterror("mprotect"); - if (mprotect((char *)p + rnewsz - malloc_guard, - malloc_guard, PROT_NONE)) - wrterror("mprotect"); - } - unmap(&g_pool, (char *)p + rnewsz, roldsz - rnewsz); - r->size = gnewsz; - return p; - } else if (rnewsz == roldsz) { - if (newsz > oldsz && malloc_junk) - memset((char *)p + newsz, SOME_JUNK, - rnewsz - malloc_guard - newsz); - r->size = gnewsz; - return p; - } - } - if (newsz <= oldsz && newsz > oldsz / 2 && !malloc_realloc) { - if (malloc_junk && newsz > 0) - memset((char *)p + newsz, SOME_JUNK, oldsz - newsz); - return p; - } else if (newsz != oldsz || malloc_realloc) { - q = omalloc(newsz, 0); - if (q == NULL) - return NULL; - if (newsz != 0 && oldsz != 0) - memcpy(q, p, oldsz < newsz ? oldsz : newsz); - ofree(p); - return q; - } else - return p; -} - -void * -realloc(void *ptr, size_t size) -{ - void *r; - - _MALLOC_LOCK(); - malloc_func = " in realloc():"; - if (!g_pool.regions_total) { - if (omalloc_init(&g_pool)) { - _MALLOC_UNLOCK(); - if (malloc_xmalloc) - wrterror("out of memory"); - errno = ENOMEM; - return NULL; - } - } - if (malloc_active++) { - malloc_recurse(); - return NULL; - } - - r = orealloc(ptr, size); - - malloc_active--; - _MALLOC_UNLOCK(); - if (r == NULL && malloc_xmalloc) { - wrterror("out of memory"); - errno = ENOMEM; - } - return r; -} diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c deleted file mode 100644 index 623f652134..0000000000 --- a/src/lib/libc/stdlib/merge.c +++ /dev/null @@ -1,333 +0,0 @@ -/* $OpenBSD: merge.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Peter McIlroy. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Hybrid exponential search/linear search merge sort with hybrid - * natural/pairwise first pass. Requires about .3% more comparisons - * for random data than LSMS with pairwise first pass alone. - * It works for objects as small as two bytes. - */ - -#define NATURAL -#define THRESHOLD 16 /* Best choice for natural merge cut-off. */ - -/* #define NATURAL to get hybrid natural merge. - * (The default is pairwise merging.) - */ - -#include - -#include -#include -#include - -static void setup(u_char *, u_char *, size_t, size_t, int (*)()); -static void insertionsort(u_char *, size_t, size_t, int (*)()); - -#define ISIZE sizeof(int) -#define PSIZE sizeof(u_char *) -#define ICOPY_LIST(src, dst, last) \ - do \ - *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ - while(src < last) -#define ICOPY_ELT(src, dst, i) \ - do \ - *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ - while (i -= ISIZE) - -#define CCOPY_LIST(src, dst, last) \ - do \ - *dst++ = *src++; \ - while (src < last) -#define CCOPY_ELT(src, dst, i) \ - do \ - *dst++ = *src++; \ - while (i -= 1) - -/* - * Find the next possible pointer head. (Trickery for forcing an array - * to do double duty as a linked list when objects do not align with word - * boundaries. - */ -/* Assumption: PSIZE is a power of 2. */ -#define EVAL(p) (u_char **) \ - ((u_char *)0 + \ - (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1))) - -/* - * Arguments are as for qsort. - */ -int -mergesort(void *base, size_t nmemb, size_t size, - int (*cmp)(const void *, const void *)) -{ - int i, sense; - int big, iflag; - u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; - u_char *list2, *list1, *p2, *p, *last, **p1; - - if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */ - errno = EINVAL; - return (-1); - } - - /* - * XXX - * Stupid subtraction for the Cray. - */ - iflag = 0; - if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE)) - iflag = 1; - - if ((list2 = malloc(nmemb * size + PSIZE)) == NULL) - return (-1); - - list1 = base; - setup(list1, list2, nmemb, size, cmp); - last = list2 + nmemb * size; - i = big = 0; - while (*EVAL(list2) != last) { - l2 = list1; - p1 = EVAL(list1); - for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) { - p2 = *EVAL(p2); - f1 = l2; - f2 = l1 = list1 + (p2 - list2); - if (p2 != last) - p2 = *EVAL(p2); - l2 = list1 + (p2 - list2); - while (f1 < l1 && f2 < l2) { - if ((*cmp)(f1, f2) <= 0) { - q = f2; - b = f1, t = l1; - sense = -1; - } else { - q = f1; - b = f2, t = l2; - sense = 0; - } - if (!big) { /* here i = 0 */ - while ((b += size) < t && cmp(q, b) >sense) - if (++i == 6) { - big = 1; - goto EXPONENTIAL; - } - } else { -EXPONENTIAL: for (i = size; ; i <<= 1) - if ((p = (b + i)) >= t) { - if ((p = t - size) > b && - (*cmp)(q, p) <= sense) - t = p; - else - b = p; - break; - } else if ((*cmp)(q, p) <= sense) { - t = p; - if (i == size) - big = 0; - goto FASTCASE; - } else - b = p; - while (t > b+size) { - i = (((t - b) / size) >> 1) * size; - if ((*cmp)(q, p = b + i) <= sense) - t = p; - else - b = p; - } - goto COPY; -FASTCASE: while (i > size) - if ((*cmp)(q, - p = b + (i >>= 1)) <= sense) - t = p; - else - b = p; -COPY: b = t; - } - i = size; - if (q == f1) { - if (iflag) { - ICOPY_LIST(f2, tp2, b); - ICOPY_ELT(f1, tp2, i); - } else { - CCOPY_LIST(f2, tp2, b); - CCOPY_ELT(f1, tp2, i); - } - } else { - if (iflag) { - ICOPY_LIST(f1, tp2, b); - ICOPY_ELT(f2, tp2, i); - } else { - CCOPY_LIST(f1, tp2, b); - CCOPY_ELT(f2, tp2, i); - } - } - } - if (f2 < l2) { - if (iflag) - ICOPY_LIST(f2, tp2, l2); - else - CCOPY_LIST(f2, tp2, l2); - } else if (f1 < l1) { - if (iflag) - ICOPY_LIST(f1, tp2, l1); - else - CCOPY_LIST(f1, tp2, l1); - } - *p1 = l2; - } - tp2 = list1; /* swap list1, list2 */ - list1 = list2; - list2 = tp2; - last = list2 + nmemb*size; - } - if (base == list2) { - memmove(list2, list1, nmemb*size); - list2 = list1; - } - free(list2); - return (0); -} - -#define swap(a, b) { \ - s = b; \ - i = size; \ - do { \ - tmp = *a; *a++ = *s; *s++ = tmp; \ - } while (--i); \ - a -= size; \ - } -#define reverse(bot, top) { \ - s = top; \ - do { \ - i = size; \ - do { \ - tmp = *bot; *bot++ = *s; *s++ = tmp; \ - } while (--i); \ - s -= size2; \ - } while(bot < s); \ -} - -/* - * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of - * increasing order, list2 in a corresponding linked list. Checks for runs - * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL - * is defined. Otherwise simple pairwise merging is used.) - */ -void -setup(u_char *list1, u_char *list2, size_t n, size_t size, - int (*cmp)(const void *, const void *)) -{ - int i, length, size2, tmp, sense; - u_char *f1, *f2, *s, *l2, *last, *p2; - - size2 = size*2; - if (n <= 5) { - insertionsort(list1, n, size, cmp); - *EVAL(list2) = (u_char*) list2 + n*size; - return; - } - /* - * Avoid running pointers out of bounds; limit n to evens - * for simplicity. - */ - i = 4 + (n & 1); - insertionsort(list1 + (n - i) * size, i, size, cmp); - last = list1 + size * (n - i); - *EVAL(list2 + (last - list1)) = list2 + n * size; - -#ifdef NATURAL - p2 = list2; - f1 = list1; - sense = (cmp(f1, f1 + size) > 0); - for (; f1 < last; sense = !sense) { - length = 2; - /* Find pairs with same sense. */ - for (f2 = f1 + size2; f2 < last; f2 += size2) { - if ((cmp(f2, f2+ size) > 0) != sense) - break; - length += 2; - } - if (length < THRESHOLD) { /* Pairwise merge */ - do { - p2 = *EVAL(p2) = f1 + size2 - list1 + list2; - if (sense > 0) - swap (f1, f1 + size); - } while ((f1 += size2) < f2); - } else { /* Natural merge */ - l2 = f2; - for (f2 = f1 + size2; f2 < l2; f2 += size2) { - if ((cmp(f2-size, f2) > 0) != sense) { - p2 = *EVAL(p2) = f2 - list1 + list2; - if (sense > 0) - reverse(f1, f2-size); - f1 = f2; - } - } - if (sense > 0) - reverse (f1, f2-size); - f1 = f2; - if (f2 < last || cmp(f2 - size, f2) > 0) - p2 = *EVAL(p2) = f2 - list1 + list2; - else - p2 = *EVAL(p2) = list2 + n*size; - } - } -#else /* pairwise merge only. */ - for (f1 = list1, p2 = list2; f1 < last; f1 += size2) { - p2 = *EVAL(p2) = p2 + size2; - if (cmp (f1, f1 + size) > 0) - swap(f1, f1 + size); - } -#endif /* NATURAL */ -} - -/* - * This is to avoid out-of-bounds addresses in sorting the - * last 4 elements. - */ -static void -insertionsort(u_char *a, size_t n, size_t size, - int (*cmp)(const void *, const void *)) -{ - u_char *ai, *s, *t, *u, tmp; - int i; - - for (ai = a+size; --n >= 1; ai += size) - for (t = ai; t > a; t -= size) { - u = t - size; - if (cmp(u, t) <= 0) - break; - swap(u, t); - } -} diff --git a/src/lib/libc/stdlib/mrand48.c b/src/lib/libc/stdlib/mrand48.c deleted file mode 100644 index 977264aba5..0000000000 --- a/src/lib/libc/stdlib/mrand48.c +++ /dev/null @@ -1,24 +0,0 @@ -/* $OpenBSD: mrand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -extern unsigned short __rand48_seed[3]; - -long -mrand48(void) -{ - __dorand48(__rand48_seed); - return ((long) __rand48_seed[2] << 16) + (long) __rand48_seed[1]; -} diff --git a/src/lib/libc/stdlib/nrand48.c b/src/lib/libc/stdlib/nrand48.c deleted file mode 100644 index f1f548c3af..0000000000 --- a/src/lib/libc/stdlib/nrand48.c +++ /dev/null @@ -1,22 +0,0 @@ -/* $OpenBSD: nrand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -long -nrand48(unsigned short xseed[3]) -{ - __dorand48(xseed); - return ((long) xseed[2] << 15) + ((long) xseed[1] >> 1); -} diff --git a/src/lib/libc/stdlib/putenv.c b/src/lib/libc/stdlib/putenv.c deleted file mode 100644 index 54482f6a14..0000000000 --- a/src/lib/libc/stdlib/putenv.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $OpenBSD: putenv.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ -/*- - * Copyright (c) 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -int -putenv(const char *str) -{ - char *p, *equal; - int rval; - - if ((p = strdup(str)) == NULL) - return (-1); - if ((equal = strchr(p, '=')) == NULL) { - (void)free(p); - return (-1); - } - *equal = '\0'; - rval = setenv(p, equal + 1, 1); - (void)free(p); - return (rval); -} diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3 deleted file mode 100644 index e022e9c1f4..0000000000 --- a/src/lib/libc/stdlib/qabs.3 +++ /dev/null @@ -1,57 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: qabs.3,v 1.11 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt QABS 3 -.Os -.Sh NAME -.Nm qabs -.Nd return the absolute value of a quad integer -.Sh SYNOPSIS -.Fd #include -.Ft quad_t -.Fn qabs "quad_t j" -.Sh DESCRIPTION -The -.Fn qabs -function returns the absolute value of the quad integer -.Fa j . -.Sh SEE ALSO -.Xr abs 3 , -.Xr cabs 3 , -.Xr floor 3 , -.Xr imaxabs 3 , -.Xr labs 3 , -.Xr math 3 -.Sh BUGS -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 deleted file mode 100644 index 656b93c822..0000000000 --- a/src/lib/libc/stdlib/qabs.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $OpenBSD: qabs.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -quad_t -qabs(quad_t j) -{ - return(j < 0 ? -j : j); -} diff --git a/src/lib/libc/stdlib/qdiv.3 b/src/lib/libc/stdlib/qdiv.3 deleted file mode 100644 index 59776bdf2d..0000000000 --- a/src/lib/libc/stdlib/qdiv.3 +++ /dev/null @@ -1,62 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: qdiv.3,v 1.8 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt QDIV 3 -.Os -.Sh NAME -.Nm qdiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.Fd #include -.Ft qdiv_t -.Fn qdiv "quad_t num" "quad_t denom" -.Sh DESCRIPTION -The -.Fn qdiv -function computes the value -.Fa num Ns / Ns Fa denom -and returns the quotient and remainder in a structure named -.Li qdiv_t -that contains two -.Li quad integer -members named -.Fa quot -and -.Fa rem . -.Sh SEE ALSO -.Xr div 3 , -.Xr imaxdiv 3 , -.Xr ldiv 3 , -.Xr lldiv 3 , -.Xr math 3 diff --git a/src/lib/libc/stdlib/qdiv.c b/src/lib/libc/stdlib/qdiv.c deleted file mode 100644 index f3db0915ed..0000000000 --- a/src/lib/libc/stdlib/qdiv.c +++ /dev/null @@ -1,50 +0,0 @@ -/* $OpenBSD: qdiv.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Chris Torek. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include /* qdiv_t */ - -qdiv_t -qdiv(quad_t num, quad_t denom) -{ - qdiv_t r; - - /* see div.c for comments */ - - r.quot = num / denom; - r.rem = num % denom; - if (num >= 0 && r.rem < 0) { - r.quot++; - r.rem -= denom; - } - return (r); -} diff --git a/src/lib/libc/stdlib/qsort.3 b/src/lib/libc/stdlib/qsort.3 deleted file mode 100644 index 92c75d5365..0000000000 --- a/src/lib/libc/stdlib/qsort.3 +++ /dev/null @@ -1,233 +0,0 @@ -.\" Copyright (c) 1990, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: qsort.3,v 1.15 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt QSORT 3 -.Os -.Sh NAME -.Nm qsort , -.Nm heapsort , -.Nm mergesort -.Nd sort functions -.Sh SYNOPSIS -.Fd #include -.Ft void -.Fn qsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" -.Ft int -.Fn heapsort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" -.Ft int -.Fn mergesort "void *base" "size_t nmemb" "size_t size" "int (*compar)(const void *, const void *)" -.Sh DESCRIPTION -The -.Fn qsort -function is a modified partition-exchange sort, or quicksort. -The -.Fn heapsort -function is a modified selection sort. -The -.Fn mergesort -function is a modified merge sort with exponential search -intended for sorting data with pre-existing order. -.Pp -The -.Fn qsort -and -.Fn heapsort -functions sort an array of -.Fa nmemb -objects, the initial member of which is pointed to by -.Fa base . -The size of each object is specified by -.Fa size . -.Fn mergesort -behaves similarly, but -.Em requires -that -.Fa size -be greater than -.Dq "sizeof(void *) / 2" . -.Pp -The contents of the array -.Fa base -are sorted in ascending order according to -a comparison function pointed to by -.Fa compar , -which requires two arguments pointing to the objects being -compared. -.Pp -The comparison function must return an integer less than, equal to, or -greater than zero if the first argument is considered to be respectively -less than, equal to, or greater than the second. -.Pp -The functions -.Fn qsort -and -.Fn heapsort -are -.Em not -stable, that is, if two members compare as equal, their order in -the sorted array is undefined. -The function -.Fn mergesort -is stable. -.Pp -The -.Fn qsort -function is an implementation of C.A.R. Hoare's -.Dq quicksort -algorithm, -a variant of partition-exchange sorting; in particular, see D.E. Knuth's -Algorithm Q. -.Fn qsort -takes O N lg N average time. -This implementation uses median selection to avoid its -O N**2 worst-case behavior. -.Pp -The -.Fn heapsort -function is an implementation of J.W.J. William's -.Dq heapsort -algorithm, -a variant of selection sorting; in particular, see D.E. Knuth's Algorithm H. -.Fn heapsort -takes O N lg N worst-case time. -This implementation of -.Fn heapsort -is implemented without recursive function calls. -.Pp -The function -.Fn mergesort -requires additional memory of size -.Fa nmemb * -.Fa size -bytes; it should be used only when space is not at a premium. -.Fn mergesort -is optimized for data with pre-existing order; its worst case -time is O N lg N; its best case is O N. -.Pp -Normally, -.Fn qsort -is faster than -.Fn mergesort , -which is faster than -.Fn heapsort . -Memory availability and pre-existing order in the data can make this untrue. -.Sh RETURN VALUES -The -.Fn qsort -function returns no value. -.Pp -Upon successful completion, -.Fn heapsort -and -.Fn mergesort -return 0. -Otherwise, they return \-1 and the global variable -.Va errno -is set to indicate the error. -.Sh ERRORS -The -.Fn heapsort -and -.Fn mergesort -functions succeed unless: -.Bl -tag -width Er -.It Bq Er EINVAL -The -.Fa size -argument is zero, or the -.Fa size -argument to -.Fn mergesort -is less than -.Dq "sizeof(void *) / 2" . -.It Bq Er ENOMEM -.Fn heapsort -or -.Fn mergesort -were unable to allocate memory. -.El -.Sh SEE ALSO -.Xr sort 1 , -.Xr radixsort 3 -.Rs -.%A Hoare, C.A.R. -.%D 1962 -.%T "Quicksort" -.%J "The Computer Journal" -.%V 5:1 -.%P pp. 10-15 -.Re -.Rs -.%A Williams, J.W.J -.%D 1964 -.%T "Heapsort" -.%J "Communications of the ACM" -.%V 7:1 -.%P pp. 347\-348 -.Re -.Rs -.%A Knuth, D.E. -.%D 1968 -.%B "The Art of Computer Programming" -.%V Vol. 3 -.%T "Sorting and Searching" -.%P pp. 114\-123, 145\-149 -.Re -.Rs -.%A McIlroy, P.M. -.%T "Optimistic Sorting and Information Theoretic Complexity" -.%J "Fourth Annual ACM-SIAM Symposium on Discrete Algorithms" -.%P pp. 467\-464 -.%D January 1993 -.Re -.Rs -.%A Bentley, J.L. -.%A McIlroy, M.D. -.%T "Engineering a Sort Function" -.%J "Software \- Practice and Experience" -.%V Vol. 23(11) -.%P pp. 1249\-1265 -.%D November 1993 -.Re -.Sh STANDARDS -Previous versions of -.Fn qsort -did not permit the comparison routine itself to call -.Fn qsort . -This is no longer true. -.Pp -The -.Fn qsort -function conforms to -.St -ansiC . diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c deleted file mode 100644 index bb4a9a11f2..0000000000 --- a/src/lib/libc/stdlib/qsort.c +++ /dev/null @@ -1,161 +0,0 @@ -/* $OpenBSD: qsort.c,v 1.10 2005/08/08 08:05:37 espie Exp $ */ -/*- - * Copyright (c) 1992, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -static __inline char *med3(char *, char *, char *, int (*)(const void *, const void *)); -static __inline void swapfunc(char *, char *, int, int); - -#define min(a, b) (a) < (b) ? a : b - -/* - * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function". - */ -#define swapcode(TYPE, parmi, parmj, n) { \ - long i = (n) / sizeof (TYPE); \ - TYPE *pi = (TYPE *) (parmi); \ - TYPE *pj = (TYPE *) (parmj); \ - do { \ - TYPE t = *pi; \ - *pi++ = *pj; \ - *pj++ = t; \ - } while (--i > 0); \ -} - -#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \ - es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1; - -static __inline void -swapfunc(char *a, char *b, int n, int swaptype) -{ - if (swaptype <= 1) - swapcode(long, a, b, n) - else - swapcode(char, a, b, n) -} - -#define swap(a, b) \ - if (swaptype == 0) { \ - long t = *(long *)(a); \ - *(long *)(a) = *(long *)(b); \ - *(long *)(b) = t; \ - } else \ - swapfunc(a, b, es, swaptype) - -#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype) - -static __inline char * -med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *)) -{ - return cmp(a, b) < 0 ? - (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a )) - :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c )); -} - -void -qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *)) -{ - char *pa, *pb, *pc, *pd, *pl, *pm, *pn; - int d, r, swaptype, swap_cnt; - char *a = aa; - -loop: SWAPINIT(a, es); - swap_cnt = 0; - if (n < 7) { - for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) - for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; - pl -= es) - swap(pl, pl - es); - return; - } - pm = (char *)a + (n / 2) * es; - if (n > 7) { - pl = (char *)a; - pn = (char *)a + (n - 1) * es; - if (n > 40) { - d = (n / 8) * es; - pl = med3(pl, pl + d, pl + 2 * d, cmp); - pm = med3(pm - d, pm, pm + d, cmp); - pn = med3(pn - 2 * d, pn - d, pn, cmp); - } - pm = med3(pl, pm, pn, cmp); - } - swap(a, pm); - pa = pb = (char *)a + es; - - pc = pd = (char *)a + (n - 1) * es; - for (;;) { - while (pb <= pc && (r = cmp(pb, a)) <= 0) { - if (r == 0) { - swap_cnt = 1; - swap(pa, pb); - pa += es; - } - pb += es; - } - while (pb <= pc && (r = cmp(pc, a)) >= 0) { - if (r == 0) { - swap_cnt = 1; - swap(pc, pd); - pd -= es; - } - pc -= es; - } - if (pb > pc) - break; - swap(pb, pc); - swap_cnt = 1; - pb += es; - pc -= es; - } - if (swap_cnt == 0) { /* Switch to insertion sort */ - for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) - for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; - pl -= es) - swap(pl, pl - es); - return; - } - - pn = (char *)a + n * es; - r = min(pa - (char *)a, pb - pa); - vecswap(a, pb - r, r); - r = min(pd - pc, pn - pd - es); - vecswap(pb, pn - r, r); - if ((r = pb - pa) > es) - qsort(a, r / es, es, cmp); - if ((r = pd - pc) > es) { - /* Iterate rather than recurse to save stack space */ - a = pn - r; - n = r / es; - goto loop; - } -/* qsort(pn - r, r / es, es, cmp);*/ -} diff --git a/src/lib/libc/stdlib/radixsort.3 b/src/lib/libc/stdlib/radixsort.3 deleted file mode 100644 index b90a557718..0000000000 --- a/src/lib/libc/stdlib/radixsort.3 +++ /dev/null @@ -1,155 +0,0 @@ -.\" Copyright (c) 1990, 1991, 1993 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: radixsort.3,v 1.11 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt RADIXSORT 3 -.Os -.Sh NAME -.Nm radixsort , -.Nm sradixsort -.Nd radix sort -.Sh SYNOPSIS -.Fd #include -.Fd #include -.Ft int -.Fn radixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte" -.Ft int -.Fn sradixsort "const u_char **base" "int nmemb" "const u_char *table" "u_int endbyte" -.Sh DESCRIPTION -The -.Fn radixsort -and -.Fn sradixsort -functions are implementations of radix sort. -.Pp -These functions sort an array of -.Fa nmemb -pointers to byte strings. -The initial member is referenced by -.Fa base . -The byte strings may contain any values; the end of each string -is denoted by the user-specified value -.Fa endbyte . -.Pp -Applications may specify a sort order by providing the -.Fa table -argument. -If non-null, -.Fa table -must reference an array of -.Dv UCHAR_MAX -+ 1 bytes which contains the sort weight of each possible byte value. -The end-of-string byte must have a sort weight of 0 or 255 -(for sorting in reverse order). -More than one byte may have the same sort weight. -The -.Fa table -argument is useful for applications which wish to sort different characters -equally; for example, providing a table with the same weights -for A\-Z as for a\-z will result in a case-insensitive sort. -If -.Fa table -is -.Dv NULL , -the contents of the array are sorted in ascending order according to the -.Tn ASCII -order of the byte strings they reference and -.Fa endbyte -has a sorting weight of 0. -.Pp -The -.Fn sradixsort -function is stable; that is, if two elements compare as equal, their -order in the sorted array is unchanged. -The -.Fn sradixsort -function uses additional memory sufficient to hold -.Fa nmemb -pointers. -.Pp -The -.Fn radixsort -function is not stable, but uses no additional memory. -.Pp -These functions are variants of most-significant-byte radix sorting; in -particular, see D.E. Knuth's Algorithm R and section 5.2.5, exercise 10. -They take linear time relative to the number of bytes in the strings. -.Sh RETURN VALUES -Upon successful completion 0 is returned. -Otherwise, \-1 is returned and the global variable -.Va errno -is set to indicate the error. -.Sh ERRORS -.Bl -tag -width Er -.It Bq Er EINVAL -The value of the -.Fa endbyte -element of -.Fa table -is not 0 or 255. -.El -.Pp -Additionally, the -.Fn sradixsort -function may fail and set -.Va errno -for any of the errors specified for the library routine -.Xr malloc 3 . -.Sh SEE ALSO -.Xr sort 1 , -.Xr qsort 3 -.Rs -.%A Knuth, D.E. -.%D 1968 -.%B "The Art of Computer Programming" -.%T "Sorting and Searching" -.%V Vol. 3 -.%P pp. 170-178 -.Re -.Rs -.%A Paige, R. -.%D 1987 -.%T "Three Partition Refinement Algorithms" -.%J "SIAM J. Comput." -.%V Vol. 16 -.%N No. 6 -.Re -.Rs -.%A McIlroy, P. -.%D 1993 -.%B "Engineering Radix Sort" -.%T "Computing Systems" -.%V Vol. 6:1 -.%P pp. 5-27 -.Re -.Sh HISTORY -The -.Fn radixsort -function first appeared in -.Bx 4.4 . diff --git a/src/lib/libc/stdlib/radixsort.c b/src/lib/libc/stdlib/radixsort.c deleted file mode 100644 index 49d03b52d5..0000000000 --- a/src/lib/libc/stdlib/radixsort.c +++ /dev/null @@ -1,294 +0,0 @@ -/* $OpenBSD: radixsort.c,v 1.9 2007/09/02 15:19:17 deraadt Exp $ */ -/*- - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. - * - * This code is derived from software contributed to Berkeley by - * Peter McIlroy and by Dan Bernstein at New York University, - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Radixsort routines. - * - * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. - * Use radixsort(a, n, trace, endchar) for this case. - * - * For stable sorting (using N extra pointers) use sradixsort(), which calls - * r_sort_b(). - * - * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, - * "Engineering Radix Sort". - */ - -#include -#include -#include - -typedef struct { - const u_char **sa; - int sn, si; -} stack; - -static __inline void simplesort -(const u_char **, int, int, const u_char *, u_int); -static void r_sort_a(const u_char **, int, int, const u_char *, u_int); -static void r_sort_b(const u_char **, - const u_char **, int, int, const u_char *, u_int); - -#define THRESHOLD 20 /* Divert to simplesort(). */ -#define SIZE 512 /* Default stack size. */ - -#define SETUP { \ - if (tab == NULL) { \ - tr = tr0; \ - for (c = 0; c < endch; c++) \ - tr0[c] = c + 1; \ - tr0[c] = 0; \ - for (c++; c < 256; c++) \ - tr0[c] = c; \ - endch = 0; \ - } else { \ - endch = tab[endch]; \ - tr = tab; \ - if (endch != 0 && endch != 255) { \ - errno = EINVAL; \ - return (-1); \ - } \ - } \ -} - -int -radixsort(const u_char **a, int n, const u_char *tab, u_int endch) -{ - const u_char *tr; - int c; - u_char tr0[256]; - - SETUP; - r_sort_a(a, n, 0, tr, endch); - return (0); -} - -int -sradixsort(const u_char **a, int n, const u_char *tab, u_int endch) -{ - const u_char *tr, **ta; - int c; - u_char tr0[256]; - - SETUP; - if (n < THRESHOLD) - simplesort(a, n, 0, tr, endch); - else { - if ((ta = calloc(n, sizeof(a))) == NULL) - return (-1); - r_sort_b(a, ta, n, 0, tr, endch); - free(ta); - } - return (0); -} - -#define empty(s) (s >= sp) -#define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si -#define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i -#define swap(a, b, t) t = a, a = b, b = t - -/* Unstable, in-place sort. */ -void -r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch) -{ - static int count[256], nc, bmin; - int c; - const u_char **ak, *r; - stack s[SIZE], *sp, *sp0, *sp1, temp; - int *cp, bigc; - const u_char **an, *t, **aj, **top[256]; - - /* Set up stack. */ - sp = s; - push(a, n, i); - while (!empty(s)) { - pop(a, n, i); - if (n < THRESHOLD) { - simplesort(a, n, i, tr, endch); - continue; - } - an = a + n; - - /* Make character histogram. */ - if (nc == 0) { - bmin = 255; /* First occupied bin, excluding eos. */ - for (ak = a; ak < an;) { - c = tr[(*ak++)[i]]; - if (++count[c] == 1 && c != endch) { - if (c < bmin) - bmin = c; - nc++; - } - } - if (sp + nc > s + SIZE) { /* Get more stack. */ - r_sort_a(a, n, i, tr, endch); - continue; - } - } - - /* - * Set top[]; push incompletely sorted bins onto stack. - * top[] = pointers to last out-of-place element in bins. - * count[] = counts of elements in bins. - * Before permuting: top[c-1] + count[c] = top[c]; - * during deal: top[c] counts down to top[c-1]. - */ - sp0 = sp1 = sp; /* Stack position of biggest bin. */ - bigc = 2; /* Size of biggest bin. */ - if (endch == 0) /* Special case: set top[eos]. */ - top[0] = ak = a + count[0]; - else { - ak = a; - top[255] = an; - } - for (cp = count + bmin; nc > 0; cp++) { - while (*cp == 0) /* Find next non-empty pile. */ - cp++; - if (*cp > 1) { - if (*cp > bigc) { - bigc = *cp; - sp1 = sp; - } - push(ak, *cp, i+1); - } - top[cp-count] = ak += *cp; - nc--; - } - swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ - - /* - * Permute misplacements home. Already home: everything - * before aj, and in bin[c], items from top[c] on. - * Inner loop: - * r = next element to put in place; - * ak = top[r[i]] = location to put the next element. - * aj = bottom of 1st disordered bin. - * Outer loop: - * Once the 1st disordered bin is done, ie. aj >= ak, - * aj<-aj + count[c] connects the bins in a linked list; - * reset count[c]. - */ - for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) - for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) - swap(*ak, r, t); - } -} - -/* Stable sort, requiring additional memory. */ -void -r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr, - u_int endch) -{ - static int count[256], nc, bmin; - int c; - const u_char **ak, **ai; - stack s[512], *sp, *sp0, *sp1, temp; - const u_char **top[256]; - int *cp, bigc; - - sp = s; - push(a, n, i); - while (!empty(s)) { - pop(a, n, i); - if (n < THRESHOLD) { - simplesort(a, n, i, tr, endch); - continue; - } - - if (nc == 0) { - bmin = 255; - for (ak = a + n; --ak >= a;) { - c = tr[(*ak)[i]]; - if (++count[c] == 1 && c != endch) { - if (c < bmin) - bmin = c; - nc++; - } - } - if (sp + nc > s + SIZE) { - r_sort_b(a, ta, n, i, tr, endch); - continue; - } - } - - sp0 = sp1 = sp; - bigc = 2; - if (endch == 0) { - top[0] = ak = a + count[0]; - count[0] = 0; - } else { - ak = a; - top[255] = a + n; - count[255] = 0; - } - for (cp = count + bmin; nc > 0; cp++) { - while (*cp == 0) - cp++; - if ((c = *cp) > 1) { - if (c > bigc) { - bigc = c; - sp1 = sp; - } - push(ak, c, i+1); - } - top[cp-count] = ak += c; - *cp = 0; /* Reset count[]. */ - nc--; - } - swap(*sp0, *sp1, temp); - - for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ - *--ak = *--ai; - for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ - *--top[tr[(*ak)[i]]] = *ak; - } -} - -static __inline void -simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch) - /* insertion sort */ -{ - u_char ch; - const u_char **ak, **ai, *s, *t; - - for (ak = a+1; --n >= 1; ak++) - for (ai = ak; ai > a; ai--) { - for (s = ai[0] + b, t = ai[-1] + b; - (ch = tr[*s]) != endch; s++, t++) - if (ch != tr[*t]) - break; - if (ch >= tr[*t]) - break; - swap(ai[0], ai[-1], s); - } -} diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3 deleted file mode 100644 index df4feaacdb..0000000000 --- a/src/lib/libc/stdlib/rand.3 +++ /dev/null @@ -1,98 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: rand.3,v 1.10 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt RAND 3 -.Os -.Sh NAME -.Nm rand , -.Nm srand -.Nd bad random number generator -.Sh SYNOPSIS -.Fd #include -.Ft void -.Fn srand "unsigned int seed" -.Ft int -.Fn rand void -.Ft int -.Fn rand_r "unsigned int *seed" -.Sh DESCRIPTION -.Bf -symbolic -These interfaces are obsoleted by -.Xr random 3 . -.Ef -.Pp -The -.Fn rand -function computes a sequence of pseudo-random integers in the range -of 0 to -.Dv RAND_MAX -(as defined by the header file -.Aq Pa stdlib.h ) . -.Pp -The -.Fn srand -function sets its argument as the seed for a new sequence of -pseudo-random numbers to be returned by -.Fn rand . -These sequences are repeatable by calling -.Fn srand -with the same seed value. -.Pp -If no seed value is provided, the functions are automatically -seeded with a value of 1. -.Pp -The -.Fn rand_r -is a thread-safe version of -.Fn rand . -Storage for the seed must be provided through the -.Fa seed -argument, and needs to have been initialized by the caller. -.Sh SEE ALSO -.Xr arc4random 3 , -.Xr rand48 3 , -.Xr random 3 -.Sh STANDARDS -The -.Fn rand -and -.Fn srand -functions conform to -.St -ansiC . -.Pp -The -.Fn rand_r -function conforms to ISO/IEC 9945-1 ANSI/IEEE -.Pq Dq Tn POSIX -Std 1003.1c Draft 10. diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c deleted file mode 100644 index 0f9c100807..0000000000 --- a/src/lib/libc/stdlib/rand.c +++ /dev/null @@ -1,52 +0,0 @@ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -static u_int next = 1; - -int -rand_r(u_int *seed) -{ - *seed = *seed * 1103515245 + 12345; - return (*seed % ((u_int)RAND_MAX + 1)); -} - -int -rand(void) -{ - return (rand_r(&next)); -} - -void -srand(u_int seed) -{ - next = seed; -} diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 deleted file mode 100644 index 0e8ca5389b..0000000000 --- a/src/lib/libc/stdlib/rand48.3 +++ /dev/null @@ -1,165 +0,0 @@ -.\" Copyright (c) 1993 Martin Birgmeier -.\" All rights reserved. -.\" -.\" You may redistribute unmodified or modified versions of this source -.\" code provided that the above copyright notice and this and the -.\" following conditions are retained. -.\" -.\" This software is provided ``as is'', and comes with no warranties -.\" of any kind. I shall in no event be liable for anything that happens -.\" to anyone/anything when using this software. -.\" -.\" $OpenBSD: rand48.3,v 1.11 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt RAND48 3 -.Os -.Sh NAME -.Nm drand48 , -.Nm erand48 , -.Nm lrand48 , -.Nm nrand48 , -.Nm mrand48 , -.Nm jrand48 , -.Nm srand48 , -.Nm seed48 , -.Nm lcong48 -.Nd pseudo-random number generators and initialization routines -.Sh SYNOPSIS -.Fd #include -.Ft double -.Fn drand48 void -.Ft double -.Fn erand48 "unsigned short xseed[3]" -.Ft long -.Fn lrand48 void -.Ft long -.Fn nrand48 "unsigned short xseed[3]" -.Ft long -.Fn mrand48 void -.Ft long -.Fn jrand48 "unsigned short xseed[3]" -.Ft void -.Fn srand48 "long seed" -.Ft "unsigned short *" -.Fn seed48 "unsigned short xseed[3]" -.Ft void -.Fn lcong48 "unsigned short p[7]" -.Sh DESCRIPTION -The -.Fn rand48 -family of functions generates pseudo-random numbers using a linear -congruential algorithm working on integers 48 bits in size. -The particular formula employed is -r(n+1) = (a * r(n) + c) mod m -where the default values are -for the multiplicand a = 0xfdeece66d = 25214903917 and -the addend c = 0xb = 11. -The modulus is always fixed at m = 2 ** 48. -r(n) is called the seed of the random number generator. -.Pp -For all the six generator routines described next, the first -computational step is to perform a single iteration of the algorithm. -.Pp -.Fn drand48 -and -.Fn erand48 -return values of type double. -The full 48 bits of r(n+1) are -loaded into the mantissa of the returned value, with the exponent set -such that the values produced lie in the interval [0.0, 1.0]. -.Pp -.Fn lrand48 -and -.Fn nrand48 -return values of type long in the range -[0, 2**31-1]. -The high-order (31) bits of -r(n+1) are loaded into the lower bits of the returned value, with -the topmost (sign) bit set to zero. -.Pp -.Fn mrand48 -and -.Fn jrand48 -return values of type long in the range -[-2**31, 2**31-1]. -The high-order (32) bits of r(n+1) are loaded into the returned value. -.Pp -.Fn drand48 , -.Fn lrand48 , -and -.Fn mrand48 -use an internal buffer to store r(n). -For these functions -the initial value of r(0) = 0x1234abcd330e = 20017429951246. -.Pp -On the other hand, -.Fn erand48 , -.Fn nrand48 , -and -.Fn jrand48 -use a user-supplied buffer to store the seed r(n), -which consists of an array of 3 shorts, where the zeroth member -holds the least significant bits. -.Pp -All functions share the same multiplicand and addend. -.Pp -.Fn srand48 -is used to initialize the internal buffer r(n) of -.Fn drand48 , -.Fn lrand48 , -and -.Fn mrand48 -such that the 32 bits of the seed value are copied into the upper 32 bits -of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. -Additionally, the constant multiplicand and addend of the algorithm are -reset to the default values given above. -.Pp -.Fn seed48 -also initializes the internal buffer r(n) of -.Fn drand48 , -.Fn lrand48 , -and -.Fn mrand48 , -but here all 48 bits of the seed can be specified in an array of 3 shorts, -where the zeroth member specifies the lowest bits. -Again, the constant multiplicand and addend of the algorithm are -reset to the default values given above. -.Fn seed48 -returns a pointer to an array of 3 shorts which contains the old seed. -This array is statically allocated, so its contents are lost after -each new call to -.Fn seed48 . -.Pp -Finally, -.Fn lcong48 -allows full control over the multiplicand and addend used in -.Fn drand48 , -.Fn erand48 , -.Fn lrand48 , -.Fn nrand48 , -.Fn mrand48 , -and -.Fn jrand48 , -and the seed used in -.Fn drand48 , -.Fn lrand48 , -and -.Fn mrand48 . -An array of 7 shorts is passed as parameter; the first three shorts are -used to initialize the seed; the second three are used to initialize the -multiplicand; and the last short is used to initialize the addend. -It is thus not possible to use values greater than 0xffff as the addend. -.Pp -Note that all three methods of seeding the random number generator -always also set the multiplicand and addend for any of the six -generator calls. -.Pp -For a more powerful random number generator, see -.Xr random 3 . -.Sh SEE ALSO -.Xr arc4random 3 , -.Xr rand 3 , -.Xr random 3 -.Sh AUTHORS -Martin Birgmeier diff --git a/src/lib/libc/stdlib/rand48.h b/src/lib/libc/stdlib/rand48.h deleted file mode 100644 index afa49f65f3..0000000000 --- a/src/lib/libc/stdlib/rand48.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - * - * $OpenBSD: rand48.h,v 1.3 2002/02/16 21:27:24 millert Exp $ - */ - -#ifndef _RAND48_H_ -#define _RAND48_H_ - -#include -#include - -void __dorand48(unsigned short[3]); - -#define RAND48_SEED_0 (0x330e) -#define RAND48_SEED_1 (0xabcd) -#define RAND48_SEED_2 (0x1234) -#define RAND48_MULT_0 (0xe66d) -#define RAND48_MULT_1 (0xdeec) -#define RAND48_MULT_2 (0x0005) -#define RAND48_ADD (0x000b) - -#endif /* _RAND48_H_ */ diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3 deleted file mode 100644 index ed05df162b..0000000000 --- a/src/lib/libc/stdlib/random.3 +++ /dev/null @@ -1,185 +0,0 @@ -.\" Copyright (c) 1983, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: random.3,v 1.19 2007/05/31 19:19:31 jmc Exp $ -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt RANDOM 3 -.Os -.Sh NAME -.Nm random , -.Nm srandom , -.Nm srandomdev , -.Nm initstate , -.Nm setstate -.Nd better random number generator; routines for changing generators -.Sh SYNOPSIS -.Fd #include -.Ft long -.Fn random void -.Ft void -.Fn srandom "unsigned int seed" -.Ft void -.Fn srandomdev void -.Ft char * -.Fn initstate "unsigned int seed" "char *state" "size_t n" -.Ft char * -.Fn setstate "const char *state" -.Sh DESCRIPTION -The -.Fn random -function uses a non-linear additive feedback random number generator employing -a default table of size 31 long integers to return successive pseudo-random -numbers in the range from 0 to (2**31)\-1. -The period of this random number generator is very large, approximately -16*((2**31)\-1). -.Pp -The -.Fn random -and -.Fn srandom -functions have (almost) the same calling sequence and initialization -properties as -.Xr rand 3 Ns / Ns Xr srand 3 . -The difference is that -.Xr rand -produces a much less random sequence \(em in fact, the low dozen bits -generated by rand go through a cyclic pattern. -All the bits generated by -.Fn random -are usable. -For example, -.Sq Li random()&01 -will produce a random binary -value. -.Pp -Like -.Xr rand 3 , -.Fn random -will by default produce a sequence of numbers that can be duplicated -by calling -.Fn srandom -with -.Ql 1 -as the seed. -.Pp -The -.Fn srandomdev -routine initializes a state array using -random numbers obtained from the kernel, -suitable for cryptographic use. -Note that this particular seeding procedure can generate -states which are impossible to reproduce by calling -.Fn srandom -with any value, since the succeeding terms in the -state buffer are no longer derived from the LC algorithm applied to -a fixed seed. -.Pp -The -.Fn initstate -routine allows a state array, passed in as an argument, to be initialized -for future use. -The size of the state array (in bytes) is used by -.Fn initstate -to decide how sophisticated a random number generator it should use \(em the -more state, the better the random numbers will be. -(Current "optimal" values for the amount of state information are -8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to -the nearest known amount. -Using less than 8 bytes will cause an error.) -The seed for the initialization (which specifies a starting point for -the random number sequence, and provides for restarting at the same -point) is also an argument. -The -.Fn initstate -function returns a pointer to the previous state information array. -.Pp -Once a state has been initialized, the -.Fn setstate -routine provides for rapid switching between states. -The -.Fn setstate -function returns a pointer to the previous state array; its -argument state array is used for further random number generation -until the next call to -.Fn initstate -or -.Fn setstate . -.Pp -Once a state array has been initialized, it may be restarted at a -different point either by calling -.Fn initstate -(with the desired seed, the state array, and its size) or by calling -both -.Fn setstate -(with the state array) and -.Fn srandom -(with the desired seed). -The advantage of calling both -.Fn setstate -and -.Fn srandom -is that the size of the state array does not have to be remembered after -it is initialized. -.Pp -With 256 bytes of state information, the period of the random number -generator is greater than 2**69 -which should be sufficient for most purposes. -.Sh DIAGNOSTICS -If -.Fn initstate -is called with less than 8 bytes of state information, or if -.Fn setstate -detects that the state information has been garbled, error -messages are printed on the standard error output. -.Sh SEE ALSO -.Xr arc4random 3 , -.Xr drand48 3 , -.Xr rand 3 , -.Xr random 4 -.Sh STANDARDS -The -.Fn random , -.Fn srandom , -.Fn initstate , -and -.Fn setstate -functions conform to -.St -xpg4.2 . -.Pp -The -.Fn srandomdev -function is an extension. -.Sh HISTORY -These -functions appeared in -.Bx 4.2 . -.Sh AUTHORS -.An Earl T. Cohen -.Sh BUGS -About 2/3 the speed of -.Xr rand 3 . diff --git a/src/lib/libc/stdlib/random.c b/src/lib/libc/stdlib/random.c deleted file mode 100644 index 48e892042b..0000000000 --- a/src/lib/libc/stdlib/random.c +++ /dev/null @@ -1,395 +0,0 @@ -/* $OpenBSD: random.c,v 1.15 2005/11/30 07:51:02 otto Exp $ */ -/* - * Copyright (c) 1983 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include -#include - -/* - * random.c: - * - * An improved random number generation package. In addition to the standard - * rand()/srand() like interface, this package also has a special state info - * interface. The initstate() routine is called with a seed, an array of - * bytes, and a count of how many bytes are being passed in; this array is - * then initialized to contain information for random number generation with - * that much state information. Good sizes for the amount of state - * information are 32, 64, 128, and 256 bytes. The state can be switched by - * calling the setstate() routine with the same array as was initiallized - * with initstate(). By default, the package runs with 128 bytes of state - * information and generates far better random numbers than a linear - * congruential generator. If the amount of state information is less than - * 32 bytes, a simple linear congruential R.N.G. is used. - * - * Internally, the state information is treated as an array of int32_t; the - * zeroeth element of the array is the type of R.N.G. being used (small - * integer); the remainder of the array is the state information for the - * R.N.G. Thus, 32 bytes of state information will give 7 int32_ts worth of - * state information, which will allow a degree seven polynomial. (Note: - * the zeroeth word of state information also has some other information - * stored in it -- see setstate() for details). - * - * The random number generation technique is a linear feedback shift register - * approach, employing trinomials (since there are fewer terms to sum up that - * way). In this approach, the least significant bit of all the numbers in - * the state table will act as a linear feedback shift register, and will - * have period 2^deg - 1 (where deg is the degree of the polynomial being - * used, assuming that the polynomial is irreducible and primitive). The - * higher order bits will have longer periods, since their values are also - * influenced by pseudo-random carries out of the lower bits. The total - * period of the generator is approximately deg*(2**deg - 1); thus doubling - * the amount of state information has a vast influence on the period of the - * generator. Note: the deg*(2**deg - 1) is an approximation only good for - * large deg, when the period of the shift register is the dominant factor. - * With deg equal to seven, the period is actually much longer than the - * 7*(2**7 - 1) predicted by this formula. - */ - -/* - * For each of the currently supported random number generators, we have a - * break value on the amount of state information (you need at least this - * many bytes of state info to support this random number generator), a degree - * for the polynomial (actually a trinomial) that the R.N.G. is based on, and - * the separation between the two lower order coefficients of the trinomial. - */ -#define TYPE_0 0 /* linear congruential */ -#define BREAK_0 8 -#define DEG_0 0 -#define SEP_0 0 - -#define TYPE_1 1 /* x**7 + x**3 + 1 */ -#define BREAK_1 32 -#define DEG_1 7 -#define SEP_1 3 - -#define TYPE_2 2 /* x**15 + x + 1 */ -#define BREAK_2 64 -#define DEG_2 15 -#define SEP_2 1 - -#define TYPE_3 3 /* x**31 + x**3 + 1 */ -#define BREAK_3 128 -#define DEG_3 31 -#define SEP_3 3 - -#define TYPE_4 4 /* x**63 + x + 1 */ -#define BREAK_4 256 -#define DEG_4 63 -#define SEP_4 1 - -/* - * Array versions of the above information to make code run faster -- - * relies on fact that TYPE_i == i. - */ -#define MAX_TYPES 5 /* max number of types above */ - -static int degrees[MAX_TYPES] = { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }; -static int seps [MAX_TYPES] = { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 }; - -/* - * Initially, everything is set up as if from: - * - * initstate(1, &randtbl, 128); - * - * Note that this initialization takes advantage of the fact that srandom() - * advances the front and rear pointers 10*rand_deg times, and hence the - * rear pointer which starts at 0 will also end up at zero; thus the zeroeth - * element of the state information, which contains info about the current - * position of the rear pointer is just - * - * MAX_TYPES * (rptr - state) + TYPE_3 == TYPE_3. - */ - -static int32_t randtbl[DEG_3 + 1] = { - TYPE_3, - 0x991539b1, 0x16a5bce3, 0x6774a4cd, 0x3e01511e, 0x4e508aaa, 0x61048c05, - 0xf5500617, 0x846b7115, 0x6a19892c, 0x896a97af, 0xdb48f936, 0x14898454, - 0x37ffd106, 0xb58bff9c, 0x59e17104, 0xcf918a49, 0x09378c83, 0x52c7a471, - 0x8d293ea9, 0x1f4fc301, 0xc3db71be, 0x39b44e1c, 0xf8a44ef9, 0x4c8b80b1, - 0x19edc328, 0x87bf4bdd, 0xc9b240e5, 0xe9ee4b1b, 0x4382aee7, 0x535b6b41, - 0xf3bec5da, -}; - -/* - * fptr and rptr are two pointers into the state info, a front and a rear - * pointer. These two pointers are always rand_sep places aparts, as they - * cycle cyclically through the state information. (Yes, this does mean we - * could get away with just one pointer, but the code for random() is more - * efficient this way). The pointers are left positioned as they would be - * from the call - * - * initstate(1, randtbl, 128); - * - * (The position of the rear pointer, rptr, is really 0 (as explained above - * in the initialization of randtbl) because the state table pointer is set - * to point to randtbl[1] (as explained below). - */ -static int32_t *fptr = &randtbl[SEP_3 + 1]; -static int32_t *rptr = &randtbl[1]; - -/* - * The following things are the pointer to the state information table, the - * type of the current generator, the degree of the current polynomial being - * used, and the separation between the two pointers. Note that for efficiency - * of random(), we remember the first location of the state information, not - * the zeroeth. Hence it is valid to access state[-1], which is used to - * store the type of the R.N.G. Also, we remember the last location, since - * this is more efficient than indexing every time to find the address of - * the last element to see if the front and rear pointers have wrapped. - */ -static int32_t *state = &randtbl[1]; -static int32_t *end_ptr = &randtbl[DEG_3 + 1]; -static int rand_type = TYPE_3; -static int rand_deg = DEG_3; -static int rand_sep = SEP_3; - -/* - * srandom: - * - * Initialize the random number generator based on the given seed. If the - * type is the trivial no-state-information type, just remember the seed. - * Otherwise, initializes state[] based on the given "seed" via a linear - * congruential generator. Then, the pointers are set to known locations - * that are exactly rand_sep places apart. Lastly, it cycles the state - * information a given number of times to get rid of any initial dependencies - * introduced by the L.C.R.N.G. Note that the initialization of randtbl[] - * for default usage relies on values produced by this routine. - */ -void -srandom(unsigned int x) -{ - int i; - int32_t test; - div_t val; - - if (rand_type == TYPE_0) - state[0] = x; - else { - state[0] = x; - for (i = 1; i < rand_deg; i++) { - /* - * Implement the following, without overflowing 31 bits: - * - * state[i] = (16807 * state[i - 1]) % 2147483647; - * - * 2^31-1 (prime) = 2147483647 = 127773*16807+2836 - */ - val = div(state[i-1], 127773); - test = 16807 * val.rem - 2836 * val.quot; - state[i] = test + (test < 0 ? 2147483647 : 0); - } - fptr = &state[rand_sep]; - rptr = &state[0]; - for (i = 0; i < 10 * rand_deg; i++) - (void)random(); - } -} - -/* - * srandomdev: - * - * Many programs choose the seed value in a totally predictable manner. - * This often causes problems. We seed the generator using random - * data from the kernel. - * Note that this particular seeding procedure can generate states - * which are impossible to reproduce by calling srandom() with any - * value, since the succeeding terms in the state buffer are no longer - * derived from the LC algorithm applied to a fixed seed. - */ -void -srandomdev(void) -{ - int mib[2]; - size_t len; - - if (rand_type == TYPE_0) - len = sizeof(state[0]); - else - len = rand_deg * sizeof(state[0]); - - mib[0] = CTL_KERN; - mib[1] = KERN_ARND; - sysctl(mib, 2, state, &len, NULL, 0); - - if (rand_type != TYPE_0) { - fptr = &state[rand_sep]; - rptr = &state[0]; - } -} - -/* - * initstate: - * - * Initialize the state information in the given array of n bytes for future - * random number generation. Based on the number of bytes we are given, and - * the break values for the different R.N.G.'s, we choose the best (largest) - * one we can and set things up for it. srandom() is then called to - * initialize the state information. - * - * Note that on return from srandom(), we set state[-1] to be the type - * multiplexed with the current value of the rear pointer; this is so - * successive calls to initstate() won't lose this information and will be - * able to restart with setstate(). - * - * Note: the first thing we do is save the current state, if any, just like - * setstate() so that it doesn't matter when initstate is called. - * - * Returns a pointer to the old state. - */ -char * -initstate(u_int seed, char *arg_state, size_t n) -{ - char *ostate = (char *)(&state[-1]); - - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = MAX_TYPES * (rptr - state) + rand_type; - if (n < BREAK_0) - return(NULL); - if (n < BREAK_1) { - rand_type = TYPE_0; - rand_deg = DEG_0; - rand_sep = SEP_0; - } else if (n < BREAK_2) { - rand_type = TYPE_1; - rand_deg = DEG_1; - rand_sep = SEP_1; - } else if (n < BREAK_3) { - rand_type = TYPE_2; - rand_deg = DEG_2; - rand_sep = SEP_2; - } else if (n < BREAK_4) { - rand_type = TYPE_3; - rand_deg = DEG_3; - rand_sep = SEP_3; - } else { - rand_type = TYPE_4; - rand_deg = DEG_4; - rand_sep = SEP_4; - } - state = &(((int32_t *)arg_state)[1]); /* first location */ - end_ptr = &state[rand_deg]; /* must set end_ptr before srandom */ - srandom(seed); - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = MAX_TYPES*(rptr - state) + rand_type; - return(ostate); -} - -/* - * setstate: - * - * Restore the state from the given state array. - * - * Note: it is important that we also remember the locations of the pointers - * in the current state information, and restore the locations of the pointers - * from the old state information. This is done by multiplexing the pointer - * location into the zeroeth word of the state information. - * - * Note that due to the order in which things are done, it is OK to call - * setstate() with the same state as the current state. - * - * Returns a pointer to the old state information. - */ -char * -setstate(const char *arg_state) -{ - int32_t *new_state = (int32_t *)arg_state; - int32_t type = new_state[0] % MAX_TYPES; - int32_t rear = new_state[0] / MAX_TYPES; - char *ostate = (char *)(&state[-1]); - - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = MAX_TYPES * (rptr - state) + rand_type; - switch(type) { - case TYPE_0: - case TYPE_1: - case TYPE_2: - case TYPE_3: - case TYPE_4: - rand_type = type; - rand_deg = degrees[type]; - rand_sep = seps[type]; - break; - default: - return(NULL); - } - state = &new_state[1]; - if (rand_type != TYPE_0) { - rptr = &state[rear]; - fptr = &state[(rear + rand_sep) % rand_deg]; - } - end_ptr = &state[rand_deg]; /* set end_ptr too */ - return(ostate); -} - -/* - * random: - * - * If we are using the trivial TYPE_0 R.N.G., just do the old linear - * congruential bit. Otherwise, we do our fancy trinomial stuff, which is - * the same in all the other cases due to all the global variables that have - * been set up. The basic operation is to add the number at the rear pointer - * into the one at the front pointer. Then both pointers are advanced to - * the next location cyclically in the table. The value returned is the sum - * generated, reduced to 31 bits by throwing away the "least random" low bit. - * - * Note: the code takes advantage of the fact that both the front and - * rear pointers can't wrap on the same call by not testing the rear - * pointer if the front one has wrapped. - * - * Returns a 31-bit random number. - */ -long -random(void) -{ - int32_t i; - - if (rand_type == TYPE_0) - i = state[0] = (state[0] * 1103515245 + 12345) & 0x7fffffff; - else { - *fptr += *rptr; - i = (*fptr >> 1) & 0x7fffffff; /* chucking least random bit */ - if (++fptr >= end_ptr) { - fptr = state; - ++rptr; - } else if (++rptr >= end_ptr) - rptr = state; - } - return((long)i); -} diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3 deleted file mode 100644 index c64fc3d637..0000000000 --- a/src/lib/libc/stdlib/realpath.3 +++ /dev/null @@ -1,117 +0,0 @@ -.\" Copyright (c) 1994 -.\" The Regents of the University of California. All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Jan-Simon Pendry. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: realpath.3,v 1.15 2007/07/06 15:42:04 millert Exp $ -.\" -.Dd $Mdocdate: July 6 2007 $ -.Dt REALPATH 3 -.Os -.Sh NAME -.Nm realpath -.Nd returns the canonicalized absolute pathname -.Sh SYNOPSIS -.Fd #include -.Fd #include -.Ft "char *" -.Fn realpath "const char *pathname" "char resolved[PATH_MAX]" -.Sh DESCRIPTION -The -.Fn realpath -function resolves all symbolic links, extra -.Dq / -characters and references to -.Pa /./ -and -.Pa /../ -in -.Fa pathname , -and copies the resulting absolute pathname into the memory referenced by -.Fa resolved . -The -.Fa resolved -argument -.Em must -refer to a buffer capable of storing at least -.Dv PATH_MAX -characters. -.Pp -The -.Fn realpath -function will resolve both absolute and relative paths -and return the absolute pathname corresponding to -.Fa pathname . -All but the last component of -.Fa pathname -must exist when -.Fn realpath -is called. -.Sh RETURN VALUES -The -.Fn realpath -function returns -.Fa resolved -on success. -If an error occurs, -.Fn realpath -returns -.Dv NULL -and the contents of -.Fa resolved -are undefined. -.Sh ERRORS -The function -.Fn realpath -may fail and set the external variable -.Va errno -for any of the errors specified for the library functions -.Xr lstat 2 , -.Xr readlink 2 , -and -.Xr getcwd 3 . -.Sh SEE ALSO -.Xr readlink 1 , -.Xr getcwd 3 -.Sh HISTORY -The -.Fn realpath -function call first appeared in -.Bx 4.4 . -.Sh CAVEATS -This implementation of -.Fn realpath -differs slightly from the Solaris implementation. -The -.Bx 4.4 -version always returns absolute pathnames, -whereas the Solaris implementation will, -under certain circumstances, return a relative -.Fa resolved -when given a relative -.Fa pathname . diff --git a/src/lib/libc/stdlib/realpath.c b/src/lib/libc/stdlib/realpath.c deleted file mode 100644 index 4cb847b313..0000000000 --- a/src/lib/libc/stdlib/realpath.c +++ /dev/null @@ -1,190 +0,0 @@ -/* $OpenBSD: realpath.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 2003 Constantin S. Svintsoff - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The names of the authors may not be used to endorse or promote - * products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -#include -#include -#include -#include - -/* - * char *realpath(const char *path, char resolved[PATH_MAX]); - * - * Find the real name of path, by removing all ".", ".." and symlink - * components. Returns (resolved) on success, or (NULL) on failure, - * in which case the path which caused trouble is left in (resolved). - */ -char * -realpath(const char *path, char resolved[PATH_MAX]) -{ - struct stat sb; - char *p, *q, *s; - size_t left_len, resolved_len; - unsigned symlinks; - int serrno, slen; - char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; - - serrno = errno; - symlinks = 0; - if (path[0] == '/') { - resolved[0] = '/'; - resolved[1] = '\0'; - if (path[1] == '\0') - return (resolved); - resolved_len = 1; - left_len = strlcpy(left, path + 1, sizeof(left)); - } else { - if (getcwd(resolved, PATH_MAX) == NULL) { - strlcpy(resolved, ".", PATH_MAX); - return (NULL); - } - resolved_len = strlen(resolved); - left_len = strlcpy(left, path, sizeof(left)); - } - if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) { - errno = ENAMETOOLONG; - return (NULL); - } - - /* - * Iterate over path components in `left'. - */ - while (left_len != 0) { - /* - * Extract the next path component and adjust `left' - * and its length. - */ - p = strchr(left, '/'); - s = p ? p : left + left_len; - if (s - left >= sizeof(next_token)) { - errno = ENAMETOOLONG; - return (NULL); - } - memcpy(next_token, left, s - left); - next_token[s - left] = '\0'; - left_len -= s - left; - if (p != NULL) - memmove(left, s + 1, left_len + 1); - if (resolved[resolved_len - 1] != '/') { - if (resolved_len + 1 >= PATH_MAX) { - errno = ENAMETOOLONG; - return (NULL); - } - resolved[resolved_len++] = '/'; - resolved[resolved_len] = '\0'; - } - if (next_token[0] == '\0') - continue; - else if (strcmp(next_token, ".") == 0) - continue; - else if (strcmp(next_token, "..") == 0) { - /* - * Strip the last path component except when we have - * single "/" - */ - if (resolved_len > 1) { - resolved[resolved_len - 1] = '\0'; - q = strrchr(resolved, '/') + 1; - *q = '\0'; - resolved_len = q - resolved; - } - continue; - } - - /* - * Append the next path component and lstat() it. If - * lstat() fails we still can return successfully if - * there are no more path components left. - */ - resolved_len = strlcat(resolved, next_token, PATH_MAX); - if (resolved_len >= PATH_MAX) { - errno = ENAMETOOLONG; - return (NULL); - } - if (lstat(resolved, &sb) != 0) { - if (errno == ENOENT && p == NULL) { - errno = serrno; - return (resolved); - } - return (NULL); - } - if (S_ISLNK(sb.st_mode)) { - if (symlinks++ > MAXSYMLINKS) { - errno = ELOOP; - return (NULL); - } - slen = readlink(resolved, symlink, sizeof(symlink) - 1); - if (slen < 0) - return (NULL); - symlink[slen] = '\0'; - if (symlink[0] == '/') { - resolved[1] = 0; - resolved_len = 1; - } else if (resolved_len > 1) { - /* Strip the last path component. */ - resolved[resolved_len - 1] = '\0'; - q = strrchr(resolved, '/') + 1; - *q = '\0'; - resolved_len = q - resolved; - } - - /* - * If there are any path components left, then - * append them to symlink. The result is placed - * in `left'. - */ - if (p != NULL) { - if (symlink[slen - 1] != '/') { - if (slen + 1 >= sizeof(symlink)) { - errno = ENAMETOOLONG; - return (NULL); - } - symlink[slen] = '/'; - symlink[slen + 1] = 0; - } - left_len = strlcat(symlink, left, sizeof(left)); - if (left_len >= sizeof(left)) { - errno = ENAMETOOLONG; - return (NULL); - } - } - left_len = strlcpy(left, symlink, sizeof(left)); - } - } - - /* - * Remove trailing slash except when the resolved pathname - * is a single "/". - */ - if (resolved_len > 1 && resolved[resolved_len - 1] == '/') - resolved[resolved_len - 1] = '\0'; - return (resolved); -} diff --git a/src/lib/libc/stdlib/remque.c b/src/lib/libc/stdlib/remque.c deleted file mode 100644 index ae249ae053..0000000000 --- a/src/lib/libc/stdlib/remque.c +++ /dev/null @@ -1,44 +0,0 @@ -/* $OpenBSD: remque.c,v 1.2 2005/08/08 08:05:37 espie Exp $ */ - -/* - * Copyright (c) 1993 John Brezak - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, - * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#include - -struct qelem { - struct qelem *q_forw; - struct qelem *q_back; -}; - -void -remque(void *element) -{ - struct qelem *e = (struct qelem *) element; - e->q_forw->q_back = e->q_back; - e->q_back->q_forw = e->q_forw; -} diff --git a/src/lib/libc/stdlib/seed48.c b/src/lib/libc/stdlib/seed48.c deleted file mode 100644 index 583262f2d5..0000000000 --- a/src/lib/libc/stdlib/seed48.c +++ /dev/null @@ -1,37 +0,0 @@ -/* $OpenBSD: seed48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -extern unsigned short __rand48_seed[3]; -extern unsigned short __rand48_mult[3]; -extern unsigned short __rand48_add; - -unsigned short * -seed48(unsigned short xseed[3]) -{ - static unsigned short sseed[3]; - - sseed[0] = __rand48_seed[0]; - sseed[1] = __rand48_seed[1]; - sseed[2] = __rand48_seed[2]; - __rand48_seed[0] = xseed[0]; - __rand48_seed[1] = xseed[1]; - __rand48_seed[2] = xseed[2]; - __rand48_mult[0] = RAND48_MULT_0; - __rand48_mult[1] = RAND48_MULT_1; - __rand48_mult[2] = RAND48_MULT_2; - __rand48_add = RAND48_ADD; - return sseed; -} diff --git a/src/lib/libc/stdlib/setenv.c b/src/lib/libc/stdlib/setenv.c deleted file mode 100644 index 36540ebb0c..0000000000 --- a/src/lib/libc/stdlib/setenv.c +++ /dev/null @@ -1,103 +0,0 @@ -/* $OpenBSD: setenv.c,v 1.9 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1987 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include - -char *__findenv(const char *name, int *offset); - -extern char **environ; - -/* - * setenv -- - * Set the value of the environmental variable "name" to be - * "value". If rewrite is set, replace any current value. - */ -int -setenv(const char *name, const char *value, int rewrite) -{ - static char **lastenv; /* last value of environ */ - char *C; - int l_value, offset; - - if (*value == '=') /* no `=' in value */ - ++value; - l_value = strlen(value); - if ((C = __findenv(name, &offset))) { /* find if already exists */ - if (!rewrite) - return (0); - if (strlen(C) >= l_value) { /* old larger; copy over */ - while ((*C++ = *value++)) - ; - return (0); - } - } else { /* create new slot */ - size_t cnt; - char **P; - - for (P = environ; *P != NULL; P++) - ; - cnt = P - environ; - P = (char **)realloc(lastenv, sizeof(char *) * (cnt + 2)); - if (!P) - return (-1); - if (lastenv != environ) - memcpy(P, environ, cnt * sizeof(char *)); - lastenv = environ = P; - offset = cnt; - environ[cnt + 1] = NULL; - } - for (C = (char *)name; *C && *C != '='; ++C) - ; /* no `=' in name */ - if (!(environ[offset] = /* name + `=' + value */ - malloc((size_t)((int)(C - name) + l_value + 2)))) - return (-1); - for (C = environ[offset]; (*C = *name++) && *C != '='; ++C) - ; - for (*C++ = '='; (*C++ = *value++); ) - ; - return (0); -} - -/* - * unsetenv(name) -- - * Delete environmental variable "name". - */ -void -unsetenv(const char *name) -{ - char **P; - int offset; - - while (__findenv(name, &offset)) /* if set multiple times */ - for (P = &environ[offset];; ++P) - if (!(*P = *(P + 1))) - break; -} diff --git a/src/lib/libc/stdlib/srand48.c b/src/lib/libc/stdlib/srand48.c deleted file mode 100644 index f76b6cca86..0000000000 --- a/src/lib/libc/stdlib/srand48.c +++ /dev/null @@ -1,31 +0,0 @@ -/* $OpenBSD: srand48.c,v 1.3 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1993 Martin Birgmeier - * All rights reserved. - * - * You may redistribute unmodified or modified versions of this source - * code provided that the above copyright notice and this and the - * following conditions are retained. - * - * This software is provided ``as is'', and comes with no warranties - * of any kind. I shall in no event be liable for anything that happens - * to anyone/anything when using this software. - */ - -#include "rand48.h" - -extern unsigned short __rand48_seed[3]; -extern unsigned short __rand48_mult[3]; -extern unsigned short __rand48_add; - -void -srand48(long seed) -{ - __rand48_seed[0] = RAND48_SEED_0; - __rand48_seed[1] = (unsigned short) seed; - __rand48_seed[2] = (unsigned short) (seed >> 16); - __rand48_mult[0] = RAND48_MULT_0; - __rand48_mult[1] = RAND48_MULT_1; - __rand48_mult[2] = RAND48_MULT_2; - __rand48_add = RAND48_ADD; -} diff --git a/src/lib/libc/stdlib/strtod.3 b/src/lib/libc/stdlib/strtod.3 deleted file mode 100644 index 979334203e..0000000000 --- a/src/lib/libc/stdlib/strtod.3 +++ /dev/null @@ -1,129 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: strtod.3,v 1.12 2008/06/13 23:50:49 jmc Exp $ -.\" -.Dd $Mdocdate: June 13 2008 $ -.Dt STRTOD 3 -.Os -.Sh NAME -.Nm strtod , -.Nm strtof -.Nd convert -.Tn ASCII -string to double or float -.Sh SYNOPSIS -.Fd #include -.Fd #include -.Ft double -.Fn strtod "const char *nptr" "char **endptr" -.Pp -.Ft float -.Fn strtof "const char *nptr" "char **endptr" -.Sh DESCRIPTION -The -.Fn strtod -function converts the initial portion of the string pointed to by -.Fa nptr -to -.Li double -representation. -The -.Fn strtof -function converts the initial portion of the string pointed to by -.Fa nptr -to -.Li float -representation. -.Pp -The expected form of the string is an optional plus -.Pq Ql + -or minus sign -.Pq Ql - -followed by a sequence of digits optionally containing -a decimal-point character, optionally followed by an exponent. -An exponent consists of an -.Sq E -or -.Sq e , -followed by an optional plus or minus sign, followed by a sequence of digits. -.Pp -Leading whitespace characters in the string (as defined by the -.Xr isspace 3 -function) are skipped. -.Sh RETURN VALUES -The -.Fn strtod -and -.Fn strtof -functions return the converted value, if any. -.Pp -If -.Fa endptr -is not -.Dv NULL , -a pointer to the character after the last character used -in the conversion is stored in the location referenced by -.Fa endptr . -.Pp -If no conversion is performed, zero is returned and the value of -.Fa nptr -is stored in the location referenced by -.Fa endptr . -.Pp -If the correct value would cause overflow, plus or minus -.Dv HUGE_VAL -is returned (according to the sign of the value), and -.Er ERANGE -is stored in -.Va errno . -If the correct value would cause underflow, zero is returned and -.Er ERANGE -is stored in -.Va errno . -.Sh ERRORS -.Bl -tag -width Er -.It Bq Er ERANGE -Overflow or underflow occurred. -.El -.Sh SEE ALSO -.Xr atof 3 , -.Xr atoi 3 , -.Xr atol 3 , -.Xr strtol 3 , -.Xr strtoul 3 -.Sh STANDARDS -The -.Fn strtod -and -.Fn strtof -functions conform to -.St -ansiC . diff --git a/src/lib/libc/stdlib/strtod.c b/src/lib/libc/stdlib/strtod.c deleted file mode 100644 index 4dc3d65a26..0000000000 --- a/src/lib/libc/stdlib/strtod.c +++ /dev/null @@ -1,2459 +0,0 @@ -/* $OpenBSD: strtod.c,v 1.30 2006/10/30 18:30:52 deraadt Exp $ */ -/**************************************************************** - * - * The author of this software is David M. Gay. - * - * Copyright (c) 1991 by AT&T. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose without fee is hereby granted, provided that this entire notice - * is included in all copies of any software which is or includes a copy - * or modification of this software and in all copies of the supporting - * documentation for such software. - * - * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED - * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR AT&T MAKES ANY - * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY - * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. - * - ***************************************************************/ - -/* Please send bug reports to - David M. Gay - AT&T Bell Laboratories, Room 2C-463 - 600 Mountain Avenue - Murray Hill, NJ 07974-2070 - U.S.A. - dmg@research.att.com or research!dmg - */ - -/* strtod for IEEE-, VAX-, and IBM-arithmetic machines. - * - * This strtod returns a nearest machine number to the input decimal - * string (or sets errno to ERANGE). With IEEE arithmetic, ties are - * broken by the IEEE round-even rule. Otherwise ties are broken by - * biased rounding (add half and chop). - * - * Inspired loosely by William D. Clinger's paper "How to Read Floating - * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101]. - * - * Modifications: - * - * 1. We only require IEEE, IBM, or VAX double-precision - * arithmetic (not IEEE double-extended). - * 2. We get by with floating-point arithmetic in a case that - * Clinger missed -- when we're computing d * 10^n - * for a small integer d and the integer n is not too - * much larger than 22 (the maximum integer k for which - * we can represent 10^k exactly), we may be able to - * compute (d*10^k) * 10^(e-k) with just one roundoff. - * 3. Rather than a bit-at-a-time adjustment of the binary - * result in the hard case, we use floating-point - * arithmetic to determine the adjustment to within - * one bit; only in really hard cases do we need to - * compute a second residual. - * 4. Because of 3., we don't need a large table of powers of 10 - * for ten-to-e (just some small tables, e.g. of 10^k - * for 0 <= k <= 22). - */ - -/* - * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least - * significant byte has the lowest address. - * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most - * significant byte has the lowest address. - * #define Long int on machines with 32-bit ints and 64-bit longs. - * #define Sudden_Underflow for IEEE-format machines without gradual - * underflow (i.e., that flush to zero on underflow). - * #define IBM for IBM mainframe-style floating-point arithmetic. - * #define VAX for VAX-style floating-point arithmetic. - * #define Unsigned_Shifts if >> does treats its left operand as unsigned. - * #define No_leftright to omit left-right logic in fast floating-point - * computation of dtoa. - * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3. - * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines - * that use extended-precision instructions to compute rounded - * products and quotients) with IBM. - * #define ROUND_BIASED for IEEE-format with biased rounding. - * #define Inaccurate_Divide for IEEE-format with correctly rounded - * products but inaccurate quotients, e.g., for Intel i860. - * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision - * integer arithmetic. Whether this speeds things up or slows things - * down depends on the machine and the number being converted. - * #define Bad_float_h if your system lacks a float.h or if it does not - * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP, - * FLT_RADIX, FLT_ROUNDS, and DBL_MAX. - * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n) - * if memory is available and otherwise does something you deem - * appropriate. If MALLOC is undefined, malloc will be invoked - * directly -- and assumed always to succeed. - */ - -#include -#if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \ - defined(__mips__) || defined(__mips64__) || defined(__ns32k__) || \ - defined(__alpha__) || defined(__powerpc__) || defined(__m88k__) || \ - defined(__hppa__) || defined(__amd64__) || defined(__sh__) || \ - defined(__sparc64__) || \ - (defined(__arm__) && defined(__VFP_FP__)) - -#if BYTE_ORDER == BIG_ENDIAN -#define IEEE_BIG_ENDIAN -#else -#define IEEE_LITTLE_ENDIAN -#endif -#endif - -#if defined(__arm__) && !defined(__VFP_FP__) -/* - * Although the CPU is little endian the FP has different - * byte and word endianness. The byte order is still little endian - * but the word order is big endian. - */ -#define IEEE_BIG_ENDIAN -#endif - -#ifdef __vax__ -#define VAX -#endif - -#define Long int32_t -#define ULong u_int32_t - -#ifdef DEBUG -#include "stdio.h" -#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);} -#endif - -#include "thread_private.h" - -_THREAD_PRIVATE_KEY(dtoa); -_THREAD_PRIVATE_KEY(pow5mult); - -#ifdef __cplusplus -#include "malloc.h" -#include "memory.h" -#else -#include "stdlib.h" -#include "string.h" -#include "locale.h" -#endif - -#ifdef MALLOC -extern void *MALLOC(size_t); -#else -#define MALLOC malloc -#endif - -#include "ctype.h" -#include "errno.h" - -#ifdef Bad_float_h -#ifdef IEEE_BIG_ENDIAN -#define IEEE_ARITHMETIC -#endif -#ifdef IEEE_LITTLE_ENDIAN -#define IEEE_ARITHMETIC -#endif - -#ifdef IEEE_ARITHMETIC -#define DBL_DIG 15 -#define DBL_MAX_10_EXP 308 -#define DBL_MAX_EXP 1024 -#define FLT_RADIX 2 -#define FLT_ROUNDS 1 -#define DBL_MAX 1.7976931348623157e+308 -#endif - -#ifdef IBM -#define DBL_DIG 16 -#define DBL_MAX_10_EXP 75 -#define DBL_MAX_EXP 63 -#define FLT_RADIX 16 -#define FLT_ROUNDS 0 -#define DBL_MAX 7.2370055773322621e+75 -#endif - -#ifdef VAX -#define DBL_DIG 16 -#define DBL_MAX_10_EXP 38 -#define DBL_MAX_EXP 127 -#define FLT_RADIX 2 -#define FLT_ROUNDS 1 -#define DBL_MAX 1.7014118346046923e+38 -#endif - -#ifndef LONG_MAX -#define LONG_MAX 2147483647 -#endif -#else -#include "float.h" -#endif -#ifndef __MATH_H__ -#include "math.h" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef CONST -#define CONST const -#endif - -#ifdef Unsigned_Shifts -#define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000; -#else -#define Sign_Extend(a,b) /*no-op*/ -#endif - -#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) + defined(VAX) + \ - defined(IBM) != 1 -#error Exactly one of IEEE_LITTLE_ENDIAN IEEE_BIG_ENDIAN, VAX, or IBM should be defined. -#endif - -typedef union { - double d; - ULong ul[2]; -} _double; -#define value(x) ((x).d) -#ifdef IEEE_LITTLE_ENDIAN -#define word0(x) ((x).ul[1]) -#define word1(x) ((x).ul[0]) -#else -#define word0(x) ((x).ul[0]) -#define word1(x) ((x).ul[1]) -#endif - -/* The following definition of Storeinc is appropriate for MIPS processors. - * An alternative that might be better on some machines is - * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff) - */ -#if defined(IEEE_LITTLE_ENDIAN) + defined(VAX) + defined(__arm__) -#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \ -((unsigned short *)a)[0] = (unsigned short)c, a++) -#else -#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \ -((unsigned short *)a)[1] = (unsigned short)c, a++) -#endif - -/* #define P DBL_MANT_DIG */ -/* Ten_pmax = floor(P*log(2)/log(5)) */ -/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */ -/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */ -/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */ - -#if defined(IEEE_LITTLE_ENDIAN) + defined(IEEE_BIG_ENDIAN) -#define Exp_shift 20 -#define Exp_shift1 20 -#define Exp_msk1 0x100000 -#define Exp_msk11 0x100000 -#define Exp_mask 0x7ff00000 -#define P 53 -#define Bias 1023 -#define IEEE_Arith -#define Emin (-1022) -#define Exp_1 0x3ff00000 -#define Exp_11 0x3ff00000 -#define Ebits 11 -#define Frac_mask 0xfffff -#define Frac_mask1 0xfffff -#define Ten_pmax 22 -#define Bletch 0x10 -#define Bndry_mask 0xfffff -#define Bndry_mask1 0xfffff -#define LSB 1 -#define Sign_bit 0x80000000 -#define Log2P 1 -#define Tiny0 0 -#define Tiny1 1 -#define Quick_max 14 -#define Int_max 14 -#define Infinite(x) (word0(x) == 0x7ff00000) /* sufficient test for here */ -#else -#undef Sudden_Underflow -#define Sudden_Underflow -#ifdef IBM -#define Exp_shift 24 -#define Exp_shift1 24 -#define Exp_msk1 0x1000000 -#define Exp_msk11 0x1000000 -#define Exp_mask 0x7f000000 -#define P 14 -#define Bias 65 -#define Exp_1 0x41000000 -#define Exp_11 0x41000000 -#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */ -#define Frac_mask 0xffffff -#define Frac_mask1 0xffffff -#define Bletch 4 -#define Ten_pmax 22 -#define Bndry_mask 0xefffff -#define Bndry_mask1 0xffffff -#define LSB 1 -#define Sign_bit 0x80000000 -#define Log2P 4 -#define Tiny0 0x100000 -#define Tiny1 0 -#define Quick_max 14 -#define Int_max 15 -#else /* VAX */ -#define Exp_shift 23 -#define Exp_shift1 7 -#define Exp_msk1 0x80 -#define Exp_msk11 0x800000 -#define Exp_mask 0x7f80 -#define P 56 -#define Bias 129 -#define Exp_1 0x40800000 -#define Exp_11 0x4080 -#define Ebits 8 -#define Frac_mask 0x7fffff -#define Frac_mask1 0xffff007f -#define Ten_pmax 24 -#define Bletch 2 -#define Bndry_mask 0xffff007f -#define Bndry_mask1 0xffff007f -#define LSB 0x10000 -#define Sign_bit 0x8000 -#define Log2P 1 -#define Tiny0 0x80 -#define Tiny1 0 -#define Quick_max 15 -#define Int_max 15 -#endif -#endif - -#ifndef IEEE_Arith -#define ROUND_BIASED -#endif - -#ifdef RND_PRODQUOT -#define rounded_product(a,b) a = rnd_prod(a, b) -#define rounded_quotient(a,b) a = rnd_quot(a, b) -extern double rnd_prod(double, double), rnd_quot(double, double); -#else -#define rounded_product(a,b) a *= b -#define rounded_quotient(a,b) a /= b -#endif - -#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1)) -#define Big1 0xffffffff - -#ifndef Just_16 -/* When Pack_32 is not defined, we store 16 bits per 32-bit Long. - * This makes some inner loops simpler and sometimes saves work - * during multiplications, but it often seems to make things slightly - * slower. Hence the default is now to store 32 bits per Long. - */ -#ifndef Pack_32 -#define Pack_32 -#endif -#endif - -#define Kmax 15 - -#ifdef __cplusplus -extern "C" double strtod(const char *s00, char **se); -extern "C" char *__dtoa(double d, int mode, int ndigits, - int *decpt, int *sign, char **rve); -#endif - - struct -Bigint { - struct Bigint *next; - int k, maxwds, sign, wds; - ULong x[1]; - }; - - typedef struct Bigint Bigint; - - static Bigint *freelist[Kmax+1]; - -#define PRIVATE_MEM 2304 -#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double)) - static double private_mem[PRIVATE_mem], *pmem_next = private_mem; - - static Bigint * -Balloc(int k) -{ - int x; - unsigned int len; - Bigint *rv; - - _THREAD_PRIVATE_MUTEX_LOCK(dtoa); - if ((rv = freelist[k])) { - freelist[k] = rv->next; - } - else { - x = 1 << k; - len = (sizeof(Bigint) + (x-1)*sizeof(Long) + sizeof(double) - 1) - /sizeof(double); - if (pmem_next - private_mem + len <= PRIVATE_mem) { - rv = (Bigint *)pmem_next; - pmem_next += len; - } - else - rv = (Bigint *)MALLOC(len *sizeof(double)); - rv->k = k; - rv->maxwds = x; - } - _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa); - rv->sign = rv->wds = 0; - return rv; - } - - static void -Bfree(Bigint *v) -{ - if (v) { - _THREAD_PRIVATE_MUTEX_LOCK(dtoa); - v->next = freelist[v->k]; - freelist[v->k] = v; - _THREAD_PRIVATE_MUTEX_UNLOCK(dtoa); - } - } - -#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \ -y->wds*sizeof(Long) + 2*sizeof(int)) - -/* return value is only used as a simple string, so mis-aligned parts - * inside the Bigint are not at risk on strict align architectures - */ - static char * -rv_alloc(int i) -{ - int j, k, *r; - - j = sizeof(ULong); - for(k = 0; - sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i; - j <<= 1) - k++; - r = (int*)Balloc(k); - *r = k; - return (char *)(r+1); - } - - static char * -nrv_alloc(char *s, char **rve, int n) -{ - char *rv, *t; - - t = rv = rv_alloc(n); - while((*t = *s++) !=0) - t++; - if (rve) - *rve = t; - return rv; - } - - void -__freedtoa(char *s) -{ - Bigint *b = (Bigint *)((int *)s - 1); - b->maxwds = 1 << (b->k = *(int*)b); - Bfree(b); - } - - static Bigint * -multadd(Bigint *b, int m, int a) /* multiply by m and add a */ -{ - int i, wds; - ULong *x, y; -#ifdef Pack_32 - ULong xi, z; -#endif - Bigint *b1; - - wds = b->wds; - x = b->x; - i = 0; - do { -#ifdef Pack_32 - xi = *x; - y = (xi & 0xffff) * m + a; - z = (xi >> 16) * m + (y >> 16); - a = (int)(z >> 16); - *x++ = (z << 16) + (y & 0xffff); -#else - y = *x * m + a; - a = (int)(y >> 16); - *x++ = y & 0xffff; -#endif - } - while(++i < wds); - if (a) { - if (wds >= b->maxwds) { - b1 = Balloc(b->k+1); - Bcopy(b1, b); - Bfree(b); - b = b1; - } - b->x[wds++] = a; - b->wds = wds; - } - return b; - } - - static Bigint * -s2b(CONST char *s, int nd0, int nd, ULong y9) -{ - Bigint *b; - int i, k; - Long x, y; - - x = (nd + 8) / 9; - for(k = 0, y = 1; x > y; y <<= 1, k++) ; -#ifdef Pack_32 - b = Balloc(k); - b->x[0] = y9; - b->wds = 1; -#else - b = Balloc(k+1); - b->x[0] = y9 & 0xffff; - b->wds = (b->x[1] = y9 >> 16) ? 2 : 1; -#endif - - i = 9; - if (9 < nd0) { - s += 9; - do b = multadd(b, 10, *s++ - '0'); - while(++i < nd0); - s++; - } - else - s += 10; - for(; i < nd; i++) - b = multadd(b, 10, *s++ - '0'); - return b; - } - - static int -hi0bits(ULong x) -{ - int k = 0; - - if (!(x & 0xffff0000)) { - k = 16; - x <<= 16; - } - if (!(x & 0xff000000)) { - k += 8; - x <<= 8; - } - if (!(x & 0xf0000000)) { - k += 4; - x <<= 4; - } - if (!(x & 0xc0000000)) { - k += 2; - x <<= 2; - } - if (!(x & 0x80000000)) { - k++; - if (!(x & 0x40000000)) - return 32; - } - return k; - } - - static int -lo0bits(ULong *y) -{ - int k; - ULong x = *y; - - if (x & 7) { - if (x & 1) - return 0; - if (x & 2) { - *y = x >> 1; - return 1; - } - *y = x >> 2; - return 2; - } - k = 0; - if (!(x & 0xffff)) { - k = 16; - x >>= 16; - } - if (!(x & 0xff)) { - k += 8; - x >>= 8; - } - if (!(x & 0xf)) { - k += 4; - x >>= 4; - } - if (!(x & 0x3)) { - k += 2; - x >>= 2; - } - if (!(x & 1)) { - k++; - x >>= 1; - if (!(x & 1)) - return 32; - } - *y = x; - return k; - } - - static Bigint * -i2b(int i) -{ - Bigint *b; - - b = Balloc(1); - b->x[0] = i; - b->wds = 1; - return b; - } - - static Bigint * -mult(Bigint *a, Bigint *b) -{ - Bigint *c; - int k, wa, wb, wc; - ULong carry, y, z; - ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0; -#ifdef Pack_32 - ULong z2; -#endif - - if (a->wds < b->wds) { - c = a; - a = b; - b = c; - } - k = a->k; - wa = a->wds; - wb = b->wds; - wc = wa + wb; - if (wc > a->maxwds) - k++; - c = Balloc(k); - for(x = c->x, xa = x + wc; x < xa; x++) - *x = 0; - xa = a->x; - xae = xa + wa; - xb = b->x; - xbe = xb + wb; - xc0 = c->x; -#ifdef Pack_32 - for(; xb < xbe; xb++, xc0++) { - if ((y = *xb & 0xffff)) { - x = xa; - xc = xc0; - carry = 0; - do { - z = (*x & 0xffff) * y + (*xc & 0xffff) + carry; - carry = z >> 16; - z2 = (*x++ >> 16) * y + (*xc >> 16) + carry; - carry = z2 >> 16; - Storeinc(xc, z2, z); - } - while(x < xae); - *xc = carry; - } - if ((y = *xb >> 16)) { - x = xa; - xc = xc0; - carry = 0; - z2 = *xc; - do { - z = (*x & 0xffff) * y + (*xc >> 16) + carry; - carry = z >> 16; - Storeinc(xc, z, z2); - z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry; - carry = z2 >> 16; - } - while(x < xae); - *xc = z2; - } - } -#else - for(; xb < xbe; xc0++) { - if (y = *xb++) { - x = xa; - xc = xc0; - carry = 0; - do { - z = *x++ * y + *xc + carry; - carry = z >> 16; - *xc++ = z & 0xffff; - } - while(x < xae); - *xc = carry; - } - } -#endif - for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ; - c->wds = wc; - return c; - } - - static Bigint *p5s; - - static Bigint * -pow5mult(Bigint *b, int k) -{ - Bigint *b1, *p5, *p51; - int i; - static int p05[3] = { 5, 25, 125 }; - - if ((i = k & 3)) - b = multadd(b, p05[i-1], 0); - - if (!(k >>= 2)) - return b; - if (!(p5 = p5s)) { - /* first time */ - _THREAD_PRIVATE_MUTEX_LOCK(pow5mult); - p5 = p5s = i2b(625); - p5->next = 0; - _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult); - } - for(;;) { - if (k & 1) { - b1 = mult(b, p5); - Bfree(b); - b = b1; - } - if (!(k >>= 1)) - break; - if (!(p51 = p5->next)) { - _THREAD_PRIVATE_MUTEX_LOCK(pow5mult); - if (!(p51 = p5->next)) { - p51 = p5->next = mult(p5,p5); - p51->next = 0; - } - _THREAD_PRIVATE_MUTEX_UNLOCK(pow5mult); - } - p5 = p51; - } - return b; - } - - static Bigint * -lshift(Bigint *b, int k) -{ - int i, k1, n, n1; - Bigint *b1; - ULong *x, *x1, *xe, z; - -#ifdef Pack_32 - n = k >> 5; -#else - n = k >> 4; -#endif - k1 = b->k; - n1 = n + b->wds + 1; - for(i = b->maxwds; n1 > i; i <<= 1) - k1++; - b1 = Balloc(k1); - x1 = b1->x; - for(i = 0; i < n; i++) - *x1++ = 0; - x = b->x; - xe = x + b->wds; -#ifdef Pack_32 - if (k &= 0x1f) { - k1 = 32 - k; - z = 0; - do { - *x1++ = *x << k | z; - z = *x++ >> k1; - } - while(x < xe); - if ((*x1 = z)) - ++n1; - } -#else - if (k &= 0xf) { - k1 = 16 - k; - z = 0; - do { - *x1++ = *x << k & 0xffff | z; - z = *x++ >> k1; - } - while(x < xe); - if (*x1 = z) - ++n1; - } -#endif - else do - *x1++ = *x++; - while(x < xe); - b1->wds = n1 - 1; - Bfree(b); - return b1; - } - - static int -cmp(Bigint *a, Bigint *b) -{ - ULong *xa, *xa0, *xb, *xb0; - int i, j; - - i = a->wds; - j = b->wds; -#ifdef DEBUG - if (i > 1 && !a->x[i-1]) - Bug("cmp called with a->x[a->wds-1] == 0"); - if (j > 1 && !b->x[j-1]) - Bug("cmp called with b->x[b->wds-1] == 0"); -#endif - if (i -= j) - return i; - xa0 = a->x; - xa = xa0 + j; - xb0 = b->x; - xb = xb0 + j; - for(;;) { - if (*--xa != *--xb) - return *xa < *xb ? -1 : 1; - if (xa <= xa0) - break; - } - return 0; - } - - static Bigint * -diff(Bigint *a, Bigint *b) -{ - Bigint *c; - int i, wa, wb; - Long borrow, y; /* We need signed shifts here. */ - ULong *xa, *xae, *xb, *xbe, *xc; -#ifdef Pack_32 - Long z; -#endif - - i = cmp(a,b); - if (!i) { - c = Balloc(0); - c->wds = 1; - c->x[0] = 0; - return c; - } - if (i < 0) { - c = a; - a = b; - b = c; - i = 1; - } - else - i = 0; - c = Balloc(a->k); - c->sign = i; - wa = a->wds; - xa = a->x; - xae = xa + wa; - wb = b->wds; - xb = b->x; - xbe = xb + wb; - xc = c->x; - borrow = 0; -#ifdef Pack_32 - do { - y = (*xa & 0xffff) - (*xb & 0xffff) + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - z = (*xa++ >> 16) - (*xb++ >> 16) + borrow; - borrow = z >> 16; - Sign_Extend(borrow, z); - Storeinc(xc, z, y); - } - while(xb < xbe); - while(xa < xae) { - y = (*xa & 0xffff) + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - z = (*xa++ >> 16) + borrow; - borrow = z >> 16; - Sign_Extend(borrow, z); - Storeinc(xc, z, y); - } -#else - do { - y = *xa++ - *xb++ + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - *xc++ = y & 0xffff; - } - while(xb < xbe); - while(xa < xae) { - y = *xa++ + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - *xc++ = y & 0xffff; - } -#endif - while(!*--xc) - wa--; - c->wds = wa; - return c; - } - - static double -ulp(double _x) -{ - _double x; - Long L; - _double a; - - value(x) = _x; - L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1; -#ifndef Sudden_Underflow - if (L > 0) { -#endif -#ifdef IBM - L |= Exp_msk1 >> 4; -#endif - word0(a) = L; - word1(a) = 0; -#ifndef Sudden_Underflow - } - else { - L = -L >> Exp_shift; - if (L < Exp_shift) { - word0(a) = 0x80000 >> L; - word1(a) = 0; - } - else { - word0(a) = 0; - L -= Exp_shift; - word1(a) = L >= 31 ? 1 : 1 << 31 - L; - } - } -#endif - return value(a); - } - - static double -b2d(Bigint *a, int *e) -{ - ULong *xa, *xa0, w, y, z; - int k; - _double d; -#ifdef VAX - ULong d0, d1; -#else -#define d0 word0(d) -#define d1 word1(d) -#endif - - xa0 = a->x; - xa = xa0 + a->wds; - y = *--xa; -#ifdef DEBUG - if (!y) Bug("zero y in b2d"); -#endif - k = hi0bits(y); - *e = 32 - k; -#ifdef Pack_32 - if (k < Ebits) { - d0 = Exp_1 | y >> Ebits - k; - w = xa > xa0 ? *--xa : 0; - d1 = y << (32-Ebits) + k | w >> Ebits - k; - goto ret_d; - } - z = xa > xa0 ? *--xa : 0; - if (k -= Ebits) { - d0 = Exp_1 | y << k | z >> 32 - k; - y = xa > xa0 ? *--xa : 0; - d1 = z << k | y >> 32 - k; - } - else { - d0 = Exp_1 | y; - d1 = z; - } -#else - if (k < Ebits + 16) { - z = xa > xa0 ? *--xa : 0; - d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k; - w = xa > xa0 ? *--xa : 0; - y = xa > xa0 ? *--xa : 0; - d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k; - goto ret_d; - } - z = xa > xa0 ? *--xa : 0; - w = xa > xa0 ? *--xa : 0; - k -= Ebits + 16; - d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k; - y = xa > xa0 ? *--xa : 0; - d1 = w << k + 16 | y << k; -#endif - ret_d: -#ifdef VAX - word0(d) = d0 >> 16 | d0 << 16; - word1(d) = d1 >> 16 | d1 << 16; -#else -#undef d0 -#undef d1 -#endif - return value(d); - } - - static Bigint * -d2b(double _d, int *e, int *bits) -{ - Bigint *b; - int de, i, k; - ULong *x, y, z; - _double d; -#ifdef VAX - ULong d0, d1; -#endif - - value(d) = _d; -#ifdef VAX - d0 = word0(d) >> 16 | word0(d) << 16; - d1 = word1(d) >> 16 | word1(d) << 16; -#else -#define d0 word0(d) -#define d1 word1(d) -#endif - -#ifdef Pack_32 - b = Balloc(1); -#else - b = Balloc(2); -#endif - x = b->x; - - z = d0 & Frac_mask; - d0 &= 0x7fffffff; /* clear sign bit, which we ignore */ -#ifdef Sudden_Underflow - de = (int)(d0 >> Exp_shift); -#ifndef IBM - z |= Exp_msk11; -#endif -#else - if (de = (int)(d0 >> Exp_shift)) - z |= Exp_msk1; -#endif -#ifdef Pack_32 - if (y = d1) { - if (k = lo0bits(&y)) { - x[0] = y | z << 32 - k; - z >>= k; - } - else - x[0] = y; - i = b->wds = (x[1] = z) ? 2 : 1; - } - else { -#ifdef DEBUG - if (!z) - Bug("Zero passed to d2b"); -#endif - k = lo0bits(&z); - x[0] = z; - i = b->wds = 1; - k += 32; - } -#else - if (y = d1) { - if (k = lo0bits(&y)) - if (k >= 16) { - x[0] = y | z << 32 - k & 0xffff; - x[1] = z >> k - 16 & 0xffff; - x[2] = z >> k; - i = 2; - } - else { - x[0] = y & 0xffff; - x[1] = y >> 16 | z << 16 - k & 0xffff; - x[2] = z >> k & 0xffff; - x[3] = z >> k+16; - i = 3; - } - else { - x[0] = y & 0xffff; - x[1] = y >> 16; - x[2] = z & 0xffff; - x[3] = z >> 16; - i = 3; - } - } - else { -#ifdef DEBUG - if (!z) - Bug("Zero passed to d2b"); -#endif - k = lo0bits(&z); - if (k >= 16) { - x[0] = z; - i = 0; - } - else { - x[0] = z & 0xffff; - x[1] = z >> 16; - i = 1; - } - k += 32; - } - while(!x[i]) - --i; - b->wds = i + 1; -#endif -#ifndef Sudden_Underflow - if (de) { -#endif -#ifdef IBM - *e = (de - Bias - (P-1) << 2) + k; - *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask); -#else - *e = de - Bias - (P-1) + k; - *bits = P - k; -#endif -#ifndef Sudden_Underflow - } - else { - *e = de - Bias - (P-1) + 1 + k; -#ifdef Pack_32 - *bits = 32*i - hi0bits(x[i-1]); -#else - *bits = (i+2)*16 - hi0bits(x[i]); -#endif - } -#endif - return b; - } -#undef d0 -#undef d1 - - static double -ratio(Bigint *a, Bigint *b) -{ - _double da, db; - int k, ka, kb; - - value(da) = b2d(a, &ka); - value(db) = b2d(b, &kb); -#ifdef Pack_32 - k = ka - kb + 32*(a->wds - b->wds); -#else - k = ka - kb + 16*(a->wds - b->wds); -#endif -#ifdef IBM - if (k > 0) { - word0(da) += (k >> 2)*Exp_msk1; - if (k &= 3) - da *= 1 << k; - } - else { - k = -k; - word0(db) += (k >> 2)*Exp_msk1; - if (k &= 3) - db *= 1 << k; - } -#else - if (k > 0) - word0(da) += k*Exp_msk1; - else { - k = -k; - word0(db) += k*Exp_msk1; - } -#endif - return value(da) / value(db); - } - -static CONST double -tens[] = { - 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, - 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, - 1e20, 1e21, 1e22 -#ifdef VAX - , 1e23, 1e24 -#endif - }; - -#ifdef IEEE_Arith -static CONST double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 }; -static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 }; -#define n_bigtens 5 -#else -#ifdef IBM -static CONST double bigtens[] = { 1e16, 1e32, 1e64 }; -static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 }; -#define n_bigtens 3 -#else -static CONST double bigtens[] = { 1e16, 1e32 }; -static CONST double tinytens[] = { 1e-16, 1e-32 }; -#define n_bigtens 2 -#endif -#endif - - double -strtod(CONST char *s00, char **se) -{ - int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign, - e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign; - CONST char *s, *s0, *s1; - double aadj, aadj1, adj; - /* - * volatile forces mem update for FPUs where reg size != sizeof double, - * which should trigger ERANGE in the case of underflow. - */ - volatile _double rv; - _double rv0; - Long L; - ULong y, z; - Bigint *bb, *bb1, *bd, *bd0, *bs, *delta; - - CONST char decimal_point = localeconv()->decimal_point[0]; - - sign = nz0 = nz = 0; - value(rv) = 0.; - - - for(s = s00; isspace((unsigned char) *s); s++) - ; - - if (*s == '-') { - sign = 1; - s++; - } else if (*s == '+') { - s++; - } - - if (*s == '\0') { - s = s00; - goto ret; - } - - if (*s == '0') { - nz0 = 1; - while(*++s == '0') ; - if (!*s) - goto ret; - } - s0 = s; - y = z = 0; - for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++) - if (nd < 9) - y = 10*y + c - '0'; - else if (nd < 16) - z = 10*z + c - '0'; - nd0 = nd; - if (c == decimal_point) { - c = *++s; - if (!nd) { - for(; c == '0'; c = *++s) - nz++; - if (c > '0' && c <= '9') { - s0 = s; - nf += nz; - nz = 0; - goto have_dig; - } - goto dig_done; - } - for(; c >= '0' && c <= '9'; c = *++s) { - have_dig: - nz++; - if (c -= '0') { - nf += nz; - for(i = 1; i < nz; i++) - if (nd++ < 9) - y *= 10; - else if (nd <= DBL_DIG + 1) - z *= 10; - if (nd++ < 9) - y = 10*y + c; - else if (nd <= DBL_DIG + 1) - z = 10*z + c; - nz = 0; - } - } - } - dig_done: - e = 0; - if (c == 'e' || c == 'E') { - if (!nd && !nz && !nz0) { - s = s00; - goto ret; - } - s00 = s; - esign = 0; - switch(c = *++s) { - case '-': - esign = 1; - case '+': - c = *++s; - } - if (c >= '0' && c <= '9') { - while(c == '0') - c = *++s; - if (c > '0' && c <= '9') { - L = c - '0'; - s1 = s; - while((c = *++s) >= '0' && c <= '9') - L = 10*L + c - '0'; - if (s - s1 > 8 || L > 19999) - /* Avoid confusion from exponents - * so large that e might overflow. - */ - e = 19999; /* safe for 16 bit ints */ - else - e = (int)L; - if (esign) - e = -e; - } - else - e = 0; - } - else - s = s00; - } - if (!nd) { - if (!nz && !nz0) - s = s00; - goto ret; - } - e1 = e -= nf; - - /* Now we have nd0 digits, starting at s0, followed by a - * decimal point, followed by nd-nd0 digits. The number we're - * after is the integer represented by those digits times - * 10**e */ - - if (!nd0) - nd0 = nd; - k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1; - value(rv) = y; - if (k > 9) - value(rv) = tens[k - 9] * value(rv) + z; - bd0 = 0; - if (nd <= DBL_DIG -#ifndef RND_PRODQUOT - && FLT_ROUNDS == 1 -#endif - ) { - if (!e) - goto ret; - if (e > 0) { - if (e <= Ten_pmax) { -#ifdef VAX - goto vax_ovfl_check; -#else - /* value(rv) = */ rounded_product(value(rv), - tens[e]); - goto ret; -#endif - } - i = DBL_DIG - nd; - if (e <= Ten_pmax + i) { - /* A fancier test would sometimes let us do - * this for larger i values. - */ - e -= i; - value(rv) *= tens[i]; -#ifdef VAX - /* VAX exponent range is so narrow we must - * worry about overflow here... - */ - vax_ovfl_check: - word0(rv) -= P*Exp_msk1; - /* value(rv) = */ rounded_product(value(rv), - tens[e]); - if ((word0(rv) & Exp_mask) - > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) - goto ovfl; - word0(rv) += P*Exp_msk1; -#else - /* value(rv) = */ rounded_product(value(rv), - tens[e]); -#endif - goto ret; - } - } -#ifndef Inaccurate_Divide - else if (e >= -Ten_pmax) { - /* value(rv) = */ rounded_quotient(value(rv), - tens[-e]); - goto ret; - } -#endif - } - e1 += nd - k; - - /* Get starting approximation = rv * 10**e1 */ - - if (e1 > 0) { - if (i = e1 & 15) - value(rv) *= tens[i]; - if (e1 &= ~15) { - if (e1 > DBL_MAX_10_EXP) { - ovfl: - errno = ERANGE; -#ifndef Bad_float_h - value(rv) = HUGE_VAL; -#else - /* Can't trust HUGE_VAL */ -#ifdef IEEE_Arith - word0(rv) = Exp_mask; - word1(rv) = 0; -#else - word0(rv) = Big0; - word1(rv) = Big1; -#endif -#endif - if (bd0) - goto retfree; - goto ret; - } - if (e1 >>= 4) { - for(j = 0; e1 > 1; j++, e1 >>= 1) - if (e1 & 1) - value(rv) *= bigtens[j]; - /* The last multiplication could overflow. */ - word0(rv) -= P*Exp_msk1; - value(rv) *= bigtens[j]; - if ((z = word0(rv) & Exp_mask) - > Exp_msk1*(DBL_MAX_EXP+Bias-P)) - goto ovfl; - if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) { - /* set to largest number */ - /* (Can't trust DBL_MAX) */ - word0(rv) = Big0; - word1(rv) = Big1; - } - else - word0(rv) += P*Exp_msk1; - } - - } - } - else if (e1 < 0) { - e1 = -e1; - if (i = e1 & 15) - value(rv) /= tens[i]; - if (e1 &= ~15) { - e1 >>= 4; - if (e1 >= 1 << n_bigtens) - goto undfl; - for(j = 0; e1 > 1; j++, e1 >>= 1) - if (e1 & 1) - value(rv) *= tinytens[j]; - /* The last multiplication could underflow. */ - value(rv0) = value(rv); - value(rv) *= tinytens[j]; - if (!value(rv)) { - value(rv) = 2.*value(rv0); - value(rv) *= tinytens[j]; - if (!value(rv)) { - undfl: - value(rv) = 0.; - errno = ERANGE; - if (bd0) - goto retfree; - goto ret; - } - word0(rv) = Tiny0; - word1(rv) = Tiny1; - /* The refinement below will clean - * this approximation up. - */ - } - } - } - - /* Now the hard part -- adjusting rv to the correct value.*/ - - /* Put digits into bd: true value = bd * 10^e */ - - bd0 = s2b(s0, nd0, nd, y); - - for(;;) { - bd = Balloc(bd0->k); - Bcopy(bd, bd0); - bb = d2b(value(rv), &bbe, &bbbits); /* rv = bb * 2^bbe */ - bs = i2b(1); - - if (e >= 0) { - bb2 = bb5 = 0; - bd2 = bd5 = e; - } - else { - bb2 = bb5 = -e; - bd2 = bd5 = 0; - } - if (bbe >= 0) - bb2 += bbe; - else - bd2 -= bbe; - bs2 = bb2; -#ifdef Sudden_Underflow -#ifdef IBM - j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3); -#else - j = P + 1 - bbbits; -#endif -#else - i = bbe + bbbits - 1; /* logb(rv) */ - if (i < Emin) /* denormal */ - j = bbe + (P-Emin); - else - j = P + 1 - bbbits; -#endif - bb2 += j; - bd2 += j; - i = bb2 < bd2 ? bb2 : bd2; - if (i > bs2) - i = bs2; - if (i > 0) { - bb2 -= i; - bd2 -= i; - bs2 -= i; - } - if (bb5 > 0) { - bs = pow5mult(bs, bb5); - bb1 = mult(bs, bb); - Bfree(bb); - bb = bb1; - } - if (bb2 > 0) - bb = lshift(bb, bb2); - if (bd5 > 0) - bd = pow5mult(bd, bd5); - if (bd2 > 0) - bd = lshift(bd, bd2); - if (bs2 > 0) - bs = lshift(bs, bs2); - delta = diff(bb, bd); - dsign = delta->sign; - delta->sign = 0; - i = cmp(delta, bs); - if (i < 0) { - /* Error is less than half an ulp -- check for - * special case of mantissa a power of two. - */ - if (dsign || word1(rv) || word0(rv) & Bndry_mask) - break; - delta = lshift(delta,Log2P); - if (cmp(delta, bs) > 0) - goto drop_down; - break; - } - if (i == 0) { - /* exactly half-way between */ - if (dsign) { - if ((word0(rv) & Bndry_mask1) == Bndry_mask1 - && word1(rv) == 0xffffffff) { - /*boundary case -- increment exponent*/ - word0(rv) = (word0(rv) & Exp_mask) - + Exp_msk1 -#ifdef IBM - | Exp_msk1 >> 4 -#endif - ; - word1(rv) = 0; - break; - } - } - else if (!(word0(rv) & Bndry_mask) && !word1(rv)) { - drop_down: - /* boundary case -- decrement exponent */ -#ifdef Sudden_Underflow - L = word0(rv) & Exp_mask; -#ifdef IBM - if (L < Exp_msk1) -#else - if (L <= Exp_msk1) -#endif - goto undfl; - L -= Exp_msk1; -#else - L = (word0(rv) & Exp_mask) - Exp_msk1; -#endif - word0(rv) = L | Bndry_mask1; - word1(rv) = 0xffffffff; -#ifdef IBM - goto cont; -#else - break; -#endif - } -#ifndef ROUND_BIASED - if (!(word1(rv) & LSB)) - break; -#endif - if (dsign) - value(rv) += ulp(value(rv)); -#ifndef ROUND_BIASED - else { - value(rv) -= ulp(value(rv)); -#ifndef Sudden_Underflow - if (!value(rv)) - goto undfl; -#endif - } -#endif - break; - } - if ((aadj = ratio(delta, bs)) <= 2.) { - if (dsign) - aadj = aadj1 = 1.; - else if (word1(rv) || word0(rv) & Bndry_mask) { -#ifndef Sudden_Underflow - if (word1(rv) == Tiny1 && !word0(rv)) - goto undfl; -#endif - aadj = 1.; - aadj1 = -1.; - } - else { - /* special case -- power of FLT_RADIX to be */ - /* rounded down... */ - - if (aadj < 2./FLT_RADIX) - aadj = 1./FLT_RADIX; - else - aadj *= 0.5; - aadj1 = -aadj; - } - } - else { - aadj *= 0.5; - aadj1 = dsign ? aadj : -aadj; -#ifdef Check_FLT_ROUNDS - switch(FLT_ROUNDS) { - case 2: /* towards +infinity */ - aadj1 -= 0.5; - break; - case 0: /* towards 0 */ - case 3: /* towards -infinity */ - aadj1 += 0.5; - } -#else - if (FLT_ROUNDS == 0) - aadj1 += 0.5; -#endif - } - y = word0(rv) & Exp_mask; - - /* Check for overflow */ - - if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) { - value(rv0) = value(rv); - word0(rv) -= P*Exp_msk1; - adj = aadj1 * ulp(value(rv)); - value(rv) += adj; - if ((word0(rv) & Exp_mask) >= - Exp_msk1*(DBL_MAX_EXP+Bias-P)) { - if (word0(rv0) == Big0 && word1(rv0) == Big1) - goto ovfl; - word0(rv) = Big0; - word1(rv) = Big1; - goto cont; - } - else - word0(rv) += P*Exp_msk1; - } - else { -#ifdef Sudden_Underflow - if ((word0(rv) & Exp_mask) <= P*Exp_msk1) { - value(rv0) = value(rv); - word0(rv) += P*Exp_msk1; - adj = aadj1 * ulp(value(rv)); - value(rv) += adj; -#ifdef IBM - if ((word0(rv) & Exp_mask) < P*Exp_msk1) -#else - if ((word0(rv) & Exp_mask) <= P*Exp_msk1) -#endif - { - if (word0(rv0) == Tiny0 - && word1(rv0) == Tiny1) - goto undfl; - word0(rv) = Tiny0; - word1(rv) = Tiny1; - goto cont; - } - else - word0(rv) -= P*Exp_msk1; - } - else { - adj = aadj1 * ulp(value(rv)); - value(rv) += adj; - } -#else - /* Compute adj so that the IEEE rounding rules will - * correctly round rv + adj in some half-way cases. - * If rv * ulp(rv) is denormalized (i.e., - * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid - * trouble from bits lost to denormalization; - * example: 1.2e-307 . - */ - if (y <= (P-1)*Exp_msk1 && aadj >= 1.) { - aadj1 = (double)(int)(aadj + 0.5); - if (!dsign) - aadj1 = -aadj1; - } - adj = aadj1 * ulp(value(rv)); - value(rv) += adj; -#endif - } - z = word0(rv) & Exp_mask; - if (y == z) { - /* Can we stop now? */ - L = aadj; - aadj -= L; - /* The tolerances below are conservative. */ - if (dsign || word1(rv) || word0(rv) & Bndry_mask) { - if (aadj < .4999999 || aadj > .5000001) - break; - } - else if (aadj < .4999999/FLT_RADIX) - break; - } - cont: - Bfree(bb); - Bfree(bd); - Bfree(bs); - Bfree(delta); - } - retfree: - Bfree(bb); - Bfree(bd); - Bfree(bs); - Bfree(bd0); - Bfree(delta); - ret: - if (se) - *se = (char *)s; - return sign ? -value(rv) : value(rv); - } - - static int -quorem(Bigint *b, Bigint *S) -{ - int n; - Long borrow, y; - ULong carry, q, ys; - ULong *bx, *bxe, *sx, *sxe; -#ifdef Pack_32 - Long z; - ULong si, zs; -#endif - - n = S->wds; -#ifdef DEBUG - /*debug*/ if (b->wds > n) - /*debug*/ Bug("oversize b in quorem"); -#endif - if (b->wds < n) - return 0; - sx = S->x; - sxe = sx + --n; - bx = b->x; - bxe = bx + n; - q = *bxe / (*sxe + 1); /* ensure q <= true quotient */ -#ifdef DEBUG - /*debug*/ if (q > 9) - /*debug*/ Bug("oversized quotient in quorem"); -#endif - if (q) { - borrow = 0; - carry = 0; - do { -#ifdef Pack_32 - si = *sx++; - ys = (si & 0xffff) * q + carry; - zs = (si >> 16) * q + (ys >> 16); - carry = zs >> 16; - y = (*bx & 0xffff) - (ys & 0xffff) + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - z = (*bx >> 16) - (zs & 0xffff) + borrow; - borrow = z >> 16; - Sign_Extend(borrow, z); - Storeinc(bx, z, y); -#else - ys = *sx++ * q + carry; - carry = ys >> 16; - y = *bx - (ys & 0xffff) + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - *bx++ = y & 0xffff; -#endif - } - while(sx <= sxe); - if (!*bxe) { - bx = b->x; - while(--bxe > bx && !*bxe) - --n; - b->wds = n; - } - } - if (cmp(b, S) >= 0) { - q++; - borrow = 0; - carry = 0; - bx = b->x; - sx = S->x; - do { -#ifdef Pack_32 - si = *sx++; - ys = (si & 0xffff) + carry; - zs = (si >> 16) + (ys >> 16); - carry = zs >> 16; - y = (*bx & 0xffff) - (ys & 0xffff) + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - z = (*bx >> 16) - (zs & 0xffff) + borrow; - borrow = z >> 16; - Sign_Extend(borrow, z); - Storeinc(bx, z, y); -#else - ys = *sx++ + carry; - carry = ys >> 16; - y = *bx - (ys & 0xffff) + borrow; - borrow = y >> 16; - Sign_Extend(borrow, y); - *bx++ = y & 0xffff; -#endif - } - while(sx <= sxe); - bx = b->x; - bxe = bx + n; - if (!*bxe) { - while(--bxe > bx && !*bxe) - --n; - b->wds = n; - } - } - return q; - } - -/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string. - * - * Inspired by "How to Print Floating-Point Numbers Accurately" by - * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101]. - * - * Modifications: - * 1. Rather than iterating, we use a simple numeric overestimate - * to determine k = floor(log10(d)). We scale relevant - * quantities using O(log2(k)) rather than O(k) multiplications. - * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't - * try to generate digits strictly left to right. Instead, we - * compute with fewer bits and propagate the carry if necessary - * when rounding the final digit up. This is often faster. - * 3. Under the assumption that input will be rounded nearest, - * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22. - * That is, we allow equality in stopping tests when the - * round-nearest rule will give the same floating-point value - * as would satisfaction of the stopping test with strict - * inequality. - * 4. We remove common factors of powers of 2 from relevant - * quantities. - * 5. When converting floating-point integers less than 1e16, - * we use floating-point arithmetic rather than resorting - * to multiple-precision integers. - * 6. When asked to produce fewer than 15 digits, we first try - * to get by with floating-point arithmetic; we resort to - * multiple-precision integer arithmetic only if we cannot - * guarantee that the floating-point calculation has given - * the correctly rounded result. For k requested digits and - * "uniformly" distributed input, the probability is - * something like 10^(k-15) that we must resort to the Long - * calculation. - */ - - char * -__dtoa(double _d, int mode, int ndigits, int *decpt, int *sign, char **rve) -{ - /* Arguments ndigits, decpt, sign are similar to those - of ecvt and fcvt; trailing zeros are suppressed from - the returned string. If not null, *rve is set to point - to the end of the return value. If d is +-Infinity or NaN, - then *decpt is set to 9999. - - mode: - 0 ==> shortest string that yields d when read in - and rounded to nearest. - 1 ==> like 0, but with Steele & White stopping rule; - e.g. with IEEE P754 arithmetic , mode 0 gives - 1e23 whereas mode 1 gives 9.999999999999999e22. - 2 ==> max(1,ndigits) significant digits. This gives a - return value similar to that of ecvt, except - that trailing zeros are suppressed. - 3 ==> through ndigits past the decimal point. This - gives a return value similar to that from fcvt, - except that trailing zeros are suppressed, and - ndigits can be negative. - 4-9 should give the same return values as 2-3, i.e., - 4 <= mode <= 9 ==> same return as mode - 2 + (mode & 1). These modes are mainly for - debugging; often they run slower but sometimes - faster than modes 2-3. - 4,5,8,9 ==> left-to-right digit generation. - 6-9 ==> don't try fast floating-point estimate - (if applicable). - - Values of mode other than 0-9 are treated as mode 0. - - Sufficient space is allocated to the return value - to hold the suppressed trailing zeros. - */ - - int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1, - j, j1, k, k0, k_check, leftright, m2, m5, s2, s5, - spec_case, try_quick; - Long L; -#ifndef Sudden_Underflow - int denorm; - ULong x; -#endif - Bigint *b, *b1, *delta, *mlo, *mhi, *S; - double ds; - char *s, *s0; - _double d, d2, eps; - - value(d) = _d; - - if (word0(d) & Sign_bit) { - /* set sign for everything, including 0's and NaNs */ - *sign = 1; - word0(d) &= ~Sign_bit; /* clear sign bit */ - } - else - *sign = 0; - -#if defined(IEEE_Arith) + defined(VAX) -#ifdef IEEE_Arith - if ((word0(d) & Exp_mask) == Exp_mask) -#else - if (word0(d) == 0x8000) -#endif - { - /* Infinity or NaN */ - *decpt = 9999; -#ifdef IEEE_Arith - if (!word1(d) && !(word0(d) & 0xfffff)) - return nrv_alloc("Infinity", rve, 8); -#endif - return nrv_alloc("NaN", rve, 3); - } -#endif -#ifdef IBM - value(d) += 0; /* normalize */ -#endif - if (!value(d)) { - *decpt = 1; - return nrv_alloc("0", rve, 1); - } - - b = d2b(value(d), &be, &bbits); -#ifdef Sudden_Underflow - i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1)); -#else - if (i = (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) { -#endif - value(d2) = value(d); - word0(d2) &= Frac_mask1; - word0(d2) |= Exp_11; -#ifdef IBM - if (j = 11 - hi0bits(word0(d2) & Frac_mask)) - value(d2) /= 1 << j; -#endif - - /* log(x) ~=~ log(1.5) + (x-1.5)/1.5 - * log10(x) = log(x) / log(10) - * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10)) - * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2) - * - * This suggests computing an approximation k to log10(d) by - * - * k = (i - Bias)*0.301029995663981 - * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 ); - * - * We want k to be too large rather than too small. - * The error in the first-order Taylor series approximation - * is in our favor, so we just round up the constant enough - * to compensate for any error in the multiplication of - * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077, - * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14, - * adding 1e-13 to the constant term more than suffices. - * Hence we adjust the constant term to 0.1760912590558. - * (We could get a more accurate k by invoking log10, - * but this is probably not worthwhile.) - */ - - i -= Bias; -#ifdef IBM - i <<= 2; - i += j; -#endif -#ifndef Sudden_Underflow - denorm = 0; - } - else { - /* d is denormalized */ - - i = bbits + be + (Bias + (P-1) - 1); - x = i > 32 ? word0(d) << 64 - i | word1(d) >> i - 32 - : word1(d) << 32 - i; - value(d2) = x; - word0(d2) -= 31*Exp_msk1; /* adjust exponent */ - i -= (Bias + (P-1) - 1) + 1; - denorm = 1; - } -#endif - ds = (value(d2)-1.5)*0.289529654602168 + 0.1760912590558 + - i*0.301029995663981; - k = (int)ds; - if (ds < 0. && ds != k) - k--; /* want k = floor(ds) */ - k_check = 1; - if (k >= 0 && k <= Ten_pmax) { - if (value(d) < tens[k]) - k--; - k_check = 0; - } - j = bbits - i - 1; - if (j >= 0) { - b2 = 0; - s2 = j; - } - else { - b2 = -j; - s2 = 0; - } - if (k >= 0) { - b5 = 0; - s5 = k; - s2 += k; - } - else { - b2 -= k; - b5 = -k; - s5 = 0; - } - if (mode < 0 || mode > 9) - mode = 0; - try_quick = 1; - if (mode > 5) { - mode -= 4; - try_quick = 0; - } - leftright = 1; - switch(mode) { - case 0: - case 1: - ilim = ilim1 = -1; - i = 18; - ndigits = 0; - break; - case 2: - leftright = 0; - /* no break */ - case 4: - if (ndigits <= 0) - ndigits = 1; - ilim = ilim1 = i = ndigits; - break; - case 3: - leftright = 0; - /* no break */ - case 5: - i = ndigits + k + 1; - ilim = i; - ilim1 = i - 1; - if (i <= 0) - i = 1; - } - s = s0 = rv_alloc(i); - - if (ilim >= 0 && ilim <= Quick_max && try_quick) { - - /* Try to get by with floating-point arithmetic. */ - - i = 0; - value(d2) = value(d); - k0 = k; - ilim0 = ilim; - ieps = 2; /* conservative */ - if (k > 0) { - ds = tens[k&0xf]; - j = k >> 4; - if (j & Bletch) { - /* prevent overflows */ - j &= Bletch - 1; - value(d) /= bigtens[n_bigtens-1]; - ieps++; - } - for(; j; j >>= 1, i++) - if (j & 1) { - ieps++; - ds *= bigtens[i]; - } - value(d) /= ds; - } - else if (j1 = -k) { - value(d) *= tens[j1 & 0xf]; - for(j = j1 >> 4; j; j >>= 1, i++) - if (j & 1) { - ieps++; - value(d) *= bigtens[i]; - } - } - if (k_check && value(d) < 1. && ilim > 0) { - if (ilim1 <= 0) - goto fast_failed; - ilim = ilim1; - k--; - value(d) *= 10.; - ieps++; - } - value(eps) = ieps*value(d) + 7.; - word0(eps) -= (P-1)*Exp_msk1; - if (ilim == 0) { - S = mhi = 0; - value(d) -= 5.; - if (value(d) > value(eps)) - goto one_digit; - if (value(d) < -value(eps)) - goto no_digits; - goto fast_failed; - } -#ifndef No_leftright - if (leftright) { - /* Use Steele & White method of only - * generating digits needed. - */ - value(eps) = 0.5/tens[ilim-1] - value(eps); - for(i = 0;;) { - L = value(d); - value(d) -= L; - *s++ = '0' + (int)L; - if (value(d) < value(eps)) - goto ret1; - if (1. - value(d) < value(eps)) - goto bump_up; - if (++i >= ilim) - break; - value(eps) *= 10.; - value(d) *= 10.; - } - } - else { -#endif - /* Generate ilim digits, then fix them up. */ - value(eps) *= tens[ilim-1]; - for(i = 1;; i++, value(d) *= 10.) { - L = value(d); - value(d) -= L; - *s++ = '0' + (int)L; - if (i == ilim) { - if (value(d) > 0.5 + value(eps)) - goto bump_up; - else if (value(d) < 0.5 - value(eps)) { - while(*--s == '0'); - s++; - goto ret1; - } - break; - } - } -#ifndef No_leftright - } -#endif - fast_failed: - s = s0; - value(d) = value(d2); - k = k0; - ilim = ilim0; - } - - /* Do we have a "small" integer? */ - - if (be >= 0 && k <= Int_max) { - /* Yes. */ - ds = tens[k]; - if (ndigits < 0 && ilim <= 0) { - S = mhi = 0; - if (ilim < 0 || value(d) <= 5*ds) - goto no_digits; - goto one_digit; - } - for(i = 1;; i++) { - L = value(d) / ds; - value(d) -= L*ds; -#ifdef Check_FLT_ROUNDS - /* If FLT_ROUNDS == 2, L will usually be high by 1 */ - if (value(d) < 0) { - L--; - value(d) += ds; - } -#endif - *s++ = '0' + (int)L; - if (i == ilim) { - value(d) += value(d); - if (value(d) > ds || value(d) == ds && L & 1) { - bump_up: - while(*--s == '9') - if (s == s0) { - k++; - *s = '0'; - break; - } - ++*s++; - } - break; - } - if (!(value(d) *= 10.)) - break; - } - goto ret1; - } - - m2 = b2; - m5 = b5; - mhi = mlo = 0; - if (leftright) { - if (mode < 2) { - i = -#ifndef Sudden_Underflow - denorm ? be + (Bias + (P-1) - 1 + 1) : -#endif -#ifdef IBM - 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3); -#else - 1 + P - bbits; -#endif - } - else { - j = ilim - 1; - if (m5 >= j) - m5 -= j; - else { - s5 += j -= m5; - b5 += j; - m5 = 0; - } - if ((i = ilim) < 0) { - m2 -= i; - i = 0; - } - } - b2 += i; - s2 += i; - mhi = i2b(1); - } - if (m2 > 0 && s2 > 0) { - i = m2 < s2 ? m2 : s2; - b2 -= i; - m2 -= i; - s2 -= i; - } - if (b5 > 0) { - if (leftright) { - if (m5 > 0) { - mhi = pow5mult(mhi, m5); - b1 = mult(mhi, b); - Bfree(b); - b = b1; - } - if (j = b5 - m5) - b = pow5mult(b, j); - } - else - b = pow5mult(b, b5); - } - S = i2b(1); - if (s5 > 0) - S = pow5mult(S, s5); - - /* Check for special case that d is a normalized power of 2. */ - - if (mode < 2) { - if (!word1(d) && !(word0(d) & Bndry_mask) -#ifndef Sudden_Underflow - && word0(d) & Exp_mask -#endif - ) { - /* The special case */ - b2 += Log2P; - s2 += Log2P; - spec_case = 1; - } - else - spec_case = 0; - } - - /* Arrange for convenient computation of quotients: - * shift left if necessary so divisor has 4 leading 0 bits. - * - * Perhaps we should just compute leading 28 bits of S once - * and for all and pass them and a shift to quorem, so it - * can do shifts and ors to compute the numerator for q. - */ -#ifdef Pack_32 - if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) - i = 32 - i; -#else - if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf) - i = 16 - i; -#endif - if (i > 4) { - i -= 4; - b2 += i; - m2 += i; - s2 += i; - } - else if (i < 4) { - i += 28; - b2 += i; - m2 += i; - s2 += i; - } - if (b2 > 0) - b = lshift(b, b2); - if (s2 > 0) - S = lshift(S, s2); - if (k_check) { - if (cmp(b,S) < 0) { - k--; - b = multadd(b, 10, 0); /* we botched the k estimate */ - if (leftright) - mhi = multadd(mhi, 10, 0); - ilim = ilim1; - } - } - if (ilim <= 0 && mode > 2) { - if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) { - /* no digits, fcvt style */ - no_digits: - k = -1 - ndigits; - goto ret; - } - one_digit: - *s++ = '1'; - k++; - goto ret; - } - if (leftright) { - if (m2 > 0) - mhi = lshift(mhi, m2); - - /* Compute mlo -- check for special case - * that d is a normalized power of 2. - */ - - mlo = mhi; - if (spec_case) { - mhi = Balloc(mhi->k); - Bcopy(mhi, mlo); - mhi = lshift(mhi, Log2P); - } - - for(i = 1;;i++) { - dig = quorem(b,S) + '0'; - /* Do we yet have the shortest decimal string - * that will round to d? - */ - j = cmp(b, mlo); - delta = diff(S, mhi); - j1 = delta->sign ? 1 : cmp(b, delta); - Bfree(delta); -#ifndef ROUND_BIASED - if (j1 == 0 && !mode && !(word1(d) & 1)) { - if (dig == '9') - goto round_9_up; - if (j > 0) - dig++; - *s++ = dig; - goto ret; - } -#endif - if (j < 0 || j == 0 && !mode -#ifndef ROUND_BIASED - && !(word1(d) & 1) -#endif - ) { - if (j1 > 0) { - b = lshift(b, 1); - j1 = cmp(b, S); - if ((j1 > 0 || j1 == 0 && dig & 1) - && dig++ == '9') - goto round_9_up; - } - *s++ = dig; - goto ret; - } - if (j1 > 0) { - if (dig == '9') { /* possible if i == 1 */ - round_9_up: - *s++ = '9'; - goto roundoff; - } - *s++ = dig + 1; - goto ret; - } - *s++ = dig; - if (i == ilim) - break; - b = multadd(b, 10, 0); - if (mlo == mhi) - mlo = mhi = multadd(mhi, 10, 0); - else { - mlo = multadd(mlo, 10, 0); - mhi = multadd(mhi, 10, 0); - } - } - } - else - for(i = 1;; i++) { - *s++ = dig = quorem(b,S) + '0'; - if (i >= ilim) - break; - b = multadd(b, 10, 0); - } - - /* Round off last digit */ - - b = lshift(b, 1); - j = cmp(b, S); - if (j > 0 || j == 0 && dig & 1) { - roundoff: - while(*--s == '9') - if (s == s0) { - k++; - *s++ = '1'; - goto ret; - } - ++*s++; - } - else { - while(*--s == '0'); - s++; - } - ret: - Bfree(S); - if (mhi) { - if (mlo && mlo != mhi) - Bfree(mlo); - Bfree(mhi); - } - ret1: - Bfree(b); - if (s == s0) { /* don't return empty string */ - *s++ = '0'; - k = 0; - } - *s = 0; - *decpt = k + 1; - if (rve) - *rve = s; - return s0; - } -#ifdef __cplusplus -} -#endif diff --git a/src/lib/libc/stdlib/strtof.c b/src/lib/libc/stdlib/strtof.c deleted file mode 100644 index 8c8db47ad8..0000000000 --- a/src/lib/libc/stdlib/strtof.c +++ /dev/null @@ -1,39 +0,0 @@ -/* $OpenBSD: strtof.c,v 1.1 2008/06/13 21:04:24 landry Exp $ */ - -/* - * Copyright (c) 2008 Landry Breuil - * All rights reserved. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include -#include - -float -strtof(const char *s00, char **se) -{ - double d; - - d = strtod(s00, se); - if (d > FLT_MAX) { - errno = ERANGE; - return (FLT_MAX); - } else if (d < -FLT_MAX) { - errno = ERANGE; - return (-FLT_MAX); - } - return ((float) d); -} diff --git a/src/lib/libc/stdlib/strtoimax.c b/src/lib/libc/stdlib/strtoimax.c deleted file mode 100644 index 2c77f41650..0000000000 --- a/src/lib/libc/stdlib/strtoimax.c +++ /dev/null @@ -1,140 +0,0 @@ -/* $OpenBSD: strtoimax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ - -/*- - * Copyright (c) 1992 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include - -/* - * Convert a string to an intmax_t - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -intmax_t -strtoimax(const char *nptr, char **endptr, int base) -{ - const char *s; - intmax_t acc, cutoff; - int c; - int neg, any, cutlim; - - /* - * Skip white space and pick up leading +/- sign if any. - * If base is 0, allow 0x for hex and 0 for octal, else - * assume decimal; if base is already 16, allow 0x. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - /* - * Compute the cutoff value between legal numbers and illegal - * numbers. That is the largest legal value, divided by the - * base. An input number that is greater than this value, if - * followed by a legal input character, is too big. One that - * is equal to this value may be valid or not; the limit - * between valid and invalid numbers is then based on the last - * digit. For instance, if the range for intmax_t is - * [-9223372036854775808..9223372036854775807] and the input base - * is 10, cutoff will be set to 922337203685477580 and cutlim to - * either 7 (neg==0) or 8 (neg==1), meaning that if we have - * accumulated a value > 922337203685477580, or equal but the - * next digit is > 7 (or 8), the number is too big, and we will - * return a range error. - * - * Set any if any `digits' consumed; make it negative to indicate - * overflow. - */ - cutoff = neg ? INTMAX_MIN : INTMAX_MAX; - cutlim = cutoff % base; - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (neg) { - if (acc < cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = INTMAX_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = INTMAX_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} diff --git a/src/lib/libc/stdlib/strtol.3 b/src/lib/libc/stdlib/strtol.3 deleted file mode 100644 index 84d9a0c427..0000000000 --- a/src/lib/libc/stdlib/strtol.3 +++ /dev/null @@ -1,264 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: strtol.3,v 1.19 2007/11/13 18:30:04 tobias Exp $ -.\" -.Dd $Mdocdate: November 13 2007 $ -.Dt STRTOL 3 -.Os -.Sh NAME -.Nm strtol , -.Nm strtoll , -.Nm strtoimax , -.Nm strtoq , -.Nd "convert string value to a long, long long or intmax_t integer" -.Sh SYNOPSIS -.Fd #include -.Fd #include -.Ft long -.Fn strtol "const char *nptr" "char **endptr" "int base" -.Pp -.Ft long long -.Fn strtoll "const char *nptr" "char **endptr" "int base" -.Pp -.Fd #include -.Ft intmax_t -.Fn strtoimax "const char *nptr" "char **endptr" "int base" -.Pp -.Fd #include -.Fd #include -.Fd #include -.Ft quad_t -.Fn strtoq "const char *nptr" "char **endptr" "int base" -.Sh DESCRIPTION -The -.Fn strtol -function converts the string in -.Fa nptr -to a -.Li long -value. -The -.Fn strtoll -function converts the string in -.Fa nptr -to a -.Li long long -value. -The -.Fn strtoimax -function converts the string in -.Fa nptr -to an -.Li intmax_t -value. -The -.Fn strtoq -function is a deprecated equivalent of -.Fn strtoll -and is provided for backwards compatibility with legacy programs. -The conversion is done according to the given -.Fa base , -which must be a number between 2 and 36 inclusive or the special value 0. -.Pp -The string may begin with an arbitrary amount of whitespace -(as determined by -.Xr isspace 3 ) -followed by a single optional -.Ql + -or -.Ql - -sign. -If -.Fa base -is zero or 16, the string may then include a -.Ql 0x -prefix, and the number will be read in base 16; otherwise, a zero -.Fa base -is taken as 10 (decimal) unless the next character is -.Ql 0 , -in which case it is taken as 8 (octal). -.Pp -The remainder of the string is converted to a -.Li long , -.Li long long , -or -.Li intmax_t , -value in the obvious manner, -stopping at the first character which is not a valid digit -in the given base. -(In bases above 10, the letter -.Ql A -in either upper or lower case represents 10, -.Ql B -represents 11, and so forth, with -.Ql Z -representing 35.) -.Pp -If -.Fa endptr -is non-null, -.Fn strtol -stores the address of the first invalid character in -.Fa *endptr . -If there were no digits at all, however, -.Fn strtol -stores the original value of -.Fa nptr -in -.Fa *endptr . -(Thus, if -.Fa *nptr -is not -.Ql \e0 -but -.Fa **endptr -is -.Ql \e0 -on return, the entire string was valid.) -.Sh RETURN VALUES -The -.Fn strtol , -.Fn strtoll , -.Fn strtoimax , -and -.Fn strtoq -functions return the result of the conversion, -unless the value would underflow or overflow. -If no conversion could be performed, 0 is returned; -the global variable -.Va errno -is also set to -.Er EINVAL, -though this is not portable across all platforms. -If overflow or underflow occurs, -.Va errno -is set to -.Er ERANGE -and the function return value is as follows: -.Bl -column -offset indent "strtoimax" "overflow" "underflow" -.It Sy Function Ta Sy underflow Ta Sy overflow -.It Fn strtol Ta Dv LONG_MIN Ta Dv LONG_MAX -.It Fn strtoll Ta Dv LLONG_MIN Ta Dv LLONG_MAX -.It Fn strtoimax Ta Dv INTMAX_MIN Ta Dv INTMAX_MAX -.It Fn strtoq Ta Dv LLONG_MIN Ta Dv LLONG_MAX -.El -.Sh EXAMPLES -Ensuring that a string is a valid number (i.e., in range and containing no -trailing characters) requires clearing -.Va errno -beforehand explicitly since -.Va errno -is not changed on a successful call to -.Fn strtol , -and the return value of -.Fn strtol -cannot be used unambiguously to signal an error: -.Bd -literal -offset indent -char *ep; -long lval; - -\&... - -errno = 0; -lval = strtol(buf, &ep, 10); -if (buf[0] == '\e0' || *ep != '\e0') - goto not_a_number; -if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) - goto out_of_range; -.Ed -.Pp -This example will accept -.Dq 12 -but not -.Dq 12foo -or -.Dq 12\en . -If trailing whitespace is acceptable, further checks must be done on -.Va *ep ; -alternately, use -.Xr sscanf 3 . -.Pp -If -.Fn strtol -is being used instead of -.Xr atoi 3 , -error checking is further complicated because the desired return value is an -.Li int -rather than a -.Li long ; -however, on some architectures integers and long integers are the same size. -Thus the following is necessary: -.Bd -literal -offset indent -char *ep; -int ival; -long lval; - -\&... - -errno = 0; -lval = strtol(buf, &ep, 10); -if (buf[0] == '\e0' || *ep != '\e0') - goto not_a_number; -if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) || - (lval > INT_MAX || lval < INT_MIN)) - goto out_of_range; -ival = lval; -.Ed -.Sh ERRORS -.Bl -tag -width Er -.It Bq Er ERANGE -The given string was out of range; the value converted has been clamped. -.El -.Sh SEE ALSO -.Xr atof 3 , -.Xr atoi 3 , -.Xr atol 3 , -.Xr atoll 3 , -.Xr sscanf 3 , -.Xr strtod 3 , -.Xr strtonum 3 , -.Xr strtoul 3 -.Sh STANDARDS -The -.Fn strtol , -.Fn strtoll , -and -.Fn strtoimax -functions conform to -.St -ansiC-99 . -The -.Fn strtoq -function is a -.Bx -extension and is provided for backwards compatibility with legacy programs. -.Sh BUGS -Ignores the current locale. diff --git a/src/lib/libc/stdlib/strtol.c b/src/lib/libc/stdlib/strtol.c deleted file mode 100644 index 5a244766db..0000000000 --- a/src/lib/libc/stdlib/strtol.c +++ /dev/null @@ -1,140 +0,0 @@ -/* $OpenBSD: strtol.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ -/*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include - - -/* - * Convert a string to a long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -long -strtol(const char *nptr, char **endptr, int base) -{ - const char *s; - long acc, cutoff; - int c; - int neg, any, cutlim; - - /* - * Skip white space and pick up leading +/- sign if any. - * If base is 0, allow 0x for hex and 0 for octal, else - * assume decimal; if base is already 16, allow 0x. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - /* - * Compute the cutoff value between legal numbers and illegal - * numbers. That is the largest legal value, divided by the - * base. An input number that is greater than this value, if - * followed by a legal input character, is too big. One that - * is equal to this value may be valid or not; the limit - * between valid and invalid numbers is then based on the last - * digit. For instance, if the range for longs is - * [-2147483648..2147483647] and the input base is 10, - * cutoff will be set to 214748364 and cutlim to either - * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated - * a value > 214748364, or equal but the next digit is > 7 (or 8), - * the number is too big, and we will return a range error. - * - * Set any if any `digits' consumed; make it negative to indicate - * overflow. - */ - cutoff = neg ? LONG_MIN : LONG_MAX; - cutlim = cutoff % base; - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (neg) { - if (acc < cutoff || acc == cutoff && c > cutlim) { - any = -1; - acc = LONG_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || acc == cutoff && c > cutlim) { - any = -1; - acc = LONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} diff --git a/src/lib/libc/stdlib/strtoll.c b/src/lib/libc/stdlib/strtoll.c deleted file mode 100644 index 37379252e3..0000000000 --- a/src/lib/libc/stdlib/strtoll.c +++ /dev/null @@ -1,153 +0,0 @@ -/* $OpenBSD: strtoll.c,v 1.6 2005/11/10 10:00:17 espie Exp $ */ -/*- - * Copyright (c) 1992 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -#include -#include -#include -#include - -/* - * Convert a string to a long long. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -long long -strtoll(const char *nptr, char **endptr, int base) -{ - const char *s; - long long acc, cutoff; - int c; - int neg, any, cutlim; - - /* - * Skip white space and pick up leading +/- sign if any. - * If base is 0, allow 0x for hex and 0 for octal, else - * assume decimal; if base is already 16, allow 0x. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - /* - * Compute the cutoff value between legal numbers and illegal - * numbers. That is the largest legal value, divided by the - * base. An input number that is greater than this value, if - * followed by a legal input character, is too big. One that - * is equal to this value may be valid or not; the limit - * between valid and invalid numbers is then based on the last - * digit. For instance, if the range for long longs is - * [-9223372036854775808..9223372036854775807] and the input base - * is 10, cutoff will be set to 922337203685477580 and cutlim to - * either 7 (neg==0) or 8 (neg==1), meaning that if we have - * accumulated a value > 922337203685477580, or equal but the - * next digit is > 7 (or 8), the number is too big, and we will - * return a range error. - * - * Set any if any `digits' consumed; make it negative to indicate - * overflow. - */ - cutoff = neg ? LLONG_MIN : LLONG_MAX; - cutlim = cutoff % base; - cutoff /= base; - if (neg) { - if (cutlim > 0) { - cutlim -= base; - cutoff += 1; - } - cutlim = -cutlim; - } - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (neg) { - if (acc < cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = LLONG_MIN; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc -= c; - } - } else { - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = LLONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= base; - acc += c; - } - } - } - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} - -#ifdef __weak_alias -__weak_alias(strtoq, strtoll); -#else -quad_t -strtoq(const char *nptr, char **endptr, int base) -{ - - return ((quad_t)strtoll(nptr, endptr, base)); -} -#endif diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3 deleted file mode 100644 index 58b4112ac3..0000000000 --- a/src/lib/libc/stdlib/strtonum.3 +++ /dev/null @@ -1,152 +0,0 @@ -.\" $OpenBSD: strtonum.3,v 1.14 2007/05/31 19:19:31 jmc Exp $ -.\" -.\" Copyright (c) 2004 Ted Unangst -.\" -.\" Permission to use, copy, modify, and distribute this software for any -.\" purpose with or without fee is hereby granted, provided that the above -.\" copyright notice and this permission notice appear in all copies. -.\" -.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt STRTONUM 3 -.Os -.Sh NAME -.Nm strtonum -.Nd "reliably convert string value to an integer" -.Sh SYNOPSIS -.Fd #include -.Ft long long -.Fo strtonum -.Fa "const char *nptr" -.Fa "long long minval" -.Fa "long long maxval" -.Fa "const char **errstr" -.Fc -.Sh DESCRIPTION -The -.Fn strtonum -function converts the string in -.Fa nptr -to a -.Li long long -value. -The -.Fn strtonum -function was designed to facilitate safe, robust programming -and overcome the shortcomings of the -.Xr atoi 3 -and -.Xr strtol 3 -family of interfaces. -.Pp -The string may begin with an arbitrary amount of whitespace -(as determined by -.Xr isspace 3 ) -followed by a single optional -.Ql + -or -.Ql - -sign. -.Pp -The remainder of the string is converted to a -.Li long long -value according to base 10. -.Pp -The value obtained is then checked against the provided -.Fa minval -and -.Fa maxval -bounds. -If -.Fa errstr -is non-null, -.Fn strtonum -stores an error string in -.Fa *errstr -indicating the failure. -.Sh RETURN VALUES -The -.Fn strtonum -function returns the result of the conversion, -unless the value would exceed the provided bounds or is invalid. -On error, 0 is returned, -.Va errno -is set, and -.Fa errstr -will point to an error message. -.Fa *errstr -will be set to -.Dv NULL -on success; -this fact can be used to differentiate -a successful return of 0 from an error. -.Sh EXAMPLES -Using -.Fn strtonum -correctly is meant to be simpler than the alternative functions. -.Bd -literal -offset indent -int iterations; -const char *errstr; - -iterations = strtonum(optarg, 1, 64, &errstr); -if (errstr) - errx(1, "number of iterations is %s: %s", errstr, optarg); -.Ed -.Pp -The above example will guarantee that the value of iterations is between -1 and 64 (inclusive). -.Sh ERRORS -.Bl -tag -width Er -.It Bq Er ERANGE -The given string was out of range. -.It Bq Er EINVAL -The given string did not consist solely of digit characters. -.It Bq Er EINVAL -.Ar minval -was larger than -.Ar maxval . -.El -.Pp -If an error occurs, -.Fa errstr -will be set to one of the following strings: -.Pp -.Bl -tag -width "too largeXX" -compact -.It too large -The result was larger than the provided maximum value. -.It too small -The result was smaller than the provided minimum value. -.It invalid -The string did not consist solely of digit characters. -.El -.Sh SEE ALSO -.Xr atof 3 , -.Xr atoi 3 , -.Xr atol 3 , -.Xr atoll 3 , -.Xr sscanf 3 , -.Xr strtod 3 , -.Xr strtol 3 , -.Xr strtoul 3 -.Sh STANDARDS -.Fn strtonum -is an -.Ox -extension. -The existing alternatives, such as -.Xr atoi 3 -and -.Xr strtol 3 , -are either impossible or difficult to use safely. -.Sh HISTORY -The -.Fn strtonum -function first appeared in -.Ox 3.6 . diff --git a/src/lib/libc/stdlib/strtonum.c b/src/lib/libc/stdlib/strtonum.c deleted file mode 100644 index e426388ed0..0000000000 --- a/src/lib/libc/stdlib/strtonum.c +++ /dev/null @@ -1,65 +0,0 @@ -/* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ - -/* - * Copyright (c) 2004 Ted Unangst and Todd Miller - * All rights reserved. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#define INVALID 1 -#define TOOSMALL 2 -#define TOOLARGE 3 - -long long -strtonum(const char *numstr, long long minval, long long maxval, - const char **errstrp) -{ - long long ll = 0; - char *ep; - int error = 0; - struct errval { - const char *errstr; - int err; - } ev[4] = { - { NULL, 0 }, - { "invalid", EINVAL }, - { "too small", ERANGE }, - { "too large", ERANGE }, - }; - - ev[0].err = errno; - errno = 0; - if (minval > maxval) - error = INVALID; - else { - ll = strtoll(numstr, &ep, 10); - if (numstr == ep || *ep != '\0') - error = INVALID; - else if ((ll == LLONG_MIN && errno == ERANGE) || ll < minval) - error = TOOSMALL; - else if ((ll == LLONG_MAX && errno == ERANGE) || ll > maxval) - error = TOOLARGE; - } - if (errstrp != NULL) - *errstrp = ev[error].errstr; - errno = ev[error].err; - if (error) - ll = 0; - - return (ll); -} diff --git a/src/lib/libc/stdlib/strtoul.3 b/src/lib/libc/stdlib/strtoul.3 deleted file mode 100644 index a7c6bbfa18..0000000000 --- a/src/lib/libc/stdlib/strtoul.3 +++ /dev/null @@ -1,248 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" Chris Torek and the American National Standards Committee X3, -.\" on Information Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: strtoul.3,v 1.19 2007/11/13 18:30:04 tobias Exp $ -.\" -.Dd $Mdocdate: November 13 2007 $ -.Dt STRTOUL 3 -.Os -.Sh NAME -.Nm strtoul , -.Nm strtoull , -.Nm strtoumax , -.Nm strtouq -.Nd "convert a string to an unsigned long, unsigned long long or uintmax_t integer" -.Sh SYNOPSIS -.Fd #include -.Fd #include -.Ft unsigned long -.Fn strtoul "const char *nptr" "char **endptr" "int base" -.Pp -.Ft unsigned long long -.Fn strtoull "const char *nptr" "char **endptr" "int base" -.Pp -.Fd #include -.Ft uintmax_t -.Fn strtoumax "const char *nptr" "char **endptr" "int base" -.Pp -.Fd #include -.Fd #include -.Fd #include -.Ft u_quad_t -.Fn strtouq "const char *nptr" "char **endptr" "int base" -.Sh DESCRIPTION -The -.Fn strtoul -function converts the string in -.Fa nptr -to an -.Li unsigned long -value. -The -.Fn strtoull -function converts the string in -.Fa nptr -to an -.Li unsigned long long -value. -The -.Fn strtoumax -function converts the string in -.Fa nptr -to a -.Li umaxint_t -value. -The -.Fn strtouq -function is a deprecated equivalent of -.Fn strtoull -and is provided for backwards compatibility with legacy programs. -The conversion is done according to the given -.Fa base , -which must be a number between 2 and 36 inclusive -or the special value 0. -If the string in -.Fa nptr -represents a negative number, it will be converted to its unsigned equivalent. -This behavior is consistent with what happens when a signed integer type is -cast to its unsigned counterpart. -.Pp -The string may begin with an arbitrary amount of whitespace -(as determined by -.Xr isspace 3 ) -followed by a single optional -.Ql + -or -.Ql - -sign. -If -.Fa base -is zero or 16, the string may then include a -.Ql 0x -prefix, and the number will be read in base 16; otherwise, a zero -.Fa base -is taken as 10 (decimal) unless the next character is -.Ql 0 , -in which case it is taken as 8 (octal). -.Pp -The remainder of the string is converted to an -.Li unsigned long -value in the obvious manner, stopping at the end of the string -or at the first character that does not produce a valid digit -in the given base. -(In bases above 10, the letter -.Ql A -in either upper or lower case represents 10, -.Ql B -represents 11, and so forth, with -.Ql Z -representing 35.) -.Pp -If -.Fa endptr -is non-null, -.Fn strtoul -stores the address of the first invalid character in -.Fa *endptr . -If there were no digits at all, however, -.Fn strtoul -stores the original value of -.Fa nptr -in -.Fa *endptr . -(Thus, if -.Fa *nptr -is not -.Ql \e0 -but -.Fa **endptr -is -.Ql \e0 -on return, the entire string was valid.) -.Sh RETURN VALUES -The -.Fn strtoul , -.Fn strtoull , -.Fn strtoumax -and -.Fn strtouq -functions return either the result of the conversion or, -if there was a leading minus sign, -the negation of the result of the conversion, -unless the original (non-negated) value would overflow. -If overflow occurs, -.Fn strtoul -returns -.Dv ULONG_MAX , -.Fn strtoull -returns -.Dv ULLONG_MAX , -.Fn strtoumax -returns -.Dv UINTMAX_MAX , -.Fn strtouq -returns -.Dv ULLONG_MAX -and the global variable -.Va errno -is set to -.Er ERANGE . -If no conversion could be performed, 0 is returned; -the global variable -.Va errno -is also set to -.Er EINVAL, -though this is not portable across all platforms. -.Pp -There is no way to determine if -.Fn strtoul -has processed a negative number (and returned an unsigned value) short of -examining the string in -.Fa nptr -directly. -.Sh EXAMPLES -Ensuring that a string is a valid number (i.e., in range and containing no -trailing characters) requires clearing -.Va errno -beforehand explicitly since -.Va errno -is not changed on a successful call to -.Fn strtoul , -and the return value of -.Fn strtoul -cannot be used unambiguously to signal an error: -.Bd -literal -offset indent -char *ep; -unsigned long ulval; - -\&... - -errno = 0; -ulval = strtoul(buf, &ep, 10); -if (buf[0] == '\e0' || *ep != '\e0') - goto not_a_number; -if (errno == ERANGE && ulval == ULONG_MAX) - goto out_of_range; -.Ed -.Pp -This example will accept -.Dq 12 -but not -.Dq 12foo -or -.Dq 12\en . -If trailing whitespace is acceptable, further checks must be done on -.Va *ep ; -alternately, use -.Xr sscanf 3 . -.Sh ERRORS -.Bl -tag -width Er -.It Bq Er ERANGE -The given string was out of range; the value converted has been clamped. -.El -.Sh SEE ALSO -.Xr sscanf 3 , -.Xr strtol 3 -.Sh STANDARDS -The -.Fn strtoul , -.Fn strtoull , -and -.Fn strtoumax -functions conform to -.St -ansiC-99 . -The -.Fn strtouq -function is a -.Bx -extension and is provided for backwards compatibility with legacy programs. -.Sh BUGS -Ignores the current locale. diff --git a/src/lib/libc/stdlib/strtoul.c b/src/lib/libc/stdlib/strtoul.c deleted file mode 100644 index d7dddab778..0000000000 --- a/src/lib/libc/stdlib/strtoul.c +++ /dev/null @@ -1,102 +0,0 @@ -/* $OpenBSD: strtoul.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1990 Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include - -/* - * Convert a string to an unsigned long integer. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -unsigned long -strtoul(const char *nptr, char **endptr, int base) -{ - const char *s; - unsigned long acc, cutoff; - int c; - int neg, any, cutlim; - - /* - * See strtol for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - cutoff = ULONG_MAX / (unsigned long)base; - cutlim = ULONG_MAX % (unsigned long)base; - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (acc > cutoff || acc == cutoff && c > cutlim) { - any = -1; - acc = ULONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= (unsigned long)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} diff --git a/src/lib/libc/stdlib/strtoull.c b/src/lib/libc/stdlib/strtoull.c deleted file mode 100644 index 37859776f9..0000000000 --- a/src/lib/libc/stdlib/strtoull.c +++ /dev/null @@ -1,115 +0,0 @@ -/* $OpenBSD: strtoull.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ -/*- - * Copyright (c) 1992 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include - -#include -#include -#include -#include - -/* - * Convert a string to an unsigned long long. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -unsigned long long -strtoull(const char *nptr, char **endptr, int base) -{ - const char *s; - unsigned long long acc, cutoff; - int c; - int neg, any, cutlim; - - /* - * See strtoq for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - cutoff = ULLONG_MAX / (unsigned long long)base; - cutlim = ULLONG_MAX % (unsigned long long)base; - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = ULLONG_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= (unsigned long long)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} - -#ifdef __weak_alias -__weak_alias(strtouq, strtoull); -#else -u_quad_t -strtouq(const char *nptr, char **endptr, int base) -{ - - return ((u_quad_t)strtoull(nptr, endptr, base)); -} -#endif diff --git a/src/lib/libc/stdlib/strtoumax.c b/src/lib/libc/stdlib/strtoumax.c deleted file mode 100644 index ce6e2c00f1..0000000000 --- a/src/lib/libc/stdlib/strtoumax.c +++ /dev/null @@ -1,102 +0,0 @@ -/* $OpenBSD: strtoumax.c,v 1.1 2006/01/13 17:58:09 millert Exp $ */ - -/*- - * Copyright (c) 1992 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include - -/* - * Convert a string to a uintmax_t. - * - * Ignores `locale' stuff. Assumes that the upper and lower case - * alphabets and digits are each contiguous. - */ -uintmax_t -strtoumax(const char *nptr, char **endptr, int base) -{ - const char *s; - uintmax_t acc, cutoff; - int c; - int neg, any, cutlim; - - /* - * See strtoq for comments as to the logic used. - */ - s = nptr; - do { - c = (unsigned char) *s++; - } while (isspace(c)); - if (c == '-') { - neg = 1; - c = *s++; - } else { - neg = 0; - if (c == '+') - c = *s++; - } - if ((base == 0 || base == 16) && - c == '0' && (*s == 'x' || *s == 'X')) { - c = s[1]; - s += 2; - base = 16; - } - if (base == 0) - base = c == '0' ? 8 : 10; - - cutoff = UINTMAX_MAX / (uintmax_t)base; - cutlim = UINTMAX_MAX % (uintmax_t)base; - for (acc = 0, any = 0;; c = (unsigned char) *s++) { - if (isdigit(c)) - c -= '0'; - else if (isalpha(c)) - c -= isupper(c) ? 'A' - 10 : 'a' - 10; - else - break; - if (c >= base) - break; - if (any < 0) - continue; - if (acc > cutoff || (acc == cutoff && c > cutlim)) { - any = -1; - acc = UINTMAX_MAX; - errno = ERANGE; - } else { - any = 1; - acc *= (uintmax_t)base; - acc += c; - } - } - if (neg && any > 0) - acc = -acc; - if (endptr != 0) - *endptr = (char *) (any ? s - 1 : nptr); - return (acc); -} diff --git a/src/lib/libc/stdlib/system.3 b/src/lib/libc/stdlib/system.3 deleted file mode 100644 index a49e595bc0..0000000000 --- a/src/lib/libc/stdlib/system.3 +++ /dev/null @@ -1,104 +0,0 @@ -.\" Copyright (c) 1990, 1991 The Regents of the University of California. -.\" All rights reserved. -.\" -.\" This code is derived from software contributed to Berkeley by -.\" the American National Standards Committee X3, on Information -.\" Processing Systems. -.\" -.\" Redistribution and use in source and binary forms, with or without -.\" modification, are permitted provided that the following conditions -.\" are met: -.\" 1. Redistributions of source code must retain the above copyright -.\" notice, this list of conditions and the following disclaimer. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice, this list of conditions and the following disclaimer in the -.\" documentation and/or other materials provided with the distribution. -.\" 3. Neither the name of the University nor the names of its contributors -.\" may be used to endorse or promote products derived from this software -.\" without specific prior written permission. -.\" -.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND -.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE -.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS -.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) -.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT -.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF -.\" SUCH DAMAGE. -.\" -.\" $OpenBSD: system.3,v 1.11 2008/04/04 19:30:41 kurt Exp $ -.\" -.Dd $Mdocdate: April 4 2008 $ -.Dt SYSTEM 3 -.Os -.Sh NAME -.Nm system -.Nd pass a command to the shell -.Sh SYNOPSIS -.Fd #include -.Ft int -.Fn system "const char *string" -.Sh DESCRIPTION -The -.Fn system -function hands the argument -.Fa string -to the command interpreter -.Xr sh 1 . -The calling process waits for the shell to finish executing the command, -ignoring -.Dv SIGINT -and -.Dv SIGQUIT , -and blocking -.Dv SIGCHLD . -.Pp -If -.Fa string -is -.Dv NULL , -.Fn system -will return non-zero. -Otherwise, -.Fn system -returns the termination status of the shell in the format specified by -.Xr waitpid 2 . -.Pp -Note that fork handlers established using -.Xr pthread_atfork 3 -are not called when a multithreaded program calls -.Fn system . -.Sh RETURN VALUES -If a child process cannot be created, or the termination status of -the shell cannot be obtained, -.Fn system -returns \-1 and sets -.Va errno -to indicate the error. -If execution of the shell fails, -.Fn system -returns the termination status for a program that terminates with a call of -.Fn exit 127 . -.Sh SEE ALSO -.Xr sh 1 , -.Xr execve 2 , -.Xr waitpid 2 , -.Xr popen 3 -.Sh STANDARDS -The -.Fn system -function conforms to -.St -ansiC -and -.St -p1003.2-92 . -.Sh CAVEATS -Never supply the -.Fn system -function with a command containing any part of an unsanitized user-supplied -string. -Shell meta-characters present will be honored by the -.Xr sh 1 -command interpreter. diff --git a/src/lib/libc/stdlib/system.c b/src/lib/libc/stdlib/system.c deleted file mode 100644 index 14ddcae8d3..0000000000 --- a/src/lib/libc/stdlib/system.c +++ /dev/null @@ -1,74 +0,0 @@ -/* $OpenBSD: system.c,v 1.8 2005/08/08 08:05:37 espie Exp $ */ -/* - * Copyright (c) 1988 The Regents of the University of California. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -#include -#include -#include -#include -#include -#include - -extern char **environ; - -int -system(const char *command) -{ - pid_t pid; - sig_t intsave, quitsave; - sigset_t mask, omask; - int pstat; - char *argp[] = {"sh", "-c", NULL, NULL}; - - if (!command) /* just checking... */ - return(1); - - argp[2] = (char *)command; - - sigemptyset(&mask); - sigaddset(&mask, SIGCHLD); - sigprocmask(SIG_BLOCK, &mask, &omask); - switch (pid = vfork()) { - case -1: /* error */ - sigprocmask(SIG_SETMASK, &omask, NULL); - return(-1); - case 0: /* child */ - sigprocmask(SIG_SETMASK, &omask, NULL); - execve(_PATH_BSHELL, argp, environ); - _exit(127); - } - - intsave = signal(SIGINT, SIG_IGN); - quitsave = signal(SIGQUIT, SIG_IGN); - pid = waitpid(pid, (int *)&pstat, 0); - sigprocmask(SIG_SETMASK, &omask, NULL); - (void)signal(SIGINT, intsave); - (void)signal(SIGQUIT, quitsave); - return (pid == -1 ? -1 : pstat); -} diff --git a/src/lib/libc/stdlib/tfind.c b/src/lib/libc/stdlib/tfind.c deleted file mode 100644 index ff6bcd742d..0000000000 --- a/src/lib/libc/stdlib/tfind.c +++ /dev/null @@ -1,41 +0,0 @@ -/* $OpenBSD: tfind.c,v 1.5 2005/03/30 18:51:49 pat Exp $ */ - -/* - * Tree search generalized from Knuth (6.2.2) Algorithm T just like - * the AT&T man page says. - * - * The node_t structure is for internal use only, lint doesn't grok it. - * - * Written by reading the System V Interface Definition, not the code. - * - * Totally public domain. - */ -/*LINTLIBRARY*/ -#include - -typedef struct node_t -{ - char *key; - struct node_t *llink, *rlink; -} node; - -/* find a node, or return 0 */ -void * -tfind(const void *vkey, void * const *vrootp, - int (*compar)(const void *, const void *)) -{ - char *key = (char *)vkey; - node **rootp = (node **)vrootp; - - if (rootp == (struct node_t **)0) - return ((struct node_t *)0); - while (*rootp != (struct node_t *)0) { /* T1: */ - int r; - if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */ - return (*rootp); /* key found */ - rootp = (r < 0) ? - &(*rootp)->llink : /* T3: follow left branch */ - &(*rootp)->rlink; /* T4: follow right branch */ - } - return (node *)0; -} diff --git a/src/lib/libc/stdlib/tsearch.3 b/src/lib/libc/stdlib/tsearch.3 deleted file mode 100644 index 0a52822620..0000000000 --- a/src/lib/libc/stdlib/tsearch.3 +++ /dev/null @@ -1,127 +0,0 @@ -.\" $OpenBSD: tsearch.3,v 1.16 2007/05/31 19:19:32 jmc Exp $ -.\" -.\" Copyright (c) 1997 Todd C. Miller -.\" -.\" Permission to use, copy, modify, and distribute this software for any -.\" purpose with or without fee is hereby granted, provided that the above -.\" copyright notice and this permission notice appear in all copies. -.\" -.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -.\" -.Dd $Mdocdate: May 31 2007 $ -.Dt TSEARCH 3 -.Os -.Sh NAME -.Nm tsearch , -.Nm tfind , -.Nm tdelete , -.Nm twalk -.Nd manipulate binary search trees -.Sh SYNOPSIS -.Fd #include -.Ft void * -.Fn tdelete "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" -.Ft void * -.Fn tfind "const void *key" "void * const *rootp" "int (*compar) (const void *, const void *)" -.Ft void * -.Fn tsearch "const void *key" "void **rootp" "int (*compar) (const void *, const void *)" -.Ft void -.Fn twalk "const void *root" "void (*action) (const void *, VISIT, int)" -.Sh DESCRIPTION -The -.Fn tdelete , -.Fn tfind , -.Fn tsearch , -and -.Fn twalk -functions manage binary search trees based on algorithms T and D -from Knuth (6.2.2). -The comparison function passed in by -the user has the same style of return values as -.Xr strcmp 3 . -.Pp -.Fn tfind -searches for the datum matched by the argument -.Fa key -in the binary tree rooted at -.Fa rootp , -returning a pointer to the datum if it is found and -.Dv NULL -if it is not. -.Pp -.Fn tsearch -is identical to -.Fn tfind -except that if no match is found, -.Fa key -is inserted into the tree and a pointer to it is returned. -If -.Fa rootp -points to a null value a new binary search tree is created. -.Pp -.Fn tdelete -deletes a node from the specified binary search tree and returns -a pointer to the parent of the node to be deleted. -It takes the same arguments as -.Fn tfind -and -.Fn tsearch . -If the node to be deleted is the root of the binary search tree, -.Fa rootp -will be adjusted and a pointer to the new root will be returned. -.Pp -.Fn twalk -walks the binary search tree rooted in -.Fa root -and calls the function -.Fa action -on each node. -.Fa action -is called with three arguments: a pointer to the current node, -a value from the enum -.Sy "typedef enum { preorder, postorder, endorder, leaf } VISIT;" -specifying the traversal type, and a node level (where level -zero is the root of the tree). -.Sh RETURN VALUES -The -.Fn tsearch -function returns -.Dv NULL -if allocation of a new node fails (usually -due to a lack of free memory). -.Pp -.Fn tfind , -.Fn tsearch , -and -.Fn tdelete -return -.Dv NULL -if -.Fa rootp -is -.Dv NULL -or the datum cannot be found. -.Pp -The -.Fn twalk -function returns no value. -.Sh SEE ALSO -.Xr bsearch 3 , -.Xr lsearch 3 -.Sh STANDARDS -These functions conform to -.St -p1003.1-2004 . -.Sh CAVEATS -The -.St -p1003.1-2004 -standard does not specify what value should be returned when deleting the -root node. -Since implementations vary, the user of the -.Fn tdelete -function should not rely on a specific behaviour. diff --git a/src/lib/libc/stdlib/tsearch.c b/src/lib/libc/stdlib/tsearch.c deleted file mode 100644 index 667c57731b..0000000000 --- a/src/lib/libc/stdlib/tsearch.c +++ /dev/null @@ -1,121 +0,0 @@ -/* $OpenBSD: tsearch.c,v 1.6 2006/04/04 11:21:50 moritz Exp $ */ - -/* - * Tree search generalized from Knuth (6.2.2) Algorithm T just like - * the AT&T man page says. - * - * The node_t structure is for internal use only, lint doesn't grok it. - * - * Written by reading the System V Interface Definition, not the code. - * - * Totally public domain. - */ -/*LINTLIBRARY*/ - -#include -#include - -typedef struct node_t { - char *key; - struct node_t *left, *right; -} node; - -/* find or insert datum into search tree */ -void * -tsearch(const void *vkey, void **vrootp, - int (*compar)(const void *, const void *)) -{ - node *q; - char *key = (char *)vkey; - node **rootp = (node **)vrootp; - - if (rootp == (struct node_t **)0) - return ((void *)0); - while (*rootp != (struct node_t *)0) { /* Knuth's T1: */ - int r; - - if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */ - return ((void *)*rootp); /* we found it! */ - rootp = (r < 0) ? - &(*rootp)->left : /* T3: follow left branch */ - &(*rootp)->right; /* T4: follow right branch */ - } - q = (node *) malloc(sizeof(node)); /* T5: key not found */ - if (q != (struct node_t *)0) { /* make new node */ - *rootp = q; /* link new node to old */ - q->key = key; /* initialize new node */ - q->left = q->right = (struct node_t *)0; - } - return ((void *)q); -} - -/* delete node with given key */ -void * -tdelete(const void *vkey, void **vrootp, - int (*compar)(const void *, const void *)) -{ - node **rootp = (node **)vrootp; - char *key = (char *)vkey; - node *p; - node *q; - node *r; - int cmp; - - if (rootp == (struct node_t **)0 || (p = *rootp) == (struct node_t *)0) - return ((struct node_t *)0); - while ((cmp = (*compar)(key, (*rootp)->key)) != 0) { - p = *rootp; - rootp = (cmp < 0) ? - &(*rootp)->left : /* follow left branch */ - &(*rootp)->right; /* follow right branch */ - if (*rootp == (struct node_t *)0) - return ((void *)0); /* key not found */ - } - r = (*rootp)->right; /* D1: */ - if ((q = (*rootp)->left) == (struct node_t *)0) /* Left (struct node_t *)0? */ - q = r; - else if (r != (struct node_t *)0) { /* Right link is null? */ - if (r->left == (struct node_t *)0) { /* D2: Find successor */ - r->left = q; - q = r; - } else { /* D3: Find (struct node_t *)0 link */ - for (q = r->left; q->left != (struct node_t *)0; q = r->left) - r = q; - r->left = q->right; - q->left = (*rootp)->left; - q->right = (*rootp)->right; - } - } - if (p == *rootp) - p = q; - free((struct node_t *) *rootp); /* D4: Free node */ - *rootp = q; /* link parent to new node */ - return(p); -} - -/* Walk the nodes of a tree */ -static void -trecurse(node *root, void (*action)(const void *, VISIT, int), int level) -{ - if (root->left == (struct node_t *)0 && root->right == (struct node_t *)0) - (*action)(root, leaf, level); - else { - (*action)(root, preorder, level); - if (root->left != (struct node_t *)0) - trecurse(root->left, action, level + 1); - (*action)(root, postorder, level); - if (root->right != (struct node_t *)0) - trecurse(root->right, action, level + 1); - (*action)(root, endorder, level); - } -} - -/* Walk the nodes of a tree */ -void -twalk(const void *vroot, void (*action)(const void *, VISIT, int)) -{ - node *root = (node *)vroot; - - if (root != (node *)0 && action != (void (*)(const void *, VISIT, int))0) - trecurse(root, action, 0); -} -- cgit v1.2.3-55-g6feb