From 9ef0d5fb5b0acfd35d73a5557198f46525ab1667 Mon Sep 17 00:00:00 2001 From: cvs2svn Date: Sun, 13 Apr 2014 15:49:51 +0000 Subject: This commit was manufactured by cvs2git to create tag 'butholakala'. --- src/lib/libc/stdlib/Makefile.inc | 63 -- 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 | 68 -- src/lib/libc/stdlib/abort.c | 80 -- src/lib/libc/stdlib/abs.3 | 70 -- src/lib/libc/stdlib/abs.c | 37 - src/lib/libc/stdlib/alloca.3 | 76 -- src/lib/libc/stdlib/atexit.3 | 77 -- src/lib/libc/stdlib/atexit.c | 194 ---- src/lib/libc/stdlib/atexit.h | 50 - src/lib/libc/stdlib/atof.3 | 74 -- src/lib/libc/stdlib/atof.c | 37 - src/lib/libc/stdlib/atoi.3 | 91 -- 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/cfree.c | 39 - src/lib/libc/stdlib/div.3 | 64 -- src/lib/libc/stdlib/div.c | 71 -- src/lib/libc/stdlib/drand48.c | 23 - src/lib/libc/stdlib/ecvt.3 | 168 ---- src/lib/libc/stdlib/ecvt.c | 106 -- src/lib/libc/stdlib/erand48.c | 24 - src/lib/libc/stdlib/exit.3 | 102 -- src/lib/libc/stdlib/exit.c | 59 -- src/lib/libc/stdlib/gcvt.c | 123 --- src/lib/libc/stdlib/getenv.3 | 175 ---- src/lib/libc/stdlib/getenv.c | 81 -- src/lib/libc/stdlib/getopt.3 | 364 ------- src/lib/libc/stdlib/getopt_long.3 | 445 --------- src/lib/libc/stdlib/getopt_long.c | 518 ---------- src/lib/libc/stdlib/getsubopt.3 | 145 --- src/lib/libc/stdlib/getsubopt.c | 92 -- src/lib/libc/stdlib/hcreate.3 | 239 ----- src/lib/libc/stdlib/hcreate.c | 191 ---- src/lib/libc/stdlib/heapsort.c | 173 ---- src/lib/libc/stdlib/imaxabs.3 | 65 -- src/lib/libc/stdlib/imaxabs.c | 38 - src/lib/libc/stdlib/imaxdiv.3 | 66 -- 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 | 72 -- src/lib/libc/stdlib/ldiv.c | 50 - src/lib/libc/stdlib/llabs.c | 38 - src/lib/libc/stdlib/lldiv.3 | 66 -- 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 | 84 -- src/lib/libc/stdlib/malloc.3 | 484 --------- src/lib/libc/stdlib/malloc.c | 1820 ---------------------------------- 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/posix_memalign.3 | 92 -- src/lib/libc/stdlib/posix_openpt.3 | 102 -- src/lib/libc/stdlib/posix_pty.c | 119 --- src/lib/libc/stdlib/ptsname.3 | 160 --- src/lib/libc/stdlib/qabs.3 | 56 -- src/lib/libc/stdlib/qabs.c | 37 - src/lib/libc/stdlib/qdiv.3 | 61 -- src/lib/libc/stdlib/qdiv.c | 50 - src/lib/libc/stdlib/qsort.3 | 238 ----- src/lib/libc/stdlib/qsort.c | 162 --- src/lib/libc/stdlib/radixsort.3 | 155 --- src/lib/libc/stdlib/radixsort.c | 294 ------ src/lib/libc/stdlib/rand.3 | 105 -- src/lib/libc/stdlib/rand.c | 67 -- src/lib/libc/stdlib/rand48.3 | 179 ---- src/lib/libc/stdlib/rand48.h | 32 - src/lib/libc/stdlib/random.3 | 185 ---- src/lib/libc/stdlib/random.c | 446 --------- src/lib/libc/stdlib/realpath.3 | 130 --- src/lib/libc/stdlib/realpath.c | 214 ---- src/lib/libc/stdlib/remque.c | 44 - src/lib/libc/stdlib/seed48.c | 37 - src/lib/libc/stdlib/setenv.c | 180 ---- src/lib/libc/stdlib/srand48.c | 31 - src/lib/libc/stdlib/strtod.3 | 167 ---- src/lib/libc/stdlib/strtoimax.c | 140 --- src/lib/libc/stdlib/strtol.3 | 264 ----- src/lib/libc/stdlib/strtol.c | 151 --- src/lib/libc/stdlib/strtoll.c | 144 --- src/lib/libc/stdlib/strtonum.3 | 152 --- src/lib/libc/stdlib/strtonum.c | 65 -- src/lib/libc/stdlib/strtoul.3 | 245 ----- src/lib/libc/stdlib/strtoul.c | 102 -- src/lib/libc/stdlib/strtoull.c | 106 -- src/lib/libc/stdlib/strtoumax.c | 102 -- src/lib/libc/stdlib/system.3 | 109 -- src/lib/libc/stdlib/system.c | 74 -- src/lib/libc/stdlib/tfind.c | 41 - src/lib/libc/stdlib/tsearch.3 | 130 --- src/lib/libc/stdlib/tsearch.c | 119 --- 107 files changed, 13802 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/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/posix_memalign.3 delete mode 100644 src/lib/libc/stdlib/posix_openpt.3 delete mode 100644 src/lib/libc/stdlib/posix_pty.c delete mode 100644 src/lib/libc/stdlib/ptsname.3 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/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 953c53382f..0000000000 --- a/src/lib/libc/stdlib/Makefile.inc +++ /dev/null @@ -1,63 +0,0 @@ -# $OpenBSD: Makefile.inc,v 1.49 2014/03/18 22:36:29 miod Exp $ - -# stdlib sources -.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib - -SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.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 posix_pty.c qsort.c radixsort.c \ - rand.c random.c realpath.c setenv.c strtoimax.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_CPU} == "i386") -SRCS+= abs.S div.S labs.S ldiv.S -.elif (${MACHINE_CPU} == "vax") -SRCS+= abs.c div.c labs.c ldiv.c -.elif (${MACHINE_CPU} == "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_CPU} == "vax") -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 posix_memalign.3 posix_openpt.3 ptsname.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+=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+=ptsname.3 grantpt.3 ptsname.3 unlockpt.3 -MLINKS+=strtod.3 strtof.3 strtod.3 strtold.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 ccf64c2e87..0000000000 --- a/src/lib/libc/stdlib/_Exit.c +++ /dev/null @@ -1,22 +0,0 @@ -/* $OpenBSD: _Exit.c,v 1.3 2013/04/03 03:39:29 guenther 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 in C99. Indeed, POSIX specifies that - * _Exit() must *not* flush stdio buffers or remove temporary files, but - * rather must behave exactly like _exit() - */ -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 11a74604ac..0000000000 --- a/src/lib/libc/stdlib/a64l.3 +++ /dev/null @@ -1,133 +0,0 @@ -.\" $OpenBSD: a64l.3,v 1.12 2013/06/05 03:39:23 tedu 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: June 5 2013 $ -.Dt A64L 3 -.Os -.Sh NAME -.Nm a64l , -.Nm l64a -.Nd convert between 32-bit integer and radix-64 ASCII string -.Sh SYNOPSIS -.In stdlib.h -.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 STANDARDS -The -.Fn a64l -and -.Fn l64a -functions conform to -.St -xpg4.2 . -.Sh CAVEATS -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. 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 322d629930..0000000000 --- a/src/lib/libc/stdlib/abort.3 +++ /dev/null @@ -1,68 +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.10 2013/07/17 05:42:11 schwarze Exp $ -.\" -.Dd $Mdocdate: July 17 2013 $ -.Dt ABORT 3 -.Os -.Sh NAME -.Nm abort -.Nd cause abnormal program termination -.Sh SYNOPSIS -.In stdlib.h -.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 . -.Sh HISTORY -The -.Fn abort -function first appeared in -.At v5 . diff --git a/src/lib/libc/stdlib/abort.c b/src/lib/libc/stdlib/abort.c deleted file mode 100644 index 4c8dc70a1d..0000000000 --- a/src/lib/libc/stdlib/abort.c +++ /dev/null @@ -1,80 +0,0 @@ -/* $OpenBSD: abort.c,v 1.16 2012/11/10 03:46:11 guenther 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" - -int _thread_sys_sigprocmask(int, const sigset_t *, sigset_t *); - -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)raise(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)raise(SIGABRT); - _exit(1); -} diff --git a/src/lib/libc/stdlib/abs.3 b/src/lib/libc/stdlib/abs.3 deleted file mode 100644 index 420bdf6fdf..0000000000 --- a/src/lib/libc/stdlib/abs.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: abs.3,v 1.11 2013/07/17 05:42:11 schwarze Exp $ -.\" -.Dd $Mdocdate: July 17 2013 $ -.Dt ABS 3 -.Os -.Sh NAME -.Nm abs -.Nd integer absolute value function -.Sh SYNOPSIS -.In stdlib.h -.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 -.Sh STANDARDS -The -.Fn abs -function conforms to -.St -ansiC . -.Sh HISTORY -The -.Fn abs -function first appeared in -.At v6 . -.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 47e9b97143..0000000000 --- a/src/lib/libc/stdlib/alloca.3 +++ /dev/null @@ -1,76 +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.13 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt ALLOCA 3 -.Os -.Sh NAME -.Nm alloca -.Nd memory allocator -.Sh SYNOPSIS -.In stdlib.h -.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 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... -.Sh BUGS -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 8e3ac3e60f..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.9 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt ATEXIT 3 -.Os -.Sh NAME -.Nm atexit -.Nd register a function to be called on exit -.Sh SYNOPSIS -.In stdlib.h -.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 049da3261d..0000000000 --- a/src/lib/libc/stdlib/atexit.c +++ /dev/null @@ -1,194 +0,0 @@ -/* $OpenBSD: atexit.c,v 1.17 2013/12/28 18:38:42 kettenis 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" - -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; - } - 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); -} - -/* - * 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; - - 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 (fn.fn_dso != NULL) - (*fn.fn_ptr.cxa_func)(fn.fn_arg); - else - (*fn.fn_ptr.std_func)(); - } - } - - call_depth--; - - /* - * 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; - } 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 c44005deda..0000000000 --- a/src/lib/libc/stdlib/atexit.h +++ /dev/null @@ -1,50 +0,0 @@ -/* $OpenBSD: atexit.h,v 1.8 2013/06/02 21:08:36 matthew 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 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 53722a6253..0000000000 --- a/src/lib/libc/stdlib/atof.3 +++ /dev/null @@ -1,74 +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.7 2013/07/17 05:42:11 schwarze Exp $ -.\" -.Dd $Mdocdate: July 17 2013 $ -.Dt ATOF 3 -.Os -.Sh NAME -.Nm atof -.Nd convert -.Tn ASCII -string to double -.Sh SYNOPSIS -.In stdlib.h -.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 . -.Sh HISTORY -An -.Fn atof -function first appeared in -.At v1 . 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 fd5c720e02..0000000000 --- a/src/lib/libc/stdlib/atoi.3 +++ /dev/null @@ -1,91 +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.11 2013/07/17 05:42:11 schwarze Exp $ -.\" -.Dd $Mdocdate: July 17 2013 $ -.Dt ATOI 3 -.Os -.Sh NAME -.Nm atoi -.Nd convert -.Tn ASCII -string to integer -.Sh SYNOPSIS -.In stdlib.h -.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 HISTORY -An -.Fn atoi -function first appeared in -.At v1 . -.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 ae4cbc7c3d..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.8 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt ATOL 3 -.Os -.Sh NAME -.Nm atol -.Nd convert -.Tn ASCII -string to long integer -.Sh SYNOPSIS -.In stdlib.h -.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 a9614ae7bb..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.6 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt ATOLL 3 -.Os -.Sh NAME -.Nm atoll -.Nd convert -.Tn ASCII -string to long long integer -.Sh SYNOPSIS -.In stdlib.h -.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 270086df36..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.9 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt BSEARCH 3 -.Os -.Sh NAME -.Nm bsearch -.Nd binary search of a sorted table -.Sh SYNOPSIS -.In stdlib.h -.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 b48747236e..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 involves 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/cfree.c b/src/lib/libc/stdlib/cfree.c deleted file mode 100644 index acf6d1c8ac..0000000000 --- a/src/lib/libc/stdlib/cfree.c +++ /dev/null @@ -1,39 +0,0 @@ -/* $OpenBSD: cfree.c,v 1.6 2012/12/05 23:20:01 deraadt 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 - -#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 1a2a326269..0000000000 --- a/src/lib/libc/stdlib/div.3 +++ /dev/null @@ -1,64 +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.11 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt DIV 3 -.Os -.Sh NAME -.Nm div -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.In stdlib.h -.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 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 d8a3489b1f..0000000000 --- a/src/lib/libc/stdlib/ecvt.3 +++ /dev/null @@ -1,168 +0,0 @@ -.\" $OpenBSD: ecvt.3,v 1.11 2013/06/05 03:39:23 tedu 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: June 5 2013 $ -.Dt ECVT 3 -.Os -.Sh NAME -.Nm ecvt , -.Nm fcvt , -.Nm gcvt -.Nd convert double to -.Tn ASCII -string -.Sh SYNOPSIS -.In stdlib.h -.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 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 ; -as of -.St -p1003.1-2008 -they are no longer a part of the standard. -.Sh CAVEATS -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. diff --git a/src/lib/libc/stdlib/ecvt.c b/src/lib/libc/stdlib/ecvt.c deleted file mode 100644 index 4562e309e8..0000000000 --- a/src/lib/libc/stdlib/ecvt.c +++ /dev/null @@ -1,106 +0,0 @@ -/* $OpenBSD: ecvt.c,v 1.8 2013/11/01 19:05:11 guenther 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 -#include "gdtoa.h" - -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 (p == NULL) - return (NULL); - 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 26922ca7b8..0000000000 --- a/src/lib/libc/stdlib/exit.3 +++ /dev/null @@ -1,102 +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.15 2013/07/17 05:42:11 schwarze Exp $ -.\" -.Dd $Mdocdate: July 17 2013 $ -.Dt EXIT 3 -.Os -.Sh NAME -.Nm exit -.Nd perform normal program termination -.Sh SYNOPSIS -.In stdlib.h -.Ft void -.Fn exit "int status" -.Sh DESCRIPTION -The -.Fn exit -function terminates a process. -.Pp -Before termination it performs the following functions 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 -Following this, -.Fn exit -calls -.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 -function never returns. -.Sh SEE ALSO -.Xr _exit 2 , -.Xr atexit 3 , -.Xr intro 3 , -.Xr sysexits 3 , -.Xr tmpfile 3 -.Sh STANDARDS -The -.Fn exit -function conforms to -.St -ansiC-99 . -.Sh HISTORY -An -.Fn exit -function first appeared as a system call in -.At v1 . -It has accepted the -.Fa status -argument since -.At v2 . -In -.At v7 , -the bare system call was renamed to -.Xr _exit 2 . 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 f233332799..0000000000 --- a/src/lib/libc/stdlib/gcvt.c +++ /dev/null @@ -1,123 +0,0 @@ -/* $OpenBSD: gcvt.c,v 1.13 2013/11/01 19:05:11 guenther Exp $ */ - -/* - * Copyright (c) 2002, 2003, 2006, 2010 - * 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 -#include "gdtoa.h" - -#define DEFPREC 6 - -char * -gcvt(double value, int ndigit, char *buf) -{ - char *digits, *dst, *src; - int i, decpt, sign; - struct lconv *lconv; - - lconv = localeconv(); - if (ndigit <= 0) { - /* Match printf(3) behavior. */ - ndigit = ndigit ? DEFPREC : 1; - } - - digits = __dtoa(value, 2, ndigit, &decpt, &sign, NULL); - if (digits == NULL) - return (NULL); - if (decpt == 9999) { - /* - * Infinity or NaN, convert to inf or nan with sign. - * We can't infer buffer size based on ndigit. - * We have to assume it is at least 5 chars. - */ - snprintf(buf, 5, "%s%s", sign ? "-" : "", - *digits == 'I' ? "inf" : "nan"); - __freedtoa(digits); - return (buf); - } - - dst = buf; - if (sign) - *dst++ = '-'; - - /* Match printf(3) behavior for exponential vs. regular fomatting. */ - if (decpt <= -4 || decpt > ndigit) { - /* exponential format (e.g. 1.2345e+13) */ - if (--decpt < 0) { - sign = 1; - decpt = -decpt; - } else - sign = 0; - src = digits; - *dst++ = *src++; - if (*src != '\0') { - *dst++ = *lconv->decimal_point; - do { - *dst++ = *src++; - } while (*src != '\0'); - } - *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; - while (decpt < 0) { - *dst++ = '0'; - decpt++; - } - 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 5239d1b0d5..0000000000 --- a/src/lib/libc/stdlib/getenv.3 +++ /dev/null @@ -1,175 +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.19 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt GETENV 3 -.Os -.Sh NAME -.Nm getenv , -.Nm putenv , -.Nm setenv , -.Nm unsetenv -.Nd environment variable functions -.Sh SYNOPSIS -.In stdlib.h -.Ft char * -.Fn getenv "const char *name" -.Ft int -.Fn setenv "const char *name" "const char *value" "int overwrite" -.Ft int -.Fn putenv "char *string" -.Ft int -.Fn unsetenv "const char *name" -.Sh DESCRIPTION -These functions set, unset, and fetch environment variables from the host -.Em environment list . -.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 . -The memory pointed to by -.Ar string -becomes part of the environment and must not be deallocated by the caller. -If the variable already exists, it will be overwritten. -A common source of bugs is to pass a -.Ar string -argument that is a locally scoped string buffer. -This will result in corruption of the environment after leaving -the scope in which the variable is defined. -For this reason, the -.Fn setenv -function is preferred over -.Fn putenv . -.Pp -The -.Fn unsetenv -function deletes all instances of the variable name pointed to by -.Fa name -from the list. -.Sh RETURN VALUES -These functions -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 EINVAL -The -.Fn setenv -or -.Fn unsetenv -function was passed an empty -.Ar name -or a NULL pointer, or was passed a -.Ar name -containing an -.Sq = -character. -.Pp -The -.Fn putenv -function was passed a -.Ar string -that did not contain an -.Sq = -character. -.It Bq Er ENOMEM -The -.Fn setenv -or -.Fn putenv -function failed because it was 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 . -The -.Fn putenv , -.Fn setenv , -and -.Fn unsetenv -functions conform to -.St -p1003.1-2008 . -.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 fd8482e9e3..0000000000 --- a/src/lib/libc/stdlib/getenv.c +++ /dev/null @@ -1,81 +0,0 @@ -/* $OpenBSD: getenv.c,v 1.10 2010/08/23 22:31:50 millert 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 len, int *offset); - -/* - * __findenv -- - * Returns pointer to value associated with name, if any, else NULL. - * Starts searching within the environmental array at offset. - * Sets offset to be the offset of the name/value combination in the - * environmental array, for use by putenv(3), 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 len, int *offset) -{ - extern char **environ; - int i; - const char *np; - char **p, *cp; - - if (name == NULL || environ == NULL) - return (NULL); - for (p = environ + *offset; (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 = 0; - const char *np; - - for (np = name; *np && *np != '='; ++np) - ; - return (__findenv(name, (int)(np - name), &offset)); -} diff --git a/src/lib/libc/stdlib/getopt.3 b/src/lib/libc/stdlib/getopt.3 deleted file mode 100644 index 6661e03841..0000000000 --- a/src/lib/libc/stdlib/getopt.3 +++ /dev/null @@ -1,364 +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.44 2014/01/21 03:15:45 schwarze Exp $ -.\" -.Dd $Mdocdate: January 21 2014 $ -.Dt GETOPT 3 -.Os -.Sh NAME -.Nm getopt -.Nd get option character from command line argument list -.Sh SYNOPSIS -.In unistd.h -.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; except in the case where -the argument is optional, denoted with two colons, no leading whitespace -is permitted. -.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 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 -.In 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 a5973d6c99..0000000000 --- a/src/lib/libc/stdlib/getopt_long.3 +++ /dev/null @@ -1,445 +0,0 @@ -.\" $OpenBSD: getopt_long.3,v 1.20 2013/06/05 03:39:23 tedu 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: June 5 2013 $ -.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 -.In getopt.h -.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 an array to be initialized describing the long -options. -Each element of the array is a structure: -.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 IMPLEMENTATION DIFFERENCES -This section describes differences to the GNU implementation -found in glibc-2.1.3: -.Bl -bullet -.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 + -in the -.Ar optstring -is ignored. -.El -.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 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 f46cd8bf4e..0000000000 --- a/src/lib/libc/stdlib/getopt_long.c +++ /dev/null @@ -1,518 +0,0 @@ -/* $OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 millert 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 - -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 */ - -#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, 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, int flags) -{ - char *current_argv, *has_equal; - size_t current_argv_len; - int i, match, exact_match, second_partial_match; - - current_argv = place; - match = -1; - exact_match = 0; - second_partial_match = 0; - - 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; - exact_match = 1; - 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) /* first partial match */ - match = i; - else if ((flags & FLAG_LONGONLY) || - long_options[i].has_arg != long_options[match].has_arg || - long_options[i].flag != long_options[match].flag || - long_options[i].val != long_options[match].val) - second_partial_match = 1; - } - if (!exact_match && second_partial_match) { - /* 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); - - /* - * 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; - - /* - * Disable GNU extensions if POSIXLY_CORRECT is set or options - * string begins with a '+'. - */ - if (posixly_correct == -1 || optreset) - posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); - if (*options == '-') - flags |= FLAG_ALLARGS; - else if (posixly_correct || *options == '+') - flags &= ~FLAG_PERMUTE; - if (*options == '+' || *options == '-') - options++; - - 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, flags); - 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, flags); - 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); -} - -/* - * 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)); -} - -/* - * 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 ef813430de..0000000000 --- a/src/lib/libc/stdlib/getsubopt.3 +++ /dev/null @@ -1,145 +0,0 @@ -.\" $OpenBSD: getsubopt.3,v 1.13 2013/06/05 03:39:23 tedu 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: June 5 2013 $ -.Dt GETSUBOPT 3 -.Os -.Sh NAME -.Nm getsubopt -.Nd get sub options from an argument -.Sh SYNOPSIS -.In stdlib.h -.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 ea264b57c8..0000000000 --- a/src/lib/libc/stdlib/hcreate.3 +++ /dev/null @@ -1,239 +0,0 @@ -.\" $OpenBSD: hcreate.3,v 1.6 2010/07/28 09:00:20 ray Exp $ -.\" $NetBSD: hcreate.3,v 1.8 2010/05/01 06:18:03 jruoho 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: July 28 2010 $ -.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 -.Vt ENTRY , -defined in the -.In search.h -header. -This is a structure type that contains two pointers: -.Pp -.Bl -tag -compact -offset indent -width "void *data " -.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 -.Vt 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 -.Pp -Note that the comparison -.Fa key -must be allocated using -.Xr malloc 3 -or -.Xr calloc 3 -if action is -.Dv ENTER -and -.Fn hdestroy -will be called. -This is because -.Fn hdestroy -will call -.Xr free 3 -for each comparison -.Fa key -(but not -.Fa data ) . -Typically the comparison -.Fa key -is allocated by using -.Xr strdup 3 . -.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 -The -.Fn hcreate , -.Fn hdestroy -and -.Fn hsearch -functions conform to -.St -xpg4.2 . -.Sh HISTORY -The -.Fn hcreate , -.Fn hdestroy -and -.Fn hsearch -functions first appeared in -.At V . -.Sh CAVEATS -At least the following limitations can be mentioned: -.Bl -bullet -.It -The interface permits the use of only one hash table at a time. -.It -Individual hash table entries can be added, but not deleted. -.It -The standard is indecipherable about the -internal memory usage of the functions, -mentioning only that -.Do -.Fn hcreate -and -.Fn hsearch -functions may use -.Fn malloc -to allocate space -.Dc . -This limits the portability of the functions, -given that other implementations may not -.Xr free 3 -the buffer pointed by -.Fa key . -.El 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 ad3fffbcd9..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 *)) -{ - size_t 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 6c4793477e..0000000000 --- a/src/lib/libc/stdlib/imaxabs.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 -.\" 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.6 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt IMAXABS 3 -.Os -.Sh NAME -.Nm imaxabs -.Nd integer absolute value function -.Sh SYNOPSIS -.In inttypes.h -.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 -.Sh STANDARDS -The -.Fn imaxabs -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 40ac0bc236..0000000000 --- a/src/lib/libc/stdlib/imaxdiv.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 -.\" 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.5 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt IMAXDIV 3 -.Os -.Sh NAME -.Nm imaxdiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.In inttypes.h -.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 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 64da36a898..0000000000 --- a/src/lib/libc/stdlib/insque.3 +++ /dev/null @@ -1,106 +0,0 @@ -.\" $OpenBSD: insque.3,v 1.9 2013/06/05 03:39:23 tedu 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: June 5 2013 $ -.Dt INSQUE 3 -.Os -.Sh NAME -.Nm insque , -.Nm remque -.Nd insert/remove element from a queue -.Sh SYNOPSIS -.In search.h -.Ft void -.Fn insque "void *elem" "void *pred" -.Ft void -.Fn remque "void *elem" -.Sh DESCRIPTION -.Bf -symbolic -These interfaces have been superseded 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 a38c64b1af..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.12 2014/01/19 05:21:12 schwarze Exp $ -.\" -.Dd $Mdocdate: January 19 2014 $ -.Dt LABS 3 -.Os -.Sh NAME -.Nm labs , -.Nm llabs -.Nd return the absolute value of a long integer -.Sh SYNOPSIS -.In stdlib.h -.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 -.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 a8835ab6ac..0000000000 --- a/src/lib/libc/stdlib/ldiv.3 +++ /dev/null @@ -1,72 +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.12 2013/07/17 05:42:11 schwarze Exp $ -.\" -.Dd $Mdocdate: July 17 2013 $ -.Dt LDIV 3 -.Os -.Sh NAME -.Nm ldiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.In stdlib.h -.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 qdiv 3 -.Sh STANDARDS -The -.Fn ldiv -function conforms to -.St -ansiC . -.Sh HISTORY -An -.Fn ldiv -function with similar functionality, but a different calling convention, -first appeared in -.At v4 . 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 9181f8b7c5..0000000000 --- a/src/lib/libc/stdlib/lldiv.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 -.\" 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.4 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt LLDIV 3 -.Os -.Sh NAME -.Nm lldiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.In stdlib.h -.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 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 c5a55980e1..0000000000 --- a/src/lib/libc/stdlib/lsearch.3 +++ /dev/null @@ -1,108 +0,0 @@ -.\" $OpenBSD: lsearch.3,v 1.10 2013/06/05 03:39:23 tedu 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: June 5 2013 $ -.Dt LSEARCH 3 -.Os -.Sh NAME -.Nm lsearch , -.Nm lfind -.Nd linear searching routines -.Sh SYNOPSIS -.In search.h -.Ft char * -.Fn lsearch "const void *key" "const void *base" "size_t *nelp" \ - "size_t width" "int (*compar)(const void *, const void *)" -.Ft char * -.Fn lfind "const void *key" "const void *base" "size_t *nelp" \ - "size_t width" "int (*compar)(const void *, const 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 a01d80e008..0000000000 --- a/src/lib/libc/stdlib/lsearch.c +++ /dev/null @@ -1,84 +0,0 @@ -/* $OpenBSD: lsearch.c,v 1.4 2009/10/27 23:59:59 deraadt 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. - */ - -#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 414f0a9770..0000000000 --- a/src/lib/libc/stdlib/malloc.3 +++ /dev/null @@ -1,484 +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.73 2013/07/18 10:14:49 schwarze Exp $ -.\" -.Dd $Mdocdate: July 18 2013 $ -.Dt MALLOC 3 -.Os -.Sh NAME -.Nm malloc , -.Nm calloc , -.Nm realloc , -.Nm free , -.Nm cfree -.Nd memory allocation and deallocation -.Sh SYNOPSIS -.In stdlib.h -.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 -.Sh MALLOC_OPTIONS -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 internal -inconsistencies or incorrect usage. -This is the default and a very handy debugging aid, -since the core file represents the time of failure, -rather than when the bogus pointer was used. -.It Cm D -.Dq Dump . -.Fn malloc -will dump statistics to the file -.Pa ./malloc.out , -if it already exists, -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 detection. -Unused pages on the freelist are read and write protected to -cause a segmentation fault upon access. -This will also switch off the delayed freeing of chunks, -reducing random behaviour but detecting double -.Fn free -calls as early as possible. -This option is intended for debugging rather than improved security -(use the -.Cm U -option for security). -.It Cm G -.Dq Guard . -Enable guard pages. -Each page size or larger allocation is followed by a guard page that will -cause a segmentation fault upon any access. -.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 P -.Dq Move allocations within a page. -Allocations larger than half a page but smaller than a page -are aligned to the end of a page to catch buffer overruns in more -cases. -This is the default. -.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 S -Enable all options suitable for security auditing. -.It Cm U -.Dq Free unmap . -Enable use after free protection for larger allocations. -Unused pages on the freelist are read and write protected to -cause a segmentation fault upon access. -.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 the cache to a quarter of the -default size and use guard pages: -.Dl # ln -s 'G\*(Lt\*(Lt' /etc/malloc.conf -.Pp -The flags are mostly for testing and debugging. -If a program changes behavior if any of these options (except -.Cm X ) -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 condition, -a message will be printed to file descriptor -2 (not using stdio). -Errors will result in the process being aborted, -unless the -.Cm a -option has been specified. -.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 X -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 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. -.It any other error -.Fn malloc -detected an internal error; -consult sources and/or wizards. -.El -.Sh SEE ALSO -.Xr brk 2 , -.Xr mmap 2 , -.Xr munmap 2 , -.Xr alloca 3 , -.Xr getpagesize 3 , -.Xr posix_memalign 3 -.Sh STANDARDS -The -.Fn malloc -function conforms to -.St -ansiC . -.Sh HISTORY -A -.Fn free -internal kernel function and a predecessor to -.Fn malloc , -.Fn alloc , -first appeared in -.At v1 . -C library functions -.Fn alloc -and -.Fn free -appeared in -.At v6 . -The functions -.Fn malloc , -.Fn calloc , -and -.Fn realloc -first appeared in -.At v7 . -.Pp -A new implementation by Chris Kingsley was introduced in -.Bx 4.2 , -followed by a complete rewrite by Poul-Henning Kamp which appeared in -.Fx 2.2 -and was included in -.Ox 2.0 . -These implementations were all -.Xr sbrk 2 -based. -In -.Ox 3.8 , -Thierry Deval rewrote -.Nm -to use the -.Xr mmap 2 -system call, -making the page addresses returned by -.Nm -random. -A rewrite by Otto Moerbeek introducing a new central data structure and more -randomization appeared in -.Ox 4.4 . diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c deleted file mode 100644 index 446a1ca254..0000000000 --- a/src/lib/libc/stdlib/malloc.c +++ /dev/null @@ -1,1820 +0,0 @@ -/* $OpenBSD: malloc.c,v 1.152 2014/04/03 16:18:11 schwarze Exp $ */ -/* - * Copyright (c) 2008, 2010, 2011 Otto Moerbeek - * Copyright (c) 2012 Matthew Dempsky - * Copyright (c) 2008 Damien Miller - * Copyright (c) 2000 Poul-Henning Kamp - * - * 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. - */ - -/* - * 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 -#include - -#ifdef MALLOC_STATS -#include -#include -#endif - -#include "thread_private.h" - -#if defined(__sparc__) && !defined(__sparcv9__) -#define MALLOC_PAGESHIFT (13U) -#elif defined(__mips64__) -#define MALLOC_PAGESHIFT (14U) -#else -#define MALLOC_PAGESHIFT (PAGE_SHIFT) -#endif - -#define MALLOC_MINSHIFT 4 -#define MALLOC_MAXSHIFT (MALLOC_PAGESHIFT - 1) -#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_MAXSHIFT) -#define MALLOC_MAXCACHE 256 -#define MALLOC_DELAYED_CHUNKS 15 /* max of getrnibble() */ -#define MALLOC_INITIAL_REGIONS 512 -#define MALLOC_DEFAULT_CACHE 64 - -/* - * When the P option is active, we move allocations between half a page - * and a whole page towards the end, subject to alignment constraints. - * This is the extra headroom we allow. Set to zero to be the most - * strict. - */ -#define MALLOC_LEEWAY 0 - -#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) - -#define MMAPA(a,sz) mmap((a), (size_t)(sz), PROT_READ | PROT_WRITE, \ - MAP_ANON | MAP_PRIVATE, -1, (off_t) 0) - -#define MQUERY(a, sz) mquery((a), (size_t)(sz), PROT_READ | PROT_WRITE, \ - MAP_ANON | MAP_PRIVATE | MAP_FIXED, -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 */ -#ifdef MALLOC_STATS - void *f; /* where allocated from */ -#endif -}; - -LIST_HEAD(chunk_head, chunk_info); - -struct dir_info { - u_int32_t canary1; - struct region_info *r; /* region slots */ - size_t regions_total; /* number of region slots */ - size_t regions_free; /* number of free slots */ - /* lists of free chunk info structs */ - struct chunk_head chunk_info_list[MALLOC_MAXSHIFT + 1]; - /* lists of chunks with free slots */ - struct chunk_head chunk_dir[MALLOC_MAXSHIFT + 1]; - 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 + 1]; - u_short chunk_start; -#ifdef MALLOC_STATS - size_t inserts; - size_t insert_collisions; - size_t finds; - size_t find_collisions; - size_t deletes; - size_t delete_moves; - size_t cheap_realloc_tries; - size_t cheap_reallocs; -#define STATS_INC(x) ((x)++) -#define STATS_ZERO(x) ((x) = 0) -#define STATS_SETF(x,y) ((x)->f = (y)) -#else -#define STATS_INC(x) /* nothing */ -#define STATS_ZERO(x) /* nothing */ -#define STATS_SETF(x,y) /* nothing */ -#endif /* MALLOC_STATS */ - u_int32_t canary2; -}; -#define DIR_INFO_RSZ ((sizeof(struct dir_info) + MALLOC_PAGEMASK) & \ - ~MALLOC_PAGEMASK) - -/* - * This structure describes a page worth of chunks. - * - * How many bits per u_short in the bitmap - */ -#define MALLOC_BITS (NBBY * sizeof(u_short)) -struct chunk_info { - LIST_ENTRY(chunk_info) entries; - 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_short bits[1]; -}; - -struct malloc_readonly { - struct dir_info *g_pool; /* Main bookkeeping information */ - int malloc_abort; /* abort() on error */ - int malloc_freenow; /* Free quickly - disable chunk rnd */ - int malloc_freeunmap; /* mprotect free pages PROT_NONE? */ - int malloc_hint; /* call madvice on free pages? */ - int malloc_junk; /* junk fill? */ - int malloc_move; /* move allocations to end of page? */ - int malloc_realloc; /* always realloc? */ - int malloc_xmalloc; /* xmalloc behaviour? */ - int malloc_zero; /* zero fill? */ - size_t malloc_guard; /* use guard pages after allocations? */ - u_int malloc_cache; /* free pages we cache */ -#ifdef MALLOC_STATS - int malloc_stats; /* dump statistics at end */ -#endif - u_int32_t malloc_canary; /* Matched against ones in g_pool */ -}; - -/* This object is mapped PROT_READ after initialisation to prevent tampering */ -static union { - struct malloc_readonly mopts; - u_char _pad[MALLOC_PAGESIZE]; -} malloc_readonly __attribute__((aligned(MALLOC_PAGESIZE))); -#define mopts malloc_readonly.mopts -#define g_pool mopts.g_pool - -char *malloc_options; /* compile-time options */ - -static char *malloc_func; /* current function */ -static int malloc_active; /* status of malloc */ - -static size_t malloc_guarded; /* bytes used for guards */ -static size_t malloc_used; /* bytes allocated */ - -static size_t rnibblesused; /* random nibbles used */ -static u_char rbytes[512]; /* random bytes */ -static u_char getrnibble(void); - -extern char *__progname; - -#ifdef MALLOC_STATS -void malloc_dump(int); -static void malloc_exit(void); -#define CALLER __builtin_return_address(0) -#else -#define CALLER NULL -#endif - -/* 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; -} - -static void -wrterror(char *msg, void *p) -{ - char *q = " error: "; - struct iovec iov[6]; - char buf[20]; - int saved_errno = errno; - - 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 = msg; - iov[3].iov_len = strlen(msg); - iov[4].iov_base = buf; - if (p == NULL) - iov[4].iov_len = 0; - else { - snprintf(buf, sizeof(buf), " %p", p); - iov[4].iov_len = strlen(buf); - } - iov[5].iov_base = "\n"; - iov[5].iov_len = 1; - writev(STDERR_FILENO, iov, 6); - -#ifdef MALLOC_STATS - if (mopts.malloc_stats) - malloc_dump(STDERR_FILENO); -#endif /* MALLOC_STATS */ - - errno = saved_errno; - if (mopts.malloc_abort) - abort(); -} - -static void -rbytes_init(void) -{ - arc4random_buf(rbytes, sizeof(rbytes)); - rnibblesused = 0; -} - -static inline u_char -getrnibble(void) -{ - u_char x; - - if (rnibblesused >= 2 * sizeof(rbytes)) - rbytes_init(); - x = rbytes[rnibblesused++ / 2]; - return (rnibblesused & 1 ? x & 0xf : x >> 4); -} - -/* - * 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 = sz >> MALLOC_PAGESHIFT; - size_t rsz, tounmap; - struct region_info *r; - u_int i, offset; - - if (sz != PAGEROUND(sz)) { - wrterror("munmap round", NULL); - return; - } - - if (psz > mopts.malloc_cache) { - if (munmap(p, sz)) - wrterror("munmap", p); - malloc_used -= sz; - return; - } - tounmap = 0; - rsz = mopts.malloc_cache - d->free_regions_size; - if (psz > rsz) - tounmap = psz - rsz; - offset = getrnibble() + (getrnibble() << 4); - for (i = 0; tounmap > 0 && i < mopts.malloc_cache; i++) { - r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; - if (r->p != NULL) { - rsz = r->size << MALLOC_PAGESHIFT; - if (munmap(r->p, rsz)) - wrterror("munmap", r->p); - r->p = NULL; - if (tounmap > r->size) - tounmap -= r->size; - else - tounmap = 0; - d->free_regions_size -= r->size; - r->size = 0; - malloc_used -= rsz; - } - } - if (tounmap > 0) - wrterror("malloc cache underflow", NULL); - for (i = 0; i < mopts.malloc_cache; i++) { - r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; - if (r->p == NULL) { - if (mopts.malloc_hint) - madvise(p, sz, MADV_FREE); - if (mopts.malloc_freeunmap) - mprotect(p, sz, PROT_NONE); - r->p = p; - r->size = psz; - d->free_regions_size += psz; - break; - } - } - if (i == mopts.malloc_cache) - wrterror("malloc free slot lost", NULL); - if (d->free_regions_size > mopts.malloc_cache) - wrterror("malloc cache overflow", NULL); -} - -static void -zapcacheregion(struct dir_info *d, void *p, size_t len) -{ - u_int i; - struct region_info *r; - size_t rsz; - - for (i = 0; i < mopts.malloc_cache; i++) { - r = &d->free_regions[i]; - if (r->p >= p && r->p <= (void *)((char *)p + len)) { - rsz = r->size << MALLOC_PAGESHIFT; - if (munmap(r->p, rsz)) - wrterror("munmap", r->p); - r->p = NULL; - d->free_regions_size -= r->size; - r->size = 0; - malloc_used -= rsz; - } - } -} - -static void * -map(struct dir_info *d, size_t sz, int zero_fill) -{ - size_t psz = sz >> MALLOC_PAGESHIFT; - struct region_info *r, *big = NULL; - u_int i, offset; - void *p; - - if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) || - d->canary1 != ~d->canary2) - wrterror("internal struct corrupt", NULL); - if (sz != PAGEROUND(sz)) { - wrterror("map round", NULL); - return MAP_FAILED; - } - if (psz > d->free_regions_size) { - p = MMAP(sz); - if (p != MAP_FAILED) - malloc_used += sz; - /* zero fill not needed */ - return p; - } - offset = getrnibble() + (getrnibble() << 4); - for (i = 0; i < mopts.malloc_cache; i++) { - r = &d->free_regions[(i + offset) & (mopts.malloc_cache - 1)]; - if (r->p != NULL) { - if (r->size == psz) { - p = r->p; - if (mopts.malloc_freeunmap) - mprotect(p, sz, PROT_READ | PROT_WRITE); - if (mopts.malloc_hint) - madvise(p, sz, MADV_NORMAL); - r->p = NULL; - r->size = 0; - d->free_regions_size -= psz; - if (zero_fill) - memset(p, 0, sz); - else if (mopts.malloc_junk && - mopts.malloc_freeunmap) - memset(p, SOME_FREEJUNK, sz); - return p; - } else if (r->size > psz) - big = r; - } - } - if (big != NULL) { - r = big; - p = (char *)r->p + ((r->size - psz) << MALLOC_PAGESHIFT); - if (mopts.malloc_freeunmap) - mprotect(p, sz, PROT_READ | PROT_WRITE); - if (mopts.malloc_hint) - madvise(p, sz, MADV_NORMAL); - r->size -= psz; - d->free_regions_size -= psz; - if (zero_fill) - memset(p, 0, sz); - else if (mopts.malloc_junk && mopts.malloc_freeunmap) - memset(p, SOME_FREEJUNK, sz); - return p; - } - p = MMAP(sz); - if (p != MAP_FAILED) - malloc_used += sz; - if (d->free_regions_size > mopts.malloc_cache) - wrterror("malloc cache", NULL); - /* zero fill not needed */ - return p; -} - -/* - * Initialize a dir_info, which should have been cleared by caller - */ -static int -omalloc_init(struct dir_info **dp) -{ - char *p, b[64]; - int i, j; - size_t d_avail, regioninfo_size; - struct dir_info *d; - - rbytes_init(); - - /* - * Default options - */ - mopts.malloc_abort = 1; - mopts.malloc_move = 1; - mopts.malloc_cache = MALLOC_DEFAULT_CACHE; - - 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 '>': - mopts.malloc_cache <<= 1; - if (mopts.malloc_cache > MALLOC_MAXCACHE) - mopts.malloc_cache = MALLOC_MAXCACHE; - break; - case '<': - mopts.malloc_cache >>= 1; - break; - case 'a': - mopts.malloc_abort = 0; - break; - case 'A': - mopts.malloc_abort = 1; - break; -#ifdef MALLOC_STATS - case 'd': - mopts.malloc_stats = 0; - break; - case 'D': - mopts.malloc_stats = 1; - break; -#endif /* MALLOC_STATS */ - case 'f': - mopts.malloc_freenow = 0; - mopts.malloc_freeunmap = 0; - break; - case 'F': - mopts.malloc_freenow = 1; - mopts.malloc_freeunmap = 1; - break; - case 'g': - mopts.malloc_guard = 0; - break; - case 'G': - mopts.malloc_guard = MALLOC_PAGESIZE; - break; - case 'h': - mopts.malloc_hint = 0; - break; - case 'H': - mopts.malloc_hint = 1; - break; - case 'j': - mopts.malloc_junk = 0; - break; - case 'J': - mopts.malloc_junk = 1; - break; - case 'n': - case 'N': - break; - case 'p': - mopts.malloc_move = 0; - break; - case 'P': - mopts.malloc_move = 1; - break; - case 'r': - mopts.malloc_realloc = 0; - break; - case 'R': - mopts.malloc_realloc = 1; - break; - case 's': - mopts.malloc_freeunmap = mopts.malloc_junk = 0; - mopts.malloc_guard = 0; - mopts.malloc_cache = MALLOC_DEFAULT_CACHE; - break; - case 'S': - mopts.malloc_freeunmap = mopts.malloc_junk = 1; - mopts.malloc_guard = MALLOC_PAGESIZE; - mopts.malloc_cache = 0; - break; - case 'u': - mopts.malloc_freeunmap = 0; - break; - case 'U': - mopts.malloc_freeunmap = 1; - break; - case 'x': - mopts.malloc_xmalloc = 0; - break; - case 'X': - mopts.malloc_xmalloc = 1; - break; - case 'z': - mopts.malloc_zero = 0; - break; - case 'Z': - mopts.malloc_zero = 1; - break; - default: { - static const char q[] = "malloc() warning: " - "unknown char in MALLOC_OPTIONS\n"; - write(STDERR_FILENO, q, sizeof(q) - 1); - break; - } - } - } - } - - /* - * We want junk in the entire allocation, and zero only in the part - * the user asked for. - */ - if (mopts.malloc_zero) - mopts.malloc_junk = 1; - -#ifdef MALLOC_STATS - if (mopts.malloc_stats && (atexit(malloc_exit) == -1)) { - static const char q[] = "malloc() warning: atexit(2) failed." - " Will not be able to dump stats on exit\n"; - write(STDERR_FILENO, q, sizeof(q) - 1); - } -#endif /* MALLOC_STATS */ - - while ((mopts.malloc_canary = arc4random()) == 0) - ; - - /* - * Allocate dir_info with a guard page on either side. Also - * randomise offset inside the page at which the dir_info - * lies (subject to alignment by 1 << MALLOC_MINSHIFT) - */ - if ((p = MMAP(DIR_INFO_RSZ + (MALLOC_PAGESIZE * 2))) == MAP_FAILED) - return -1; - mprotect(p, MALLOC_PAGESIZE, PROT_NONE); - mprotect(p + MALLOC_PAGESIZE + DIR_INFO_RSZ, - MALLOC_PAGESIZE, PROT_NONE); - d_avail = (DIR_INFO_RSZ - sizeof(*d)) >> MALLOC_MINSHIFT; - d = (struct dir_info *)(p + MALLOC_PAGESIZE + - (arc4random_uniform(d_avail) << MALLOC_MINSHIFT)); - - d->regions_free = d->regions_total = MALLOC_INITIAL_REGIONS; - regioninfo_size = d->regions_total * sizeof(struct region_info); - d->r = MMAP(regioninfo_size); - if (d->r == MAP_FAILED) { - wrterror("malloc init mmap failed", NULL); - d->regions_total = 0; - return 1; - } - for (i = 0; i <= MALLOC_MAXSHIFT; i++) { - LIST_INIT(&d->chunk_info_list[i]); - LIST_INIT(&d->chunk_dir[i]); - } - malloc_used += regioninfo_size; - d->canary1 = mopts.malloc_canary ^ (u_int32_t)(uintptr_t)d; - d->canary2 = ~d->canary1; - - *dp = d; - - /* - * Options have been set and will never be reset. - * Prevent further tampering with them. - */ - if (((uintptr_t)&malloc_readonly & MALLOC_PAGEMASK) == 0) - mprotect(&malloc_readonly, sizeof(malloc_readonly), PROT_READ); - - return 0; -} - -static int -omalloc_grow(struct dir_info *d) -{ - 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; - - 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("munmap", d->r); - else - malloc_used -= d->regions_total * sizeof(struct region_info); - d->regions_free = d->regions_free + d->regions_total; - d->regions_total = newtotal; - d->r = p; - return 0; -} - -static struct chunk_info * -alloc_chunk_info(struct dir_info *d, int bits) -{ - struct chunk_info *p; - size_t size, count; - - if (bits == 0) - count = MALLOC_PAGESIZE / MALLOC_MINSIZE; - else - count = MALLOC_PAGESIZE >> bits; - - size = howmany(count, MALLOC_BITS); - size = sizeof(struct chunk_info) + (size - 1) * sizeof(u_short); - size = ALIGN(size); - - if (LIST_EMPTY(&d->chunk_info_list[bits])) { - char *q; - int i; - - q = MMAP(MALLOC_PAGESIZE); - if (q == MAP_FAILED) - return NULL; - malloc_used += MALLOC_PAGESIZE; - count = MALLOC_PAGESIZE / size; - for (i = 0; i < count; i++, q += size) - LIST_INSERT_HEAD(&d->chunk_info_list[bits], - (struct chunk_info *)q, entries); - } - p = LIST_FIRST(&d->chunk_info_list[bits]); - LIST_REMOVE(p, entries); - memset(p, 0, size); - p->canary = d->canary1; - return p; -} - - -/* - * The hashtable uses the assumption that p is never NULL. This holds since - * non-MAP_FIXED mappings with hint 0 start at BRKSIZ. - */ -static int -insert(struct dir_info *d, void *p, size_t sz, void *f) -{ - 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; -#ifdef MALLOC_STATS - d->r[index].f = f; -#endif - 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 (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) || - d->canary1 != ~d->canary2) - wrterror("internal struct corrupt", NULL); - 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 && r != NULL) ? &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", NULL); - 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; - int i, k; - - /* Allocate a new bucket */ - pp = map(d, MALLOC_PAGESIZE, 0); - if (pp == MAP_FAILED) - return NULL; - - bp = alloc_chunk_info(d, bits); - 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); - LIST_INSERT_HEAD(&d->chunk_info_list[0], bp, entries); - return NULL; - } - } else { - bp->size = 1U << 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] = (u_short)~0U; - - for (; i < k; i++) - bp->bits[i / MALLOC_BITS] |= (u_short)1U << (i % MALLOC_BITS); - - LIST_INSERT_HEAD(&d->chunk_dir[bits], bp, entries); - - bits++; - if ((uintptr_t)pp & bits) - wrterror("pp & bits", pp); - - insert(d, (void *)((uintptr_t)pp | bits), (uintptr_t)bp, NULL); - return bp; -} - - -/* - * Allocate a chunk - */ -static void * -malloc_bytes(struct dir_info *d, size_t size, void *f) -{ - int i, j; - size_t k; - u_short u, *lp; - struct chunk_info *bp; - - if (mopts.malloc_canary != (d->canary1 ^ (u_int32_t)(uintptr_t)d) || - d->canary1 != ~d->canary2) - wrterror("internal struct corrupt", NULL); - /* 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 */ - if (LIST_EMPTY(&d->chunk_dir[j])) { - bp = omalloc_make_chunks(d, j); - if (bp == NULL) - return NULL; - } else - bp = LIST_FIRST(&d->chunk_dir[j]); - - if (bp->canary != d->canary1) - wrterror("chunk info corrupted", NULL); - - i = d->chunk_start; - if (bp->free > 1) - i += getrnibble(); - if (i >= bp->total) - i &= bp->total - 1; - for (;;) { - for (;;) { - lp = &bp->bits[i / MALLOC_BITS]; - if (!*lp) { - i += MALLOC_BITS; - i &= ~(MALLOC_BITS - 1); - if (i >= bp->total) - i = 0; - } else - break; - } - k = i % MALLOC_BITS; - u = 1 << k; - if (*lp & u) - break; - if (++i >= bp->total) - i = 0; - } - d->chunk_start += i + 1; -#ifdef MALLOC_STATS - if (i == 0) { - struct region_info *r = find(d, bp->page); - r->f = f; - } -#endif - - *lp ^= u; - - /* If there are no more free, remove from free-list */ - if (!--bp->free) - LIST_REMOVE(bp, entries); - - /* Adjust to the real offset of that chunk */ - k += (lp - bp->bits) * MALLOC_BITS; - k <<= bp->shift; - - if (mopts.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_head *mp; - struct chunk_info *info; - int i; - - info = (struct chunk_info *)r->size; - if (info->canary != d->canary1) - wrterror("chunk info corrupted", NULL); - - /* Find the chunk number on the page */ - i = ((uintptr_t)ptr & MALLOC_PAGEMASK) >> info->shift; - - if ((uintptr_t)ptr & ((1U << (info->shift)) - 1)) { - wrterror("modified chunk-pointer", ptr); - return; - } - if (info->bits[i / MALLOC_BITS] & (1U << (i % MALLOC_BITS))) { - wrterror("chunk is already free", ptr); - return; - } - - info->bits[i / MALLOC_BITS] |= 1U << (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 */ - LIST_INSERT_HEAD(mp, info, entries); - return; - } - if (info->free != info->total) - return; - - LIST_REMOVE(info, entries); - - if (info->size == 0 && !mopts.malloc_freeunmap) - mprotect(info->page, MALLOC_PAGESIZE, PROT_READ | PROT_WRITE); - unmap(d, info->page, MALLOC_PAGESIZE); - - delete(d, r); - if (info->size != 0) - mp = &d->chunk_info_list[info->shift]; - else - mp = &d->chunk_info_list[0]; - LIST_INSERT_HEAD(mp, info, entries); -} - - - -static void * -omalloc(size_t sz, int zero_fill, void *f) -{ - void *p; - size_t psz; - - if (sz > MALLOC_MAXCHUNK) { - if (sz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { - errno = ENOMEM; - return NULL; - } - sz += mopts.malloc_guard; - psz = PAGEROUND(sz); - p = map(g_pool, psz, zero_fill); - if (p == MAP_FAILED) { - errno = ENOMEM; - return NULL; - } - if (insert(g_pool, p, sz, f)) { - unmap(g_pool, p, psz); - errno = ENOMEM; - return NULL; - } - if (mopts.malloc_guard) { - if (mprotect((char *)p + psz - mopts.malloc_guard, - mopts.malloc_guard, PROT_NONE)) - wrterror("mprotect", NULL); - malloc_guarded += mopts.malloc_guard; - } - - if (mopts.malloc_move && - sz - mopts.malloc_guard < MALLOC_PAGESIZE - - MALLOC_LEEWAY) { - /* fill whole allocation */ - if (mopts.malloc_junk) - memset(p, SOME_JUNK, psz - mopts.malloc_guard); - /* shift towards the end */ - p = ((char *)p) + ((MALLOC_PAGESIZE - MALLOC_LEEWAY - - (sz - mopts.malloc_guard)) & ~(MALLOC_MINSIZE-1)); - /* fill zeros if needed and overwritten above */ - if (zero_fill && mopts.malloc_junk) - memset(p, 0, sz - mopts.malloc_guard); - } else { - if (mopts.malloc_junk) { - if (zero_fill) - memset((char *)p + sz - mopts.malloc_guard, - SOME_JUNK, psz - sz); - else - memset(p, SOME_JUNK, - psz - mopts.malloc_guard); - } - } - - } else { - /* takes care of SOME_JUNK */ - p = malloc_bytes(g_pool, sz, f); - 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; - wrterror("recursive call", NULL); - } - malloc_active--; - _MALLOC_UNLOCK(); - errno = EDEADLK; -} - -static int -malloc_init(void) -{ - if (omalloc_init(&g_pool)) { - _MALLOC_UNLOCK(); - if (mopts.malloc_xmalloc) - wrterror("out of memory", NULL); - errno = ENOMEM; - return -1; - } - return 0; -} - -void * -malloc(size_t size) -{ - void *r; - int saved_errno = errno; - - _MALLOC_LOCK(); - malloc_func = " in malloc():"; - if (g_pool == NULL) { - if (malloc_init() != 0) - return NULL; - } - if (malloc_active++) { - malloc_recurse(); - return NULL; - } - r = omalloc(size, mopts.malloc_zero, CALLER); - malloc_active--; - _MALLOC_UNLOCK(); - if (r == NULL && mopts.malloc_xmalloc) { - wrterror("out of memory", NULL); - errno = ENOMEM; - } - if (r != NULL) - errno = saved_errno; - return r; -} - -static void -ofree(void *p) -{ - struct region_info *r; - size_t sz; - - r = find(g_pool, p); - if (r == NULL) { - wrterror("bogus pointer (double free?)", p); - return; - } - REALSIZE(sz, r); - if (sz > MALLOC_MAXCHUNK) { - if (sz - mopts.malloc_guard >= MALLOC_PAGESIZE - - MALLOC_LEEWAY) { - if (r->p != p) { - wrterror("bogus pointer", p); - return; - } - } else { -#if notyetbecause_of_realloc - /* shifted towards the end */ - if (p != ((char *)r->p) + ((MALLOC_PAGESIZE - - MALLOC_MINSIZE - sz - mopts.malloc_guard) & - ~(MALLOC_MINSIZE-1))) { - } -#endif - p = r->p; - } - if (mopts.malloc_guard) { - if (sz < mopts.malloc_guard) - wrterror("guard size", NULL); - if (!mopts.malloc_freeunmap) { - if (mprotect((char *)p + PAGEROUND(sz) - - mopts.malloc_guard, mopts.malloc_guard, - PROT_READ | PROT_WRITE)) - wrterror("mprotect", NULL); - } - malloc_guarded -= mopts.malloc_guard; - } - if (mopts.malloc_junk && !mopts.malloc_freeunmap) - memset(p, SOME_FREEJUNK, - PAGEROUND(sz) - mopts.malloc_guard); - unmap(g_pool, p, PAGEROUND(sz)); - delete(g_pool, r); - } else { - void *tmp; - int i; - - if (mopts.malloc_junk && sz > 0) - memset(p, SOME_FREEJUNK, sz); - if (!mopts.malloc_freenow) { - i = getrnibble(); - 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) { - wrterror("bogus pointer (double free?)", p); - return; - } - free_bytes(g_pool, r, p); - } - } -} - -void -free(void *ptr) -{ - int saved_errno = errno; - - /* This is legal. */ - if (ptr == NULL) - return; - - _MALLOC_LOCK(); - malloc_func = " in free():"; - if (g_pool == NULL) { - _MALLOC_UNLOCK(); - wrterror("free() called before allocation", NULL); - return; - } - if (malloc_active++) { - malloc_recurse(); - return; - } - ofree(ptr); - malloc_active--; - _MALLOC_UNLOCK(); - errno = saved_errno; -} - - -static void * -orealloc(void *p, size_t newsz, void *f) -{ - struct region_info *r; - size_t oldsz, goldsz, gnewsz; - void *q; - - if (p == NULL) - return omalloc(newsz, 0, f); - - r = find(g_pool, p); - if (r == NULL) { - wrterror("bogus pointer (double free?)", p); - return NULL; - } - if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { - errno = ENOMEM; - return NULL; - } - - REALSIZE(oldsz, r); - goldsz = oldsz; - if (oldsz > MALLOC_MAXCHUNK) { - if (oldsz < mopts.malloc_guard) - wrterror("guard size", NULL); - oldsz -= mopts.malloc_guard; - } - - gnewsz = newsz; - if (gnewsz > MALLOC_MAXCHUNK) - gnewsz += mopts.malloc_guard; - - if (newsz > MALLOC_MAXCHUNK && oldsz > MALLOC_MAXCHUNK && p == r->p && - !mopts.malloc_realloc) { - size_t roldsz = PAGEROUND(goldsz); - size_t rnewsz = PAGEROUND(gnewsz); - - if (rnewsz > roldsz) { - if (!mopts.malloc_guard) { - void *hint = (char *)p + roldsz; - size_t needed = rnewsz - roldsz; - - STATS_INC(g_pool->cheap_realloc_tries); - zapcacheregion(g_pool, hint, needed); - q = MQUERY(hint, needed); - if (q == hint) - q = MMAPA(hint, needed); - else - q = MAP_FAILED; - if (q == hint) { - malloc_used += needed; - if (mopts.malloc_junk) - memset(q, SOME_JUNK, needed); - r->size = newsz; - STATS_SETF(r, f); - STATS_INC(g_pool->cheap_reallocs); - return p; - } else if (q != MAP_FAILED) { - if (munmap(q, needed)) - wrterror("munmap", q); - } - } - } else if (rnewsz < roldsz) { - if (mopts.malloc_guard) { - if (mprotect((char *)p + roldsz - - mopts.malloc_guard, mopts.malloc_guard, - PROT_READ | PROT_WRITE)) - wrterror("mprotect", NULL); - if (mprotect((char *)p + rnewsz - - mopts.malloc_guard, mopts.malloc_guard, - PROT_NONE)) - wrterror("mprotect", NULL); - } - unmap(g_pool, (char *)p + rnewsz, roldsz - rnewsz); - r->size = gnewsz; - STATS_SETF(r, f); - return p; - } else { - if (newsz > oldsz && mopts.malloc_junk) - memset((char *)p + newsz, SOME_JUNK, - rnewsz - mopts.malloc_guard - newsz); - r->size = gnewsz; - STATS_SETF(r, f); - return p; - } - } - if (newsz <= oldsz && newsz > oldsz / 2 && !mopts.malloc_realloc) { - if (mopts.malloc_junk && newsz > 0) - memset((char *)p + newsz, SOME_JUNK, oldsz - newsz); - STATS_SETF(r, f); - return p; - } else if (newsz != oldsz || mopts.malloc_realloc) { - q = omalloc(newsz, 0, f); - if (q == NULL) - return NULL; - if (newsz != 0 && oldsz != 0) - memcpy(q, p, oldsz < newsz ? oldsz : newsz); - ofree(p); - return q; - } else { - STATS_SETF(r, f); - return p; - } -} - -void * -realloc(void *ptr, size_t size) -{ - void *r; - int saved_errno = errno; - - _MALLOC_LOCK(); - malloc_func = " in realloc():"; - if (g_pool == NULL) { - if (malloc_init() != 0) - return NULL; - } - if (malloc_active++) { - malloc_recurse(); - return NULL; - } - r = orealloc(ptr, size, CALLER); - - malloc_active--; - _MALLOC_UNLOCK(); - if (r == NULL && mopts.malloc_xmalloc) { - wrterror("out of memory", NULL); - errno = ENOMEM; - } - if (r != NULL) - errno = saved_errno; - return r; -} - - -#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) - -void * -calloc(size_t nmemb, size_t size) -{ - void *r; - int saved_errno = errno; - - _MALLOC_LOCK(); - malloc_func = " in calloc():"; - if (g_pool == NULL) { - if (malloc_init() != 0) - return NULL; - } - if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && - nmemb > 0 && SIZE_MAX / nmemb < size) { - _MALLOC_UNLOCK(); - if (mopts.malloc_xmalloc) - wrterror("out of memory", NULL); - errno = ENOMEM; - return NULL; - } - - if (malloc_active++) { - malloc_recurse(); - return NULL; - } - - size *= nmemb; - r = omalloc(size, 1, CALLER); - - malloc_active--; - _MALLOC_UNLOCK(); - if (r == NULL && mopts.malloc_xmalloc) { - wrterror("out of memory", NULL); - errno = ENOMEM; - } - if (r != NULL) - errno = saved_errno; - return r; -} - -static void * -mapalign(struct dir_info *d, size_t alignment, size_t sz, int zero_fill) -{ - char *p, *q; - - if (alignment < MALLOC_PAGESIZE || ((alignment - 1) & alignment) != 0) { - wrterror("mapalign bad alignment", NULL); - return MAP_FAILED; - } - if (sz != PAGEROUND(sz)) { - wrterror("mapalign round", NULL); - return MAP_FAILED; - } - - /* Allocate sz + alignment bytes of memory, which must include a - * subrange of size bytes that is properly aligned. Unmap the - * other bytes, and then return that subrange. - */ - - /* We need sz + alignment to fit into a size_t. */ - if (alignment > SIZE_MAX - sz) - return MAP_FAILED; - - p = map(d, sz + alignment, zero_fill); - if (p == MAP_FAILED) - return MAP_FAILED; - q = (char *)(((uintptr_t)p + alignment - 1) & ~(alignment - 1)); - if (q != p) { - if (munmap(p, q - p)) - wrterror("munmap", p); - } - if (munmap(q + sz, alignment - (q - p))) - wrterror("munmap", q + sz); - malloc_used -= alignment; - - return q; -} - -static void * -omemalign(size_t alignment, size_t sz, int zero_fill, void *f) -{ - size_t psz; - void *p; - - if (alignment <= MALLOC_PAGESIZE) { - /* - * max(size, alignment) is enough to assure the requested alignment, - * since the allocator always allocates power-of-two blocks. - */ - if (sz < alignment) - sz = alignment; - return omalloc(sz, zero_fill, f); - } - - if (sz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { - errno = ENOMEM; - return NULL; - } - - sz += mopts.malloc_guard; - psz = PAGEROUND(sz); - - p = mapalign(g_pool, alignment, psz, zero_fill); - if (p == NULL) { - errno = ENOMEM; - return NULL; - } - - if (insert(g_pool, p, sz, f)) { - unmap(g_pool, p, psz); - errno = ENOMEM; - return NULL; - } - - if (mopts.malloc_guard) { - if (mprotect((char *)p + psz - mopts.malloc_guard, - mopts.malloc_guard, PROT_NONE)) - wrterror("mprotect", NULL); - malloc_guarded += mopts.malloc_guard; - } - - if (mopts.malloc_junk) { - if (zero_fill) - memset((char *)p + sz - mopts.malloc_guard, - SOME_JUNK, psz - sz); - else - memset(p, SOME_JUNK, psz - mopts.malloc_guard); - } - - return p; -} - -int -posix_memalign(void **memptr, size_t alignment, size_t size) -{ - int res, saved_errno = errno; - void *r; - - /* Make sure that alignment is a large enough power of 2. */ - if (((alignment - 1) & alignment) != 0 || alignment < sizeof(void *)) - return EINVAL; - - _MALLOC_LOCK(); - malloc_func = " in posix_memalign():"; - if (g_pool == NULL) { - if (malloc_init() != 0) - goto err; - } - if (malloc_active++) { - malloc_recurse(); - goto err; - } - r = omemalign(alignment, size, mopts.malloc_zero, CALLER); - malloc_active--; - _MALLOC_UNLOCK(); - if (r == NULL) { - if (mopts.malloc_xmalloc) { - wrterror("out of memory", NULL); - errno = ENOMEM; - } - goto err; - } - errno = saved_errno; - *memptr = r; - return 0; - -err: - res = errno; - errno = saved_errno; - return res; -} - -#ifdef MALLOC_STATS - -struct malloc_leak { - void (*f)(); - size_t total_size; - int count; -}; - -struct leaknode { - RB_ENTRY(leaknode) entry; - struct malloc_leak d; -}; - -static int -leakcmp(struct leaknode *e1, struct leaknode *e2) -{ - return e1->d.f < e2->d.f ? -1 : e1->d.f > e2->d.f; -} - -static RB_HEAD(leaktree, leaknode) leakhead; -RB_GENERATE_STATIC(leaktree, leaknode, entry, leakcmp) - -static void -putleakinfo(void *f, size_t sz, int cnt) -{ - struct leaknode key, *p; - static struct leaknode *page; - static int used; - - if (cnt == 0) - return; - - key.d.f = f; - p = RB_FIND(leaktree, &leakhead, &key); - if (p == NULL) { - if (page == NULL || - used >= MALLOC_PAGESIZE / sizeof(struct leaknode)) { - page = MMAP(MALLOC_PAGESIZE); - if (page == MAP_FAILED) - return; - used = 0; - } - p = &page[used++]; - p->d.f = f; - p->d.total_size = sz * cnt; - p->d.count = cnt; - RB_INSERT(leaktree, &leakhead, p); - } else { - p->d.total_size += sz * cnt; - p->d.count += cnt; - } -} - -static struct malloc_leak *malloc_leaks; - -static void -dump_leaks(int fd) -{ - struct leaknode *p; - char buf[64]; - int i = 0; - - snprintf(buf, sizeof(buf), "Leak report\n"); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), " f sum # avg\n"); - write(fd, buf, strlen(buf)); - /* XXX only one page of summary */ - if (malloc_leaks == NULL) - malloc_leaks = MMAP(MALLOC_PAGESIZE); - if (malloc_leaks != MAP_FAILED) - memset(malloc_leaks, 0, MALLOC_PAGESIZE); - RB_FOREACH(p, leaktree, &leakhead) { - snprintf(buf, sizeof(buf), "%12p %7zu %6u %6zu\n", p->d.f, - p->d.total_size, p->d.count, p->d.total_size / p->d.count); - write(fd, buf, strlen(buf)); - if (malloc_leaks == MAP_FAILED || - i >= MALLOC_PAGESIZE / sizeof(struct malloc_leak)) - continue; - malloc_leaks[i].f = p->d.f; - malloc_leaks[i].total_size = p->d.total_size; - malloc_leaks[i].count = p->d.count; - i++; - } -} - -static void -dump_chunk(int fd, struct chunk_info *p, void *f, int fromfreelist) -{ - char buf[64]; - - while (p != NULL) { - snprintf(buf, sizeof(buf), "chunk %12p %12p %4d %d/%d\n", - p->page, ((p->bits[0] & 1) ? NULL : f), - p->size, p->free, p->total); - write(fd, buf, strlen(buf)); - if (!fromfreelist) { - if (p->bits[0] & 1) - putleakinfo(NULL, p->size, p->total - p->free); - else { - putleakinfo(f, p->size, 1); - putleakinfo(NULL, p->size, - p->total - p->free - 1); - } - break; - } - p = LIST_NEXT(p, entries); - 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, count; - - snprintf(buf, sizeof(buf), "Free chunk structs:\n"); - write(fd, buf, strlen(buf)); - for (i = 0; i <= MALLOC_MAXSHIFT; i++) { - struct chunk_info *p; - - count = 0; - LIST_FOREACH(p, &d->chunk_info_list[i], entries) - count++; - p = LIST_FIRST(&d->chunk_dir[i]); - if (p == NULL && count == 0) - continue; - snprintf(buf, sizeof(buf), "%2d) %3d ", i, count); - write(fd, buf, strlen(buf)); - if (p != NULL) - dump_chunk(fd, p, NULL, 1); - else - write(fd, "\n", 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 < mopts.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)); - if (d == NULL) - return; - snprintf(buf, sizeof(buf), "Region slots free %zu/%zu\n", - d->regions_free, d->regions_total); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Finds %zu/%zu\n", d->finds, - d->find_collisions); - write(fd, buf, strlen(buf)); - snprintf(buf, sizeof(buf), "Inserts %zu/%zu\n", d->inserts, - d->insert_collisions); - 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), "Cheap reallocs %zu/%zu\n", - d->cheap_reallocs, d->cheap_realloc_tries); - write(fd, buf, strlen(buf)); - dump_free_chunk_info(fd, d); - dump_free_page_info(fd, d); - snprintf(buf, sizeof(buf), - "slot) hash d type page f size [free/n]\n"); - 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) #%4zx %zd ", - i, h, h - i); - write(fd, buf, strlen(buf)); - REALSIZE(realsize, &d->r[i]); - if (realsize > MALLOC_MAXCHUNK) { - putleakinfo(d->r[i].f, realsize, 1); - snprintf(buf, sizeof(buf), - "pages %12p %12p %zu\n", d->r[i].p, - d->r[i].f, realsize); - write(fd, buf, strlen(buf)); - } else - dump_chunk(fd, - (struct chunk_info *)d->r[i].size, - d->r[i].f, 0); - } - } - 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)); - dump_leaks(fd); - write(fd, "\n", 1); -} - -void -malloc_dump(int fd) -{ - int i; - void *p; - struct region_info *r; - int saved_errno = errno; - - for (i = 0; i <= MALLOC_DELAYED_CHUNKS; i++) { - p = g_pool->delayed_chunks[i]; - if (p == NULL) - continue; - r = find(g_pool, p); - if (r == NULL) - wrterror("bogus pointer in malloc_dump", p); - free_bytes(g_pool, r, p); - g_pool->delayed_chunks[i] = NULL; - } - /* XXX leak when run multiple times */ - RB_INIT(&leakhead); - malloc_dump1(fd, g_pool); - errno = saved_errno; -} - -static void -malloc_exit(void) -{ - static const 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, sizeof(q) - 1); - errno = save_errno; -} - -#endif /* MALLOC_STATS */ diff --git a/src/lib/libc/stdlib/merge.c b/src/lib/libc/stdlib/merge.c deleted file mode 100644 index 43ef8b08e2..0000000000 --- a/src/lib/libc/stdlib/merge.c +++ /dev/null @@ -1,333 +0,0 @@ -/* $OpenBSD: merge.c,v 1.9 2011/03/06 00:55:38 deraadt 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, sense; - u_char tmp, *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/posix_memalign.3 b/src/lib/libc/stdlib/posix_memalign.3 deleted file mode 100644 index 05ec1b9d14..0000000000 --- a/src/lib/libc/stdlib/posix_memalign.3 +++ /dev/null @@ -1,92 +0,0 @@ -.\" $OpenBSD: posix_memalign.3,v 1.3 2012/06/18 17:03:52 matthew Exp $ -.\" Copyright (C) 2006 Jason Evans . -.\" 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(s), this list of conditions and the following disclaimer as -.\" the first lines of this file unmodified other than the possible -.\" addition of one or more copyright notices. -.\" 2. Redistributions in binary form must reproduce the above copyright -.\" notice(s), 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 HOLDER(S) ``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 HOLDER(S) 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. -.\" -.\" $FreeBSD: src/lib/libc/stdlib/posix_memalign.3,v 1.3 2007/03/28 04:32:51 jasone Exp $ -.\" -.Dd $Mdocdate: June 18 2012 $ -.Dt POSIX_MEMALIGN 3 -.Os -.Sh NAME -.Nm posix_memalign -.Nd aligned memory allocation -.Sh SYNOPSIS -.In stdlib.h -.Ft int -.Fn posix_memalign "void **ptr" "size_t alignment" "size_t size" -.Sh DESCRIPTION -The -.Fn posix_memalign -function allocates -.Fa size -bytes of memory such that the allocation's base address is a multiple of -.Fa alignment , -and returns the allocation in the value pointed to by -.Fa ptr . -.Pp -The requested -.Fa alignment -must be a power of 2 at least as large as -.Fn sizeof "void *" . -.Pp -Memory that is allocated via -.Fn posix_memalign -can be used as an argument in subsequent calls to -.Xr realloc 3 -and -.Xr free 3 . -.Sh RETURN VALUES -The -.Fn posix_memalign -function returns the value 0 if successful; otherwise it returns an error value. -.Sh ERRORS -The -.Fn posix_memalign -function will fail if: -.Bl -tag -width Er -.It Bq Er EINVAL -The -.Fa alignment -parameter is not a power of 2 at least as large as -.Fn sizeof "void *" . -.It Bq Er ENOMEM -Memory allocation error. -.El -.Sh SEE ALSO -.Xr free 3 , -.Xr malloc 3 , -.Xr realloc 3 -.Sh STANDARDS -The -.Fn posix_memalign -function conforms to -.St -p1003.1-2001 . -.Sh HISTORY -The -.Fn posix_memalign -function first appeared in -.Ox 4.8 . diff --git a/src/lib/libc/stdlib/posix_openpt.3 b/src/lib/libc/stdlib/posix_openpt.3 deleted file mode 100644 index 376772bb07..0000000000 --- a/src/lib/libc/stdlib/posix_openpt.3 +++ /dev/null @@ -1,102 +0,0 @@ -.\" $OpenBSD: posix_openpt.3,v 1.3 2012/12/05 06:40:59 jmc Exp $ -.\" -.\" Copyright (c) 2012 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: December 5 2012 $ -.Dt POSIX_OPENPT 3 -.Os -.Sh NAME -.Nm posix_openpt -.Nd open a pseudo-terminal device -.Sh SYNOPSIS -.In stdlib.h -.In fcntl.h -.Ft int -.Fn posix_openpt "int oflag" -.Sh DESCRIPTION -The -.Fn posix_openpt -function finds the next available pseudo-terminal and returns an open -file descriptor for its master device. -The path name of the slave device may be determined via the -.Fn ptsname -function. -Note that the -.Fn unlockpt -and -.Fn grantpt -functions should be called before opening the slave device. -.Pp -The -.Ar oflag -argument is formed by bitwise-inclusive -.Tn OR Ns 'ing -the following values defined in -.In fcntl.h : -.Bl -tag -width O_NOCTTY -offset indent -.It Dv O_RDWR -Open for reading and writing. -.It Dv O_NOCTTY -Prevent the device from being made the controlling terminal for the session. -This flag has no effect on -.Ox -and is included for compatibility with other systems. -.El -.Pp -The -.Dv O_RDWR -flag must be specified in -.Fa oflag . -If -.Fa oflag -contains values other than those listed above, -.Fn posix_openpt -will return an error. -.Sh RETURN VALUES -If successful, -.Fn posix_openpt -returns a non-negative integer, the file descriptor for the -pseudo-terminal master device. -Otherwise, a value of \-1 is returned and -.Va errno -is set to indicate the error. -.Sh ERRORS -The -.Fn posix_openpt -function will fail if: -.Bl -tag -width Er -.It Bq Er EMFILE -The per-process descriptor table is full. -.It Bq Er ENFILE -The system file table is full. -.It Bq Er EINVAL -The value of -.Fa oflag -is not valid. -.El -.Sh SEE ALSO -.Xr ptsname 3 , -.Xr pty 4 , -.Xr tty 4 -.Sh STANDARDS -The -.Fn posix_openpt -function conforms to -.St -p1003.1-2001 . -.Sh HISTORY -The -.Fn posix_openpt -function appeared in -.Ox 5.3 . diff --git a/src/lib/libc/stdlib/posix_pty.c b/src/lib/libc/stdlib/posix_pty.c deleted file mode 100644 index a2025ddbb6..0000000000 --- a/src/lib/libc/stdlib/posix_pty.c +++ /dev/null @@ -1,119 +0,0 @@ -/* $OpenBSD: posix_pty.c,v 1.1 2012/12/03 20:08:33 millert Exp $ */ - -/* - * Copyright (c) 2012 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. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -int -posix_openpt(int oflag) -{ - struct ptmget ptm; - int fd, mfd = -1; - - /* User must specify O_RDWR in oflag. */ - if (!(oflag & O_RDWR)) { - errno = EINVAL; - return -1; - } - - /* Get pty master and slave (this API only uses the master). */ - fd = open(PATH_PTMDEV, O_RDWR); - if (fd != -1) { - if (ioctl(fd, PTMGET, &ptm) != -1) { - close(ptm.sfd); - mfd = ptm.cfd; - } - close(fd); - } - - return mfd; -} - -/* - * Look up the name of the specified pty master fd. - * Note that the name returned does *not* include the /dev/ prefix. - * Returns the name on success and NULL on error, setting errno. - */ -static const char * -ptmname(int mfd) -{ - struct stat sb; - const char *name; - - /* Make sure it is a pty master. */ - if (fstat(mfd, &sb) != 0) - return NULL; - if (!S_ISCHR(sb.st_mode)) { - errno = EINVAL; - return NULL; - } - name = devname(sb.st_rdev, S_IFCHR); - if (strncmp(name, "pty", 3) != 0) { - errno = EINVAL; - return NULL; - } - return name; -} - -/* - * The PTMGET ioctl handles the mode and owner for us. - */ -int -grantpt(int mfd) -{ - return ptmname(mfd) ? 0 : -1; -} - -/* - * The PTMGET ioctl unlocks the pty master and slave for us. - */ -int -unlockpt(int mfd) -{ - return ptmname(mfd) ? 0 : -1; -} - -/* - * Look up the path of the slave pty that corresponds to the master fd. - * Returns the path if successful or NULL on error. - */ -char * -ptsname(int mfd) -{ - const char *master; - static char slave[sizeof(((struct ptmget *)NULL)->sn)]; - - if ((master = ptmname(mfd)) == NULL) - return NULL; - - /* Add /dev/ prefix and convert "pty" to "tty". */ - strlcpy(slave, _PATH_DEV, sizeof(slave)); - strlcat(slave, master, sizeof(slave)); - slave[sizeof(_PATH_DEV) - 1] = 't'; - - return slave; -} diff --git a/src/lib/libc/stdlib/ptsname.3 b/src/lib/libc/stdlib/ptsname.3 deleted file mode 100644 index 98705528f5..0000000000 --- a/src/lib/libc/stdlib/ptsname.3 +++ /dev/null @@ -1,160 +0,0 @@ -.\" $OpenBSD: ptsname.3,v 1.2 2012/12/04 18:42:16 millert Exp $ -.\" -.\" Copyright (c) 2002 The FreeBSD Project, Inc. -.\" All rights reserved. -.\" -.\" This software includes code contributed to the FreeBSD Project -.\" by Ryan Younce of North Carolina State 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 FreeBSD Project 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 FREEBSD PROJECT 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 FREEBSD PROJECT -.\" OR ITS 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. -.\" -.\" $FreeBSD: head/lib/libc/stdlib/ptsname.3 240412 2012-09-12 17:54:09Z emaste $ -.\" -.Dd $Mdocdate: December 4 2012 $ -.Dt PTSNAME 3 -.Os -.Sh NAME -.Nm grantpt , -.Nm ptsname , -.Nm unlockpt -.Nd pseudo-terminal access functions -.Sh SYNOPSIS -.In stdlib.h -.Ft int -.Fn grantpt "int fildes" -.Ft "char *" -.Fn ptsname "int fildes" -.Ft int -.Fn unlockpt "int fildes" -.Sh DESCRIPTION -The -.Fn grantpt , -.Fn ptsname , -and -.Fn unlockpt -functions allow access to pseudo-terminal devices. -These three functions accept a file descriptor that references the -master half of a pseudo-terminal pair. -This file descriptor is created with -.Xr posix_openpt 3 . -.Pp -The -.Fn grantpt -function is used to establish ownership and permissions -of the slave device counterpart to the master device -specified with -.Fa fildes . -The slave device's ownership is set to the real user ID -of the calling process, and the permissions are set to -user readable-writable and group writable. -The group owner of the slave device is also set to the -group -.Dq Li tty . -.Pp -The -.Fn ptsname -function returns the full path name of the slave device -counterpart to the master device specified with -.Fa fildes . -This value can be used -to subsequently open the appropriate slave after -.Xr posix_openpt 3 -and -.Fn grantpt -have been called. -.Pp -The -.Fn unlockpt -function clears the lock held on the pseudo-terminal pair -for the master device specified with -.Fa fildes . -.Sh RETURN VALUES -.Rv -std grantpt unlockpt -.Pp -The -.Fn ptsname -function returns a pointer to the name -of the slave device on success; otherwise a -.Dv NULL -pointer is returned. -.Sh ERRORS -The -.Fn grantpt , -.Fn ptsname -and -.Fn unlockpt -functions may fail and set -.Va errno -to: -.Bl -tag -width Er -.It Bq Er EBADF -.Fa fildes -is not a valid open file descriptor. -.It Bq Er EINVAL -.Fa fildes -is not a master pseudo-terminal device. -.El -.Pp -In addition, the -.Fn grantpt -function may set -.Va errno -to: -.Bl -tag -width Er -.It Bq Er EACCES -The slave pseudo-terminal device could not be accessed. -.El -.Sh SEE ALSO -.Xr posix_openpt 3 , -.Xr pty 4 , -.Xr tty 4 -.Sh STANDARDS -The -.Fn ptsname -function conforms to -.St -p1003.1-2008 . -.Pp -This implementation of -.Fn grantpt -and -.Fn unlockpt -does not conform to -.St -p1003.1-2008 , -because it depends on -.Xr posix_openpt 3 -to create the pseudo-terminal device with proper permissions in place. -It only validates whether -.Fa fildes -is a valid pseudo-terminal master device. -Future revisions of the specification will likely allow this behaviour, -as stated by the Austin Group. -.Sh HISTORY -The -.Fn grantpt , -.Fn ptsname -and -.Fn unlockpt -functions appeared in -.Ox 5.3 . diff --git a/src/lib/libc/stdlib/qabs.3 b/src/lib/libc/stdlib/qabs.3 deleted file mode 100644 index 6703072e72..0000000000 --- a/src/lib/libc/stdlib/qabs.3 +++ /dev/null @@ -1,56 +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.13 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt QABS 3 -.Os -.Sh NAME -.Nm qabs -.Nd return the absolute value of a quad integer -.Sh SYNOPSIS -.In stdlib.h -.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 -.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 5f5275493e..0000000000 --- a/src/lib/libc/stdlib/qdiv.3 +++ /dev/null @@ -1,61 +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.10 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt QDIV 3 -.Os -.Sh NAME -.Nm qdiv -.Nd return quotient and remainder from division -.Sh SYNOPSIS -.In stdlib.h -.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 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 4481a96ee4..0000000000 --- a/src/lib/libc/stdlib/qsort.3 +++ /dev/null @@ -1,238 +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.17 2013/07/17 05:42:11 schwarze Exp $ -.\" -.Dd $Mdocdate: July 17 2013 $ -.Dt QSORT 3 -.Os -.Sh NAME -.Nm qsort , -.Nm heapsort , -.Nm mergesort -.Nd sort functions -.Sh SYNOPSIS -.In stdlib.h -.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 . -.Sh HISTORY -A -.Fn qsort -function first appeared in -.At v3 . diff --git a/src/lib/libc/stdlib/qsort.c b/src/lib/libc/stdlib/qsort.c deleted file mode 100644 index f28449fb5b..0000000000 --- a/src/lib/libc/stdlib/qsort.c +++ /dev/null @@ -1,162 +0,0 @@ -/* $OpenBSD: qsort.c,v 1.11 2010/02/08 11:04:07 otto 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 *, size_t, 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) { \ - size_t 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, size_t 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 cmp_result, swaptype, swap_cnt; - size_t d, r; - 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 && (cmp_result = cmp(pb, a)) <= 0) { - if (cmp_result == 0) { - swap_cnt = 1; - swap(pa, pb); - pa += es; - } - pb += es; - } - while (pb <= pc && (cmp_result = cmp(pc, a)) >= 0) { - if (cmp_result == 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 e62f760270..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.12 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.Dt RADIXSORT 3 -.Os -.Sh NAME -.Nm radixsort , -.Nm sradixsort -.Nd radix sort -.Sh SYNOPSIS -.In limits.h -.In stdlib.h -.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 75395976f8..0000000000 --- a/src/lib/libc/stdlib/rand.3 +++ /dev/null @@ -1,105 +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.15 2014/04/07 17:57:56 schwarze Exp $ -.\" -.Dd $Mdocdate: April 7 2014 $ -.Dt RAND 3 -.Os -.Sh NAME -.Nm rand , -.Nm rand_r , -.Nm srand -.Nd bad random number generator -.Sh SYNOPSIS -.In stdlib.h -.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 -.In 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 -.St -p1003.1-2008 . -.Sh HISTORY -The functions -.Fn rand -and -.Fn srand -first appeared in -.At v3 . diff --git a/src/lib/libc/stdlib/rand.c b/src/lib/libc/stdlib/rand.c deleted file mode 100644 index 6860dd4f71..0000000000 --- a/src/lib/libc/stdlib/rand.c +++ /dev/null @@ -1,67 +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)); -} - -#if defined(APIWARN) -__warn_references(rand_r, - "warning: rand_r() isn't random; consider using arc4random()"); -#endif - -int -rand(void) -{ - return (rand_r(&next)); -} - -#if defined(APIWARN) -__warn_references(rand, - "warning: rand() isn't random; consider using arc4random()"); -#endif - -void -srand(u_int seed) -{ - next = seed; -} - -#if defined(APIWARN) -__warn_references(srand, - "warning: srand() seed choices are invariably poor"); -#endif diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3 deleted file mode 100644 index 8b7c572fb4..0000000000 --- a/src/lib/libc/stdlib/rand48.3 +++ /dev/null @@ -1,179 +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.14 2014/01/21 23:25:03 deraadt Exp $ -.\" -.Dd $Mdocdate: January 21 2014 $ -.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 -.In stdlib.h -.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 arc4random 3 . -.Sh SEE ALSO -.Xr arc4random 3 , -.Xr rand 3 , -.Xr random 3 -.Sh STANDARDS -The -.Fn drand48 , -.Fn erand48 , -.Fn jrand48 , -.Fn lcong48 , -.Fn lrand48 , -.Fn mrand48 , -.Fn nrand48 , -.Fn seed48 , -and -.Fn srand48 -functions conform to -.St -p1003.1-2008 . -.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 fdb78848d8..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.21 2013/06/05 03:39:23 tedu Exp $ -.\" -.Dd $Mdocdate: June 5 2013 $ -.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 -.In stdlib.h -.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 "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 00edf2dca1..0000000000 --- a/src/lib/libc/stdlib/random.c +++ /dev/null @@ -1,446 +0,0 @@ -/* $OpenBSD: random.c,v 1.19 2013/08/01 19:42:08 kettenis 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 - -#include "thread_private.h" - -/* - * 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; - -_THREAD_PRIVATE_MUTEX(random); -static long random_l(void); - -#define LOCK() _THREAD_PRIVATE_MUTEX_LOCK(random) -#define UNLOCK() _THREAD_PRIVATE_MUTEX_UNLOCK(random) - -/* - * 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. - */ -static void -srandom_l(unsigned int x) -{ - int i; - int32_t test; - div_t val; - - if (rand_type == TYPE_0) - state[0] = x; - else { - /* A seed of 0 would result in state[] always being zero. */ - state[0] = x ? x : 1; - 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_l(); - } -} - -void -srandom(unsigned int x) -{ - LOCK(); - srandom_l(x); - UNLOCK(); -} - -#if defined(APIWARN) -__warn_references(srandom, - "warning: srandom() seed choices are invariably poor"); -#endif - -/* - * 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; - - LOCK(); - 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]; - } - UNLOCK(); -} - -#if defined(APIWARN) -__warn_references(srandomdev, - "warning: srandomdev() usage; consider switching to arc4random()"); -#endif - -/* - * 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]); - - LOCK(); - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = MAX_TYPES * (rptr - state) + rand_type; - if (n < BREAK_0) { - UNLOCK(); - 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_l(seed); - if (rand_type == TYPE_0) - state[-1] = rand_type; - else - state[-1] = MAX_TYPES*(rptr - state) + rand_type; - UNLOCK(); - 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(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]); - - LOCK(); - 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: - UNLOCK(); - 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 */ - UNLOCK(); - 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. - */ -static long -random_l(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); -} - -long -random(void) -{ - long r; - LOCK(); - r = random_l(); - UNLOCK(); - return r; -} - -#if defined(APIWARN) -__warn_references(random, - "warning: random() isn't random; consider using arc4random()"); -#endif diff --git a/src/lib/libc/stdlib/realpath.3 b/src/lib/libc/stdlib/realpath.3 deleted file mode 100644 index 5966b38058..0000000000 --- a/src/lib/libc/stdlib/realpath.3 +++ /dev/null @@ -1,130 +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.19 2014/01/20 22:40:06 schwarze Exp $ -.\" -.Dd $Mdocdate: January 20 2014 $ -.Dt REALPATH 3 -.Os -.Sh NAME -.Nm realpath -.Nd returns the canonicalized absolute pathname -.Sh SYNOPSIS -.In limits.h -.In stdlib.h -.Ft "char *" -.Fn realpath "const char *pathname" "char *resolved" -.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, or be -.Dv NULL . -.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 -.Fa resolved -is -.Dv NULL -and no error occurred, then -.Fn realpath -returns a NUL-terminated string in a newly allocated buffer. -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 STANDARDS -The -.Fn realpath -function conforms to -.St -p1003.1-2008 . -.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 e0f9b123b3..0000000000 --- a/src/lib/libc/stdlib/realpath.c +++ /dev/null @@ -1,214 +0,0 @@ -/* $OpenBSD: realpath.c,v 1.16 2013/04/05 12:59:54 kurt 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 - -/* A slightly modified copy of this file exists in libexec/ld.so */ - -/* - * 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) -{ - struct stat sb; - char *p, *q, *s; - size_t left_len, resolved_len; - unsigned symlinks; - int serrno, slen, mem_allocated; - char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX]; - - if (path[0] == '\0') { - errno = ENOENT; - return (NULL); - } - - serrno = errno; - - if (resolved == NULL) { - resolved = malloc(PATH_MAX); - if (resolved == NULL) - return (NULL); - mem_allocated = 1; - } else - mem_allocated = 0; - - 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) { - if (mem_allocated) - free(resolved); - else - 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; - goto err; - } - - /* - * 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; - goto err; - } - 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; - goto err; - } - 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; - goto err; - } - if (lstat(resolved, &sb) != 0) { - if (errno == ENOENT && p == NULL) { - errno = serrno; - return (resolved); - } - goto err; - } - if (S_ISLNK(sb.st_mode)) { - if (symlinks++ > MAXSYMLINKS) { - errno = ELOOP; - goto err; - } - slen = readlink(resolved, symlink, sizeof(symlink) - 1); - if (slen < 0) - goto err; - 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; - goto err; - } - symlink[slen] = '/'; - symlink[slen + 1] = 0; - } - left_len = strlcat(symlink, left, sizeof(symlink)); - if (left_len >= sizeof(left)) { - errno = ENAMETOOLONG; - goto err; - } - } - 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); - -err: - if (mem_allocated) - free(resolved); - return (NULL); -} 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 9060fdba88..0000000000 --- a/src/lib/libc/stdlib/setenv.c +++ /dev/null @@ -1,180 +0,0 @@ -/* $OpenBSD: setenv.c,v 1.14 2012/09/23 16:08:04 jeremy 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 -#include - -char *__findenv(const char *name, int len, int *offset); - -extern char **environ; -static char **lastenv; /* last value of environ */ - -/* - * putenv -- - * Add a name=value string directly to the environmental, replacing - * any current value. - */ -int -putenv(char *str) -{ - char **P, *cp; - size_t cnt; - int offset = 0; - - for (cp = str; *cp && *cp != '='; ++cp) - ; - if (*cp != '=') { - errno = EINVAL; - return (-1); /* missing `=' in string */ - } - - if (__findenv(str, (int)(cp - str), &offset) != NULL) { - environ[offset++] = str; - /* could be set multiple times */ - while (__findenv(str, (int)(cp - str), &offset)) { - for (P = &environ[offset];; ++P) - if (!(*P = *(P + 1))) - break; - } - return (0); - } - - /* create new slot for string */ - 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; - environ[cnt] = str; - environ[cnt + 1] = NULL; - return (0); -} - -/* - * 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) -{ - char *C, **P; - const char *np; - int l_value, offset = 0; - - if (!name || !*name) { - errno = EINVAL; - return (-1); - } - for (np = name; *np && *np != '='; ++np) - ; - if (*np) { - errno = EINVAL; - return (-1); /* has `=' in name */ - } - - l_value = strlen(value); - if ((C = __findenv(name, (int)(np - name), &offset)) != NULL) { - int tmpoff = offset + 1; - if (!rewrite) - return (0); -#if 0 /* XXX - existing entry may not be writable */ - if (strlen(C) >= l_value) { /* old larger; copy over */ - while ((*C++ = *value++)) - ; - return (0); - } -#endif - /* could be set multiple times */ - while (__findenv(name, (int)(np - name), &tmpoff)) { - for (P = &environ[tmpoff];; ++P) - if (!(*P = *(P + 1))) - break; - } - } else { /* create new slot */ - size_t cnt; - - 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; - } - if (!(environ[offset] = /* name + `=' + value */ - malloc((size_t)((int)(np - 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". - */ -int -unsetenv(const char *name) -{ - char **P; - const char *np; - int offset = 0; - - if (!name || !*name) { - errno = EINVAL; - return (-1); - } - for (np = name; *np && *np != '='; ++np) - ; - if (*np) { - errno = EINVAL; - return (-1); /* has `=' in name */ - } - - /* could be set multiple times */ - while (__findenv(name, (int)(np - name), &offset)) { - for (P = &environ[offset];; ++P) - if (!(*P = *(P + 1))) - break; - } - return (0); -} 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 6f079b73a9..0000000000 --- a/src/lib/libc/stdlib/strtod.3 +++ /dev/null @@ -1,167 +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.19 2014/01/19 10:39:00 schwarze Exp $ -.\" -.Dd $Mdocdate: January 19 2014 $ -.Dt STRTOD 3 -.Os -.Sh NAME -.Nm strtod , -.Nm strtof , -.Nm strtold -.Nd convert ASCII string to double, float or long double -.Sh SYNOPSIS -.In stdlib.h -.Ft double -.Fn strtod "const char *nptr" "char **endptr" -.Pp -.Ft float -.Fn strtof "const char *nptr" "char **endptr" -.Pp -.Ft long double -.Fn strtold "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. -The -.Fn strtold -function converts the initial portion of the string pointed to by -.Fa nptr -to -.Li long double -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 -Alternatively, if the portion of the string following the optional -plus or minus sign begins with -.Dq INF -or -.Dq NAN , -ignoring case, it is interpreted as an infinity or a quiet \*(Na, -respectively. -The syntax -.Dq NAN Ns Pq Ar s , -where -.Ar s -is an alphanumeric string, produces the same value as the call -.Fo nan -.Qq Ar s Ns -.Fc -(respectively, -.Fo nanf -.Qq Ar s Ns -.Fc -and -.Fo nanl -.Qq Ar s Ns -.Fc . ) -.Pp -In any of the above cases, leading whitespace characters in the -string (as defined by the -.Xr isspace 3 -function) are skipped. -.Sh RETURN VALUES -The -.Fn strtod , -.Fn strtof -and -.Fn strtold -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 -function conforms to -.St -ansiC-89 . -The -.Fn strtof -and -.Fn strtold -functions conform to -.St -ansiC-99 . 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 c0a60979d9..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.24 2013/08/14 06:32:28 jmc Exp $ -.\" -.Dd $Mdocdate: August 14 2013 $ -.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 -.In limits.h -.In stdlib.h -.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 -.In inttypes.h -.Ft intmax_t -.Fn strtoimax "const char *nptr" "char **endptr" "int base" -.Pp -.In sys/types.h -.In limits.h -.In stdlib.h -.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 "strtoimaxXX" "INTMAX_MIN" "INTMAX_MAX" -offset indent -.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 dc2cf8871c..0000000000 --- a/src/lib/libc/stdlib/strtol.c +++ /dev/null @@ -1,151 +0,0 @@ -/* $OpenBSD: strtol.c,v 1.9 2013/04/17 17:40:35 tedu 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; - - /* - * Ensure that base is between 2 and 36 inclusive, or the special - * value of 0. - */ - if (base != 0 && (base < 2 || base > 36)) { - if (endptr != 0) - *endptr = (char *)nptr; - errno = EINVAL; - return 0; - } - - /* - * 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 4bcc5565be..0000000000 --- a/src/lib/libc/stdlib/strtoll.c +++ /dev/null @@ -1,144 +0,0 @@ -/* $OpenBSD: strtoll.c,v 1.7 2013/03/28 18:09:38 martynas 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); -} - -__strong_alias(strtoq, strtoll); diff --git a/src/lib/libc/stdlib/strtonum.3 b/src/lib/libc/stdlib/strtonum.3 deleted file mode 100644 index ed638cdbe2..0000000000 --- a/src/lib/libc/stdlib/strtonum.3 +++ /dev/null @@ -1,152 +0,0 @@ -.\" $OpenBSD: strtonum.3,v 1.17 2013/08/14 06:32:28 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: August 14 2013 $ -.Dt STRTONUM 3 -.Os -.Sh NAME -.Nm strtonum -.Nd reliably convert string value to an integer -.Sh SYNOPSIS -.In stdlib.h -.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 Qq too large -The result was larger than the provided maximum value. -.It Qq too small -The result was smaller than the provided minimum value. -.It Qq 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 1aeee3467b..0000000000 --- a/src/lib/libc/stdlib/strtonum.c +++ /dev/null @@ -1,65 +0,0 @@ -/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu 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; - int error = 0; - char *ep; - 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 1353637d68..0000000000 --- a/src/lib/libc/stdlib/strtoul.3 +++ /dev/null @@ -1,245 +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.22 2013/08/14 06:32:28 jmc Exp $ -.\" -.Dd $Mdocdate: August 14 2013 $ -.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 -.In limits.h -.In stdlib.h -.Ft unsigned long -.Fn strtoul "const char *nptr" "char **endptr" "int base" -.Ft unsigned long long -.Fn strtoull "const char *nptr" "char **endptr" "int base" -.In inttypes.h -.Ft uintmax_t -.Fn strtoumax "const char *nptr" "char **endptr" "int base" -.In sys/types.h -.In limits.h -.In stdlib.h -.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 a236365d2f..0000000000 --- a/src/lib/libc/stdlib/strtoul.c +++ /dev/null @@ -1,102 +0,0 @@ -/* $OpenBSD: strtoul.c,v 1.8 2013/04/17 17:40:35 tedu 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 28f613a087..0000000000 --- a/src/lib/libc/stdlib/strtoull.c +++ /dev/null @@ -1,106 +0,0 @@ -/* $OpenBSD: strtoull.c,v 1.6 2013/03/28 18:09:38 martynas 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); -} - -__strong_alias(strtouq, strtoull); 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 878bb0a9d2..0000000000 --- a/src/lib/libc/stdlib/system.3 +++ /dev/null @@ -1,109 +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.13 2013/07/18 10:14:50 schwarze Exp $ -.\" -.Dd $Mdocdate: July 18 2013 $ -.Dt SYSTEM 3 -.Os -.Sh NAME -.Nm system -.Nd pass a command to the shell -.Sh SYNOPSIS -.In stdlib.h -.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 HISTORY -The -.Fn system -function first appeared in -.At v6 . -.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 0d1d5196d8..0000000000 --- a/src/lib/libc/stdlib/tfind.c +++ /dev/null @@ -1,41 +0,0 @@ -/* $OpenBSD: tfind.c,v 1.6 2014/03/16 18:38:30 guenther 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 - * - * 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 46d0bba10c..0000000000 --- a/src/lib/libc/stdlib/tsearch.3 +++ /dev/null @@ -1,130 +0,0 @@ -.\" $OpenBSD: tsearch.3,v 1.19 2013/06/05 03:39:23 tedu 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: June 5 2013 $ -.Dt TSEARCH 3 -.Os -.Sh NAME -.Nm tsearch , -.Nm tfind , -.Nm tdelete , -.Nm twalk -.Nd manipulate binary search trees -.Sh SYNOPSIS -.In search.h -.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. -If the node to be deleted is the root of the binary search tree, -.Fa rootp -will be adjusted and an unspecified non-null pointer will be returned. -It takes the same arguments as -.Fn tfind -and -.Fn tsearch . -.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 tdelete -returns a pointer to the parent of the deleted node or an unspecified -non-null pointer if the root node is deleted. -.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-2008 . -.Sh CAVEATS -The value returned when deleting the root node was unspecified before -the -.St -p1003.1-2008 -standard, so users of the -.Fn tdelete -function should be wary of relying 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 a141085d2b..0000000000 --- a/src/lib/libc/stdlib/tsearch.c +++ /dev/null @@ -1,119 +0,0 @@ -/* $OpenBSD: tsearch.c,v 1.8 2014/03/16 18:38:30 guenther 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 - * - * 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 *)1; - node *q; - node *r; - int cmp; - - if (rootp == (struct node_t **)0 || *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; - } - } - 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